-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patheval_ens.py
80 lines (75 loc) · 4.35 KB
/
eval_ens.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
import numpy as np
import torch
import pandas as pd
import statistics
from test import test_ens_by_date, max_drawdown, save_res, get_test_setting
if __name__ == '__main__':
for dataset in ['acl18', 'sz_50']:
# for dataset in ['sz_50']:
for model in ['MLP_clf']:
# for model in ['Transformer_clf']:
# for model in ['SFM_clf']:
# for model in ['LSTM_clf', 'GRU_clf', 'MLP_clf', 'ALSTM_clf']:
ret, sr, vol, dd, mdd, cr, sor = [], [], [], [], [], [], []
for seed in range(10):
net_lst = []
net, stock_lst, feature_lst, target, test_date, criterion = get_test_setting(dataset, model, seed % 10)
net_lst.append(net)
net, stock_lst, feature_lst, target, test_date, criterion = get_test_setting(dataset, model, (seed+1)%10)
net_lst.append(net)
net, stock_lst, feature_lst, target, test_date, criterion = get_test_setting(dataset, model,
(seed + 2) % 10)
net_lst.append(net)
if dataset == 'acl18':
_, ret_lst, _ = test_ens_by_date(model, net_lst, test_date, dataset, stock_lst, feature_lst, target,
10, 4, 'clf', criterion)
elif dataset == 'sz_50':
_, ret_lst, _ = test_ens_by_date(model, net_lst, test_date, dataset, stock_lst, feature_lst, target,
25, 4, 'clf', criterion)
ret.append(sum(ret_lst))
sr.append(sum(ret_lst) / np.std(ret_lst) / np.sqrt(len(ret_lst)))
vol.append(np.std(ret_lst))
mdd.append(max_drawdown(ret_lst))
cr.append(sum(ret_lst) / max_drawdown(ret_lst))
neg_ret_lst = []
for day_ret in ret_lst:
if day_ret < 0:
neg_ret_lst.append(day_ret)
dd.append(np.std(neg_ret_lst))
sor.append(sum(ret_lst) / np.std(neg_ret_lst) / np.sqrt(len(ret_lst)))
model = 'MLP_clf_ens'
save_res(ret, sr, vol, dd, mdd, cr, sor, dataset, model)
for dataset in ['acl18', 'sz_50']:
# for model in ['Transformer_reg']:
for model in ['MLP_reg','GRU_reg']:
# for model in ['LSTM_reg', 'GRU_reg', 'MLP_reg', 'ALSTM_reg']:
ret, sr, vol, dd, mdd, cr, sor = [], [], [], [], [], [], []
for seed in range(10):
net_lst = []
net, stock_lst, feature_lst, target, test_date, criterion = get_test_setting(dataset, model, seed % 10)
net_lst.append(net)
net, stock_lst, feature_lst, target, test_date, criterion = get_test_setting(dataset, model,
(seed + 1) % 10)
net_lst.append(net)
net, stock_lst, feature_lst, target, test_date, criterion = get_test_setting(dataset, model,
(seed + 2) % 10)
net_lst.append(net)
if dataset == 'acl18':
_, ret_lst, _ = test_ens_by_date(model, net_lst, test_date, dataset, stock_lst, feature_lst, target,
10, 4, 'reg', criterion)
elif dataset == 'sz_50':
_, ret_lst, _ = test_ens_by_date(model, net_lst, test_date, dataset, stock_lst, feature_lst, target,
25, 4, 'reg', criterion)
ret.append(sum(ret_lst))
sr.append(sum(ret_lst) / np.std(ret_lst) / np.sqrt(len(ret_lst)))
vol.append(np.std(ret_lst))
mdd.append(max_drawdown(ret_lst))
cr.append(sum(ret_lst) / max_drawdown(ret_lst))
neg_ret_lst = []
for day_ret in ret_lst:
if day_ret < 0:
neg_ret_lst.append(day_ret)
dd.append(np.std(neg_ret_lst))
sor.append(sum(ret_lst) / np.std(neg_ret_lst) / np.sqrt(len(ret_lst)))
model = model+'_ens'
save_res(ret, sr, vol, dd, mdd, cr, sor, dataset, model)