"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(combination): report no IC spread for a single observation
Rudy Celekli6f5c47c7d4a4
diff --git a/agent/src/quantlib/combination.py b/agent/src/quantlib/combination.pyindex 34bff16a1..e6822fdea 100644--- a/agent/src/quantlib/combination.py+++ b/agent/src/quantlib/combination.py@@ -166,11 +166,15 @@ def _ic_stats(ic: pd.Series) -> dict[str, Any]:"n_obs": 0,}mean = float(clean.mean())- std = float(clean.std(ddof=1)) if count > 1 else 0.0+ # A sample standard deviation needs two observations; one IC is a point,+ # not a spread. Reporting 0.0 claims the IC is perfectly stable on the+ # strength of a single date, which is why ir and weight_turnover already+ # answer None for the same sample size.+ std = float(clean.std(ddof=1)) if count > 1 else Nonereturn {"ic_mean": mean,"ic_std": std,- "ir": (mean / std) if std > 0 else None,+ "ir": (mean / std) if std is not None and std > 0 else None,"ic_positive_ratio": float((clean > 0).mean()),"n_obs": count,}diff --git a/agent/tests/quantlib/test_combination_single_ic_std.py b/agent/tests/quantlib/test_combination_single_ic_std.pynew file mode 100644index 000000000..bb7f1c49b--- /dev/null+++ b/agent/tests/quantlib/test_combination_single_ic_std.py@@ -0,0 +1,87 @@+"""A single IC observation has no sample spread to report.++``_ic_stats`` fell back to ``ic_std = 0.0`` when only one cross-section cleared+``min_assets``, so the diagnostics table claimed the factor's IC was perfectly+stable on the strength of one date -- while ``ir`` in the same row already+answered None because the spread was unestimable. ``weight_turnover`` answers+None for the same sample size. This pins the third field to that convention.++The repo's position is already stated in+``agent/tests/test_bench_single_ic_observation.py``: "A single IC cannot+estimate sample spread or enter the factor ranking."+"""++from __future__ import annotations++import math++import numpy as np+import pandas as pd++from src.quantlib.combination import combine_factors++_DATES = pd.date_range("2024-01-01", periods=6, freq="B")+_COLUMNS = [f"S{i}" for i in range(6)]+++def _panels(seed: int = 7) -> dict[str, pd.DataFrame]:+ rng = np.random.default_rng(seed)+ return {+ "mom": pd.DataFrame(rng.normal(size=(6, 6)), index=_DATES, columns=_COLUMNS),+ "val": pd.DataFrame(rng.normal(size=(6, 6)), index=_DATES, columns=_COLUMNS),+ }+++def _forward(rows: list[int], seed: int = 11) -> pd.DataFrame:+ """Forward returns readable on ``rows`` only; every other row is all-NaN."""+ rng = np.random.default_rng(seed)+ frame = pd.DataFrame(np.nan, index=_DATES, columns=_COLUMNS)+ for row in rows:+ frame.iloc[row] = rng.normal(size=len(_COLUMNS))+ return frame+++def test_one_observation_reports_no_ic_std() -> None:+ result = combine_factors(+ _panels(), _forward([2]), method="ic", min_periods=2, min_assets=3+ )++ table = result.diagnostics["factor_ic"]+ for name in ("mom", "val"):+ assert table.loc[name, "n_obs"] == 1+ # Not 0.0: there is no spread to measure from a single point.+ assert math.isnan(float(table.loc[name, "ic_std"]))+ # ir already answered None for this sample size; ic_std now agrees.+ assert math.isnan(float(table.loc[name, "ir"]))+ # The convention this follows, in the same diagnostics dict.+ assert result.diagnostics["weight_turnover"] is None+++def test_the_composite_summary_reports_no_ic_std_either() -> None:+ """composite_ic is a plain dict, so None survives instead of becoming NaN.++ ``insufficient="equal"`` gives every row a weight, so the composite is+ scored on the one date the forward returns cover.+ """+ result = combine_factors(+ _panels(), _forward([2]), method="ic", min_periods=2, min_assets=3,+ insufficient="equal",+ )++ composite = result.diagnostics["composite_ic"]+ assert composite["n_obs"] == 1+ assert composite["ic_std"] is None+ assert composite["ir"] is None+++def test_two_observations_keep_the_measured_sample_std() -> None:+ result = combine_factors(+ _panels(), _forward([2, 4]), method="ic", min_periods=2, min_assets=3+ )++ table = result.diagnostics["factor_ic"]+ for name in ("mom", "val"):+ assert table.loc[name, "n_obs"] == 2+ measured = float(table.loc[name, "ic_std"])+ assert math.isfinite(measured)+ assert measured > 0.0