Skip to main content
src/eventstudy/car.py · chapter The batch event study, file 2 · 193 lines · open in the repository

What it does

Batch event study: one cumulative abnormal return (CAR) per stock per event. This is the “answer key” the streaming job is compared against (decision 0004). The idea, for one stock and one event (say Walmart on Black Friday 2023):
  1. Normal behaviour. Over the ESTIMATION_DAYS trading days before the event window, fit the market model by ordinary least squares (OLS):
beta says how much the stock usually moves when the market (SPY) moves. 2. Abnormal return (AR) on each day of the event window (days -5..+5 around day 0):
i.e. what the stock did minus what the model expected from the market that day. 3. CAR = the sum of the window’s ARs. A CAR of 0.03 means the stock beat its normal behaviour by about 3% over the 11 days. Rules shared with the streaming job (src/common/study.py, decision 0004):
  • simple returns, SPY as the market, window -5..+5, 120 estimation days (at least 100)
  • day 0 = first trading day on or after the event date (weekend events move to Monday)
  • events come from the calendar (expand_events), not from master’s event_type column
  • the estimation window ends before day -5, so the event never leaks into its own baseline (“no look-ahead”)
It also scores attention exactly like the stream: each day’s Wikipedia views against the ATTENTION_BASELINE_DAYS days that end ESTIMATION_GAP_DAYS earlier, in standard deviations (z_views); a spike is z_views >= ATTENTION_SPIKE_Z. This file is deliberately written apart from src/streaming/signals.py (numpy on whole arrays instead of the stream’s day-by-day state), so matching numbers mean both are right. Run from anywhere: python3 src/eventstudy/car.py Output: data/results/batch_event_car.parquet (contract: batch_event_car)

What is inside

  • OUTPUT · line 50
  • fit_market_model(ret, mkt) · line 54: OLS fit of ret = alpha + beta * mkt on the days where both are present.
  • attention_z(views) · line 72: z-score of each day’s views against its baseline, NaN without enough baseline.
  • event_car(ticker, event, dates, ret, mkt, z_views) · line 92: The CAR row for one ticker and one event occurrence, or None when the event is outside this ticker’s data or its window is not complete yet.
  • compute_cars(master, events) · line 136: batch_event_car rows for every retailer in master and every calendar event.
  • code_version() · line 163: The git commit the results came from, marked -dirty if files were changed since.
  • main() · line 171

Connections

Uses: src/common/contracts.py, src/common/settings.py, src/common/study.py Used by: src/eventstudy/stats.py, src/eventstudy/charts.py, scripts/export_results.py, scripts/compare_stream_batch.py, src/ml/train.py, src/hosting/snapshot.py, src/hosting/publish_hf.py, tests/test_eventstudy.py

Source

src/eventstudy/car.py