-
Notifications
You must be signed in to change notification settings - Fork 1
/
arimavariations_istanbul_data_mean_sdsh.py
148 lines (128 loc) · 6.51 KB
/
arimavariations_istanbul_data_mean_sdsh.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from statsmodels.tsa.arima_model import ARIMA
import pandas as pd
def armodel(train_data, test_data):
arima = ARIMA(train_data, order=(5,0,0))
arima_fit = arima.fit()
print(arima_fit.summary())
parameters = arima_fit.params
a1 = parameters[1]
a2 = parameters[2]
a3 = parameters[3]
a4 = parameters[4]
a5 = parameters[5]
train_predictions = []
for t in range(4,len(train_data)):
output_train = (train_data[t-4] * a5) + (train_data[t-3] * a4) + (train_data[t-2] * a3) + (train_data[t-1] * a2) + (train_data[t] * a1)
train_predictions.append(output_train)
test_data2=[]
test_data2.append(train_data[-5])
test_data2.append(train_data[-4])
test_data2.append(train_data[-3])
test_data2.append(train_data[-2])
test_data2.append(train_data[-1])
for i in range(len(test_data)-1):
test_data2.append(test_data[i])
test_predictions = []
for t in range(4,len(test_data2)):
output_test = (test_data2[t-4] * a5) + (test_data2[t-3] * a4) + (test_data2[t-2] * a3) + (test_data2[t-1] * a2) + (test_data2[t] * a1)
test_predictions.append(output_test)
pd.DataFrame(train_predictions).to_csv("point_forecasts/ar_istanbul_data_mean_sdsh_train.csv")
pd.DataFrame(test_predictions).to_csv("point_forecasts/ar_istanbul_data_mean_sdsh_test.csv")
return train_predictions, test_predictions
def armamodel(train_data, test_data):
arima = ARIMA(train_data, order=(4,0,5))
arima_fit = arima.fit()
print(arima_fit.summary())
parameters = arima_fit.params
a1 = parameters[1]
a2 = parameters[2]
a3 = parameters[3]
a4 = parameters[4]
b1 = parameters[5]
b2 = parameters[6]
b3 = parameters[7]
b4 = parameters[8]
b5 = parameters[9]
train_predictions = []
outputs = arima_fit.predict(start=len(train_data),end=len(train_data)+4,dynamic=test_data.all())
for i in range(len(outputs)):
train_predictions.append(outputs[i])
for t in range(4,len(train_data)):
output_train = (train_data[t-3] * a4) + (train_data[t-2] * a3) + (train_data[t-1] * a2) + (train_data[t] * a1) + ((train_data[t-4] - train_predictions[-5]) * b5) + ((train_data[t-3] - train_predictions[-4]) * b4) + ((train_data[t-2] - train_predictions[-3]) * b3) + ((train_data[t-1] - train_predictions[-2]) * b2) + ((train_data[t] - train_predictions[-1]) * b1)
train_predictions.append(output_train)
test_data2=[]
test_data2.append(train_data[-4])
test_data2.append(train_data[-3])
test_data2.append(train_data[-2])
test_data2.append(train_data[-1])
for i in range(len(test_data)-1):
test_data2.append(test_data[i])
test_predictions = []
outputs = arima_fit.predict(start=len(train_data),end=len(train_data)+4,dynamic=test_data.all())
for i in range(len(outputs)):
test_predictions.append(outputs[i])
for t in range(4,len(test_data2)):
output_test = (test_data2[t-3] * a4) + (test_data2[t-2] * a3) + (test_data2[t-1] * a2) + (test_data2[t] * a1) + ((test_data2[t-4] - test_predictions[-5]) * b5) + ((test_data2[t-3] - test_predictions[-4]) * b4) + ((test_data2[t-2] - test_predictions[-3]) * b3) + ((test_data2[t-1] - test_predictions[-2]) * b2) + ((test_data2[t] - test_predictions[-1]) * b1)
test_predictions.append(output_test)
test_predictions = test_predictions[4:]
pd.DataFrame(train_predictions).to_csv("point_forecasts/arma_istanbul_data_mean_sdsh_train.csv")
pd.DataFrame(test_predictions).to_csv("point_forecasts/arma_istanbul_data_mean_sdsh_test.csv")
return train_predictions, test_predictions
def arimamodel(train_data, test_data):
arima = ARIMA(train_data, order=(0,1,3))
arima_fit = arima.fit()
print(arima_fit.summary())
train_predictions = arima_fit.predict(start=len(train_data),end=len(train_data)+len(train_data),dynamic=train_data.all())
train_predictions2 = []
for t in range(len(train_data)):
output_train = train_predictions[t] + train_data[t]
train_predictions2.append(output_train)
test_predictions = arima_fit.predict(start=len(train_data),end=len(train_data)+len(test_data)-1,dynamic=test_data.all())
test_predictions2 = []
test_data2=[]
test_data2.append(train_data[-1])
for i in range(len(test_data)-1):
test_data2.append(test_data[i])
for t in range(len(test_data2)):
output_test = test_predictions[t] + test_data2[t]
test_predictions2.append(output_test)
pd.DataFrame(train_predictions2).to_csv("point_forecasts/arima_istanbul_data_mean_sdsh_train.csv")
pd.DataFrame(test_predictions2).to_csv("point_forecasts/arima_istanbul_data_mean_sdsh_test.csv")
return train_predictions2, test_predictions2
def sarimamodel(data):
data2 = pd.DataFrame(data)
data3 = pd.concat([data2.shift(169),data2.shift(168),data2.shift(25),data2.shift(24),data2], axis=1)
data3.columns = ['t-169','t-168','t-25','t-24','t']
data4 = data3.values
train_size = int(len(data4) * 0.70)
train, test = data4[169:train_size], data4[train_size:]
train_X, train_y = train[:,:4], train[:,-1]
test_X, test_y = test[:,:4], test[:,-1]
sarima = ARIMA(train_y, order=(1,1,2), exog=train_X)
sarima_fit = sarima.fit()
print(sarima_fit.summary())
train_predictions = sarima_fit.predict(start=len(train_y),end=len(train_y)+len(train_y)-1,dynamic=train_data.all(),exog=train_X)
train_predictions2 = []
for t in range(len(train_y)):
output_train = train_predictions[t] + train_y[t]
train_predictions2.append(output_train)
test_predictions = sarima_fit.predict(start=len(train_y),end=len(train_y)+len(test_y)-1,dynamic=test_data.all(),exog=test_X)
test_predictions2 = []
test_y2=[]
test_y2.append(train_y[-1])
for i in range(len(test_y)-1):
test_y2.append(test_y[i])
for t in range(len(test_y2)):
output_test = test_predictions[t] + test_y2[t]
test_predictions2.append(output_test)
pd.DataFrame(train_predictions2).to_csv("point_forecasts/sarima_istanbul_data_mean_sdsh_train.csv")
pd.DataFrame(test_predictions2).to_csv("point_forecasts/sarima_istanbul_data_mean_sdsh_test.csv")
return train_predictions2, test_predictions2
data = pd.read_csv('data/istanbul/istanbul_data_mean_sh.csv')[['NUMBER_OF_VEHICLES']]
data = data.values
train_size = int(len(data) * 0.70)
train_data, test_data = data[:train_size], data[train_size:]
armamodel(train_data, test_data)
armodel(train_data, test_data)
arimamodel(train_data, test_data)
sarimamodel(data)