-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharima_baseline_itemized.py
401 lines (293 loc) · 15.4 KB
/
arima_baseline_itemized.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.arima_model import ARIMA
import os
import warnings
import time
from scipy.stats import pearsonr
import sys
import pickle
from pandas import HDFStore
from pandas import read_hdf
from datetime import datetime
import warnings
warnings.filterwarnings("ignore")
FORECAST_AHEAD = 7
LEN_TEST = 20
'''
#############################################################################################
########### LOADING FROM DISK #############
#############################################################################################
'''
#Load data
with open('generated_data/data_prepared_itemized.pickle', 'rb') as f:
orders = pickle.load(f)
print('Dataset is loaded from disk')
'''
#############################################################################################
########### ARIMA CLASS #############
#############################################################################################
'''
class ARIMABaseLiner():
def __init__(self):
self.counter = 0
self.errorcounter = 0
self.correctioncounter = 0
def counter_plusplus(self):
self.counter += 1
def get_counter_str(self):
return str(self.counter)
#Searching the ideal parameters for the given timeseries based on aic value
def search_parameters(self, prod_name):
'''
#############################################################################################
########### GENERAL #############
#############################################################################################
'''
plt.ioff()
self.counter_plusplus()
print('Product #', self.get_counter_str())
print('Processing: ', prod_name)
starttime = time.time()
'''
#############################################################################################
########### SELECTING #############
#############################################################################################
'''
df_orders = orders[orders['ITEM_ID'] == prod_name].copy()
df_orders.set_index('REQUESTED_DELIVERY_DATE', inplace=True)
df_orders = df_orders[['REQUESTED_QUANTITY', 'QTY_LOG']]
'''
#############################################################################################
########### SET TRAIN/TEST RATIO #############
#############################################################################################
'''
LEN_FULL = df_orders.shape[0]
LEN_TRAIN_VALID = LEN_FULL - LEN_TEST
'''
#############################################################################################
########### TEST PARAMS #############
#############################################################################################
'''
print('Searching for ideal parameters..')
testcases = {}
d=1
for p in range(1,7):
for q in range(1,5):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
test_model = ARIMA(df_orders['QTY_LOG'][:LEN_TRAIN_VALID], order=(p, d, q))
test_results_ARIMA = test_model.fit(disp=-1)
aic = test_results_ARIMA.aic
param = p,d,q
testcases[param] = aic
except:
pass #ignore the error and go on
best_params = min(testcases, key=testcases.get)
p,d,q = best_params
print('Best p,d,q params are: ')
print('(',p,d,q,')')
print('Running took {} seconds'.format(time.time()-starttime))
print('-' * 60)
return pd.DataFrame({'p':p,'d':d, 'q':q}, index=[prod_name])
#Fitting ARIMA models using the already found ideal p,d,q parameters
def fit_arima(self, prod_name, parameters):
'''
#############################################################################################
########### GENERAL #############
#############################################################################################
'''
plt.ioff()
self.counter_plusplus()
print('Product #', self.get_counter_str())
print('Processing: ', prod_name)
starttime = time.time()
'''
#############################################################################################
########### SELECTING #############
#############################################################################################
'''
df_orders = orders[orders['ITEM_ID'] == prod_name].copy()
df_orders.set_index('REQUESTED_DELIVERY_DATE', inplace=True)
df_orders = df_orders[['REQUESTED_QUANTITY', 'QTY_LOG']]
'''
#############################################################################################
########### SET TRAIN/TEST RATIO #############
#############################################################################################
'''
LEN_FULL = df_orders.shape[0]
LEN_TRAIN_VALID = LEN_FULL - LEN_TEST
#For being consequent with NN approach
ts_test = df_orders[LEN_TRAIN_VALID+FORECAST_AHEAD:]
'''
#############################################################################################
########### ERROR HANDLING, FIND ANOTHER PARAMETER TUPLE #############
#############################################################################################
'''
def find_other_pdq(p, q):
if p > q:
p-=1
else:
q-=1
return p,q
def forecast_x_ahead_one_obs(i,p,q):
try:
results_ARIMA = ARIMA(df_orders['QTY_LOG'][:LEN_TRAIN_VALID+1+i], order=(p, d, q)).fit(disp=-1)
res = results_ARIMA.forecast(FORECAST_AHEAD)[0]
return res[-1], p, q
except (ValueError, np.linalg.LinAlgError) as e:
self.errorcounter += 1
print('Error occured ({})'.format(self.errorcounter))
p,q = find_other_pdq(p,q)
res, p, q = forecast_x_ahead_one_obs(i,p,q)
return res, p, q
'''
#############################################################################################
########### ARIMA FIT AND FORECAST #############
#############################################################################################
'''
forecasted_values = []
for i in range(LEN_TEST-FORECAST_AHEAD): #9 or 13
p,d,q = parameters
p_original = p
q_original = q
forcasted_value, p, q = forecast_x_ahead_one_obs(i,p,q)
if(p_original!=p or q_original!=q):
self.correctioncounter += 1
print('One correction happened ({})'.format(self.correctioncounter))
forecasted_values.append(forcasted_value)
ts_test['QTY_LOG_PRED'] = np.array(forecasted_values)
ts_test[['QTY_LOG','QTY_LOG_PRED']].plot()
'''
#############################################################################################
########### EVALUATION (TEST SET) #############
#############################################################################################
'''
#Predicted calc back
ts_test['QTY_PREDICTED'] = np.exp(ts_test['QTY_LOG_PRED'])
#Fill the Nans generated because of errors
mean_actual = df_orders['REQUESTED_QUANTITY'][:LEN_TRAIN_VALID].mean()
ts_test['QTY_PREDICTED'].fillna(mean_actual, inplace=True)
#Thresholding
max_actual = df_orders['REQUESTED_QUANTITY'][:LEN_TRAIN_VALID].max()
ts_test['QTY_PREDICTED'] = ts_test['QTY_PREDICTED'].clip(0,max_actual)
#Evaluate losses
corr_qtylog = pearsonr(ts_test['QTY_LOG'], ts_test['QTY_LOG_PRED'])
corr_qty = pearsonr(ts_test['QTY_PREDICTED'], ts_test['REQUESTED_QUANTITY'])
mae_final = np.mean(abs(ts_test['QTY_PREDICTED']-ts_test['REQUESTED_QUANTITY']))
rmse_final = np.sqrt(np.mean((ts_test['QTY_PREDICTED']-ts_test['REQUESTED_QUANTITY'])**2))
mape_final = np.mean(abs((ts_test['REQUESTED_QUANTITY']-ts_test['QTY_PREDICTED']) / ts_test['REQUESTED_QUANTITY'])) * 100
#Plot prediction
ax = ts_test[['REQUESTED_QUANTITY','QTY_PREDICTED']].plot()
plt.title('RMSE: {:.4f} - ({})'.format(rmse_final, prod_name))
fig = ax.get_figure()
file_name = os.path.join('arima', self.get_counter_str() + '.jpg')
fig.savefig(file_name)
plt.close(fig)
#Print results
print('Correlation of Quantity Log: {}'.format(corr_qtylog))
print('Correlation of Quantity: {}'.format(corr_qty))
print('MAE Quantity: {:.4f}'.format(mae_final))
print('RMSE Quantity: {:.4f}'.format(rmse_final))
print('MAPE Quantity: {:.4f}'.format(mape_final))
print('Running took {} seconds'.format(time.time()-starttime))
print('-' * 60)
df_preds = pd.DataFrame({ 'ITEM_ID' : prod_name,
'QTY_LOG_ORIGINAL' : ts_test['QTY_LOG'],
'QTY_LOG_PRED' : ts_test['QTY_LOG_PRED'],
'QTY_ORIGINAL' : ts_test['REQUESTED_QUANTITY'],
'QTY_PREDICTED' : ts_test['QTY_PREDICTED']
})
df_losses = pd.DataFrame({
'PRODUCT' : prod_name,
'PDQ' : '{},{},{}'.format(p,d,q),
'RMSE' : rmse_final,
'MAE' : mae_final,
'MAPE' : mape_final,
'CORR_QTYLOG' : corr_qtylog[0],
'CORR_QTY' : corr_qty[0]
},index=[0])
return df_preds, df_losses
'''
#############################################################################################
########### RUN ARIMA FOR ALL ITEMS #############
#############################################################################################
'''
#mode of running
RUNNING_MODE = 'PARAMETER_SEARCH'
#RUNNING_MODE = 'ARIMA_FITTING'
NB_OF_ITEMS = 2
#Run for all in a loop
prod_names = orders['ITEM_ID'].unique()
arima_model = ARIMABaseLiner()
start = time.time()
#Only search
if RUNNING_MODE == 'PARAMETER_SEARCH':
parameters = []
for name in prod_names:
param = arima_model.search_parameters(name)
parameters.append(param)
#Save PARAMETERS (p,d,q)
path_params = os.path.join('generated_by_arima_itemized', 'arima_parameters.h5')
df_parameters = pd.concat(parameters)
df_parameters.to_hdf(path_params, 'params', mode='w')
#Only fitting
elif RUNNING_MODE == 'ARIMA_FITTING':
#Load parameters
path_params = os.path.join('generated_by_arima_itemized', 'arima_parameters.h5')
df_parameters = read_hdf(path_params, 'params')
predictions = []
losses = []
# for name in prod_names[650:]:
for name in prod_names:
#Check whether we have parameter for that product
if name in df_parameters.index:
param = df_parameters.loc[name][['p','d','q']].values
prediction, loss = arima_model.fit_arima(name,param)
predictions.append(prediction)
losses.append(loss)
'''
###############################################################################
################## EVALUATION #################
###############################################################################
'''
#Final result set
df_predictions = pd.concat(predictions)
df_losses = pd.concat(losses)
#Losses
finals = {}
finals['MAE'] = np.mean(abs(df_predictions['QTY_ORIGINAL']-df_predictions['QTY_PREDICTED']))
finals['RMSE'] = np.sqrt(np.mean((df_predictions['QTY_ORIGINAL']-df_predictions['QTY_PREDICTED']) ** 2))
finals['MAPE'] = np.mean(abs((df_predictions['QTY_ORIGINAL']-df_predictions['QTY_PREDICTED']) / df_predictions['QTY_ORIGINAL'])) * 100
finals['CORR'] = pearsonr(df_predictions['QTY_ORIGINAL'], df_predictions['QTY_PREDICTED'])[0]
df_finals = pd.DataFrame(finals, index=[0])
#Print results
print('MAE final: ', finals['MAE'])
print('RMSE final: ', finals['RMSE'])
print('MAPE final: ', finals['MAPE'])
print('CORR final: ', finals['CORR'])
'''
###############################################################################
################## SAVE RESULTS #################
###############################################################################
'''
#Create folder with date and model type
date_time = datetime.now()
unique_time = date_time.strftime('%Y-%m-%d_%H-%M')
date_time_folder = unique_time + '_ARIMA_' + str(FORECAST_AHEAD) #like: 2017-02-27_13-ARIMA_1
path_folder = os.path.join('generated_by_arima_itemized', date_time_folder)
os.mkdir(path_folder)
path_losses = os.path.join(path_folder, 'losses.h5')
path_predictions = os.path.join(path_folder, 'predictions.h5')
#Save losses and predictions
df_finals.to_hdf(path_losses, 'losses', mode='w')
df_predictions.to_hdf(path_predictions, 'predictions', mode='w')
print('Results are saved to disk')
#Common
duration = (time.time()-start) /60
print('The test took {} minutes'.format(duration))
print('{} errors occured'.format(arima_model.errorcounter))
print('{} corrections happened'.format(arima_model.correctioncounter))