Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(ml-strategy): skip retrain on a single class training window

Lanre Shittu8e9dedeaf0d1
diff --git a/agent/src/skills/ml-strategy/SKILL.md b/agent/src/skills/ml-strategy/SKILL.md
index efe62e602..47b431f27 100644
--- a/agent/src/skills/ml-strategy/SKILL.md
+++ b/agent/src/skills/ml-strategy/SKILL.md
@@ -150,6 +150,13 @@ def walk_forward_predict(
if len(X_train) < 50:
continue
+ # A single-class window (e.g. a sustained one-directional trend)
+ # crashes fit()/predict_proba() on every model type below. Skip
+ # the retrain and keep serving the previous model, same as the
+ # too-few-samples case above.
+ if len(np.unique(y_train)) < 2:
+ continue
+
# Standardization: fit only on training set
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
diff --git a/agent/tests/test_ml_strategy_skill.py b/agent/tests/test_ml_strategy_skill.py
index 0c0f0555f..0c5b76791 100644
--- a/agent/tests/test_ml_strategy_skill.py
+++ b/agent/tests/test_ml_strategy_skill.py
@@ -65,7 +65,7 @@ def test_walk_forward_purges_labels_not_observable_at_prediction_time() -> None:
_namespace(),
)
features = pd.DataFrame({"row": np.arange(70, dtype=float)})
- labels = pd.Series(np.zeros(70, dtype=float))
+ labels = pd.Series(np.arange(70, dtype=float) % 2)
_RecordingModel.fits.clear()
walk_forward_predict(features, labels, min_train_size=60, retrain_freq=100)
@@ -80,7 +80,7 @@ def test_one_bar_horizon_preserves_existing_training_window() -> None:
_namespace(),
)
features = pd.DataFrame({"row": np.arange(70, dtype=float)})
- labels = pd.Series(np.zeros(70, dtype=float))
+ labels = pd.Series(np.arange(70, dtype=float) % 2)
_RecordingModel.fits.clear()
walk_forward_predict(
@@ -94,6 +94,40 @@ def test_one_bar_horizon_preserves_existing_training_window() -> None:
assert _RecordingModel.fits[0][-1, 0] == 59.0
+def test_walk_forward_skips_a_single_class_window_with_a_real_classifier() -> None:
+ """The stub model above accepts any label distribution; scikit-learn's
+ classifiers do not. A training window that happens to hold only one
+ class (a sustained one-directional trend) must not crash fit() or
+ predict_proba() on any of the three supported model types."""
+ from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
+ from sklearn.linear_model import LogisticRegression
+ from sklearn.preprocessing import StandardScaler
+
+ namespace = {
+ "np": np,
+ "pd": pd,
+ "StandardScaler": StandardScaler,
+ "RandomForestClassifier": RandomForestClassifier,
+ "GradientBoostingClassifier": GradientBoostingClassifier,
+ "LogisticRegression": LogisticRegression,
+ }
+ walk_forward_predict = _load_node(
+ "walk_forward_predict", ast.FunctionDef, namespace
+ )
+
+ rng = np.random.default_rng(0)
+ features = pd.DataFrame({f"f{i}": rng.standard_normal(300) for i in range(5)})
+ labels = pd.Series(0.0, index=range(300))
+ labels.iloc[280:] = 1.0 # single class for every window until near the end
+
+ for model_type in ("random_forest", "gradient_boosting", "ridge"):
+ result = walk_forward_predict(
+ features, labels, min_train_size=252, retrain_freq=20, model_type=model_type
+ )
+ assert not result.isna().any()
+ assert result.between(-1.0, 1.0).all()
+
+
def test_signal_engine_preserves_unavailable_future_labels_as_nan() -> None:
captured: dict[str, object] = {}