forked from stefan-jansen/machine-learning-for-trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_evaluating_signals_using_alphalens.py
executable file
·178 lines (90 loc) · 3.35 KB
/
06_evaluating_signals_using_alphalens.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
#!/usr/bin/env python
# coding: utf-8
# # Alphalens Analysis
# In[1]:
import warnings
warnings.filterwarnings('ignore')
# In[2]:
from pathlib import Path
import pandas as pd
from alphalens.tears import create_summary_tear_sheet
from alphalens.utils import get_clean_factor_and_forward_returns
# In[3]:
idx = pd.IndexSlice
# ## Load Data
# In[4]:
with pd.HDFStore('data.h5') as store:
lr_predictions = store['lr/predictions']
lasso_predictions = store['lasso/predictions']
lasso_scores = store['lasso/scores']
ridge_predictions = store['ridge/predictions']
ridge_scores = store['ridge/scores']
# In[5]:
DATA_STORE = Path('..', 'data', 'assets.h5')
# In[6]:
def get_trade_prices(tickers, start, stop):
prices = (pd.read_hdf(DATA_STORE, 'quandl/wiki/prices').swaplevel().sort_index())
prices.index.names = ['symbol', 'date']
prices = prices.loc[idx[tickers, str(start):str(stop)], 'adj_open']
return (prices
.unstack('symbol')
.sort_index()
.shift(-1)
.tz_localize('UTC'))
# In[7]:
def get_best_alpha(scores):
return scores.groupby('alpha').ic.mean().idxmax()
# In[8]:
def get_factor(predictions):
return (predictions.unstack('symbol')
.dropna(how='all')
.stack()
.tz_localize('UTC', level='date')
.sort_index())
# ## Linear Regression
# In[9]:
lr_factor = get_factor(lr_predictions.predicted.swaplevel())
lr_factor.head()
# In[10]:
tickers = lr_factor.index.get_level_values('symbol').unique()
# In[11]:
trade_prices = get_trade_prices(tickers, 2014, 2017)
trade_prices.info()
# In[12]:
lr_factor_data = get_clean_factor_and_forward_returns(factor=lr_factor,
prices=trade_prices,
quantiles=5,
periods=(1, 5, 10, 21))
lr_factor_data.info()
# In[13]:
create_summary_tear_sheet(lr_factor_data);
# ## Ridge Regression
# In[14]:
best_ridge_alpha = get_best_alpha(ridge_scores)
ridge_predictions = ridge_predictions[ridge_predictions.alpha==best_ridge_alpha].drop('alpha', axis=1)
# In[15]:
ridge_factor = get_factor(ridge_predictions.predicted.swaplevel())
ridge_factor.head()
# In[16]:
ridge_factor_data = get_clean_factor_and_forward_returns(factor=ridge_factor,
prices=trade_prices,
quantiles=5,
periods=(1, 5, 10, 21))
ridge_factor_data.info()
# In[17]:
create_summary_tear_sheet(ridge_factor_data);
# ## Lasso Regression
# In[18]:
best_lasso_alpha = get_best_alpha(lasso_scores)
lasso_predictions = lasso_predictions[lasso_predictions.alpha==best_lasso_alpha].drop('alpha', axis=1)
# In[19]:
lasso_factor = get_factor(lasso_predictions.predicted.swaplevel())
lasso_factor.head()
# In[20]:
lasso_factor_data = get_clean_factor_and_forward_returns(factor=lasso_factor,
prices=trade_prices,
quantiles=5,
periods=(1, 5, 10, 21))
lasso_factor_data.info()
# In[21]:
create_summary_tear_sheet(lasso_factor_data);