# A recommender from training to rollback

A movie recommender trained on MovieLens ratings up to a cut-off and served on the 18 quarters that followed, the way a
team would run one: retrain on a schedule, register each release, score in a batch, watch inputs and outputs, and roll
back by a written rule. It asks how much a model loses when nobody retrains it, whether monitors and a rollback rule
catch a bad release, and what new users and new movies get. The page is `index.html`.

## What is here

| File | What it is |
|---|---|
| `index.html` | The study page, built by `render.py` from `results.json` |
| `results.json` | Every aggregate the page shows: per model, per quarter, per month; monitors; planted failures |
| `registry/` | The release registry without model files: `index.json` and one manifest per release |
| `fetch.py` | Downloads MovieLens 32M from GroupLens and checks its sha256 and the per-file md5 sums |
| `run.py`, `rlc/` | The pipeline: data, models, the lifecycle (train, register, serve, monitor, roll back), scoring |
| `render.py` | Builds the page; it refuses to build if a sentence stops matching the results |
| `check_numbers.py` | Traces every number on the page to `results.json` or a declared assumption |
| `tests/` | Metric cases, a replay of every reported decision from `results.json`, a seeded end-to-end run on synthetic ratings, the page, and the licence guard |
| `NOTICE.txt` | Data attribution and the licence terms followed |

No rating is published. The archive is downloaded from the source and every file derived from it stays in folders that
`.gitignore` excludes.

## Reproduce it

Python 3.12, with the pinned, hashed dependencies:

```
python3.12 -m venv .venv
.venv/bin/pip install --require-hashes -r requirements.lock
make fetch      # downloads and checks ml-32m.zip into data/downloads/
make grid       # the factor model's grid on the validation quarter
make releases   # trains and registers the 20 releases, in order (resumable)
make main       # serves, monitors and scores the 18 quarters (resumable)
make plants     # the four planted failures, each in its own replay
make results    # writes results.json and registry/
make render test numbers
```

`RLC_DATA_DIR` and `RLC_WORK_DIR` move the download and the working files (model files, caches) elsewhere. Every stage
writes a marker when it finishes, so a stopped run picks up where it left off. If `fetch.py` cannot verify the
server's certificate on your system, download the archive in a browser and put it where the script says; it checks the
file either way.

The tests need no network and no download: `make test`.

## Design summary

- Split by calendar quarter. Training uses every rating before a release's first day; validation is 2019 Q1; serving is
  2019 Q2 to 2023 Q3.
- Models: 180-day popularity; implicit ALS (128 factors); a LightGBM re-ranker on the ALS top 100, trained on the
  previous release's served candidates and what users then liked (one quarter of labels; a wider window was not tried).
- Metrics: NDCG@10 and recall@20 per quarter, never pooled; paired bootstrap intervals; calibration error on the
  served top 20; new users scored after their first 5 ratings against a genre popularity fallback; movies with no or
  few ratings reported apart.
- Lifecycle: a versioned folder registry (not MLflow), batch scoring on each quarter's first day with the last
  release not rolled back in shadow, five monitors with written lines and a monthly quality check, quarterly retraining, and a rollback rule (live under 0.95 times
  the shadow on a month of labels with at least 300 users).
- Four planted failures, each predicted before the run: a feature join bug, a burst of new movies, late labels, and
  the bug with late labels. They are a self-test; the untouched quarters are the real test.

## Licence

MovieLens 32M is published by GroupLens Research at the University of Minnesota for research use; see `NOTICE.txt`.
This study is independent and non-commercial. The code here may be reused; the data stays under its own licence.

Some files are packed in ranking-lifecycle-data-1.zip; unzip it here to restore the full export, which the manifest lists file by file.
