forked from RL-MLDM/alphagen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gp.py
138 lines (111 loc) · 4.59 KB
/
gp.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
import json
import os
from collections import Counter
import numpy as np
from alphagen.data.expression import *
from alphagen.models.alpha_pool import AlphaPool
from alphagen.utils.correlation import batch_pearsonr, batch_spearmanr
from alphagen.utils.pytorch_utils import normalize_by_day
from alphagen.utils.random import reseed_everything
from alphagen_generic.operators import funcs as generic_funcs
from alphagen_generic.features import *
from gplearn.fitness import make_fitness
from gplearn.functions import make_function
from gplearn.genetic import SymbolicRegressor
funcs = [make_function(**func._asdict()) for func in generic_funcs]
instruments = 'csi300'
seed = 4
reseed_everything(seed)
cache = {}
device = torch.device('cuda:1')
data = StockData(instruments, '2009-01-01', '2018-12-31', device=device)
data_valid = StockData(instruments, '2019-01-01', '2019-12-31', device=device)
data_test = StockData(instruments, '2020-01-01', '2021-12-31', device=device)
pool = AlphaPool(capacity=10,
stock_data=data,
target=target,
ic_lower_bound=None)
target_factor = target.evaluate(data)
target_factor_valid = target.evaluate(data_valid)
target_factor_test = target.evaluate(data_test)
def _metric(x, y, w):
key = y[0]
if key in cache:
return cache[key]
token_len = key.count('(') + key.count(')')
if token_len > 20:
return -1.
expr = eval(key)
try:
factor = expr.evaluate(data)
factor = normalize_by_day(factor)
ic = batch_pearsonr(factor, target_factor).mean().item()
except OutOfDataRangeError:
ic = -1.
if np.isnan(ic):
ic = -1.
cache[key] = ic
return ic
Metric = make_fitness(function=_metric, greater_is_better=True)
def try_single():
top_key = Counter(cache).most_common(1)[0][0]
v_valid = eval(top_key).evaluate(data_valid)
v_test = eval(top_key).evaluate(data_test)
ic_test = batch_pearsonr(v_test, target_factor_test).mean().item()
ic_valid = batch_pearsonr(v_valid, target_factor_valid).mean().item()
ric_test = batch_spearmanr(v_test, target_factor_test).mean().item()
ric_valid = batch_spearmanr(v_valid, target_factor_valid).mean().item()
return {'ic_test': ic_test, 'ic_valid': ic_valid, 'ric_test': ric_test, 'ric_valid': ric_valid}
def try_pool(capacity):
pool = AlphaPool(capacity=capacity,
stock_data=data,
target=target,
ic_lower_bound=None)
exprs = []
for key in dict(Counter(cache).most_common(capacity)):
exprs.append(eval(key))
pool.force_load_exprs(exprs)
pool._optimize(alpha=5e-3, lr=5e-4, n_iter=2000)
ic_test, ric_test = pool.test_ensemble(data_test, target)
ic_valid, ric_valid = pool.test_ensemble(data_valid, target)
return {'ic_test': ic_test, 'ic_valid': ic_valid, 'ric_test': ric_test, 'ric_valid': ric_valid}
generation = 0
def ev():
global generation
generation += 1
res = (
[{'pool': 0, 'res': try_single()}] +
[{'pool': cap, 'res': try_pool(cap)} for cap in (10, 20, 50, 100)]
)
print(res)
dir_ = '/path/to/save/results'
os.makedirs(dir_, exist_ok=True)
if generation % 2 == 0:
with open(f'{dir_}/{generation}.json', 'w') as f:
json.dump({'cache': cache, 'res': res}, f)
if __name__ == '__main__':
features = ['open_', 'close', 'high', 'low', 'volume', 'vwap']
constants = [f'Constant({v})' for v in [-30., -10., -5., -2., -1., -0.5, -0.01, 0.01, 0.5, 1., 2., 5., 10., 30.]]
terminals = features + constants
X_train = np.array([terminals])
y_train = np.array([[1]])
est_gp = SymbolicRegressor(population_size=1000,
generations=40,
init_depth=(2, 6),
tournament_size=600,
stopping_criteria=1.,
p_crossover=0.3,
p_subtree_mutation=0.1,
p_hoist_mutation=0.01,
p_point_mutation=0.1,
p_point_replace=0.6,
max_samples=0.9,
verbose=1,
parsimony_coefficient=0.,
random_state=seed,
function_set=funcs,
metric=Metric,
const_range=None,
n_jobs=1)
est_gp.fit(X_train, y_train, callback=ev)
print(est_gp._program.execute(X_train))