forked from Hamidnsh/M6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline.py
More file actions
134 lines (100 loc) · 4.34 KB
/
baseline.py
File metadata and controls
134 lines (100 loc) · 4.34 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 2 11:08:09 2022
@author: snourashrafeddin
"""
import pandas as pd
import numpy as np
import optuna
import seaborn as sns
from optuna.samplers import TPESampler
from matplotlib import pyplot as plt
from lightgbm import LGBMRegressor
np.random.seed(0)
sampler = TPESampler(seed=0)
starting_date = "2015-01-01"
future_starting_date = pd.to_datetime("2022-03-06")
future_end_date = pd.to_datetime("2022-04-01")
forcast_horizon_days = (future_end_date - future_starting_date).days + 1
df_history = pd.read_csv('full_asset_m6_history.csv')
df_history['date'] = pd.to_datetime(df_history['date'])
df_future = pd.read_csv('full_asset_m6_future.csv')
df_future['date'] = pd.to_datetime(df_future['date'])
df_history['symbol'] = df_history['symbol'] .astype('category')
df_future['symbol'] = df_future['symbol'] .astype('category')
df_train = df_history.loc[df_history['date'] <= future_starting_date - pd.DateOffset(days=forcast_horizon_days)].copy()
df_test = df_history.loc[df_history['date'] > future_starting_date - pd.DateOffset(days=forcast_horizon_days)].copy()
X_cols = ['symbol', 'dayofyear', 'week', 'month', 'year', 'quarter', 'shift_close', 'shift_high', 'shift_low', 'shift_open', 'shift_volume']
y_col = 'price'
def train_model(df_train, k=3, n_trails=20, es=20):
studies = []
def LGBM_objective(trial):
param = {
'boosting_type':trial.suggest_categorical('boosting_type', ['gbdt', 'dart']),
'n_estimators': trial.suggest_int('n_estimators', 50, 200),
'lambda_l1': trial.suggest_loguniform('lambda_l1', 1e-5, 10.0),
'lambda_l2': trial.suggest_loguniform('lambda_l2', 1e-5, 10.0),
'learning_rate': trial.suggest_float('learning_rate', 1e-10, 0.3),
'num_leaves': trial.suggest_int('num_leaves', 4, 32),
'feature_fraction': trial.suggest_uniform('feature_fraction', 0.5, 1.0),
'min_child_samples': 10,
}
model = LGBMRegressor(**param)
model.fit(df_train_fold[X_cols],
df_train_fold[y_col],
eval_set=[(df_eval_fold[X_cols],
df_eval_fold[y_col])],
early_stopping_rounds=es)
pred = model.predict(df_eval_fold[X_cols])
score = np.mean(np.abs(df_eval_fold[y_col] - pred))
return score
last_index = int(len(df_train)*0.8)
eval_size = int(len(df_train)*0.2)
for i in range(k):
df_train_fold = df_train.iloc[0:last_index]
if i == 0:
df_eval_fold = df_train.iloc[last_index:]
else:
df_eval_fold = df_train.iloc[last_index:last_index+eval_size]
last_index -= eval_size
study = optuna.create_study(direction='minimize', sampler=sampler)
study.optimize(LGBM_objective, n_trials=n_trails)
studies.append(study)
return studies
studies = train_model(df_train)
models = []
vals = []
for study in studies:
model = LGBMRegressor(**study.best_trial.params)
model.fit(df_train[X_cols], df_train[y_col])
models.append(model)
vals.append(study.best_trial.value)
model_weights = 1/np.array(vals)
model_weights = model_weights/np.sum(model_weights)
model_weights
pred = np.array([0] * len(df_test))
for i in range(len(models)):
pred = pred + model_weights[i]*models[i].predict(df_test[X_cols])
truth = df_test[y_col].values
bias_val = np.divide(np.sum(truth), np.sum(pred)) - 1
print("bias: " + str(bias_val))
errors = np.sum(np.abs(pred - truth))
wmape = errors / np.sum(truth)
print("wmape: " + str(wmape))
Accuracy = (1 - wmape)*100
print("Accuracy: " + str(Accuracy))
models = []
for study in studies:
model = LGBMRegressor(**study.best_trial.params)
model.fit(df_history[X_cols], df_history[y_col])
models.append(model)
for i in range(len(models)):
models[i].booster_.save_model(f'./models/model{i}.txt')
with open('./models/weights.npy', 'wb') as f:
np.save(f, model_weights)
# load models
# save_models = []
# for i in range(len(models)):
# save_models.append(lightgbm.Booster(model_file=f'./models/model{i}.txt'))
# with open('./models/weights.npy', 'rb') as f:
# weights = np.load(f)