From 034bc1f2132c552d4071fd6494b6f576947af75f Mon Sep 17 00:00:00 2001 From: Mark Aron Szulyovszky Date: Sun, 19 Dec 2021 13:14:14 +0100 Subject: [PATCH] fix(Training): major bug in scaling, we only scaled training data, not the ones we're predicting from (#45) --- training/walk_forward.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/training/walk_forward.py b/training/walk_forward.py index 877ec7b..5aacc0e 100644 --- a/training/walk_forward.py +++ b/training/walk_forward.py @@ -21,16 +21,14 @@ def walk_forward_train_test( 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 + iterations_before_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]): + if iterations_before_retrain <= 0 or pd.isna(models[index-1]): train_window_start = index - window_size - 1 train_window_end = index - 1 @@ -38,26 +36,30 @@ def walk_forward_train_test( # 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) + scaler.fit(X_expanding_window.values) 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) + X_slice = scaler.transform(X_slice.values) else: X_slice = X_slice.to_numpy() current_model = clone(model) current_model.fit(X_slice, y_slice.to_numpy()) - iterations_since_retrain = 0 + iterations_before_retrain = retrain_every else: current_model = models[index-1] models[index] = current_model next_timestep = X.iloc[index].to_numpy().reshape(1, -1) + if scaler is not None: + next_timestep = scaler.transform(next_timestep) + prediction = current_model.predict(next_timestep).item() predictions[index] = prediction + iterations_before_retrain -= 1 return models, predictions