-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfeature_engineering.py
More file actions
executable file
·284 lines (247 loc) · 11.1 KB
/
feature_engineering.py
File metadata and controls
executable file
·284 lines (247 loc) · 11.1 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# import libraries
import pandas as pd
import numpy as np
from scipy import stats
from sklearn.preprocessing import KBinsDiscretizer
# compute price log returns
def returns(series, lags=1):
"""
Computes the log returns of a price series.
Parameters
----------
series: Series or DataFrame
DatetimeIndex and price series.
lags: int, default 1
Interval over which to compute returns: 1 computes daily return, 5 weekly returns, etc
Returns
-------
ret: Series or DataFrame
Log differenced returns of price series.
"""
# compute log returns
ret = np.log(series) - np.log(series).shift(lags)
# drop emtpy rows
ret.dropna(how='all', inplace=True)
return ret
# set target volatility for macro factors
def target_vol(ret_df, ann_vol=0.15):
"""
Set volatility of returns to be equal to a specific vol target.
Parameters
----------
ret_df: DataFrame
DataFrame with DatetimeIndex and returns to be vol-adjusted.
ann_vol: float
Target annualized volatility.
Returns
-------
df: DataFrame
DataFrame with vol-adjusted returns.
"""
# set target vol
norm_factor = 1 / ((ret_df.std() / ann_vol) * np.sqrt(12))
df = ret_df * norm_factor
return df
# normalize features
def normalize(features, window_type='fixed', lookback=10, method='z-score'):
"""
Normalizes features.
Parameters
----------
features: Series or DataFrame
Series or DataFrame with DatetimeIndex and features to normalize.
window_type: str, {'fixed', 'expanding', 'rolling}, default 'fixed'
Provide a window type. If None, all observations are used in the calculation.
lookback: int, default 10
Size of the moving window. This is the minimum number of observations used for the rolling or expanding statistic.
method: str, {'z-score', 'quantile', 'min-max', 'percentile'}, default 'z-score'
z-score: subtracts mean and divides by standard deviation.
quantile: subtracts median and divides by interquartile range.
min-max: brings all values into the range [0,1] by subtracting the min and divising by the range (max - min).
percentile: converts values to their percentile rank relative to the observations in the defined window type.
Returns
-------
norm_features: Series or DataFrame
DatetimeIndex and normalized features
"""
# rolling window type
if window_type == 'rolling':
# z-score method
if method == 'z-score':
norm_features = (features - features.rolling(lookback).mean()) / features.rolling(lookback).std()
# quantile method
elif method == 'quantile':
norm_features = (features - features.rolling(lookback).median()) / (features.rolling(lookback).quantile(0.75) - features.rolling(looback).quantile(0.25))
# min-max method
elif method == 'min-max':
norm_features = (features - features.rolling(lookback).min()) / (features.rolling(lookback).max() - features.rolling(lookback).min())
# percentile method
elif method == 'percentile':
norm_features = features.rolling(lookback).apply(lambda x: stats.percentileofscore(x, x[-1])/100, raw=True)
# None
else:
norm_features = features
# expanding window type
elif window_type == 'expanding':
# z-score method
if method == 'z-score':
norm_features = (features - features.expanding(lookback).mean()) / features.expanding(lookback).std()
# quantile method
elif method == 'quantile':
norm_features = (features - features.expanding(lookback).median()) / (features.expanding(lookback).quantile(0.75) - features.expanding(looback).quantile(0.25))
# min-max method
elif method == 'min-max':
norm_features = (features - features.expanding(lookback).min()) / (features.expanding(lookback).max() - features.expanding(lookback).min())
# percentile method
elif method == 'percentile':
norm_features = features.expanding(lookback).apply(lambda x: stats.percentileofscore(x, x[-1])/100, raw=True)
# None
else:
norm_features = features
# fixed window type
else:
# z-score method
if method == 'z-score':
norm_features = (features - features.mean()) / features.std()
# quantile method
elif method == 'quantile':
norm_features = (features - features.median()) / (features.quantile(0.75) - features.quantile(0.25))
# min-max method
elif method == 'min-max':
norm_features = (features - features.min()) / (features.max() - features.min())
# percentile method
elif method == 'percentile':
norm_features = features.rank(pct=True)
# None
else:
norm_features = features
# drop NaNs
norm_features = norm_features.dropna(how='all')
return norm_features
# discretize features
def discretize(features, discretization='quantile', bins=5, signal=False, tails=None):
"""
Discretizes normalized factors or targets. Discretization is the process of transforming a continuous variable
into a discrete one by creating a set of bins (or equivalently contiguous intervals/cuttoffs) that spans the range
of the variable’s values.
Parameters
----------
features: Series or Dataframe
Series or DataFrame with DatetimeIndex and features. Features are typically normalized.
discretization: str, {'quantile', 'uniform', 'kmeans'}, default 'quantile'
quantile: all bins have the same number of values.
uniform: all bins have identical widths.
kmeans: values in each bin have the same nearest center of a 1D k-means cluster.
bins: int, default 5
Number of bins.
labels: bool, default False
Converts discretized values to signal between [-1,1], otherwise defautls to range(0,bins)
tails: str, {'two', 'left', 'right'}, default None, optional
Keeps tail bins and ignores middle bins. 'two' for both tails, 'left' for left tail, 'right' for right tail.
Returns
-------
disc_features: DataFrame
Series or DataFrame with DatetimeIndex and discretized features.
"""
# return features if bin is None
if bins is None:
return features
# less than or equal 1 bin
elif bins <= 1:
print('Number of bins must be larger than 1. Please increase number of bins. \n')
return
# more than 1 bin
else:
# convert to df if series and drop NaNs
if isinstance(features, pd.Series):
features = features.to_frame().dropna()
else:
features.dropna(inplace=True)
# discretize features
discretize = KBinsDiscretizer(n_bins=bins, encode='ordinal', strategy=discretization)
disc_df = pd.DataFrame(discretize.fit_transform(features), index=features.index, columns=features.columns)
# convert labels to signal
if signal:
disc_df = ((disc_df + 1) - (disc_df + 1).median()) / disc_df.median()
# convert discretized values to tails only
if tails == 'two':
disc_df = disc_df[(disc_df == disc_df.min()) | (disc_df == disc_df.max())].fillna(0)
elif tails == 'left':
disc_df = disc_df[disc_df == disc_df.min()].fillna(0)
elif tails == 'right':
disc_df = disc_df[disc_df == disc_df.max()].fillna(0)
return disc_df.round(2)
# convert to signal
def convert_to_signal(features, window_type='fixed', lookback=10, method='z-score'):
"""
Normalizes features.
Parameters
----------
features: Series or DataFrame
Series or DataFrame with DatetimeIndex and features to normalize.
window_type: str, {'fixed', 'expanding', 'rolling}, default 'fixed'
Provide a window type. If None, all observations are used in the calculation.
lookback: int, default 10
Size of the moving window. This is the minimum number of observations used for the rolling or expanding statistic.
method: str, {'min-max', 'percentile'}, default 'percentile'
min-max: brings all values into the range [0,1] by subtracting the min and dividing by the range (max - min), then rescales to between -1 and 1.
percentile: converts values to their percentile rank relative to the observations in the defined window type, then rescales to between -1 and 1.
Returns
-------
norm_features: Series or DataFrame
DatetimeIndex and normalized features
"""
# rolling window type
if window_type == 'rolling':
# z-score method
if method == 'z-score':
norm_features = (features - features.rolling(lookback).mean()) / features.rolling(lookback).std()
# quantile method
elif method == 'quantile':
norm_features = (features - features.rolling(lookback).median()) / (features.rolling(lookback).quantile(0.75) - features.rolling(looback).quantile(0.25))
# min-max method
elif method == 'min-max':
norm_features = (features - features.rolling(lookback).min()) / (features.rolling(lookback).max() - features.rolling(lookback).min())
# percentile method
elif method == 'percentile':
norm_features = features.rolling(lookback).apply(lambda x: stats.percentileofscore(x, x[-1])/100, raw=True)
# None
else:
norm_features = features
# expanding window type
elif window_type == 'expanding':
# z-score method
if method == 'z-score':
norm_features = (features - features.expanding(lookback).mean()) / features.expanding(lookback).std()
# quantile method
elif method == 'quantile':
norm_features = (features - features.expanding(lookback).median()) / (features.expanding(lookback).quantile(0.75) - features.expanding(looback).quantile(0.25))
# min-max method
elif method == 'min-max':
norm_features = (features - features.expanding(lookback).min()) / (features.expanding(lookback).max() - features.expanding(lookback).min())
# percentile method
elif method == 'percentile':
norm_features = features.expanding(lookback).apply(lambda x: stats.percentileofscore(x, x[-1])/100, raw=True)
# None
else:
norm_features = features
# fixed window type
else:
# z-score method
if method == 'z-score':
norm_features = (features - features.mean()) / features.std()
# quantile method
elif method == 'quantile':
norm_features = (features - features.median()) / (features.quantile(0.75) - features.quantile(0.25))
# min-max method
elif method == 'min-max':
norm_features = (features - features.min()) / (features.max() - features.min())
# percentile method
elif method == 'percentile':
norm_features = features.rank(pct=True)
# None
else:
norm_features = features
# drop NaNs
norm_features = norm_features.dropna(how='all')
return norm_features