"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(quantlib): one table for the parameters each normal-return model estimates (#1449)
Haozhe Wu844b13b955a1
diff --git a/agent/src/quantlib/eventstudy.py b/agent/src/quantlib/eventstudy.pyindex fc0dc0ffb..c2b94b169 100644--- a/agent/src/quantlib/eventstudy.py+++ b/agent/src/quantlib/eventstudy.py@@ -96,9 +96,9 @@ MIN_ESTIMATION_OBSERVATIONS: int = 30#: ``mean_adjusted`` E[R] = mean of the estimation window. Ignores the market.NORMAL_RETURN_MODELS: tuple[str, ...] = ("market", "market_adjusted", "mean_adjusted")-#: Parameters estimated by each normal-return model, i.e. the ``k`` in the-#: Patell degrees of freedom ``n - k``. Must mirror the ``dof`` branches in-#: :func:`estimate_market_model`.+#: Parameters estimated by each normal-return model: the ``k`` in the residual+#: degrees of freedom ``n - k`` that :func:`estimate_market_model` divides by and+#: the Patell correction ``df / (df - 2)`` reads back. One table, both uses._MODEL_PARAMETER_COUNT: dict[str, int] = {"market": 2,"market_adjusted": 0,@@ -265,16 +265,14 @@ def estimate_market_model(beta = float(np.sum((market - market_mean) * (asset - asset.mean())) / market_sum_squares)alpha = float(asset.mean() - beta * market_mean)residuals = asset - (alpha + beta * market)- dof = n - 2elif model == "market_adjusted":alpha, beta = 0.0, 1.0residuals = asset - market- dof = nelse: # mean_adjustedalpha, beta = float(asset.mean()), 0.0residuals = asset - alpha- dof = n - 1+ dof = n - _MODEL_PARAMETER_COUNT[model]residual_std = float(np.sqrt(np.sum(residuals**2) / dof)) if dof > 0 else float("nan")return MarketModelFit(diff --git a/agent/tests/quantlib/test_eventstudy.py b/agent/tests/quantlib/test_eventstudy.pyindex 4f4405b16..39e7df592 100644--- a/agent/tests/quantlib/test_eventstudy.py+++ b/agent/tests/quantlib/test_eventstudy.py@@ -292,10 +292,13 @@ def test_bmp_survives_event_induced_variance_where_patell_over_rejects():def test_patell_variance_uses_each_models_own_degrees_of_freedom():# The Patell correction is df/(df-2) with df = n - k, k being the# parameters the fitted model actually estimated: 2 for "market", 0 for- # "market_adjusted", 1 for "mean_adjusted". Using the "market" k=2 for- # every model (the bug) understates the correction for "market_adjusted"- # and "mean_adjusted", so their patell_z must land strictly below what- # the k=2 formula would have produced.+ # "market_adjusted", 1 for "mean_adjusted". n/(n-2) and (n-1)/(n-3) are+ # both smaller than (n-2)/(n-4), so using the "market" k=2 for every model+ # (the bug) over-stated the variance correction and left |patell_z|+ # slightly too small: the corrected |z| lands strictly above it.+ from src.quantlib.eventstudy import NORMAL_RETURN_MODELS, _MODEL_PARAMETER_COUNT++ assert set(_MODEL_PARAMETER_COUNT) == set(NORMAL_RETURN_MODELS)returns, market = _panel(seed=42)events = [(f"S{i:02d}", returns.index[300]) for i in range(returns.shape[1])]@@ -315,6 +318,7 @@ def test_patell_variance_uses_each_models_own_degrees_of_freedom():correction_correct = df_correct / (df_correct - 2)correction_market_k = (n - 2) / (n - 4)assert correction_correct != pytest.approx(correction_market_k)+ assert correction_correct < correction_market_kscar_sum = sum(o.standardised_car for o in result.events if np.isfinite(o.standardised_car)@@ -322,6 +326,8 @@ def test_patell_variance_uses_each_models_own_degrees_of_freedom():n_scored = sum(1 for o in result.events if np.isfinite(o.standardised_car))expected_patell_z = scar_sum / np.sqrt(n_scored * correction_correct)assert result.patell_z == pytest.approx(expected_patell_z)+ bugged_patell_z = scar_sum / np.sqrt(n_scored * correction_market_k)+ assert abs(result.patell_z) > abs(bugged_patell_z)# --- dropped events are reported, never silently skipped ---