mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-16 20:38:07 +00:00
feat(Core): ensemble models, correct forward returns calculation, scaling, only train from when asset returns are available, major bug fixed in walk_forward_train_test (#35)
* fix(Core): correct forward returns calculation, classifiers are now working again, only train from when asset returns are available * feat(Utils): added get_first_valid_return_index() * feat(Ensemble): return models from `run_whole_pipeline` * feat(Ensemble): added ensemble step, fixed walk_forward_train_test predictions index confusion, * chore(Pipeline): remove unnecessary extra ensemble results dataframe * refactor(Core): removed unnecessary ensemble_train_predict, moved run_single_asset_trainig_pipeline to a separate file * feat(Training): added scaling on expanding window (the past) to walk_forward_train_test(), now printing out mean sharpe ratio * feat(CI): added environment.yml file * chore(Environment): update env.yml * feat(CI): added testing workflow * fix(CI): renamed enviroment.yml * fix(Tests): added missing new parameter to walk_forward_train_test()
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import pandas as pd
|
||||
from typing import Literal
|
||||
from training.walk_forward import walk_forward_train_test
|
||||
from sklearn.preprocessing import MinMaxScaler, Normalizer, StandardScaler
|
||||
from utils.evaluate import evaluate_predictions
|
||||
from utils.typing import SKLearnModel
|
||||
|
||||
def __get_scaler(type: Literal['normalize', 'minmax', 'standardize', 'none']):
|
||||
if type == 'normalize':
|
||||
return Normalizer()
|
||||
elif type == 'minmax':
|
||||
return MinMaxScaler(feature_range= (-1, 1))
|
||||
elif type == 'standardize':
|
||||
return StandardScaler()
|
||||
else:
|
||||
return None
|
||||
|
||||
def run_single_asset_trainig_pipeline(
|
||||
ticker_to_predict: str,
|
||||
X: pd.DataFrame,
|
||||
y: pd.Series,
|
||||
target_returns: pd.Series,
|
||||
models: list[tuple[str, SKLearnModel]],
|
||||
method: Literal['regression', 'classification'],
|
||||
sliding_window_size: int,
|
||||
retrain_every: int,
|
||||
scaler: Literal['normalize', 'minmax', 'standardize', 'none'],
|
||||
) -> tuple[pd.DataFrame, pd.DataFrame]:
|
||||
|
||||
|
||||
scaler = __get_scaler(scaler)
|
||||
|
||||
results = pd.DataFrame()
|
||||
predictions = pd.DataFrame()
|
||||
|
||||
for model_name, model in models:
|
||||
|
||||
model_over_time, preds = walk_forward_train_test(
|
||||
model_name=model_name,
|
||||
model = model,
|
||||
X = X,
|
||||
y = y,
|
||||
target_returns = target_returns,
|
||||
window_size = sliding_window_size,
|
||||
retrain_every = retrain_every,
|
||||
scaler = scaler
|
||||
)
|
||||
assert len(preds) == len(y)
|
||||
result = evaluate_predictions(
|
||||
model_name = model_name,
|
||||
target_returns = target_returns,
|
||||
y_pred = preds,
|
||||
sliding_window_size = sliding_window_size,
|
||||
method = method,
|
||||
)
|
||||
column_name = ticker_to_predict + "_" + model_name
|
||||
results[column_name] = result
|
||||
predictions[column_name] = preds
|
||||
|
||||
return results, predictions
|
||||
@@ -0,0 +1,63 @@
|
||||
import pandas as pd
|
||||
from sklearn.base import clone
|
||||
from utils.typing import SKLearnModel
|
||||
import numpy as np
|
||||
from utils.helpers import get_first_valid_return_index
|
||||
|
||||
def walk_forward_train_test(
|
||||
model_name: str,
|
||||
model: SKLearnModel,
|
||||
X: pd.DataFrame,
|
||||
y: pd.Series,
|
||||
target_returns: pd.Series,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
scaler,
|
||||
) -> tuple[pd.Series, pd.Series]:
|
||||
assert len(X) == len(y)
|
||||
predictions = pd.Series(index=y.index).rename(model_name)
|
||||
models = pd.Series(index=y.index).rename(model_name)
|
||||
|
||||
first_nonzero_return = max(get_first_valid_return_index(target_returns), get_first_valid_return_index(X.iloc[:,0]))
|
||||
train_from = first_nonzero_return + window_size + 1
|
||||
train_till = len(y)
|
||||
|
||||
iterations_since_retrain = 0
|
||||
if scaler is not None:
|
||||
scaler = clone(scaler)
|
||||
|
||||
for index in range(train_from, train_till):
|
||||
|
||||
iterations_since_retrain += 1
|
||||
|
||||
if iterations_since_retrain >= retrain_every or pd.isna(models[index-1]):
|
||||
train_window_start = index - window_size - 1
|
||||
train_window_end = index - 1
|
||||
|
||||
if scaler is not None:
|
||||
# First we need to fit on the expanding window data slice
|
||||
# This is our only way to avoid lookahead bia
|
||||
X_expanding_window = X[first_nonzero_return:train_window_end]
|
||||
scaler.fit(X_expanding_window)
|
||||
|
||||
X_slice = X[train_window_start:train_window_end]
|
||||
y_slice = y[train_window_start:train_window_end]
|
||||
|
||||
if scaler is not None:
|
||||
X_slice = scaler.transform(X_slice)
|
||||
else:
|
||||
X_slice = X_slice.to_numpy()
|
||||
|
||||
current_model = clone(model)
|
||||
current_model.fit(X_slice, y_slice.to_numpy())
|
||||
iterations_since_retrain = 0
|
||||
else:
|
||||
current_model = models[index-1]
|
||||
|
||||
models[index] = current_model
|
||||
|
||||
next_timestep = X.iloc[index].to_numpy().reshape(1, -1)
|
||||
prediction = current_model.predict(next_timestep).item()
|
||||
predictions[index] = prediction
|
||||
|
||||
return models, predictions
|
||||
Reference in New Issue
Block a user