-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_HAR_model.py
89 lines (74 loc) · 2.99 KB
/
run_HAR_model.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
from config import *
from HAR_Model import *
def load_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")
return df_m
def estimate_and_predict_har_models(df_input, save=True):
all_models = {"future": [1, 5, 20], "semi_variance": [True, False]}
all_results = {}
os.chdir(folder_structure.output_path + "/" + folder_structure.HARModel)
for i in all_models["future"]:
for k in all_models["semi_variance"]:
for log_t in [True, False]:
all_results[
"har_{}_{}_{}".format(i, k, log_t)
] = HARModelLogTransformed(
df=df_input,
future=i,
lags=[4, 20],
feature="RV",
semi_variance=k,
jump_detect=True,
log_transformation=log_t,
period_train=list(
[
pd.to_datetime("20030910", format="%Y%m%d"),
pd.to_datetime("20091231", format="%Y%m%d"),
]
),
period_test=list(
[
pd.to_datetime("20100101", format="%Y%m%d"),
pd.to_datetime("20101231", format="%Y%m%d"),
]
),
)
all_results["har_{}_{}_{}".format(i, k, log_t)].run_complete_model()
if save:
estimation_results = open(
"har_{}_{}_{}_estimation.txt".format(i, k, log_t), "a+"
)
estimation_results.write(
all_results[
"har_{}_{}_{}".format(i, k, log_t)
].estimation_results
)
accuracy_results = open(
"har_{}_{}_{}_accuracy.txt".format(i, k, log_t), "a+"
)
accuracy_results.write("Train Accuracy:")
accuracy_results.write(
str(
all_results[
"har_{}_{}_{}".format(i, k, log_t)
].train_accuracy
)
)
accuracy_results.write("Test Accuracy:")
accuracy_results.write(
str(
all_results[
"har_{}_{}_{}".format(i, k, log_t)
].test_accuracy
)
)
return all_results
def run_all(save_output=True):
df = load_data()
res = estimate_and_predict_har_models(df_input=df, save=save_output)
return res
results_har = run_all(save_output=False)