mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-15 20:08:08 +00:00
refactor(WalkForward): separate train / test functions to help with inference later (#158)
* refactor(WalkForward): separate train / test functions (draft) to potentially help with inference later * fix(Training): use the new separate train / test functions * feat(Training): return and pass in scalers that are necessary for inference * fix(Project): runtime errors * fix(WalkForward): use the correct `train_from` value * fix(Tests): for new walk_forward functions() * refactor(WalkForward): rename `walk_forward_test()` to `walk_forward_inference()`
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import pandas as pd
|
||||
from typing import Literal
|
||||
from training.walk_forward import walk_forward_train_test
|
||||
from training.walk_forward import walk_forward_train, walk_forward_inference
|
||||
from utils.evaluate import evaluate_predictions
|
||||
from models.base import Model
|
||||
from utils.scaler import get_scaler
|
||||
@@ -31,7 +31,7 @@ def train_primary_model(
|
||||
probabilities = pd.DataFrame(index=y.index)
|
||||
|
||||
for model_name, model in models:
|
||||
model_over_time, preds, probs = walk_forward_train_test(
|
||||
model_over_time, scaler_over_time = walk_forward_train(
|
||||
model_name=model_name,
|
||||
model = model,
|
||||
X = X if model.feature_selection == 'on' else original_X,
|
||||
@@ -42,6 +42,14 @@ def train_primary_model(
|
||||
retrain_every = retrain_every,
|
||||
scaler = scaler
|
||||
)
|
||||
preds, probs = walk_forward_inference(
|
||||
model_name = model_name,
|
||||
models = model_over_time,
|
||||
X = X if model.feature_selection == 'on' else original_X,
|
||||
expanding_window = expanding_window,
|
||||
window_size = sliding_window_size,
|
||||
scalers = scaler_over_time
|
||||
)
|
||||
|
||||
assert len(preds) == len(y)
|
||||
result = evaluate_predictions(
|
||||
|
||||
+60
-32
@@ -3,24 +3,24 @@ from models.base import Model
|
||||
import numpy as np
|
||||
from utils.helpers import get_first_valid_return_index
|
||||
from tqdm import tqdm
|
||||
|
||||
from sklearn.preprocessing import MinMaxScaler, Normalizer, StandardScaler
|
||||
from typing import Union
|
||||
from sklearn.base import clone
|
||||
|
||||
def walk_forward_train_test(
|
||||
model_name: str,
|
||||
model: Model,
|
||||
X: pd.DataFrame,
|
||||
y: pd.Series,
|
||||
target_returns: pd.Series,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
scaler,
|
||||
) -> tuple[pd.Series, pd.Series, pd.DataFrame]:
|
||||
def walk_forward_train(
|
||||
model_name: str,
|
||||
model: Model,
|
||||
X: pd.DataFrame,
|
||||
y: pd.Series,
|
||||
target_returns: pd.Series,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
scaler: Union[MinMaxScaler, Normalizer, StandardScaler],
|
||||
) -> tuple[pd.Series, pd.Series]:
|
||||
assert len(X) == len(y)
|
||||
predictions = pd.Series(index=y.index).rename(model_name)
|
||||
probabilities = pd.DataFrame(index=y.index)
|
||||
models = pd.Series(index=y.index).rename(model_name)
|
||||
scalers = pd.Series(index=y.index).rename("scaler_" + model_name)
|
||||
|
||||
first_nonzero_return = max(get_first_valid_return_index(target_returns), get_first_valid_return_index(X.iloc[:,0]), get_first_valid_return_index(y))
|
||||
train_from = first_nonzero_return + window_size + 1
|
||||
@@ -29,10 +29,8 @@ def walk_forward_train_test(
|
||||
|
||||
if model.only_column is not None:
|
||||
X = X[[column for column in X.columns if model.only_column in column]]
|
||||
|
||||
|
||||
|
||||
is_scaling_on = scaler is not None and model.data_scaling == 'scaled'
|
||||
is_scaling_on = model.data_scaling == 'scaled'
|
||||
|
||||
if is_scaling_on:
|
||||
scaler = clone(scaler)
|
||||
@@ -47,39 +45,71 @@ def walk_forward_train_test(
|
||||
|
||||
train_window_end = index - 1
|
||||
|
||||
current_scaler = None
|
||||
if is_scaling_on:
|
||||
# We need to fit on the expanding window data slice
|
||||
# This is our only way to avoid lookahead bias
|
||||
current_scaler = clone(scaler)
|
||||
X_expanding_window = X[first_nonzero_return:train_window_end]
|
||||
scaler.fit(X_expanding_window.values)
|
||||
current_scaler.fit(X_expanding_window.values)
|
||||
|
||||
X_slice = X[train_window_start:train_window_end]
|
||||
y_slice = y[train_window_start:train_window_end]
|
||||
X_slice = X[train_window_start:train_window_end].to_numpy()
|
||||
y_slice = y[train_window_start:train_window_end].to_numpy()
|
||||
|
||||
if is_scaling_on:
|
||||
X_slice = scaler.transform(X_slice.values)
|
||||
else:
|
||||
X_slice = X_slice.to_numpy()
|
||||
|
||||
X_slice = current_scaler.transform(X_slice)
|
||||
|
||||
current_model = model.clone()
|
||||
|
||||
current_model.initialize_network(input_dim = len(X_slice[0]), output_dim=1)
|
||||
current_model.fit(X_slice, y_slice.to_numpy())
|
||||
current_model.fit(X_slice, y_slice)
|
||||
|
||||
iterations_before_retrain = retrain_every
|
||||
else:
|
||||
current_model = models[index-1]
|
||||
|
||||
models[index] = current_model
|
||||
scalers[index] = current_scaler
|
||||
|
||||
if model.predict_window_size == 'window_size':
|
||||
iterations_before_retrain -= 1
|
||||
|
||||
return models, scalers
|
||||
|
||||
def walk_forward_inference(
|
||||
model_name: str,
|
||||
models: pd.Series,
|
||||
scalers: pd.Series,
|
||||
X: pd.DataFrame,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
) -> tuple[pd.Series, pd.DataFrame]:
|
||||
predictions = pd.Series(index=X.index).rename(model_name)
|
||||
probabilities = pd.DataFrame(index=X.index)
|
||||
|
||||
first_nonzero_return = get_first_valid_return_index(models)
|
||||
train_from = first_nonzero_return
|
||||
train_till = X.shape[0]
|
||||
first_model = models[first_nonzero_return]
|
||||
|
||||
if first_model.only_column is not None:
|
||||
X = X[[column for column in X.columns if first_model.only_column in column]]
|
||||
|
||||
is_scaling_on = first_model.data_scaling == 'scaled'
|
||||
|
||||
for index in tqdm(range(train_from, train_till)):
|
||||
if expanding_window:
|
||||
train_window_start = first_nonzero_return
|
||||
else:
|
||||
train_window_start = index - window_size - 1
|
||||
|
||||
current_model = models[index]
|
||||
curren_scaler = scalers[index]
|
||||
|
||||
if current_model.predict_window_size == 'window_size':
|
||||
next_timestep = X.iloc[train_window_start:index].to_numpy()#.reshape(1, -1)
|
||||
else:
|
||||
next_timestep = X.iloc[index].to_numpy().reshape(1, -1)
|
||||
|
||||
if is_scaling_on:
|
||||
next_timestep = scaler.transform(next_timestep)
|
||||
next_timestep = curren_scaler.transform(next_timestep)
|
||||
|
||||
prediction, probs = current_model.predict(next_timestep)
|
||||
predictions[index] = prediction
|
||||
@@ -87,6 +117,4 @@ def walk_forward_train_test(
|
||||
probabilities = probabilities.reindex(columns = ["prob_" + str(num) for num in range(0, len(probs.T))])
|
||||
probabilities.iloc[index] = probs
|
||||
|
||||
iterations_before_retrain -= 1
|
||||
|
||||
return models, predictions, probabilities
|
||||
return predictions, probabilities
|
||||
|
||||
Reference in New Issue
Block a user