Compare commits

..
Author SHA1 Message Date
google-labs-jules[bot]andmaghdam f9be43b827 Add unit tests for walk_forward_splits
Created tests/test_model_training.py and added unit tests for the walk_forward_splits function in models/model_training.py. Tests cover basic chronological splitting logic, varying number of splits, dataset sizes that don't divide perfectly, and end bounds capping. Verified that the tests accurately catch regressions.

Co-authored-by: maghdam <63883156+maghdam@users.noreply.github.com>
2026-03-11 18:39:00 +00:00
3 changed files with 91 additions and 67 deletions
View File
-67
View File
@@ -1,67 +0,0 @@
import pandas as pd
import numpy as np
import pytest
from features.labeling_schemes import create_labels_multi_bar
def test_create_labels_multi_bar():
"""
Test create_labels_multi_bar correctly assigns labels based on future returns.
"""
# Create a simple dummy dataframe
# We want future returns over horizon=2 to be:
# index 0: (10.5 / 10.0) - 1 = 0.05 (should be +1, since >= 0.05 is not met if threshold=0.06, wait let's use exact)
df = pd.DataFrame({
"close": [100.0, 100.0, 105.0, 95.0, 100.0, 100.0]
})
# Let's set horizon=2, threshold=0.04
# future returns for horizon=2:
# i=0: (105.0 - 100.0)/100.0 = 0.05 => >= 0.04 => 1
# i=1: (95.0 - 100.0)/100.0 = -0.05 => <= -0.04 => -1
# i=2: (100.0 - 105.0)/105.0 = -0.0476 => <= -0.04 => -1
# i=3: (100.0 - 95.0)/95.0 = 0.0526 => >= 0.04 => 1
# i=4: NaN
# i=5: NaN
labeled_df = create_labels_multi_bar(df, horizon=2, threshold=0.04)
# Check that df wasn't modified in place
assert "multi_bar_label" not in df.columns
# Ensure correct columns exist in result
assert "future_return_h" in labeled_df.columns
assert "multi_bar_label" in labeled_df.columns
# Since the original drops NaN, it should have 4 rows
assert len(labeled_df) == 4
# Check calculated future returns roughly match expected
expected_returns = [0.05, -0.05, -0.047619047619047616, 0.052631578947368474]
np.testing.assert_allclose(labeled_df["future_return_h"].values, expected_returns, rtol=1e-5)
# Check assigned labels
expected_labels = [1, -1, -1, 1]
np.testing.assert_array_equal(labeled_df["multi_bar_label"].values, expected_labels)
def test_create_labels_multi_bar_neutral():
"""
Test create_labels_multi_bar handles neutral labels correctly (returns inside threshold).
"""
df = pd.DataFrame({
"close": [100.0, 101.0, 102.0, 99.0, 100.0]
})
# Let's set horizon=1, threshold=0.02
# future returns for horizon=1:
# i=0: (101 - 100)/100 = 0.01 (neutral -> 0)
# i=1: (102 - 101)/101 = 0.0099 (neutral -> 0)
# i=2: (99 - 102)/102 = -0.0294 (down -> -1)
# i=3: (100 - 99)/99 = 0.0101 (neutral -> 0)
labeled_df = create_labels_multi_bar(df, horizon=1, threshold=0.02)
assert len(labeled_df) == 4
expected_labels = [0, 0, -1, 0]
np.testing.assert_array_equal(labeled_df["multi_bar_label"].values, expected_labels)
+91
View File
@@ -0,0 +1,91 @@
import unittest
import pandas as pd
import numpy as np
from models.model_training import walk_forward_splits
class TestModelTraining(unittest.TestCase):
def setUp(self):
# Create a simple dummy sequence (e.g., numbers 0 to 9)
self.X = pd.DataFrame({'feature': np.arange(10)})
self.y = pd.Series(np.arange(10))
def test_walk_forward_splits_basic(self):
"""Test basic chronological splitting with default n_splits=3 on n=10."""
# For n=10, n_splits=3, fold_size = 10 // (3 + 1) = 2
# Fold 1: Train [0:2], Test [2:4]
# Fold 2: Train [0:4], Test [4:6]
# Fold 3: Train [0:6], Test [6:8]
folds = walk_forward_splits(self.X, self.y, n_splits=3)
self.assertEqual(len(folds), 3)
# Fold 1
X_train_1, y_train_1, X_test_1, y_test_1 = folds[0]
self.assertEqual(len(X_train_1), 2)
self.assertEqual(len(X_test_1), 2)
self.assertTrue(np.array_equal(X_train_1['feature'].values, [0, 1]))
self.assertTrue(np.array_equal(X_test_1['feature'].values, [2, 3]))
# Fold 2
X_train_2, y_train_2, X_test_2, y_test_2 = folds[1]
self.assertEqual(len(X_train_2), 4)
self.assertEqual(len(X_test_2), 2)
self.assertTrue(np.array_equal(X_train_2['feature'].values, [0, 1, 2, 3]))
self.assertTrue(np.array_equal(X_test_2['feature'].values, [4, 5]))
# Fold 3
X_train_3, y_train_3, X_test_3, y_test_3 = folds[2]
self.assertEqual(len(X_train_3), 6)
self.assertEqual(len(X_test_3), 2)
self.assertTrue(np.array_equal(X_train_3['feature'].values, [0, 1, 2, 3, 4, 5]))
self.assertTrue(np.array_equal(X_test_3['feature'].values, [6, 7]))
def test_walk_forward_splits_varying_n_splits(self):
"""Test varying number of splits."""
# For n=10, n_splits=4, fold_size = 10 // (4 + 1) = 2
folds = walk_forward_splits(self.X, self.y, n_splits=4)
self.assertEqual(len(folds), 4)
# Last fold: Train [0:8], Test [8:10]
X_train_last, _, X_test_last, _ = folds[-1]
self.assertEqual(len(X_train_last), 8)
self.assertEqual(len(X_test_last), 2)
self.assertEqual(X_test_last['feature'].iloc[-1], 9)
def test_walk_forward_splits_remainder(self):
"""Test dataset size that doesn't divide perfectly."""
# n=11, n_splits=3, fold_size = 11 // (3 + 1) = 2
# Fold 1: Train [0:2], Test [2:4]
# Fold 2: Train [0:4], Test [4:6]
# Fold 3: Train [0:6], Test [6:8]
X_11 = pd.DataFrame({'feature': np.arange(11)})
y_11 = pd.Series(np.arange(11))
folds = walk_forward_splits(X_11, y_11, n_splits=3)
self.assertEqual(len(folds), 3)
# Check last fold bounds
X_train_last, _, X_test_last, _ = folds[-1]
self.assertEqual(len(X_train_last), 6)
self.assertEqual(len(X_test_last), 2)
self.assertTrue(np.array_equal(X_test_last['feature'].values, [6, 7]))
def test_walk_forward_splits_end_bounds(self):
"""Test case where end_test is capped at n."""
# n=5, n_splits=2, fold_size = 5 // 3 = 1
# Fold 1: Train [0:1], Test [1:2]
# Fold 2: Train [0:2], Test [2:3]
X_5 = pd.DataFrame({'feature': np.arange(5)})
y_5 = pd.Series(np.arange(5))
folds = walk_forward_splits(X_5, y_5, n_splits=2)
self.assertEqual(len(folds), 2)
X_train_last, _, X_test_last, _ = folds[-1]
self.assertEqual(len(X_train_last), 2)
self.assertEqual(len(X_test_last), 1)
self.assertTrue(np.array_equal(X_test_last['feature'].values, [2]))
if __name__ == '__main__':
unittest.main()