Skip to main content
Status: AGREED 2026-11-25. The contracts below describe the files as built (decisions 0005, 0006, 0007, 0010). Change one only together with src/common/contracts.py and a decision record (AGENTS.md rule 1). To change a contract, edit this file and src/common/contracts.py in the same commit. tests/test_contracts.py fails if they disagree. The tables between the contract: markers are read by that test, so keep each row as | `column` | type | meaning |. Four kinds of dataset, each in its own place: How it is studied (years, market index, windows, thresholds) is in src/common/study.py, and file locations and Kafka servers are in src/common/settings.py. Code reads all of them from there and never types them in (decision 0005; tests/test_no_hardcoding.py checks).

Rules for every dataset

  • Types: string, long (64-bit integer), int (32-bit), double, date, timestamp.
  • Time: timestamps are UTC, and their column names end in _utc. Dates are calendar dates with no time zone. Spark sessions set spark.sql.session.timeZone=UTC.
  • Tickers: Yahoo Finance symbols in capitals, for example WMT, AMZN, or L.TO for TSX listings.
  • Countries and regions: two-letter codes (US, CA, CN), or global, so a retailer’s country can be matched to an event’s region.
  • Validate before writing. Every script that writes a dataset calls common.contracts.require_contract(dataset, pandas_columns(df)) first. tests/test_clean_files.py checks the committed clean files against their contracts and against config/.
  • Kafka: one JSON object per message, with field names exactly as below and dates as YYYY-MM-DD strings.

Registries (config/)

Each file’s header must be exactly its contract’s columns, in order; the loaders in common.contracts (load_event_types, load_retailers, load_wiki_articles, load_retail_series) refuse anything else. tests/test_config.py and tests/test_event_calendar.py check every row, so adding a row can’t silently break anything.

Event calendar (expandable)

Events are data, not code (decision 0003). Three things work together:
  1. config/event_types.csv (the registry): one row per kind of event, with a rule for computing its dates. Add an event by adding a row.
  2. config/event_dates.csv: dates for events whose rule is listed, such as lunar-calendar holidays or announced sales. Each row needs a source.
  3. The events table: every occurrence of every in-scope event over the study period, computed from the two files by common.contracts.expand_events(STUDY_START_YEAR, STUDY_END_YEAR). Code calls that function instead of reading a stored copy, so it can never be out of date.
Date rules in date_rule (checked by tests/test_event_calendar.py):

event_types

Owner: Anushiya (rows agreed in a meeting or a decision record). Stored at config/event_types.csv.

event_dates

Owner: Anushiya, with a source for every row. Stored at config/event_dates.csv. Only for listed events.

events

Computed by expand_events, not stored (etl/build_events.py writes a copy to data/events.parquet for browsing). It returns a list of dicts with the columns below, dates as Python date objects. The streaming job and the batch study take event windows from here, not from master.event_type. Open question: should regional events (e.g., Boxing Day for TSX stocks) apply only to retailers whose country is in the event’s region?

retailers

Owner: Anushiya. Stored at config/retailers.csv. The stocks the project studies: the downloads, the join and the streaming job cover exactly the in-scope rows. The market index (common.study.MARKET_TICKER) is downloaded with them but is not a retailer, so it is not listed. etl/join_master.py copies each retailer’s country and retailer_type into master, so analyses can read them there; this file stays the one place they are typed.

wiki_articles

Owner: Sanchi. Stored at config/wiki_articles.csv. Every in-scope retailer needs at least one title. When an article was renamed, list both titles: Wikipedia counts views under the title readers asked for, and etl/pageviews_clean.py adds them together per ticker and day.

retail_series

Owner: Areej. Stored at config/retail_series.csv. US Census monthly retail sales series, downloaded from FRED as raw/census/<series_id>.csv.

Clean tables (clean/)

Built by etl/ from raw/ and config/, one row per key listed, over the study period in common.study. The original plan’s hourly prices and attention tables were replaced by these daily tables in the ETL phase (decisions 0004 and 0005).

prices

Owner: Mahad (etl/prices_download.py, etl/prices_clean.py). One row per ticker per trading day, for every in-scope retailer plus the market index.

pageviews

Owner: Sanchi (etl/pageviews_download.py, etl/pageviews_clean.py). One row per in-scope retailer per calendar day; days with no data are 0.

retail_sales

Owner: Areej (etl/retail_clean.py). One row per series per month. FRED gives the latest revised numbers, not the first release.

master (what Kafka carries)

Owner: Sanchi (etl/join_master.py builds clean/master.parquet; src/producers/replay_master.py sends it). Consumer: the streaming job. Kafka topic calendar-effects-master (KAFKA_TOPICS["master"]), keyed by ticker, one JSON object per row, one row per ticker per trading day (the same rows as prices). The streaming job adds timestamp_utc (the trading day at 00:00 UTC) as its event time.

Outputs

The streaming vs batch comparison (scripts/compare_stream_batch.py, rules in src/streaming/compare.py) joins stream_event_car and batch_event_car on ticker, event_type, event_date and expects every value to agree within 1e-9. Both sides take the return definition, market index and window lengths from src/common/study.py (decision 0004).

stream_signals

Owner: Mahad (the streaming job). One row per ticker per trading day, the market index excluded. Stored as Parquet in data/stream/signals/batch_id=<N>/, one folder per micro-batch (rewritten whole if Spark retries the batch, so reruns give the same files).

stream_event_car

Owner: Mahad (the streaming job). One row per ticker per event occurrence, emitted when day +5 has been processed. Stored as Parquet in data/stream/event_car/batch_id=<N>/. Each window has one market model, fitted on the ESTIMATION_DAYS trading days before day −5, the same way as the batch study. So with the same data, car should equal the batch CAR. (The daily ar in stream_signals uses a model re-fitted every day, so summing it does not give car.) alpha, beta, ar_sd, car, car_z and max_z_views are null when n_days = 0, which happens to windows too early in the data to have a model. Reading the outputs: Spark and pyarrow turn the batch_id=<N> folders into a batch_id column. Drop it before check_columns. The last few trading days of a replay stay behind the watermark and are not written until newer data arrives.

batch_event_car

Owner: Areej (src/eventstudy/car.py, the batch event study and answer key). One row per ticker per event occurrence whose window −5..+5 is complete in clean/master.parquet. Stored as Parquet in data/results/batch_event_car.parquet, with batch_event_car.json next to it recording the code version and time of the run. Same columns and meanings as stream_event_car, computed from the whole of master at once with numpy instead of day by day. Events come from expand_events (not master’s event_type), day 0 is the first trading day on or after event_date, and the market model is fitted on the ESTIMATION_DAYS trading days before day −5. z_views follows the stream’s attention rule, so max_z_views and n_spike_days should match too. The comparison joins on ticker, event_type, event_date and expects equal values.

batch_car_summary

Owner: Areej (src/eventstudy/stats.py). Significance of the mean CAR, for the report. One row per event type (plus all, where windows shared by two event types, such as US Thanksgiving and Black Friday, count once) per group: all retailers, each retailer_type, and the difference between the two types (e.g. online_minus_store, the per-event-date difference of group means). Only windows with a CAR (n_days > 0). Stored as CSV in data/results/batch_car_summary.csv.

batch_car_trend

Owner: Areej (src/eventstudy/stats.py). Does the CAR change over the years? Per event type (and all), the mean CAR of each event date regressed on its year. Stored as CSV in data/results/batch_car_trend.csv.

Machine learning (decision 0007)

The big-move model asks: from today’s daily row, will this stock make a big move tomorrow (tomorrow’s abnormal_return = 1)? src/ml/train.py trains it with Spark MLlib on stream_signals, using the features in src/ml/features.py, and the streaming job scores it live. Owner: Mahad.

stream_predictions

Stored as Parquet in data/stream/predictions/batch_id=<N>/, written by the streaming job when a trained model exists in data/models/big_move/. One row per retailer per trading day whose features are all present.

ml_metrics

Stored as CSV in data/results/ml_metrics.csv. One row per model tried: each candidate on the validation year, then the chosen model, the same model without attention features, and a no-model baseline on the test years.

ml_test_predictions

Stored as Parquet in data/results/ml_test_predictions.parquet: the chosen model’s predictions on the test years, for charts and the report.

ml_feature_importance

Stored as CSV in data/results/ml_feature_importance.csv. For trees, MLlib’s feature importances; for logistic regression, the absolute coefficients on standardized features. Normalized to sum to 1.