-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patharima.py
82 lines (61 loc) · 1.87 KB
/
arima.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from pandas import read_csv
from pandas import datetime
from pandas import DataFrame
import math
import pandas as pd
from processing import *
from pandas.tools.plotting import autocorrelation_plot
from statsmodels.tsa.arima_model import ARIMA
#dataframe = read_csv('ibov_google_15jun2017_1min_15d.csv', sep = ',', usecols=[1],
# engine='python', skiprows=8, decimal='.',header=None)
dataframe = read_csv('minidolar/wdo.csv', sep = '|', usecols=[5], engine='python', decimal='.',header=0)
dataframe = dataframe['fechamento']
dataframe = dataframe[0:540]
# BTC-USD
# btc = read_csv('btc-usd.csv', sep = ',', engine='python', decimal='.',header=0)
# dataframe = btc['close']
start_time = time.time()
# test_stationarity(dataframe)
#
# plt.plot(dataframe.values)
# autocorrelation_plot(dataframe)
# plt.show()
# model = ARIMA(dataframe.as_matrix(), order=(5,1,0))
# model_fit = model.fit(disp=0)
# print(model_fit.summary())
# # plot residual errors
# residuals = DataFrame(model_fit.resid)
# residuals.plot()
# plt.show()
# residuals.plot(kind='kde')
# plt.show()
# print(residuals.describe())
# DOLLAR ARIMA(0,1,1)
# BTC ARIMA(3,1,5)
p=2
d=1
q=1
STEP = 1
FORECAST = 1
predictions,test = [],[]
for i in range(int(len(dataframe)-11), len(dataframe), STEP):
try:
x_i = np.asarray(dataframe[0:i]) #ARIMA have to use all timeseries
y_i = dataframe[i+FORECAST]
model = ARIMA(x_i, order=(p,d,q))
model_fit = model.fit(disp=0)
output = model_fit.forecast()
yhat = output[0][0]
except Exception as e:
break
predictions.append(yhat)
test.append(y_i)
error = math.sqrt(mean_squared_error(test, predictions))
print('Test RMSE: %.3f' % error)
elapsed_time = (time.time() - start_time)
with open("output/arima.csv", "a") as fp:
fp.write("p %i, d %i, q %i, rmse %f --%s seconds\n" % (p, d, q, error, elapsed_time))
# plot
#plt.plot(test)
#plt.plot(predictions, color='red')
#plt.show()