Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(eventstudy): keep the estimation window before the event for post-event windows

Rudy Celekli2481abae1160
diff --git a/agent/src/quantlib/eventstudy.py b/agent/src/quantlib/eventstudy.py
index c2b94b169..73b5d49f0 100644
--- a/agent/src/quantlib/eventstudy.py
+++ b/agent/src/quantlib/eventstudy.py
@@ -345,7 +345,8 @@ def event_study(
event, but must not exceed ``end``.
estimation_window: Rows used to fit the normal-return model.
estimation_gap: Rows left between the estimation window and the event
- window so the model cannot see the event.
+ window (or the event row, when the window opens after it) so the
+ model cannot see the event.
model: One of :data:`NORMAL_RETURN_MODELS`.
Returns:
@@ -403,7 +404,10 @@ def event_study(
event_start = position + start
event_end = position + end
- estimation_end = position + start - estimation_gap
+ # Anchor the estimation window before the event row as well as the
+ # event window: a window opening after the event (start > 0) must not
+ # pull the event row, or the rows after it, into the fitting sample.
+ estimation_end = position + min(start, 0) - estimation_gap
estimation_start = estimation_end - estimation_window
if estimation_start < 0:
diff --git a/agent/tests/quantlib/test_eventstudy_post_event_window.py b/agent/tests/quantlib/test_eventstudy_post_event_window.py
new file mode 100644
index 000000000..eff64802c
--- /dev/null
+++ b/agent/tests/quantlib/test_eventstudy_post_event_window.py
@@ -0,0 +1,49 @@
+"""A post-event window must not pull the event row into the estimation sample."""
+
+from __future__ import annotations
+
+import numpy as np
+import pandas as pd
+import pytest
+
+from src.quantlib.eventstudy import estimate_market_model, event_study
+
+
+def shocked_frame():
+ index = pd.bdate_range("2023-01-02", periods=400)
+ rng = np.random.default_rng(0)
+ market = pd.Series(rng.normal(0.0, 0.01, len(index)), index=index)
+ returns = pd.DataFrame({"A": market + rng.normal(0.0, 0.01, len(index))}, index=index)
+ position = 300
+ returns.iloc[position, 0] += 0.5 # the event-day reaction
+ return returns, market, index, position
+
+
[email protected]("event_window", [(15, 20), (1, 5), (11, 30)])
+def test_post_event_window_fits_on_rows_before_the_event(shocked_frame, event_window) -> None:
+ returns, market, index, position = shocked_frame
+ gap, window = 10, 120
+ result = event_study(
+ returns, market, [("A", index[position])], event_window=event_window,
+ estimation_window=window, estimation_gap=gap,
+ )
+ end = position - gap
+ expected = estimate_market_model(
+ returns["A"].iloc[end - window:end], market.iloc[end - window:end]
+ )
+ assert result.events[0].fit.residual_std == pytest.approx(expected.residual_std)
+
+
+def test_pre_event_windows_keep_their_estimation_sample(shocked_frame) -> None:
+ returns, market, index, position = shocked_frame
+ gap, window, start = 10, 120, -5
+ result = event_study(
+ returns, market, [("A", index[position])], event_window=(start, 5),
+ estimation_window=window, estimation_gap=gap,
+ )
+ end = position + start - gap
+ expected = estimate_market_model(
+ returns["A"].iloc[end - window:end], market.iloc[end - window:end]
+ )
+ assert result.events[0].fit.residual_std == pytest.approx(expected.residual_std)