-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_train_crsp.py
250 lines (217 loc) · 10.9 KB
/
tree_train_crsp.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
# Lasso regression , randomforest, and neural network.
# author: [email protected]
import os
import datetime
import warnings
import mltnet
import pandas as pd
import numpy as np
import xgboost as xg
from mylog import logger
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import Lasso
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import StandardScaler
ANCHORS = ['date', 'name']
MONFEATURES = ['A2ME', 'AC', 'AT', 'ATO', 'BEME', 'Beta', 'C',
'CF', 'CF2P', 'CTO', 'D2A', 'D2P', 'DPI2A', 'E2P', 'FC2Y', 'IdioVol',
'Investment', 'Lev', 'LME', 'LT_Rev', 'LTurnover', 'MktBeta', 'NI',
'NOA', 'OA', 'OL', 'OP', 'PCM', 'PM', 'PROF', 'Q', 'r2_1', 'r12_2',
'r12_7', 'r36_13', 'Rel2High', 'Resid_Var', 'RNA', 'ROA', 'ROE', 'S2P',
'SGA2S', 'Spread', 'ST_REV', 'SUV', 'Variance']
TARGET = ['ret']
warnings.filterwarnings('ignore')
filename = os.path.basename(__file__)
logger = logger(filename)
# sort dates
def sort_dates(date):
return datetime.datetime.strptime(date,"%Y-%m-%d").timestamp()
# delete extreme values:
def del_extreme(df_data):
cols = MONFEATURES + TARGET
for col_name in cols:
p025 = df_data[col_name].quantile(q=0.001)
p975 = df_data[col_name].quantile(q=0.995)
#df_data = df_data[df_data[col_name] <= p975]
df_data = df_data[(df_data[col_name] > p025) & (df_data[col_name] < p975)]
return df_data
def get_df_train(df_data, st_date, target_date):
logger.info('processing data')
# get train and test dataframe:
df_test = df_data[df_data['date'] == target_date]
df_train = df_data[(df_data['date'] >= st_date) & (df_data['date'] < target_date)]
#df_train = df_data[df_data['date'] < target_date]
# delete train data extreme values:
pred_df = df_test[['date', 'name', TARGET[0]]]
df_train = del_extreme(df_train)
# normalize train data and test data
scaler = StandardScaler()
df_train[MONFEATURES] = scaler.fit_transform(df_train[MONFEATURES])
df_test[MONFEATURES] = scaler.transform(df_test[MONFEATURES])
df_train = df_train.drop(ANCHORS, axis=1)
df_test = df_test.drop(ANCHORS, axis=1)
y_train = pd.DataFrame(columns=TARGET)
y_test = pd.DataFrame(columns=TARGET)
X_train = df_train.drop(TARGET, axis=1)
y_train[TARGET] = df_train[TARGET]
X_test = df_test.drop(TARGET, axis=1)
y_test[TARGET] = df_test[TARGET]
return X_train, y_train, X_test, y_test, pred_df
# the documents of randomforest:
# https://scikit-learn.org/0.15/modules/generated/sklearn.ensemble.RandomForestRegressor.html
def rf_model(X_train, y_train, X_test, y_test, params_dict):
logger.info('traning randomforest regeression model')
rf_reg1 = RandomForestRegressor(n_estimators=params_dict['n_estimators'],
criterion=params_dict['loss'],
max_depth=params_dict['max_depth'],
min_samples_split=params_dict['min_samples_split'],
min_samples_leaf=params_dict['min_samples_leaf'],
max_features='auto',
max_leaf_nodes=None,
bootstrap=True,
oob_score=False,
n_jobs=params_dict['n_jobs'],
random_state=2,
verbose=True)
rf_reg = xg.XGBRegressor(objective='reg:squarederror',
learning_rate=0.30,
eval_metric=["error", "logloss"],
n_estimators=8,
max_depth=10,
min_child_weight=20,
subsample=1.0,
colsample_bytree=1,
seed=0)
rf_reg.fit(X_train, y_train)
R2_train = rf_reg.score(X_train, y_train) * 100
R2_test = rf_reg.score(X_test, y_test) * 100
pred_y_test = rf_reg.predict(X_test)
pred_y_train = rf_reg.predict(X_train)
mse_test = mean_squared_error(y_test, pred_y_test)
mse_train = mean_squared_error(y_train, pred_y_train)
feats_importance = {} # a dict to hold feature_name: feature_importance
for feature, importance in zip(X_train.columns, rf_reg.feature_importances_):
feats_importance[feature] = importance # add the name/value pair
logger.info(feats_importance)
return rf_reg, mse_train, R2_train, mse_test, R2_test, feats_importance
# the documents link:
# https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html
def neural_model(X_train, y_train, X_test, y_test, params_dict):
logger.info('traning neural regeression model')
neueral_reg = MLPRegressor(hidden_layer_sizes=params_dict['layers_unit'],
activation='relu',
solver='sgd',
alpha=params_dict['alpha'],
batch_size='auto',
learning_rate='adaptive',
learning_rate_init=params_dict['init_lr'],
power_t=0.5,
max_iter=params_dict['max_iter'],
shuffle=True,
random_state=2,
tol=params_dict['tol'],
verbose=True,
warm_start=True,
momentum=0.9,
nesterovs_momentum=True,
early_stopping=True,
validation_fraction=0.1,
n_iter_no_change=params_dict['n_iter_no_change'])
neueral_reg.fit(X_train, y_train)
logger.info('neueral Regression: R^2 score on training set', neueral_reg.score(X_train, y_train) * 100)
logger.info('neueral Regression: R^2 score on test set', neueral_reg.score(X_test, y_test) * 100)
return neueral_reg
def rolling_fit(df_train, pred_var, roll_length, cfg_rf, cfg_net):
dates = list(set(df_train['date']))
dates = sorted(dates, key=lambda date:sort_dates(date))
date_num = len(dates)
df_preds = []
# R2_ls_net_train, MSE_ls_net_train = [], []
R2_ls_rf_train, MSE_ls_rf_train = [], []
# R2_ls_net_test, MSE_ls_net_test = [], []
R2_ls_rf_test, MSE_ls_rf_test = [], []
feat_importance_all = {}
for feat_i in MONFEATURES:
feat_importance_all[feat_i] = 0
rooling_i = 0
for i in range(roll_length+1, date_num):
rooling_i += 1
target_date = dates[i]
st_date = dates[i-roll_length]
logger.info('*--{}---Training {}/{} day prediction model----*'.format(target_date,
rooling_i, date_num-roll_length+1))
# get train data :
X_train, y_train, X_test, y_test, pred_df = get_df_train(df_train, st_date, target_date)
# train random forest model:
rf_reg, mse_train_rf, R2_train_rf, mse_test_rf, R2_test_rf, feat_importance = rf_model(X_train, y_train[pred_var],
X_test, y_test[pred_var], cfg_rf)
# record feature importance:
for feat_i in MONFEATURES:
feat_importance_all[feat_i] = feat_importance_all[feat_i] + feat_importance[feat_i]
# predict return on target date:
pred_rf = rf_reg.predict(X_test)
logger.info('random forest train MSE: {:.6f}, random forest train R2: {:.6f}'.format(mse_train_rf, R2_train_rf))
logger.info('random forest test MSE: {:.6f}, random forest test R2: {:.6f}'.format(mse_test_rf, R2_test_rf))
MSE_ls_rf_train.append(mse_train_rf)
MSE_ls_rf_test.append(mse_test_rf)
R2_ls_rf_train.append(R2_train_rf)
R2_ls_rf_test.append(R2_test_rf)
# neural_reg = neural_model(X_train, y_train, X_test, y_test, cfg_net)
# pred_net = neural_reg.predict(X_test)
# net_trianed = mltnet.train(X_train, y_train, cfg_net)
# train_pred_net, mse_train_net, R2_train_net = mltnet.predict(X_train, y_train, net_trianed, cfg_net.batch, cfg_net.GPU)
# test_pred_net, mse_test_net, R2_test_net = mltnet.predict(X_test, y_test, net_trianed, cfg_net.batch, cfg_net.GPU)
# logger.info('network train MSE: {:.6f}, network train R2: {:.6f}'.format(mse_train_net, R2_train_net))
# logger.info('network test MSE: {:.6f}, network test R2: {:.6f}'.format(mse_test_net, R2_test_net))
# MSE_ls_net_train.append(mse_train_net)
# MSE_ls_net_test.append(mse_test_net)
# R2_ls_net_train.append(R2_train_net)
# R2_ls_net_test.append(R2_test_net)
df_pred_i = pred_df
df_pred_i['pred_rf'] = pred_rf
# df_pred_i['pred_nnet'] = test_pred_net
df_preds.append(df_pred_i)
df_pred = pd.concat(df_preds, ignore_index=True)
logger.info('RF: average train MSE: {:.6f}, average train R2: {:.6f}'.format(np.mean(MSE_ls_rf_train), np.mean(R2_ls_rf_train)))#
logger.info('RF: average test MSE: {:.6f}, average test R2 : {:.6f}'.format(np.mean(MSE_ls_rf_test), np.mean(R2_ls_rf_test)))
# logger.info('DL: average train MSE of neteork: {:.6f}, the average train R2 of neteork: {:.6f}'.format(np.mean(MSE_ls_net_train), np.mean(R2_ls_net_train)))
# logger.info('DL: average test MSE of neteork: {:.6f}, the average test R2 of neteork: {:.6f}'.format(np.mean(MSE_ls_net_test), np.mean(R2_ls_net_test)))
for feat_i in MONFEATURES:
feat_importance_all[feat_i] = feat_importance_all[feat_i]/rooling_i
logger.info(feat_importance_all)
return df_pred
if __name__ == '__main__':
data_path = '../data/test_data_crsp.csv'
path_pred = '../data/df_pred_crsp.csv'
roll_length = 12
pred_var = 'ret'
df_train = pd.read_csv(data_path)
df_train = df_train.drop(['Unnamed: 0'], axis=1)
# random forest parameters setting:
cfg_rf = {'n_estimators':8,
'loss':'mse',
'max_depth':8,
'min_samples_split':20,
'min_samples_leaf':10,
'n_jobs': 4}
# neural network parameters setting:
params_ne = {'layers_unit':[70, 35, 18, 9],
'alpha': 0.05,
'init_lr': 0.01,
'max_iter': 1000,
'tol': 0.00001,
'n_iter_no_change': 40}
batch = 2000
max_epoch = 15
lr = 0.01
gamma = 0.1
weight_decay = 1e-4
momentum = 0.9
GPU = False
net_loss = 'huber'
delta = 0.3
cfg_net = mltnet.train_cfg(lr, batch, max_epoch, momentum,
gamma, weight_decay, net_loss, GPU, delta)
df_pred = rolling_fit(df_train, pred_var, roll_length, cfg_rf, cfg_net)
df_pred.to_csv(path_pred)