fix(FeatureExtractor): use a rolling z-score instead of StandardScaler with unavoidable lookahead bias (#146)

* fix(FeatureExtractor): use a rolling z-score instead of StandardScaler with unavoidable lookahead bias

* chore(Archive): removed archived models

* fix(FeatureExtractors): syntax

* fix(FeatureExtractors): mistake with expanding window
This commit is contained in:
Mark Aron Szulyovszky
2022-01-10 14:24:51 +01:00
committed by GitHub
parent 73cfc67336
commit 18768c3925
9 changed files with 9 additions and 384 deletions
@@ -1,4 +1,4 @@
from feature_extractors.feature_extractors import feature_lag, feature_mom, feature_ROC, feature_RSI, feature_STOD, feature_STOK, feature_standard_scaling, feature_vol, feature_day_of_month, feature_day_of_week, feature_month, feature_debug_future_lookahead
from feature_extractors.feature_extractors import feature_lag, feature_mom, feature_ROC, feature_RSI, feature_STOD, feature_STOK, feature_expanding_zscore, feature_vol, feature_day_of_month, feature_day_of_week, feature_month, feature_debug_future_lookahead
from utils.types import FeatureExtractorConfig
from utils.helpers import flatten
from feature_extractors.fractional_differentiation import feature_fractional_differentiation, feature_fractional_differentiation_log
@@ -25,7 +25,7 @@ __presets = dict(
stok = [('stok', feature_STOK, [10, 30, 200])],
fracdiff = [('fracdiff', feature_fractional_differentiation, [10, 30])],
fracdiff_log = [('fracdiff_log', feature_fractional_differentiation_log, [10, 30])],
standard_scaling = [('standard_scaling', feature_standard_scaling, [0])],
z_score = [('z_score', feature_expanding_zscore, [10])],
)
presets = __presets | dict(
+3 -4
View File
@@ -2,7 +2,6 @@ import pandas as pd
import numpy as np
from feature_extractors.utils import get_close_low_high
from feature_extractors.utils import apply_log_if_necessary_series
from sklearn.preprocessing import StandardScaler
def feature_debug_future_lookahead(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
return df['returns'].shift(-period)
@@ -11,9 +10,9 @@ def feature_lag(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series
assert period > 0
return df['returns'].shift(period)
def feature_standard_scaling(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
scaler = StandardScaler()
return pd.Series(scaler.fit_transform(df['close'].to_numpy().reshape(-1, 1)).squeeze(), index = df.index)
def feature_expanding_zscore(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.Series:
close = df['close']
return (close - close.expanding(period).mean()) / close.expanding(period).std()
def feature_day_of_week(df: pd.DataFrame, period: int, is_log_return: bool) -> pd.DataFrame:
return pd.get_dummies(pd.DatetimeIndex(df.index).dayofweek, drop_first=True, prefix="date_day_week").set_index(df.index)