> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 1. Orientation

> What the project is, what one command runs, and what it installs.

Before any detail, know the shape: **data comes in, gets cleaned into one table, is replayed as a stream, is analysed twice (live and in batch), and the two answers are compared.** A model and a dashboard sit on top. One shell script, `run_everything.sh`, runs all of it in order, so it is the best map of the project.

## The big idea

An **event study** asks: did something unusual happen to a stock around an event? "Unusual" needs a baseline. For each stock we learn how it normally moves with the market (its **market model**: when the market goes up 1%, this stock usually goes up β%). On an event day, the **abnormal return** is the actual move minus the move the market model expected. Add the abnormal returns over a window of days around the event and you get the **cumulative abnormal return (CAR)**.

For example, if Walmart rises 2% on a day the model expected 0.5%, its abnormal return is +1.5 percentage points. If that happens on several days around Black Friday, its CAR is large, and if CARs are large for many retailers across many years, Black Friday has a "calendar effect".

## Words you will meet

> [accordion] Return
>
> How much a price changed in a day, as a fraction: 100 → 102 is a return of 0.02 (+2%). We use simple returns of the adjusted close price.

> [accordion] Market model, alpha and beta
>
> A straight line fitted by ordinary least squares (OLS): stock return = alpha + beta × market return. The market is SPY, a fund that follows the S\&P 500.

> [accordion] Event window
>
> Trading days −5 to +5 around day 0, the first trading day on or after the event date.

> [accordion] Estimation window
>
> The 120 trading days before the event window, on which the market model is fitted, so the event never influences its own baseline.

> [accordion] Kafka
>
> A message broker: producers write messages to a topic, consumers read them in order. Here it carries one message per stock per trading day.

> [accordion] Spark Structured Streaming
>
> Spark's streaming engine. It treats a stream as a table that keeps growing and processes it in small **micro-batches**.

## How it runs

`run_everything.sh` sets up Python and Java, optionally re-runs the ETL, replays the data (through Kafka, or through files when Kafka is not available), runs the streaming job, checks it against a direct computation, runs the batch study, compares the two, and trains the model. `./run_everything.sh --help` lists every option; `--skip-etl` reuses the committed clean tables and takes about 20 minutes.

### The picture: The big picture

```mermaid theme={null}
%% Calendar Effects · 1 of 8 · The big picture. Read docs/architecture/README.md alongside.
%% Paste into Miro: Creation bar > Diagram > Build with code. Status as of 2026-11-24.
flowchart TB
  question(["THE QUESTION<br/>Do shopping events and holidays (Black Friday, Prime Day, Halloween...)<br/>make retail stocks move more than normal?<br/>And can a LIVE stream spot it as exactly as a careful BATCH study?"])

  subgraph S1["1 · Decide what to study · config/ and src/common/ · diagram 2"]
    cfg["config/ = the lists<br/>38 retailers, 6 events, Wikipedia titles, retail series<br/>Add a row to study something new, no code change"]
    rules["src/common/ = the rule book<br/>study.py: years, windows, thresholds<br/>contracts.py: the exact columns of every table"]
  end

  subgraph S2["2 · Collect and clean the data · ETL in etl/ · diagram 3"]
    dl["Download from the internet<br/>stock prices from Yahoo Finance<br/>page views from Wikipedia<br/>retail sales from FRED"]
    cl["Clean each source into one tidy table<br/>saved as Parquet in clean/"]
    master[("clean/master.parquet<br/>ONE row per stock per trading day, 2016–2025<br/>price, return, volume, views, event, labels<br/>98,046 rows")]
  end

  subgraph S3["3 · Pretend the past is happening live · Kafka · diagram 4"]
    producer["Producer: replay_master.py<br/>sends master one trading day at a time,<br/>like a news ticker, oldest first"]
    topic[["Kafka topic: calendar-effects-master<br/>a queue of JSON messages,<br/>one per stock per day"]]
  end

  subgraph S4["4 · Watch it live · Spark Structured Streaming · diagram 5"]
    job["Streaming job: src/streaming/job.py<br/>reads the topic in small micro-batches<br/>and remembers each stock's history (state)"]
    alerts[("Daily alerts · data/stream/signals<br/>was today's return abnormal?<br/>was today's attention a spike?")]
    cars[("Event CARs · data/stream/event_car<br/>how much each stock beat normal<br/>in the 11 days around each event")]
    preds[("Big-move watchlist · data/stream/predictions<br/>which retailers may move big tomorrow")]
  end

  subgraph S8["Learn and show · diagram 11"]
    model["Big-move model · src/ml/train.py<br/>Spark MLlib, learns from the daily alerts<br/>top 5% flags hit 4× more often than chance"]
    dash["Live dashboard · src/dashboard/app.py<br/>Streamlit: alerts, watchlist and CARs<br/>as the stream writes them"]
  end

  subgraph S5["5 · Work it out slowly · batch event study · diagram 6"]
    answer["car.py: the same CARs from all the data at once<br/>= the ANSWER KEY"]
    stats["stats.py: is the average real or luck?<br/>significance tests"]
    charts["charts.py: 5 charts for the report"]
  end

  compare{"6 · Check · diagram 7<br/>stream vs answer key:<br/>2,280 of 2,280 event windows equal"}

  subgraph S7["7 · Tell the story · Nov 23 – Dec 8"]
    report["Report"]
    video["Demo video"]
  end

  question --> S1
  cfg --> dl
  rules --> dl
  dl --> cl --> master
  master --> producer --> topic --> job
  job --> alerts
  job --> cars
  alerts --> model -->|loaded by the job| job
  job --> preds
  alerts --> dash
  cars --> dash
  preds --> dash
  dash --> video
  master --> answer --> stats --> charts
  cars --> compare
  answer --> compare
  compare --> report
  charts --> report
  alerts --> video
  cars --> video

  classDef done fill:#EAF3DE,stroke:#3B6D11,color:#27500A
  classDef todo fill:#F1EFE8,stroke:#5F5E5A,color:#444441
  classDef ask fill:#EEEDFE,stroke:#534AB7,color:#3C3489
  class cfg,rules,dl,cl,master,producer,topic,job,alerts,cars,preds,model,dash,answer,stats,charts,compare done
  class report,video todo
  class question ask
```

The [full diagram and its guide](https://calendar-effects.mintlify.site/diagrams/01-big-picture.md) explain every box.

### The picture: How to run it: on a laptop, or on the SFU cluster

```mermaid theme={null}
%% Calendar Effects · 8 · How to run it: on a laptop, or on the SFU cluster. Every command, in order: docs/running.md.
%% Paste into Miro: Creation bar > Diagram > Build with code.
flowchart TB
  subgraph LAPTOP["On a laptop · one command"]
    setup["Setup once<br/>python3 -m venv .venv · source .venv/bin/activate<br/>pip install -r requirements.txt<br/>Spark 4 needs Java 17 or 21, not 26<br/>pyarrow is pinned below 21 for PySpark 4.0"]
    runall["./run_everything.sh --skip-etl<br/>Python and Java · ETL skipped · etl/join_master.py<br/>replay to files + streaming job · check_streaming.py<br/>batch study · compare_stream_batch.py · train the model<br/>about 20 minutes, ends with PASSED"]
    batchrun["Then refresh and publish<br/>python3 src/hosting/snapshot.py · scripts/export_results.py<br/>commit and push: hooks copy to github.com<br/>python3 src/hosting/publish_hf.py --upload"]
    demo["Live demo · diagram 12<br/>scripts/demo_live.sh on a laptop<br/>or the cloud streamer on GitHub Actions"]
    localkafka["Optional: a local Kafka broker<br/>brew install kafka · see the handoff<br/>docs/handoffs/2026-11-17-mahad-kafka-test.md"]
  end

  subgraph CLUSTER["On the SFU cluster · ssh cluster.cs.sfu.ca, port 24 · VPN off campus"]
    gateway["Gateway node · ~/Calendar-Effects<br/>export PYTHONPATH=src<br/>its python3 has kafka-python, not pyspark"]
    prod["python3 scripts/create_topics.py<br/>python3 src/producers/replay_master.py<br/>sends to the course Kafka node1.local, node2.local"]
    submit["spark-submit --packages<br/>org.apache.spark:spark-sql-kafka-0-10_2.13:3.5.8<br/>src/streaming/job.py --source kafka<br/>--out and --checkpoint are HDFS paths"]
    yarn["YARN runs the job on the cluster's workers<br/>Spark 3.5.8, Scala 2.13, Java 17"]
    get["hdfs dfs -get the output<br/>then the same two checks"]
  end

  setup --> runall --> batchrun --> demo
  localkafka -.-> runall
  gateway --> prod --> submit --> yarn --> get
```

The [full diagram and its guide](https://calendar-effects.mintlify.site/diagrams/08-running-it.md) explain every box.

### Read these files in this order

**1. README.md**

Calendar Effects [Read it](https://calendar-effects.mintlify.site/reference/README-md.md)

**2. run\_everything.sh**

Run the whole Calendar Effects pipeline in one command: ETL -> master table -> streaming job -> check against a direct computation -> batch event study -> stream vs batch comparison -> big-move model. See README "Running it locally". [Read it](https://calendar-effects.mintlify.site/reference/run_everything-sh.md)

**3. requirements.txt**

Everything the pipeline needs: python3 -m pip install -r requirements.txt (tests also need requirements-dev.txt) [Read it](https://calendar-effects.mintlify.site/reference/requirements-txt.md)

**4. requirements-dev.txt**

Tools for running the tests locally: python3 -m pip install -r requirements-dev.txt [Read it](https://calendar-effects.mintlify.site/reference/requirements-dev-txt.md)

**5. pyproject.toml**

Makes src/ an installable package, so `import common` works from any script and any folder. requirements.txt installs it (the `-e .` line); dependencies are pinned there. [Read it](https://calendar-effects.mintlify.site/reference/pyproject-toml.md)

## Questions you should be able to answer

> [accordion] Why compute everything twice?
>
> Because a streaming job is easy to get subtly wrong: late messages, out-of-order days, state kept between batches. An independent batch computation of the same numbers is an answer key. When all 2,280 event windows agree to about 1e-15, both are almost certainly right.

> [accordion] Why replay old data instead of streaming live prices?
>
> Ten years of history make the statistics meaningful, and a replay is repeatable: the same run gives the same answer, so it can be checked. The job does not know the data is old; it processes each message as it arrives.

> [accordion] Why does the script need Java?
>
> Spark runs on the Java virtual machine. PySpark 4.0 needs Java 17 or 21 (not newer), so the script picks a supported one.

## Ideas to improve it

* Run the pipeline in CI on a small sample, so a teammate's push is tested on a clean machine.
* Stream live prices for the current week alongside the replay, to show the job works on real time too.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.