# Online retail time to next order and a churn score (part five)

A churn score tells you who is likely to leave. It does not tell you who a contact would keep. Part five of the online retail
study, on the same public orders as the [win-back study](README.md): every order of one UK online gift-ware retailer from December
2009 to December 2011 (Online Retail II, UCI Machine Learning Repository, Daqing Chen, CC BY 4.0; see [NOTICE.txt](NOTICE.txt)). Open
`time-to-next-order.html` for the page.

It fits a survival model that treats customers still quiet as censored, on orders before 1 December 2010 only, and freezes it. It
scores that model against a plain recency rule and three shortcuts on four later dates it never saw, checks the churn score for
calibration by band, measures drift across the December and June dates, and sets a contact rule from a stated cost and order value,
against contacting everyone quiet. No contact is recorded in the data, so every net figure is a scenario on an assumed response, and
the holdout from the first part is the test of what a contact adds. It shows a method on retail orders, and says nothing about
customers of other businesses.

## How the later dates were kept out

1. `DESIGN-survival.md` and `protocol-survival.json` set the cutoff, dates, features, arms, score bands, stated contact inputs, response
   patterns, resampling and eighteen predictions with their bands before any model was fitted. `survival_pipeline.py` pins the
   protocol's SHA-256 (`PROTOCOL_SHA256`) and refuses every stage unless it matches and, inside a git checkout, is committed unmodified.
2. `prepare` writes the lines before the cutoff and the lines from it to two separate files. `fit` reads only the first and writes
   `results-survival-fit.json`, the frozen arms.
3. `score`, `boot` and `collect` read later orders only after `require_fit_committed()` passes: `results-survival-fit.json` must be
   committed unmodified. Their outputs are `data/derived/survival-scored.csv` and `results-survival.json`, with each prediction scored as held
   or missed.

What the gates prove: when a later order was read, the protocol and the fitted model were committed and unchanged. What was known
before the design is listed in its section "What was already known".

## Added after the results

- The paired intervals on the rule minus everyone quiet (`intervals_added_after_results` in `results-survival.json`), on the same
  resamples. Not scored.
- `results-survival-added.json`, from `survival_added.py`. The like-for-like comparison of the arms with no season term, because the
  shortcuts have none: the absolute gaps at December, March and June, how far counting censored as churn lifts the mean score, and how far
  each arm's gap moved from December to June, paired on the same 500 resamples. The holdout sizes for showing a lift clears break-even, since
  the design's sizes tell a lift from no lift. Not scored. No fit, score, prediction or value in `results-survival.json` changed.
- The page's readings that two errors most likely offset at December and March for the "censored counted as churn" arm, that the training
  year is the likeliest cause of the low reading, and what the odds ratios show. They are readings of the arms, not tested claims.

## Reproduce

Prerequisites as for the [main study](README.md) (Linux, Python 3.12.3, uv 0.11.7, curl). This part needs only the packages in
`requirements.lock`.

```sh
uv venv --python python3.12 .venv
uv pip sync --python .venv/bin/python --require-hashes requirements.lock
make download
make survival
make survival-score
```

`make survival` verifies the download, prepares the two line files, fits the arms and writes `results-survival-fit.json`. Inside a git
checkout, commit that file before going on. Outside one, the commit checks are skipped and only the hashes are checked.
`make survival-score` scores the later dates, runs the 500 joint resamples (each chunk writes a done-marker, so a stopped run resumes),
writes the results and the comparisons added after them, renders `time-to-next-order.html` and runs the tests. Run the numeric steps with `OPENBLAS_NUM_THREADS=1` to
reproduce the fits exactly. The work folder is `data/work/survival` unless `SURVIVAL_WORK` names another, and `RETAIL_ZIP` can name the
download.

With no download at all, `tests/test_render_survival.py` replays every date's measures and the contact values from the published
`data/derived/survival-scored.csv`, and replays the prediction scores and the added point estimates and holdout sizes from the stored
measures. The added intervals need the resamples, which are not published, so they rerun only through `make survival-score`.

## Files

- `retail/survival.py`: landmark tables, the hazard model, calibration, drift and contact arithmetic, each tested in `tests/test_survival.py`
  on small constructed customers, with a positive control on data with a known hazard.
- `survival_pipeline.py`: the stages above. `render_survival.py`: the page.
- `results-survival-fit.json`: the frozen arms, with no order after the cutoff. `results-survival.json`: every measure and interval.
  `survival_added.py` and `results-survival-added.json`: what was added after the results.
- `data/derived/survival-scored.csv`: per customer and scored date, the hashed key, days since the last order, the quiet flag, the value
  tier, each arm's score, the days to the next order and whether it was seen.
- Any score band with fewer than 11 customers is withheld in the results file and the page, because a share of so few customers says
  little. It is not a privacy wall: the scored table holds each hashed key's score, so such a band can be recounted from it. The page also
  leaves out the sweep rows whose list has fewer than 11 customers; the full grid in `results-survival.json` keeps them. The worked example's
  exact score, in the same file, matches one row of the scored table.
