"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(quantlib): use each model's own degrees of freedom in Patell z
Lanre Shittudd008d84dc3c
diff --git a/agent/src/quantlib/eventstudy.py b/agent/src/quantlib/eventstudy.pyindex 3485179a2..2d4bb349c 100644--- a/agent/src/quantlib/eventstudy.py+++ b/agent/src/quantlib/eventstudy.py@@ -96,6 +96,15 @@ 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`.+_MODEL_PARAMETER_COUNT: dict[str, int] = {+ "market": 2,+ "market_adjusted": 0,+ "mean_adjusted": 1,+}+@dataclass(frozen=True)class MarketModelFit:@@ -470,15 +479,14 @@ def event_study(# Patell: the standardised CARs are unit-variance under the null, up to the# t-distribution correction for having estimated the residual variance.if finite_scar.size:- corrections = np.array(- [- (o.fit.observations - 2) / (o.fit.observations - 4)- if o.fit.observations > 4- else np.nan- for o in event_outcomes- if np.isfinite(o.standardised_car)- ]- )+ corrections = []+ for o in event_outcomes:+ if not np.isfinite(o.standardised_car):+ continue+ k = _MODEL_PARAMETER_COUNT[o.fit.model]+ df = o.fit.observations - k+ corrections.append(df / (df - 2) if df > 2 else np.nan)+ corrections = np.array(corrections)variance = float(np.nansum(corrections))patell_z = float(finite_scar.sum() / np.sqrt(variance)) if variance > 0 else float("nan")else:diff --git a/agent/tests/quantlib/test_eventstudy.py b/agent/tests/quantlib/test_eventstudy.pyindex 9de48c314..4df1348d8 100644--- a/agent/tests/quantlib/test_eventstudy.py+++ b/agent/tests/quantlib/test_eventstudy.py@@ -209,6 +209,41 @@ 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.+ returns, market = _panel(seed=42)+ events = [(f"S{i:02d}", returns.index[300]) for i in range(returns.shape[1])]++ for model, k in (("market_adjusted", 0), ("mean_adjusted", 1)):+ result = event_study(+ returns,+ market,+ events,+ event_window=(-1, 1),+ estimation_window=MIN_ESTIMATION_OBSERVATIONS,+ model=model,+ )+ n = result.events[0].fit.observations+ assert all(o.fit.observations == n for o in result.events)++ df_correct = n - k+ correction_correct = df_correct / (df_correct - 2)+ correction_market_k = (n - 2) / (n - 4)+ assert correction_correct != pytest.approx(correction_market_k)++ scar_sum = sum(+ o.standardised_car for o in result.events if np.isfinite(o.standardised_car)+ )+ 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)++# --- dropped events are reported, never silently skipped ---