README.md · chapter Orientation, file 1 · 124 lines · open in the repository
What it does
The full text is below.Source
README.md
# Calendar Effects
**How holidays and shopping events move retail stocks, in real time.**
CMPT 732 (Programming for Big Data I) group project, Fall 2026, Simon Fraser University.
Do retail stocks and public attention react to calendar events: Black Friday, Cyber Monday, Prime Day, Halloween, Chinese New Year and more? And can a streaming pipeline detect those reactions as reliably as a batch event study?
We replay ten years of daily prices and Wikipedia page views for 38 US-listed retailers (plus the S&P 500 as the market) through Kafka into Spark Structured Streaming. The pipeline flags abnormal returns and attention spikes around every event in an expandable calendar (`config/event_types.csv`), computes each event's cumulative abnormal return the moment its window closes, and scores a Spark MLlib model that predicts tomorrow's big moves. A batch market-model event study gives the answer key, and a live dashboard shows the alerts as they happen.
> **For markers:** the `final` tag marks the submitted version. Things worth looking at:
> - **Streaming:** `src/streaming/job.py` (watermark, stream-stream join with the market, per-ticker state with `applyInPandasWithState`, idempotent `foreachBatch` output), run locally and on the SFU cluster (YARN + the course Kafka), with late data.
> - **Streaming = batch, exactly:** all 2,280 event windows match an independently written batch event study (`scripts/compare_stream_batch.py`) to about 1e-15, and every daily value matches a direct numpy recomputation (`scripts/check_streaming.py`).
> - **Machine learning:** `src/ml/` trains a Spark MLlib model on the stream's output with a time-ordered, purged split and scores it live in the streaming job (decision 0007).
> - **One dashboard, on a laptop and hosted:** [calendar-effects.streamlit.app](https://calendar-effects.streamlit.app), the same `src/dashboard/app.py` (Streamlit). It shows a live run as it happens (the job publishes each micro-batch with `--publish-live`, from a laptop or from the cloud streamer on GitHub Actions, decision 0015), replays the recorded stream for any years, and shows each event's history with a trend forecast (decisions 0008, 0013).
> - **Results for the report:** [`docs/results/`](docs/results/README.md): every number, table and chart, written from the run by `scripts/export_results.py` (decision 0014).
> - **Published by the team:** GitHub [Calendar-Effects](https://github.com/Calendar-Effects), Hugging Face [calendar-effects](https://huggingface.co/calendar-effects): the [dataset](https://huggingface.co/datasets/calendar-effects/calendar-effects), the [model](https://huggingface.co/calendar-effects/big-move-model) and the [Space](https://huggingface.co/spaces/calendar-effects/dashboard). No prices are published (decisions 0010, 0014).
> - **Data:** 3 live sources, 38 retailers, about 98,000 stock-days, every list in `config/` (decisions 0005, 0009), every table under a data contract (`docs/data-contracts.md`), and over 140 tests run before every commit.
> - **How it all fits:** the docs site, [calendar-effects.mintlify.app](https://calendar-effects.mintlify.app) (`docs-site/`, Mintlify): a ten-chapter tour of the codebase with a page for every file, rebuilt from the code and checked by a test on every commit (decision 0011). Diagrams in `docs/architecture/`.
## Where things are
| Path | What |
|---|---|
| `config/` | What is studied, one CSV each: events (`event_types.csv`, `event_dates.csv`), retailers, Wikipedia articles, retail sales series. Add a row to extend the study |
| `src/common/` | Shared code: schemas and config loaders (`contracts.py`), study settings such as years and windows (`study.py`), paths and Kafka (`settings.py`) |
| `etl/` | Download and clean scripts: `raw/` (not committed) to `clean/` |
| `clean/` | The clean tables the pipeline replays |
| `src/producers/`, `src/streaming/`, `src/eventstudy/` | Kafka replay, the streaming job, the batch event study |
| `src/ml/`, `src/dashboard/` | The big-move model (Spark MLlib), the live alerts dashboard (Streamlit) |
| `src/hosting/`, `hosting/` | The dashboard's recorded data (`snapshot.py` → `hosting/snapshot/`), the live feed (`live.py`), the hosted app's extras (`hosting/streamlit/`), the Hugging Face Space (`hosting/space/`) and upload (`publish_hf.py`, cards in `hosting/cards/`), the cloud streamer's workflow and page (`hosting/workflows/`, `hosting/live-stream/`) |
| `docs/results/` | The results pack for the report: numbers, tables, charts and check outputs (`scripts/export_results.py`) |
| `docs-site/` | The docs site (Mintlify): a tour of the whole codebase in reading order, built by `scripts/build_docs.py`; preview with `cd docs-site && npx mint dev` |
| `docs/architecture/` | Diagrams and guides that explain the whole codebase (paste into Miro; the docs site shows them too) |
| `docs/running.md` | Every command, in the order you would use them |
| `docs/etl-guide.md` | How the clean data was first built, and the ETL phase's split and timeline |
| `docs/data-contracts.md` | Schemas for every dataset and Kafka message |
| `docs/STATUS.md`, `docs/decisions/` | Current status per member; decision records |
| `tests/` | Tests (`python3 -m pytest -q`), including checks that nothing in `config/` is hard-coded |
| `AGENTS.md`, `CLAUDE.md`, `.claude/` | Shared setup and rules for AI coding assistants (see `docs/decisions/0001-...`) |
| `run_everything.sh` | One command: data, streaming job, checks, batch study, comparison, model |
## Running it locally
**Every command, in the order you would use them:** [`docs/running.md`](docs/running.md) (on the docs site: *Run it*). The essentials:
**One command:** from the repo root,
```bash
./run_everything.sh --skip-etl
```
That reproduces the pipeline on the committed `clean/*.parquet` files, checks the stream against a direct computation and the batch answer key, and trains the big-move model (about 20 minutes for all 38 retailers). `./run_everything.sh --help` lists every option.
**Dashboard:** the same app runs here and hosted. Its *Replay* view plays back a recorded run with nothing else running. To watch a run live on your computer, start the dashboard, then the demo in a second terminal (it sets Java 21 itself and writes to `data/demo/`, never touching `data/stream/`):
```bash
streamlit run src/dashboard/app.py
```
```bash
scripts/demo_live.sh --private
```
The dashboard's *Live stream* view picks *A job on this computer* by itself while the job runs (folder `data/demo/stream`). Without `--private` the demo also publishes each micro-batch to the hosted dashboard (needs `hf auth login`). With no computer at all, team members start the *Live stream* workflow of `Calendar-Effects/live-stream` on github.com (decision 0015).
**After a full run:** refresh the dashboard's recording and the results pack, then publish (needs `hf auth login`; uploads only with `--upload`):
```bash
python3 src/hosting/snapshot.py
python3 scripts/export_results.py
python3 src/hosting/publish_hf.py --upload
```
### Setup
- **Python:** the script activates `.venv` (creating it if needed) and runs `pip install -r requirements.txt`. By hand:
```bash
python3 -m venv .venv
source .venv/bin/activate && pip install -r requirements.txt -r requirements-dev.txt
python3 -m pytest -q
```
- **Java:** Spark 4 needs Java 17 or 21, not a newer default such as 26. On a Mac with Homebrew:
```bash
export JAVA_HOME=/opt/homebrew/opt/openjdk@21
```
The script checks this itself. If `JAVA_HOME` is not Java 17 or 21, it looks for a Homebrew `openjdk@21` or `openjdk@17` (or a JDK registered with macOS) and sets `JAVA_HOME`. If it finds neither, it stops and tells you what to install.
- `requirements.txt` pins `pyarrow` below 21, because pyarrow 21+ breaks `applyInPandasWithState`.
### What `run_everything.sh` does
1. **Python and Java:** sets up as described above.
2. **ETL:** runs `etl/prices_download.py`, `prices_clean.py`, `pageviews_download.py`, `pageviews_clean.py` and `retail_clean.py`. The downloads call Yahoo Finance and Wikimedia live and can be slow or rate-limited. Retail sales are cleaned only if the hand-downloaded FRED files are in `raw/census/`. **`--skip-etl` skips this step** and uses the clean files already in the repo, which is the normal choice.
3. **Master table:** `etl/join_master.py` rebuilds `clean/master.parquet`, adding each retailer's `country` and `retailer_type` from `config/retailers.csv`. It is fast and always runs.
4. **Streaming:** two modes.
- **Local files (default, no Kafka):** `src/producers/replay_to_files.py` writes the replay to `data/replay/`, then `src/streaming/job.py --source files --once --fresh` processes it. Use this for a quick correctness check (about 5 minutes for all 10 years).
- **Kafka (`--kafka`):** against the course cluster (`node1.local:9092,node2.local:9092`; change it with `--bootstrap`). Runs `scripts/create_topics.py`, then `src/producers/replay_master.py` to send all 10 years to the topic, then `src/streaming/job.py --source kafka --once --fresh` to read the topic from the start and stop once it has caught up. The cluster's nodes are reachable only from the cluster network; to try Kafka on a laptop, see `docs/handoffs/2026-11-17-mahad-kafka-test.md`.
5. **Check:** `scripts/check_streaming.py` recomputes every daily abnormal return and event CAR from `clean/master.parquet` with numpy. It prints `OK` (the script then prints `PASSED`) or `FAILED`. The script exits with a non-zero code on a failed check or on any failed step.
6. **Batch event study:** `src/eventstudy/car.py` (the answer key), `stats.py` (significance tests) and `charts.py` (report charts) write to `data/results/`.
7. **Comparison:** `scripts/compare_stream_batch.py` joins the stream's event CARs to the answer key and fails on any difference beyond 1e-9.
8. **Big-move model:** `src/ml/train.py` trains and tests the Spark MLlib model and saves it to `data/models/`; the next streaming run scores it live into `data/stream/predictions/`. Skip with `--skip-ml`.
Add `--late-test` to send about 2% of messages 3 trading days late, which tests the job's watermark. The output goes to `data/stream/signals/` and `data/stream/event_car/` (and `data/stream/predictions/` when a trained model exists), the batch study's to `data/results/`, and the model to `data/models/`.
The same steps by hand, local mode:
```bash
source .venv/bin/activate && pip install -r requirements.txt
python3 etl/join_master.py
python3 src/producers/replay_to_files.py && python3 src/streaming/job.py --source files --once --fresh && python3 scripts/check_streaming.py
python3 src/eventstudy/car.py && python3 src/eventstudy/stats.py && python3 src/eventstudy/charts.py
python3 scripts/compare_stream_batch.py
python3 src/ml/train.py
```
## Team
Calendar Effects is the work of **Muhammad Mahad, Sanchi Kataria, Anushiya and Areej Siddiqui** (CMPT 732, SFU, Fall 2026). To cite it, use `CITATION.cff` (GitHub's "Cite this repository"). Current split by name (from `AGENTS.md`; details and timeline in `docs/etl-guide.md`):
| Name | ETL (due Nov 3–4) | Nov 14–22: Kafka and streaming | Nov 23–Dec 1: report and video |
|---|---|---|---|
| Mahad | Stock prices and volume | Price producer and the Spark streaming job (lead) | Demo video |
| Sanchi | Wikipedia pageviews, final join | Kafka topics, attention producer | Report: results |
| Anushiya | Ticker list and event calendar | Spike rule (settled 2026-11-17, see decision 0004), streaming job with Mahad, run-everything script, README | Report: data and methods |
| Areej | Retail sales | Batch event study and the answer key | Report: intro, assembling and editing; project management |