Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix: Preserve missing observations until OU lag pairs are built so a gap cannot become a one-step transition.

Rudy Celekliac9ea9925859
diff --git a/README.md b/README.md
index b359b3ac8..2a561b541 100644
--- a/README.md
+++ b/README.md
@@ -849,7 +849,7 @@ that is a bug, not a pattern.
| `options` | Black-Scholes price + greeks, implied-volatility inversion |
| `fixedincome` | Bond math, Nelson-Siegel / Svensson curve fitting |
| `credit` | Altman Z-score, Merton / KMV distance-to-default |
-| `timeseries` | Stationarity, cointegration, GARCH, bootstrap |
+| `timeseries` | Stationarity, cointegration, GARCH, bootstrap; OU fits use adjacent observed lag pairs; missing observations never become artificial one-step transitions. |
| `risk` · `var_backtest` | VaR / CVaR / EVT and their backtests |
| `attribution` | Brinson-Fachler decomposition |
| `performance` · `fundmath` | TWR / MWR / Modified Dietz; XIRR / MOIC / DPI / TVPI |
diff --git a/agent/src/quantlib/timeseries.py b/agent/src/quantlib/timeseries.py
index ec57379fb..9e9c1997d 100644
--- a/agent/src/quantlib/timeseries.py
+++ b/agent/src/quantlib/timeseries.py
@@ -274,8 +274,8 @@ def fit_ornstein_uhlenbeck(series: pd.Series, dt: float = 1.0) -> dict:
raise ValueError(f"dt must be strictly positive, got {dt}")
sm = _require("statsmodels.api", "statsmodels", "fit_ornstein_uhlenbeck")
- s = pd.Series(series, dtype=float).dropna()
- if not np.isfinite(s.values).all():
+ s = pd.Series(series, dtype=float)
+ if not np.isfinite(s.dropna().values).all():
raise ValueError("series contains non-finite values")
lagged = s.shift(1)
diff --git a/agent/tests/test_ou_missing_lag_pairs_regressions.py b/agent/tests/test_ou_missing_lag_pairs_regressions.py
new file mode 100644
index 000000000..60191c070
--- /dev/null
+++ b/agent/tests/test_ou_missing_lag_pairs_regressions.py
@@ -0,0 +1,39 @@
+import numpy as np
+import pandas as pd
+import pytest
+from src.quantlib.timeseries import fit_ornstein_uhlenbeck
+
+
+def test_ou_only_fits_adjacent_observed_pairs():
+ values = pd.Series([1.0, 2.0, 1.5, np.nan, 9.0, 6.0, 4.0, 3.0])
+ pairs = pd.concat({"curr": values, "lag": values.shift()}, axis=1).dropna()
+ design = np.column_stack([np.ones(len(pairs)), pairs["lag"]])
+ a, b = np.linalg.lstsq(design, pairs["curr"], rcond=None)[0]
+ actual = fit_ornstein_uhlenbeck(values)
+ assert actual["ar1_a"] == pytest.approx(a)
+ assert actual["ar1_b"] == pytest.approx(b)
+
+
+def test_ou_rejects_sample_with_no_three_adjacent_pairs():
+ with pytest.raises(ValueError, match="3 lag pairs"):
+ fit_ornstein_uhlenbeck(pd.Series([1.0, np.nan, 2.0, np.nan, 3.0, np.nan, 4.0]))
+
+
+def test_quantlib_tool_refuses_insufficient_adjacent_ou_pairs():
+ import json
+ from src.tools.quantlib_tool import QuantlibCallTool
+
+ result = json.loads(
+ QuantlibCallTool().execute(
+ action="call",
+ module="timeseries",
+ function="fit_ornstein_uhlenbeck",
+ kwargs={
+ "series": {
+ "__series__": {"values": [1.0, None, 2.0, None, 3.0, None, 4.0]}
+ }
+ },
+ )
+ )
+ assert result["ok"] is False
+ assert "3 lag pairs" in result["error"]