-
Notifications
You must be signed in to change notification settings - Fork 3
/
dashboard_data_prep.py
456 lines (385 loc) · 16.7 KB
/
dashboard_data_prep.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
from run_AutoRegression_Model import *
from run_HAR_model import results_har
from config import folder_structure
from LSTM import TimeSeriesDataPreparationLSTM
class DashboardDataPrep:
def __init__(self, df_tt, df_validation):
self.df_tt = df_tt
self.df_validation = df_validation
self.df_future = None
self.df_ar = None
self.df_har = None
self.df_lstm_tt = None
self.df_lstm_valid = None
self.df_all = None
self.df_final = None
def prepare_future_data(self):
data_input = pd.concat(
[self.df_tt[["DATE", "RV", "RSV_minus", "RSV_plus"]], self.df_validation],
sort=True,
).reset_index(drop=True)
periods_list = [1, 5, 20]
df_future = pd.DataFrame()
for period in range(3):
har_instance = HARModelLogTransformed(
df=data_input.copy(),
future=periods_list[period],
lags=[4, 20],
feature="RV",
semi_variance=False,
jump_detect=False,
log_transformation=False,
period_train=list(
[
pd.to_datetime("20030910", format="%Y%m%d"),
pd.to_datetime("20111231", format="%Y%m%d"),
]
),
period_test=list(
[
pd.to_datetime("20030910", format="%Y%m%d"),
pd.to_datetime("20111231", format="%Y%m%d"),
]
),
)
har_instance.make_testing_training_set()
df_tmp = har_instance.training_set[["DATE", "future"]].copy()
df_tmp["period"] = periods_list[period]
if period == 0:
df_future = df_tmp
else:
df_future = pd.concat([df_future, df_tmp])
self.df_future = df_future
def prepare_ar_data(self):
periods_list = [1, 5, 20]
lags_list = [1, 3]
df_tmp_3 = pd.DataFrame()
df_ar_ = pd.DataFrame()
data_input = pd.concat(
[self.df_tt[["DATE", "RV", "RSV_minus", "RSV_plus"]], self.df_validation],
sort=True,
).reset_index(drop=True)
for lags in range(2):
for period in range(3):
ar_instance = TimeSeriesDataPreparationLSTM(
df=data_input.copy(),
future=periods_list[period],
lag=lags_list[lags],
standard_scaler=False,
min_max_scaler=False,
log_transform=False,
semi_variance=False,
jump_detect=False,
period_train=list(
[
pd.to_datetime("20030910", format="%Y%m%d"),
pd.to_datetime("20111231", format="%Y%m%d"),
]
),
period_test=list(
[
pd.to_datetime("20030910", format="%Y%m%d"),
pd.to_datetime("20030910", format="%Y%m%d"),
]
),
)
ar_instance.prepare_complete_data_set()
if lags_list[lags] == 3:
ar_vars = ["RV", "lag_1", "lag_2"]
else:
ar_vars = ["RV"]
df_tmp = pd.DataFrame(
results_auto_regression[
"AR_{}_{}".format(periods_list[period], lags_list[lags],)
].ar_model.predict(ar_instance.training_set[ar_vars]),
columns={"A({})".format(lags_list[lags])},
).reset_index()
df_tmp["period"] = periods_list[period]
df_tmp_2 = pd.DataFrame(
ar_instance.training_set.DATE, columns={"DATE"}
).reset_index()
df_tmp_2 = df_tmp_2.merge(df_tmp, on="index")
df_tmp_2 = df_tmp_2.drop(columns=["index"])
if period == 0:
df_tmp_3 = df_tmp_2
else:
df_tmp_3 = pd.concat([df_tmp_3, df_tmp_2])
if lags == 0:
df_ar_ = df_tmp_3
else:
df_ar_ = df_ar_.merge(df_tmp_3, on=["period", "DATE"])
self.df_ar = df_ar_
@staticmethod
def make_exponential(series, log_transformed: bool = False):
if log_transformed:
return np.exp(series)
else:
return series
def prepare_har_data(self):
data_input = pd.concat(
[self.df_tt[["DATE", "RV", "RSV_minus", "RSV_plus"]], self.df_validation],
sort=True,
).reset_index(drop=True)
future_periods = [1, 5, 20]
semi_variance_list = [True, False]
log_trans_list = [True, False]
df_tmp_3 = pd.DataFrame()
df_tmp_4 = pd.DataFrame()
df_har_ = pd.DataFrame()
for log_trans in range(2):
for semi_variance in range(2):
for i in range(3):
har_instance = HARModelLogTransformed(
df=data_input.copy(),
future=future_periods[i],
lags=[4, 20],
feature="RV",
semi_variance=semi_variance_list[semi_variance],
jump_detect=False,
log_transformation=log_trans_list[log_trans],
period_train=list(
[
pd.to_datetime("20030910", format="%Y%m%d"),
pd.to_datetime("20111231", format="%Y%m%d"),
]
),
period_test=list(
[
pd.to_datetime("20030910", format="%Y%m%d"),
pd.to_datetime("20111231", format="%Y%m%d"),
]
),
)
har_instance.make_testing_training_set()
if semi_variance_list[semi_variance] is True:
har_variables = ["RSV_plus", "RSV_minus", "RV_w", "RV_m"]
semi_variance_indication = "SV"
else:
har_variables = ["RV_t", "RV_w", "RV_m"]
semi_variance_indication = "RV"
if log_trans_list[log_trans] is True:
log_trans_indication = ",L"
else:
log_trans_indication = ""
df_tmp = pd.DataFrame(
results_har[
"har_{}_{}_{}".format(
future_periods[i],
semi_variance_list[semi_variance],
log_trans_list[log_trans],
)
].model.predict(har_instance.training_set[har_variables]),
columns={
"H({}{})".format(
semi_variance_indication, log_trans_indication
)
},
).reset_index()
df_tmp["period"] = future_periods[i]
df_tmp[
"H({}{})".format(semi_variance_indication, log_trans_indication)
] = self.make_exponential(
df_tmp[
"H({}{})".format(
semi_variance_indication, log_trans_indication
)
],
log_transformed=log_trans_list[log_trans],
)
df_tmp_2 = pd.DataFrame(
har_instance.training_set.DATE, columns={"DATE"}
).reset_index()
df_tmp_2 = df_tmp_2.merge(df_tmp, on="index")
df_tmp_2 = df_tmp_2.drop(columns=["index"])
if i == 0:
df_tmp_3 = df_tmp_2
else:
df_tmp_3 = pd.concat([df_tmp_3, df_tmp_2])
if semi_variance == 0:
df_tmp_4 = df_tmp_3
else:
df_tmp_4 = df_tmp_4.merge(df_tmp_3, on=["period", "DATE"])
if log_trans == 0:
df_har_ = df_tmp_4
else:
df_har_ = df_har_.merge(df_tmp_4, on=["period", "DATE"])
self.df_har = df_har_
@staticmethod
def load_lstm_models():
semi_variance_list = [True, False]
periods_list = [1, 5, 20]
lags_list = [20, 40]
lstm_dict = {}
for semi_variance in range(2):
for lags in range(2):
for period in range(3):
lstm_dict[
"LSTM_{}_{}_{}".format(
semi_variance_list[semi_variance],
periods_list[period],
lags_list[lags],
)
] = tf.keras.models.load_model(
folder_structure.output_LSTM
+ "/"
+ "LSTM_{}_{}_{}.h5".format(
semi_variance_list[semi_variance],
periods_list[period],
lags_list[lags],
)
)
return lstm_dict
def prepare_lstm_data(self):
semi_variance_list = [True, False]
periods_list = [1, 5, 20]
lags_list = [20, 40]
data_set_list = [self.df_tt, self.df_validation]
df_tpm_3 = pd.DataFrame()
df_tmp_4 = pd.DataFrame()
df_lstm_ = pd.DataFrame()
lstm_dict = self.load_lstm_models()
for data_set in range(2):
for semi_variance in range(2):
for lags in range(2):
for period in range(3):
lstm_instance = TimeSeriesDataPreparationLSTM(
df=data_set_list[data_set].copy(),
future=periods_list[period],
lag=lags_list[lags],
standard_scaler=False,
min_max_scaler=True,
log_transform=True,
semi_variance=semi_variance_list[semi_variance],
jump_detect=False,
period_train=list(
[
pd.to_datetime("20030910", format="%Y%m%d"),
pd.to_datetime("20111231", format="%Y%m%d"),
]
),
period_test=list(
[
pd.to_datetime("20030910", format="%Y%m%d"),
pd.to_datetime("20111231", format="%Y%m%d"),
]
),
)
lstm_instance.prepare_complete_data_set()
lstm_instance.reshape_input_data()
if semi_variance_list[semi_variance] is True:
semi_variance_indication = "SV"
else:
semi_variance_indication = "RV"
df_tmp = pd.DataFrame(
lstm_instance.back_transformation(
lstm_dict[
"LSTM_{}_{}_{}".format(
semi_variance_list[semi_variance],
periods_list[period],
lags_list[lags],
)
].predict(lstm_instance.train_matrix)
),
columns={
"L({},{})".format(
semi_variance_indication, lags_list[lags]
)
},
).reset_index()
df_tmp["period"] = periods_list[period]
df_tmp_2 = pd.DataFrame(
lstm_instance.training_set.DATE, columns={"DATE"}
).reset_index()
df_tmp_2 = df_tmp_2.merge(df_tmp, on="index")
df_tmp_2 = df_tmp_2.drop(columns=["index"])
if period == 0:
df_tpm_3 = df_tmp_2
else:
df_tpm_3 = pd.concat([df_tpm_3, df_tmp_2])
if lags == 0:
df_tmp_4 = df_tpm_3
else:
df_tmp_4 = df_tmp_4.merge(df_tpm_3, on=["period", "DATE"])
if semi_variance == 0:
df_lstm_ = df_tmp_4
else:
df_lstm_ = df_lstm_.merge(df_tmp_4, on=["period", "DATE"])
if data_set == 0:
self.df_lstm_tt = df_lstm_
else:
self.df_lstm_valid = df_lstm_
def prepare_all_data(self):
if self.df_future is None:
self.prepare_future_data()
if self.df_ar is None:
self.prepare_ar_data()
if self.df_har is None:
self.prepare_har_data()
if any(df is None for df in [self.df_lstm_tt, self.df_lstm_valid]):
self.prepare_lstm_data()
def merge_all(self):
if any(
data_frame is None
for data_frame in [
self.df_ar,
self.df_har,
self.df_lstm_tt,
self.df_lstm_valid,
self.df_future,
]
):
self.prepare_all_data()
df_tmp = self.df_ar.merge(self.df_har, on=["period", "DATE"])
df_lstm_complete = pd.concat([self.df_lstm_tt, self.df_lstm_valid]).reset_index(
drop=True
)
self.df_final = df_tmp.merge(df_lstm_complete, on=["period", "DATE"])
self.df_final = self.df_final.merge(self.df_future, on=["period", "DATE"])
# add training, testing, validation indicator
self.df_final["dataset"] = np.NAN
self.df_final.dataset[
(self.df_final.DATE >= pd.to_datetime("20030910", format="%Y%m%d"))
& (self.df_final.DATE <= pd.to_datetime("20091231", format="%Y%m%d"))
] = "training"
self.df_final.dataset[
(self.df_final.DATE >= pd.to_datetime("20100101", format="%Y%m%d"))
& (self.df_final.DATE <= pd.to_datetime("20101231", format="%Y%m%d"))
] = "validation"
self.df_final.dataset[
(self.df_final.DATE >= pd.to_datetime("20110101", format="%Y%m%d"))
& (self.df_final.DATE <= pd.to_datetime("20111231", format="%Y%m%d"))
] = "testing"
def load_dashboard_data():
df_m = pd.read_csv(
folder_structure.path_input + "/" + "RealizedMeasures03_10.csv", index_col=0
)
df_m.DATE = df_m.DATE.values
df_m.DATE = pd.to_datetime(df_m.DATE, format="%Y%m%d")
df = pd.read_csv(
folder_structure.path_input + "/" + "DataFeatures.csv", index_col=0
)
df.DATE = df.DATE.values
df.DATE = pd.to_datetime(df.DATE, format="%Y%m%d")
data_input = pd.concat(
[df_m[["DATE", "RV", "RSV_minus", "RSV_plus"]], df], sort=True,
).reset_index(drop=True)
df_tmp = data_input.copy()
df_tmp["threshold"] = df_tmp["RV"].rolling(window=200).std() * 4
df_tmp.threshold = np.where(df_tmp.threshold.isna(), 1, df_tmp.threshold)
df_tmp["larger"] = np.where(df_tmp.RV > df_tmp.threshold, True, False)
df_tmp = df_tmp[df_tmp.larger == False]
df_tmp.drop(columns={"threshold", "larger"}, axis=1, inplace=True)
data_input = df_tmp
data_training = data_input[
data_input.DATE <= pd.to_datetime("20101231", format="%Y%m%d")
]
data_validation = data_input[
data_input.DATE > pd.to_datetime("20101231", format="%Y%m%d")
]
return data_input, data_training, data_validation
def run_data_preprocessing_dashboard():
df_input_all, data_training, data_validation = load_dashboard_data()
x = DashboardDataPrep(df_tt=data_training, df_validation=data_validation)
x.merge_all()
x.df_final.to_csv(folder_structure.output_Predictions + "/" + "DashboardData.csv")
run_data_preprocessing_dashboard()