-
Notifications
You must be signed in to change notification settings - Fork 37
/
base_model.py
211 lines (165 loc) · 7.23 KB
/
base_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
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
import numpy as np
import pandas as pd
import copy
from torch.utils.data import DataLoader
from torch.utils.data import Sampler
import torch
import torch.optim as optim
def calc_ic(pred, label):
df = pd.DataFrame({'pred':pred, 'label':label})
ic = df['pred'].corr(df['label'])
ric = df['pred'].corr(df['label'], method='spearman')
return ic, ric
def zscore(x):
return (x - x.mean()).div(x.std())
def drop_extreme(x):
sorted_tensor, indices = x.sort()
N = x.shape[0]
percent_2_5 = int(0.025*N)
# Exclude top 2.5% and bottom 2.5% values
filtered_indices = indices[percent_2_5:-percent_2_5]
mask = torch.zeros_like(x, device=x.device, dtype=torch.bool)
mask[filtered_indices] = True
return mask, x[mask]
def drop_na(x):
N = x.shape[0]
mask = ~x.isnan()
return mask, x[mask]
class DailyBatchSamplerRandom(Sampler):
def __init__(self, data_source, shuffle=False):
self.data_source = data_source
self.shuffle = shuffle
# calculate number of samples in each batch
self.daily_count = pd.Series(index=self.data_source.get_index()).groupby("datetime").size().values
self.daily_index = np.roll(np.cumsum(self.daily_count), 1) # calculate begin index of each batch
self.daily_index[0] = 0
def __iter__(self):
if self.shuffle:
index = np.arange(len(self.daily_count))
np.random.shuffle(index)
for i in index:
yield np.arange(self.daily_index[i], self.daily_index[i] + self.daily_count[i])
else:
for idx, count in zip(self.daily_index, self.daily_count):
yield np.arange(idx, idx + count)
def __len__(self):
return len(self.data_source)
class SequenceModel():
def __init__(self, n_epochs, lr, GPU=None, seed=None, train_stop_loss_thred=None, save_path = 'model/', save_prefix= ''):
self.n_epochs = n_epochs
self.lr = lr
self.device = torch.device(f"cuda:{GPU}" if torch.cuda.is_available() else "cpu")
self.seed = seed
self.train_stop_loss_thred = train_stop_loss_thred
if self.seed is not None:
np.random.seed(self.seed)
torch.manual_seed(self.seed)
torch.cuda.manual_seed_all(self.seed)
torch.backends.cudnn.deterministic = True
self.fitted = -1
self.model = None
self.train_optimizer = None
self.save_path = save_path
self.save_prefix = save_prefix
def init_model(self):
if self.model is None:
raise ValueError("model has not been initialized")
self.train_optimizer = optim.Adam(self.model.parameters(), self.lr)
self.model.to(self.device)
def loss_fn(self, pred, label):
mask = ~torch.isnan(label)
loss = (pred[mask]-label[mask])**2
return torch.mean(loss)
def train_epoch(self, data_loader):
self.model.train()
losses = []
for data in data_loader:
data = torch.squeeze(data, dim=0)
'''
data.shape: (N, T, F)
N - number of stocks
T - length of lookback_window, 8
F - 158 factors + 63 market information + 1 label
'''
feature = data[:, :, 0:-1].to(self.device)
label = data[:, -1, -1].to(self.device)
# Additional process on labels
# If you use original data to train, you won't need the following lines because we already drop extreme when we dumped the data.
# If you use the opensource data to train, use the following lines to drop extreme labels.
#########################
mask, label = drop_extreme(label)
feature = feature[mask, :, :]
label = zscore(label) # CSZscoreNorm
#########################
pred = self.model(feature.float())
loss = self.loss_fn(pred, label)
losses.append(loss.item())
self.train_optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_value_(self.model.parameters(), 3.0)
self.train_optimizer.step()
return float(np.mean(losses))
def test_epoch(self, data_loader):
self.model.eval()
losses = []
for data in data_loader:
data = torch.squeeze(data, dim=0)
feature = data[:, :, 0:-1].to(self.device)
label = data[:, -1, -1].to(self.device)
# You cannot drop extreme labels for test.
label = zscore(label)
pred = self.model(feature.float())
loss = self.loss_fn(pred, label)
losses.append(loss.item())
return float(np.mean(losses))
def _init_data_loader(self, data, shuffle=True, drop_last=True):
sampler = DailyBatchSamplerRandom(data, shuffle)
data_loader = DataLoader(data, sampler=sampler, drop_last=drop_last)
return data_loader
def load_param(self, param_path):
self.model.load_state_dict(torch.load(param_path, map_location=self.device))
self.fitted = 'Previously trained.'
def fit(self, dl_train, dl_valid=None):
train_loader = self._init_data_loader(dl_train, shuffle=True, drop_last=True)
best_param = None
for step in range(self.n_epochs):
train_loss = self.train_epoch(train_loader)
self.fitted = step
if dl_valid:
predictions, metrics = self.predict(dl_valid)
print("Epoch %d, train_loss %.6f, valid ic %.4f, icir %.3f, rankic %.4f, rankicir %.3f." % (step, train_loss, metrics['IC'], metrics['ICIR'], metrics['RIC'], metrics['RICIR']))
else: print("Epoch %d, train_loss %.6f" % (step, train_loss))
if train_loss <= self.train_stop_loss_thred:
best_param = copy.deepcopy(self.model.state_dict())
torch.save(best_param, f'{self.save_path}/{self.save_prefix}_{self.seed}.pkl')
break
def predict(self, dl_test):
if self.fitted<0:
raise ValueError("model is not fitted yet!")
else:
print('Epoch:', self.fitted)
test_loader = self._init_data_loader(dl_test, shuffle=False, drop_last=False)
preds = []
ic = []
ric = []
self.model.eval()
for data in test_loader:
data = torch.squeeze(data, dim=0)
feature = data[:, :, 0:-1].to(self.device)
label = data[:, -1, -1]
# nan label will be automatically ignored when compute metrics.
# zscorenorm will not affect the results of ranking-based metrics.
with torch.no_grad():
pred = self.model(feature.float()).detach().cpu().numpy()
preds.append(pred.ravel())
daily_ic, daily_ric = calc_ic(pred, label.detach().numpy())
ic.append(daily_ic)
ric.append(daily_ric)
predictions = pd.Series(np.concatenate(preds), index=dl_test.get_index())
metrics = {
'IC': np.mean(ic),
'ICIR': np.mean(ic)/np.std(ic),
'RIC': np.mean(ric),
'RICIR': np.mean(ric)/np.std(ric)
}
return predictions, metrics