mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-17 21:08:08 +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,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