-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
218 lines (154 loc) · 6.75 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# This code can be used to reproduce the forecasts of M4 Competition NN benchmarks and evaluate their accuracy
import tensorflow as tf
import numpy as np
import pandas as pd
import gc
from functions import *
from models_bench import *
from metrics import *
import matplotlib.pyplot as plt
#reset the image folder, not working if called from an imported script
if os.path.isdir('image'):
delete_image_folder()
create_image_folder()
else:
create_image_folder()
#def main():
data_all = pd.read_csv(f'/Users/tomdarmon/Documents/Thesis Bocconi/project/train/Daily-train.csv')
freq = 30
data_all = data_all.drop('V1', axis = 1)
number_of_time_series = 2
#Replace all NaN values
data_all = data_all.values
#Chose how many timeseries
data_all = data_all[:number_of_time_series,:]
compute_all_acf_plots(data_all, lag = 120)
# forecasting horizon year/quarterly/Month/days/hourly : 6 / 8 / 18 / 14 / 48
fh = 14
# number of points used as input for each forecast during training for LSTM and RNN
# 24 * 30 for hourly to englobe 1 month of data
in_size =
err_MLP_sMAPE = []
err_MLP_MASE = []
err_ES_sMAPE = []
err_ES_MASE = []
err_RNN_sMAPE = []
err_RNN_MASE = []
err_LSTM_sMAPE = []
err_LSTM_MASE = []
err_ARIMA_sMAPE = []
err_ARIMA_MASE = []
y_hats_ARIMA = []
y_hats_RNN = []
y_hats_MLP = []
y_hats_LSTM = []
y_hats_ES = []
y_test_all = []
counter = 0
# ===== Main loop which goes through all timeseries =====
for j in range(len(data_all)):
ts = data_all[j, :]
#Take out all the ending NaN values of the time series
ts = ts[~np.isnan(ts)]
#ARIMA benchmark before pre processing for neural networks
y_hat_test_ARIMA = arima_bench(ts, fh)
#Exponential_smoothing_holt_winterbenchmark before pre processing for neural networks
y_hat_test_ES1, y_hat_test_ES2, y_hat_test_ES3 = Exponential_smoothing_bench(ts, fh)
# remove seasonality
seasonality_in = deseasonalize(ts, freq)
for i in range(0, len(ts)):
ts[i] = ts[i] * 100 / seasonality_in[i % freq]
# detrending
a, b = detrend(ts)
for i in range(0, len(ts)):
ts[i] = ts[i] - ((a * i) + b)
x_train, y_train, x_test, y_test = split_into_train_test(ts, in_size, fh)
# LSTM benchmark - Produce forecasts
y_hat_test_LSTM = lstm_bench(x_train, y_train, x_test, fh, in_size)
# RNN benchmark - Produce forecasts
y_hat_test_RNN = np.reshape(rnn_bench(x_train, y_train, x_test, fh, in_size), (-1))
# MLP benchmark - Produce forecasts
y_hat_test_MLP = mlp_bench(x_train, y_train, x_test, fh)
for i in range(0, 29):
y_hat_test_MLP = np.vstack((y_hat_test_MLP, mlp_bench(x_train, y_train, x_test, fh)))
y_hat_test_MLP = np.median(y_hat_test_MLP, axis=0)
# add trend
for i in range(0, len(ts)):
ts[i] = ts[i] + ((a * i) + b)
for i in range(0, fh):
y_hat_test_MLP[i] = y_hat_test_MLP[i] + ((a * (len(ts) + i + 1)) + b)
y_hat_test_RNN[i] = y_hat_test_RNN[i] + ((a * (len(ts) + i + 1)) + b)
y_hat_test_LSTM[i] = y_hat_test_LSTM[i] + ((a * (len(ts) + i + 1)) + b)
for i in range(0, len(ts)):
ts[i] = ts[i] * seasonality_in[i % freq] / 100
for i in range(len(ts), len(ts) + fh):
y_hat_test_MLP[i - len(ts)] = y_hat_test_MLP[i - len(ts)] * seasonality_in[i % freq] / 100
y_hat_test_RNN[i - len(ts)] = y_hat_test_RNN[i - len(ts)] * seasonality_in[i % freq] / 100
y_hat_test_LSTM[i - len(ts)] = y_hat_test_LSTM[i - len(ts)] * seasonality_in[i % freq] / 100
# check if negative or extreme
for i in range(len(y_hat_test_MLP)):
if y_hat_test_MLP[i] < 0:
y_hat_test_MLP[i] = 0
if y_hat_test_RNN[i] < 0:
y_hat_test_RNN[i] = 0
if y_hat_test_LSTM[i] < 0:
y_hat_test_LSTM[i] = 0
if y_hat_test_ARIMA[i] < 0:
y_hat_test_ARIMA[i] = 0
if y_hat_test_MLP[i] > (1000 * max(ts)):
y_hat_test_MLP[i] = max(ts)
if y_hat_test_RNN[i] > (1000 * max(ts)):
y_hat_test_RNN[i] = max(ts)
if y_hat_test_LSTM[i] > (1000 * max(ts)):
y_hat_test_LSTM = max(ts)
if y_hat_test_ARIMA[i] > (1000* max(ts)):
y_hat_test_ARIMA[i] = max(ts)
x_train, y_train, x_test, y_test = split_into_train_test(ts, in_size, fh)
print(x_train)
print(y_train)
### CHOSE THE BEST ES MODEL BASED ON SMAPE####
y_hat_test_ES = pick_best_ES(y_hat_test_ES1,y_hat_test_ES2, y_hat_test_ES3, y_test)
# Calculate errors and save forecast plots
err_MLP_sMAPE.append(smape(y_test, y_hat_test_MLP))
err_RNN_sMAPE.append(smape(y_test, y_hat_test_RNN))
err_LSTM_sMAPE.append(smape(y_test, y_hat_test_LSTM))
err_ARIMA_sMAPE.append(smape(y_test, y_hat_test_ARIMA))
err_ES_sMAPE.append(smape(y_test, y_hat_test_ES))
err_MLP_MASE.append(mase(ts[:-fh], y_test, y_hat_test_MLP, freq))
err_RNN_MASE.append(mase(ts[:-fh], y_test, y_hat_test_RNN, freq))
err_LSTM_MASE.append(mase(ts[:-fh], y_test, y_hat_test_LSTM, freq))
err_ARIMA_MASE.append(mase(ts[:-fh], y_test, y_hat_test_ARIMA, freq))
err_ES_MASE.append(mase(ts[:-fh], y_test, y_hat_test_ES, freq))
y_hats_ARIMA.append(y_hat_test_ARIMA)
y_hats_RNN.append(y_hat_test_RNN)
y_hats_MLP.append(y_hat_test_MLP)
y_hats_LSTM.append(y_hat_test_LSTM)
y_hats_ES.append(y_hat_test_ES)
y_test_all.append(y_test)
save_forecasted_values(x_test, y_test, y_hat_test_ARIMA, name = 'ARIMA')
save_forecasted_values(x_test, y_test, y_hat_test_MLP, name = 'MLP')
save_forecasted_values(x_test, y_test, y_hat_test_RNN, name = 'RNN')
save_forecasted_values(x_test, y_test, y_hat_test_LSTM, name = 'LSTM')
save_forecasted_values(x_test, y_test, y_hat_test_ES, name = 'ES')
# memory handling
tf.keras.backend.clear_session()
tf.compat.v1.reset_default_graph()
gc.collect()
plt.clf()
counter = counter + 1
print("-------------TS ID: ", counter, "-------------")
print("\n\n---------FINAL RESULTS---------")
print("=============sMAPE=============\n")
print("#### MLP ####\n", np.mean(err_MLP_sMAPE), "\n")
print("#### RNN ####\n", np.mean(err_RNN_sMAPE), "\n")
print("#### LSTM ####\n", np.mean(err_LSTM_sMAPE), "\n")
print("#### ARIMA ####\n", np.mean(err_ARIMA_sMAPE), "\n")
print("#### Holt ####\n", np.mean(err_ES_sMAPE), "\n")
print("==============MASE=============")
print("#### MLP ####\n", np.mean(err_MLP_MASE), "\n")
print("#### RNN ####\n", np.mean(err_RNN_MASE), "\n")
print("#### LSTM ####\n", np.mean(err_LSTM_MASE), "\n")
print("#### ARIMA ####\n", np.mean(err_ARIMA_MASE), "\n")
print("#### ES ####\n", np.mean(err_ES_MASE), "\n")
#if __name__ == '__main__':
# main()