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

# 8. The big-move model

> A Spark MLlib model, trained on the stream's output, that scores tomorrow's big moves live.

The stream already says whether **today** was abnormal. The model asks: **will tomorrow be a big move** (|z\_ar| ≥ 1.96) for this retailer? Direction is close to a coin toss; size is more predictable, because big moves cluster.

## The big idea

* **One feature builder** (`features.py`) is used both for training and live scoring, so the model sees the same inputs in both: today's |z\_ar|, the volatility of abnormal returns, beta, the market's absolute move, Wikipedia z-score and spike, log volume ratio, days to and since an event, day of week, month, and online vs store.
* **Training** (`train.py`) uses Spark MLlib Pipelines: a `VectorAssembler`, then logistic regression (with a `StandardScaler`) or gradient-boosted trees.
* **The split is by time, never at random:** fit on 2016–2022, choose the model on 2023, test once on 2024–2025. Rows whose "tomorrow" falls in the next period are dropped (**purged**), so no label leaks across the split.
* **Big moves are about 5% of days**, so the classes are weighted equally during training.
* **The alert** flags the top 5% of scores (the cut-off is set on the validation year), and the streaming job writes it each micro-batch.

On 2024–2025 the trees reach ROC-AUC 0.69; 21.5% of flagged days are big moves, 4.1 times the 5.2% base rate. Today's |z\_ar| alone gives 0.58. Removing the Wikipedia features changes nothing.

## Words you will meet

> [accordion] ROC-AUC
>
> The chance that a random big-move day gets a higher score than a random quiet day. 0.5 is guessing, 1 is perfect.

> [accordion] PR-AUC and lift
>
> Precision (share of flags that were right) across recall levels. Compare it with the base rate; lift is precision divided by the base rate.

> [accordion] Gradient-boosted trees
>
> Many small decision trees, each correcting the errors of the ones before it.

> [accordion] Calibration
>
> Whether a score of 0.3 really means a 30% chance. These scores are not calibrated (the class weights shift them up), so the dashboard shows shares of flags, not raw scores, as rates.

### The picture: The big-move model (Spark MLlib) and the live dashboard

```mermaid theme={null}
%% Calendar Effects · 11 · The big-move model (Spark MLlib) and the live dashboard · Mahad · decisions 0007, 0008.
%% Paste into Miro: Creation bar > Diagram > Build with code.
flowchart LR
  signals[("data/stream/signals<br/>the stream's daily rows<br/>95,190 stock-days")]

  subgraph FEAT["src/ml/features.py · the same features for training and live"]
    f1["today's row<br/>|z_ar| · ar_sd · beta · |mkt_ret|<br/>z_views · spike · log vol_ratio"]
    f2["calendar<br/>days to / since an event<br/>day of week · month"]
    f3["config/retailers.csv<br/>is_online"]
  end

  subgraph TRAIN["src/ml/train.py · Spark MLlib Pipelines"]
    split["TIME SPLIT, purged<br/>train 2016–2022 · choose on 2023<br/>test 2024–2025"]
    cands["CANDIDATES<br/>logistic regression (scaled)<br/>gradient-boosted trees<br/>classes weighted equally"]
    pick["PICK on 2023 by PR-AUC<br/>→ trees, depth 3<br/>refit on 2016–2023"]
    test{"TEST 2024–2025<br/>ROC-AUC 0.69 · PR-AUC 0.135<br/>top 5% hit 21.5% vs 5.2% base<br/>baseline |z_ar|: 0.58"}
  end

  model[("data/models/big_move<br/>PipelineModel + alert cut-off")]
  results[("data/results/<br/>ml_metrics · ml_test_predictions<br/>ml_feature_importance · ml charts")]

  subgraph LIVE["src/streaming/job.py · each micro-batch"]
    score["score today's rows<br/>inside the JVM<br/>no Python on workers"]
    preds[("data/stream/predictions<br/>p_big_move_next<br/>big_move_alert = top 5%")]
  end

  subgraph DASH["src/dashboard/app.py · on a laptop and hosted · diagram 12"]
    d1["reads finished batches only<br/>_SUCCESS marker · refreshes every 5 s"]
    d2["today's alerts · tomorrow's watchlist<br/>event CARs as windows close<br/>alerts per day · one retailer's path"]
    d3["views: Live stream · Replay any years<br/>Event history & forecast · About"]
  end

  signals --> FEAT --> split --> cands --> pick --> test
  pick --> model
  test --> results
  model --> score --> preds
  signals --> d1
  preds --> d1
  d1 --> d2 --> d3
```

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

### Read these files in this order

**1. src/ml/**init**.py**

The big-move model (decision 0007): Spark MLlib on the streaming job's daily output. [Read it](https://calendar-effects.mintlify.site/reference/src/ml/init.md)

**2. src/ml/features.py**

Features for the big-move model (decision 0007). [Read it](https://calendar-effects.mintlify.site/reference/src/ml/features.md)

**3. src/ml/train.py**

Train and test the big-move model with Spark MLlib (decision 0007). [Read it](https://calendar-effects.mintlify.site/reference/src/ml/train.md)

## Questions you should be able to answer

> [accordion] Why not split the rows at random?
>
> Days next to each other are alike. A random split puts tomorrow in training and today in testing, so the model looks better than it would be on the future. A time split tests it like it will be used.

> [accordion] Is 0.69 good?
>
> It is a real ranking edge over the baseline (0.58), and flagged days are four times as likely to be big moves. It is not a trading strategy: it predicts size, not direction, and ignores costs.

> [accordion] Does Wikipedia attention help?
>
> Not for this model: the ablation without the two attention features scores the same. That is a finding worth reporting, not a failure.

## Ideas to improve it

* Predict the CAR of an upcoming event window directly, with event-level features.
* Calibrate the scores (isotonic regression) so they can be read as probabilities.


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