src/common/study.py · chapter Shared rules, file 2 · 74 lines · open in the repository
What it does
Study settings shared by the streaming job (src/streaming/) and the batch event study (src/eventstudy/). The streaming vs batch comparison only lines up if both sides use the same return definition, market index and window lengths, so they are defined here once (decision 0004). Import them; never copy the numbers into another file. Usage:What is inside
STUDY_START_YEAR· line 16: The study period. Every download, cleaning step and event list uses these. To change the period, change these two lines, delete raw/prices/ and raw/wiki/ (the downloads skip files that already exist), and re-run the ETL.STUDY_END_YEAR· line 17STUDY_START· line 18STUDY_END· line 19MARKET_TICKER· line 24: Market model: ret_stock = alpha + beta * ret_market + error, fitted by OLS. The market index is downloaded alongside the retailers but is not one of them, so it is not in config/retailers.csv.MARKET_COUNTRY· line 26: Labels the market index gets in master. Retailers get theirs from config/retailers.csv.MARKET_RETAILER_TYPE· line 27RETURN_TYPE· line 29: Simple returns, ret = adj_close / previous adj_close - 1, as stored in clean/prices.parquet.EVENT_WINDOW· line 34: Event window in trading days around day 0, both ends included. Day 0 is the first trading day on or after the calendar date, so an event on a weekend or market holiday (US Thanksgiving, some Halloweens) starts on the next open day.ESTIMATION_DAYS· line 37: Estimation window: this many trading days, ending right before the event window.MIN_ESTIMATION_DAYS· line 39: Fewer usable days than this (start of the data, missing returns) means no estimate.ESTIMATION_GAP_DAYS· line 45: The streaming job re-estimates the model every day. Skipping the most recent ESTIMATION_GAP_DAYS trading days keeps every day of an event window out of its own baseline: for day +5 the estimation window then ends on day -6, exactly where the batch study’s fixed window ends.CONFIDENCE· line 48: Confidence level of the intervals in the batch study’s tables (src/eventstudy/stats.py).ABNORMAL_RETURN_Z· line 51: Streaming alerts. |abnormal return| / its standard deviation at or above this is “abnormal”.ATTENTION_BASELINE_DAYS· line 58: Attention spike: views at least this many standard deviations above a baseline of ATTENTION_BASELINE_DAYS trading days (with the same gap as the market model). Anushiya owns the spike rule, settled 2026-11-17. Confirmed against 10 years of real data: ~6% of ticker-days flag at z>=3, and Black Friday/Cyber Monday windows flag about 3x as often as other days; z=2.5 or z=3.5 give the same lift, just a different count, so 3.0 is kept as a reasonable middle choice (see docs/decisions/0004).MIN_ATTENTION_DAYS· line 59ATTENTION_SPIKE_Z· line 60ML_TRAIN_END_YEAR· line 67: Machine learning (decision 0007): from what the stream knows about a stock today, will tomorrow be a big move, i.e. tomorrow’s daily alert (|z_ar| >= ABNORMAL_RETURN_Z)? The split follows time, never random rows, so the model is always judged on days after the ones it learned from: fit on the training years, pick the model on the validation year, then refit on both and report on the test years.ML_VALIDATION_YEAR· line 68ML_TEST_START_YEAR· line 69ML_ALERT_TOP_SHARE· line 72: The live job flags this share of stock-days, the highest predicted probabilities, as likely big movers. The cut-off comes from the training years’ predictions.ML_EVENT_DAYS_CAP· line 74: Days to and since the nearest in-scope event are capped here (a quiet stretch is quiet).
Connections
Used by:src/common/contracts.py, etl/prices_download.py, etl/prices_clean.py, etl/pageviews_download.py, etl/pageviews_clean.py, etl/retail_clean.py, etl/build_events.py, etl/join_master.py, src/streaming/job.py, src/streaming/signals.py, scripts/check_streaming.py, src/eventstudy/car.py, src/eventstudy/stats.py, src/eventstudy/charts.py, scripts/export_results.py, src/streaming/compare.py, scripts/compare_stream_batch.py, src/ml/features.py, src/ml/train.py, src/dashboard/app.py, src/hosting/snapshot.py, src/hosting/publish_hf.py, tests/test_config.py, tests/test_event_calendar.py, tests/test_clean_files.py, tests/test_no_hardcoding.py, tests/test_streaming_signals.py, tests/test_eventstudy.py, tests/test_compare.py, tests/test_ml_features.py, tests/test_dashboard.py, tests/test_hosting.py
Source
src/common/study.py