> ## 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.

# 0007. A Spark MLlib model that predicts tomorrow's big moves, scored live

* **Date:** 2026-11-24
* **Status:** Agreed 2026-11-25 (proposed by Mahad)
* **Agreed by:** Mahad (2026-11-25), for the team

## Context

The project had no machine learning, and the ProjectSummary's "Algorithmic work" category names "data mining and machine learning techniques". The streaming job already flags a big move each day (`abnormal_return`, |z\_ar| ≥ `ABNORMAL_RETURN_Z`). That flag only says what happened. A model can say what is likely to happen next, using what the stream knows today, which is a natural use of the live pipeline.

## Decision

* **Question:** from a retailer's daily row today, will its next trading day be a big move? The label is the same stock's `abnormal_return` on its next trading day.
* **Features** (`src/ml/features.py`) use only today's row, the event calendar and `config/retailers.csv`:

  * from today's row: |z\_ar|, z\_ar, ar\_sd, beta, |mkt\_ret|, z\_views, attention\_spike, log(vol\_ratio), abnormal\_return;
  * from the calendar: days to the next in-scope event and since the last one, capped at `ML_EVENT_DAYS_CAP`;
  * day of the week, month, and is\_online.

  Training and live scoring use the same function, so features cannot differ between the two.
* **Training data:** the streaming job's own daily output (`stream_signals`), which matches the direct computation exactly. The model learns from the same numbers it is later shown live.
* **Time split, never random** (`common.study`): train on 2016–`ML_TRAIN_END_YEAR`, choose the model on `ML_VALIDATION_YEAR`, refit on both, and report on `ML_TEST_START_YEAR`–2025. Rows whose "tomorrow" falls in the next period are dropped (a purged split).
* **Models** (`src/ml/train.py`, Spark MLlib Pipelines):

  * logistic regression on standardized features, a transparent baseline;
  * gradient-boosted trees, for non-linear effects and interactions.

  Each gets a small grid. Each class gets equal total weight, because big moves are only about 5% of days. The model is chosen by PR-AUC on the validation year, because accuracy is meaningless at a 5% base rate.
* **Judged against:**
  * chance, the base rate;
  * a no-model baseline that ranks by today's |z\_ar|;
  * the same model without the attention features, to measure what Wikipedia adds.
* **Live:** `src/streaming/job.py` loads the saved `PipelineModel` and scores each micro-batch inside the JVM (no Python on the workers). It writes `stream_predictions`. `big_move_alert` = 1 for the top `ML_ALERT_TOP_SHARE` of probabilities, with the cut-off taken from the training years.

## Results (2026-11-24, 38 retailers; test years 2024–2025, 18,696 stock-days)

| Model | ROC-AUC | PR-AUC | Precision of top 5% | Lift |
| - | - | - | - | - |
| Gradient-boosted trees (depth 3, chosen on 2023) | 0.688 | 0.135 | 21.5% | 4.1× |
| Same, without attention features | 0.689 | 0.133 | 21.6% | 4.2× |
| Baseline: today's \|z\_ar\| | 0.582 | 0.082 | 12.6% | 2.4× |
| Chance (base rate) | 0.5 | 0.052 | 5.2% | 1× |

* The model ranks tomorrow's big movers clearly better than chance and better than "big moves cluster".
* Wikipedia attention adds nothing once volume, the market's move, volatility and the calendar are known.
* The most important features are month (earnings seasons), log volume ratio, the market's absolute move, volatility (ar\_sd) and days to or since an event.

## Alternatives considered

* **Predicting the direction of tomorrow's return:** daily direction is close to a coin toss in liquid stocks. Predicting a big move in either direction is realistic, and it is what an alert system needs.
* **Predicting each event's CAR:** only 2,280 windows across 49 event dates. Too few for a held-out test with any power.
* **A random train/test split:** it would let the model learn from days after the ones it is tested on, which flatters the result.
* **scikit-learn instead of MLlib:** fine at this size, but MLlib runs on the cluster, plugs into Structured Streaming, and scales with the data.
* **Lagged features (yesterday's values):** they would need extra state in the streaming job. Today's row already carries volatility (ar\_sd) and the market regime.

## Consequences

* **Everyone:** `python3 src/ml/train.py` after the streaming job (about 1 minute locally). It writes `data/models/` (gitignored) and `ml_metrics.csv`, `ml_test_predictions.parquet`, `ml_feature_importance.csv`, `charts/ml_roc_pr.png` and `charts/ml_feature_importance.png` in `data/results/`. Restart the streaming job to get live predictions.
* **Contracts:** new `stream_predictions`, `ml_metrics`, `ml_test_predictions`, `ml_feature_importance`.
* **Settings:** `common.study` gains `ML_TRAIN_END_YEAR`, `ML_VALIDATION_YEAR`, `ML_TEST_START_YEAR`, `ML_ALERT_TOP_SHARE`, `ML_EVENT_DAYS_CAP`. `common.settings` gains `MODEL_DIR`, `BIG_MOVE_MODEL`, `BIG_MOVE_MODEL_INFO`, and `STREAM_SUBDIRS["stream_predictions"]`.
* **Cluster:** a model saved by Spark 4 may not load in the cluster's Spark 3.5. Train it there with `spark-submit src/ml/train.py --signals <HDFS signals folder>`.
* **Report:** results section and ProjectSummary "Algorithmic work". State plainly that attention adds nothing to the model, and that a 0.69 AUC is a ranking edge, not a trading strategy.


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