forked from SJTU-DMTai/MASTER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_model.py
170 lines (133 loc) · 5.66 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
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
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)
self.fitted = False
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)
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)
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 = True
def fit(self, dl_train, dl_valid):
train_loader = self._init_data_loader(dl_train, shuffle=True, drop_last=True)
valid_loader = self._init_data_loader(dl_valid, shuffle=False, drop_last=True)
self.fitted = True
best_param = None
for step in range(self.n_epochs):
train_loss = self.train_epoch(train_loader)
val_loss = self.test_epoch(valid_loader)
print("Epoch %d, train_loss %.6f, valid_loss %.6f " % (step, train_loss, val_loss))
best_param = copy.deepcopy(self.model.state_dict())
if train_loss <= self.train_stop_loss_thred:
break
torch.save(best_param, f'{self.save_path}{self.save_prefix}master_{self.seed}.pkl')
def predict(self, dl_test):
if not self.fitted:
raise ValueError("model is not fitted yet!")
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]
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(ic)/np.std(ric)
}
return predictions, metrics