-
Notifications
You must be signed in to change notification settings - Fork 0
/
eng_feature_functions.py
396 lines (300 loc) · 10.7 KB
/
eng_feature_functions.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import numpy as np
import pandas as pd
from scipy.signal import lfilter
# Base stats
def mean(x):
return np.mean(x)
def sum(x):
return np.sum(x)
def size(x):
return x.size
def std(x):
return np.std(x)
def first(x):
return x[0]
def last(x):
return x[-1]
def min(x):
return np.min(x)
def max(x):
return np.max(x)
def median(x):
return np.median(x)
def skewness(x):
if not isinstance(x, pd.Series):
x = pd.Series(x)
return pd.Series.skew(x)
def kurtosis(x):
if not isinstance(x, pd.Series):
x = pd.Series(x)
return pd.Series.kurtosis(x)
base_stats = [mean, sum, size, std, first, last, min, max, median, skewness, kurtosis]
# Higher order stats
def abs_energy(x):
if not isinstance(x, (np.ndarray, pd.Series)):
x = np.asarray(x)
return np.dot(x, x)
def root_mean_square(x):
return np.sqrt(np.mean(np.square(x))) if len(x) > 0 else np.NaN
def sum_values(x):
if len(x) == 0:
return 0
return np.sum(x)
def realized_volatility(series):
return np.sqrt(np.sum(series**2))
def realized_abs_skew(series):
return np.power(np.abs(np.sum(series**3)),1/3)
def realized_skew(series):
return np.sign(np.sum(series**3))*np.power(np.abs(np.sum(series**3)),1/3)
def realized_vol_skew(series):
return np.power(np.abs(np.sum(series**6)),1/6)
def realized_quarticity(series):
return np.power(np.sum(series**4),1/4)
higher_order_stats = [abs_energy, root_mean_square, sum_values, realized_volatility, realized_abs_skew, realized_skew, realized_vol_skew, realized_quarticity]
# Quantiles
def quantile(x, q):
if len(x) == 0:
return np.NaN
return np.quantile(x, q)
def quantile_01(x):
return quantile(x, 0.01)
def quantile_025(x):
return quantile(x, 0.025)
def quantile_75(x):
return quantile(x, 0.75)
def quantile_09(x):
return quantile(x, 0.9)
additional_quantiles = [quantile_01, quantile_025, quantile_75, quantile_09]
# Min/Max related
def absolute_maximum(x):
return np.max(np.absolute(x)) if len(x) > 0 else np.NaN
def max_over_min(series):
if len(series)<2:
return 0
if np.min(series) == 0:
return np.nan
return np.max(series)/np.min(series)
other_min_max = [absolute_maximum, max_over_min]
# Location of min/max
def last_location_of_maximum(x):
x = np.asarray(x)
return 1.0 - np.argmax(x[::-1]) / len(x) if len(x) > 0 else np.NaN
def first_location_of_maximum(x):
if not isinstance(x, (np.ndarray, pd.Series)):
x = np.asarray(x)
return np.argmax(x) / len(x) if len(x) > 0 else np.NaN
def last_location_of_minimum(x):
x = np.asarray(x)
return 1.0 - np.argmin(x[::-1]) / len(x) if len(x) > 0 else np.NaN
def first_location_of_minimum(x):
if not isinstance(x, (np.ndarray, pd.Series)):
x = np.asarray(x)
return np.argmin(x) / len(x) if len(x) > 0 else np.NaN
min_max_positions = [last_location_of_maximum, first_location_of_maximum, last_location_of_minimum, first_location_of_minimum]
# Peak related
def number_peaks(x, n):
x_reduced = x[n:-n]
res = None
for i in range(1, n + 1):
result_first = x_reduced > _roll(x, i)[n:-n]
if res is None:
res = result_first
else:
res &= result_first
res &= x_reduced > _roll(x, -i)[n:-n]
return np.sum(res)
def mean_n_absolute_max(x, number_of_maxima = 1):
assert (
number_of_maxima > 0
), f" number_of_maxima={number_of_maxima} which is not greater than 1"
n_absolute_maximum_values = np.sort(np.absolute(x))[-number_of_maxima:]
return np.mean(n_absolute_maximum_values) if len(x) > number_of_maxima else np.NaN
def number_peaks_2(x):
return number_peaks(x, 2)
def mean_n_absolute_max_2(x):
return mean_n_absolute_max(x, 2)
def number_peaks_5(x):
return number_peaks(x, 5)
def mean_n_absolute_max_5(x):
return mean_n_absolute_max(x, 5)
def number_peaks_10(x):
return number_peaks(x, 10)
def mean_n_absolute_max_10(x):
return mean_n_absolute_max(x, 10)
peaks = [number_peaks_2, mean_n_absolute_max_2, number_peaks_5, mean_n_absolute_max_5, number_peaks_10, mean_n_absolute_max_10]
# Count related
def count_unique(series):
return len(np.unique(series))
def count(series):
return series.size
def count_above(x, t):
if len(x)==0:
return np.nan
else:
return np.sum(x >= t) / len(x)
def count_below(x, t):
if len(x)==0:
return np.nan
else:
return np.sum(x <= t) / len(x)
def count_above_0(x):
return count_above(x, 0)
def count_below_0(x):
return count_below(x, 0)
def value_count(x, value):
if not isinstance(x, (np.ndarray, pd.Series)):
x = np.asarray(x)
if np.isnan(value):
return np.isnan(x).sum()
else:
return x[x == value].size
def value_count_0(x):
return value_count(x, 0)
def count_near_0(x, epsilon=1e-8):
return range_count(x, -epsilon, epsilon)
counts = [count_unique, count, count_above_0, count_below_0, value_count_0, count_near_0]
# Occurrence related
def count_above_mean(x):
m = np.mean(x)
return np.where(x > m)[0].size
def count_below_mean(x):
m = np.mean(x)
return np.where(x < m)[0].size
def percentage_of_reoccurring_values_to_all_values(x):
if len(x) == 0:
return np.nan
unique, counts = np.unique(x, return_counts=True)
if counts.shape[0] == 0:
return 0
return np.sum(counts > 1) / float(counts.shape[0])
def percentage_of_reoccurring_datapoints_to_all_datapoints(x):
if len(x) == 0:
return np.nan
if not isinstance(x, pd.Series):
x = pd.Series(x)
value_counts = x.value_counts()
reoccuring_values = value_counts[value_counts > 1].sum()
if np.isnan(reoccuring_values):
return 0
return reoccuring_values / x.size
def sum_of_reoccurring_values(x):
unique, counts = np.unique(x, return_counts=True)
counts[counts < 2] = 0
counts[counts > 1] = 1
return np.sum(counts * unique)
def sum_of_reoccurring_data_points(x):
unique, counts = np.unique(x, return_counts=True)
counts[counts < 2] = 0
return np.sum(counts * unique)
def ratio_value_number_to_time_series_length(x):
if not isinstance(x, (np.ndarray, pd.Series)):
x = np.asarray(x)
if x.size == 0:
return np.nan
return np.unique(x).size / x.size
reoccuring_values = [count_above_mean, count_below_mean, percentage_of_reoccurring_values_to_all_values,
percentage_of_reoccurring_datapoints_to_all_datapoints, sum_of_reoccurring_values,
sum_of_reoccurring_data_points, ratio_value_number_to_time_series_length]
# Duplicate related
def has_duplicate_max(x):
return np.sum(x == np.max(x)) >= 2
def has_duplicate_min(x):
return np.sum(x == np.min(x)) >= 2
def has_duplicate(x):
return x.size != np.unique(x).size
def count_duplicate_max(x):
return np.sum(x == np.max(x))
def count_duplicate_min(x):
return np.sum(x == np.min(x))
def count_duplicate(x):
return x.size - np.unique(x).size
count_duplicate = [count_duplicate, count_duplicate_min, count_duplicate_max]
# Change stats
def mean_abs_change(x):
return np.mean(np.abs(np.diff(x)))
def mean_change(x):
x = np.asarray(x)
return (x[-1] - x[0]) / (len(x) - 1) if len(x) > 1 else np.NaN
def mean_second_derivative_central(x):
x = np.asarray(x)
return (x[-1] - x[-2] - x[1] + x[0]) / (2 * (len(x) - 2)) if len(x) > 2 else np.NaN
def absolute_sum_of_changes(x):
return np.sum(np.abs(np.diff(x)))
def number_crossing_m(x, m):
if not isinstance(x, (np.ndarray, pd.Series)):
x = np.asarray(x)
positive = x > m
return np.where(np.diff(positive))[0].size
def number_crossing_0(x):
return number_crossing_m(x, 0)
variations = [mean_abs_change, mean_change, mean_second_derivative_central, absolute_sum_of_changes, number_crossing_0]
# Range stats
def variance(x):
return np.var(x)
def variation_coefficient(x):
mean = np.mean(x)
if mean != 0:
return np.std(x) / mean
else:
return np.nan
def large_standard_deviation(x):
if (np.max(x)-np.min(x)) == 0:
return np.nan
else:
return np.std(x)/(np.max(x)-np.min(x))
def variance_std_ratio(x):
y = np.var(x)
if y != 0:
return y/np.sqrt(y)
else:
return np.nan
def range_ratio(x):
mean_median_difference = np.abs(np.mean(x) - np.median(x))
max_min_difference = np.max(x) - np.min(x)
if max_min_difference == 0:
return np.nan
else:
return mean_median_difference / max_min_difference
def ratio_beyond_r_sigma(x, r):
if x.size == 0:
return np.nan
else:
return np.sum(np.abs(x - np.mean(x)) > r * np.asarray(np.std(x))) / x.size
def ratio_beyond_01_sigma(x):
return ratio_beyond_r_sigma(x, 0.1)
def ratio_beyond_02_sigma(x):
return ratio_beyond_r_sigma(x, 0.2)
def ratio_beyond_03_sigma(x):
return ratio_beyond_r_sigma(x, 0.3)
ranges = [variance, variation_coefficient, large_standard_deviation, variance_std_ratio, range_ratio,
ratio_beyond_01_sigma, ratio_beyond_02_sigma, ratio_beyond_03_sigma]
# Other
def log_return(list_stock_prices):
return np.log(list_stock_prices).diff()
def mean_diff(x):
return np.nanmean(np.diff(x.values))
def range_count(x, min, max):
return np.sum((x >= min) & (x < max))
def longest_strike_below_mean(x):
if not isinstance(x, (np.ndarray, pd.Series)):
x = np.asarray(x)
return np.max(_get_length_sequences_where(x < np.mean(x))) if x.size > 0 else 0
def longest_strike_above_mean(x):
if not isinstance(x, (np.ndarray, pd.Series)):
x = np.asarray(x)
return np.max(_get_length_sequences_where(x > np.mean(x))) if x.size > 0 else 0
others = [log_return, mean_diff, range_count, longest_strike_below_mean, longest_strike_above_mean]
# All functions
all_functions = base_stats + higher_order_stats + additional_quantiles + other_min_max + min_max_positions + peaks + counts + reoccuring_values + count_duplicate + variations + ranges + others
# Helper function
def _roll(a, shift):
if not isinstance(a, np.ndarray):
a = np.asarray(a)
idx = shift % len(a)
return np.concatenate([a[-idx:], a[:-idx]])
def _get_length_sequences_where(x):
if len(x) == 0:
return [0]
else:
res = [len(list(group)) for value, group in itertools.groupby(x) if value]
return res if len(res) > 0 else [0]