# Online retail lifecycle accounting (part two)

Which lifecycle moves explain the change in a retailer's active buyers, and which one is worth a test? Part two 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 November 2011 (Online Retail II, UCI Machine Learning Repository, Daqing Chen, CC BY 4.0; see
[NOTICE.txt](NOTICE.txt)). Open `lifecycle-cohorts.html` for the page.

## How the held-out year was kept out

1. `DESIGN-lifecycle.md` set the definitions, seasons, scoring rule and ranking rule before any result was computed. It is
   published here unchanged except for its heading (which carried the date) and notes added at its end after the results.
2. `lifecycle_pipeline.py prepare` writes the first-year lines (orders before 1 December 2010) and the second-year lines to two
   separate files. `build` reads only the first and writes `results-lifecycle-year1.json`.
3. The twelve statements in `lifecycle-statements.json` were written from that file alone. The statements file's SHA-256 is
   pinned in `lifecycle_pipeline.py` (`STATEMENTS_SHA256`).
4. `score` opens the second-year file only after `require_pinned()` passes: the statements must match the pin and, inside a
   git checkout, be committed and unmodified. It also recomputes the first year and refuses if it differs from the pinned
   file, then writes `results-lifecycle.json` with each statement scored: holds when true in at least 95% of 2,000 customer
   resamples of the second year, fails at 5% or fewer, unclear otherwise.

What the pin proves: when the second year was scored, the statements matched their hash and were committed unmodified.
It does not prove that no second-year number was seen earlier; that rests on the order of the commits. The second-year
line file was written, unread, by `prepare` before the statements were committed. The first part's published results (the
autumn peak, and how many quiet customers came back at two dates) were known before, which bears on statements 11 and 12.

`lifecycle_added.py` writes `results-lifecycle-added.json`: analyses added after the results, none scored. Come-back
windows kept in February to August and by event month, a start-of-data test, distinct buyers for the missed-month test,
and the share of lines without a customer number.

## Reproduce

Same prerequisites as the [main study](README.md) (Python 3.12.3, uv 0.11.7, curl; packages pinned with hashes).

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

`make lifecycle` verifies the download, prepares the two line files, rebuilds the first year, scores the second, runs the
analyses added after the results, renders `lifecycle-cohorts.html` and runs the tests. A rebuilt `results-lifecycle-year1.json` must match the committed one byte for
byte, or the statements no longer describe it.

## Files

- `retail/lifecycle.py`: the flows, the revenue accounting, the three lifecycle moments, cohort retention and the
  customer bootstrap, as pure functions.
- `lifecycle_pipeline.py`: the prepare, build and score stages, the cut between the years and the pin.
- `lifecycle_added.py`: the analyses added after the results.
- `render_lifecycle.py`: the page.
- `results-lifecycle-year1.json`, `lifecycle-statements.json`, `results-lifecycle.json`, `results-lifecycle-added.json`:
  first year, statements, both years, and the analyses added after the results.
- `tests/test_lifecycle.py`, `tests/test_render_lifecycle.py`: constructed fixtures whose answers follow from their
  construction, the pin, the scoring thresholds, and every number on the page traced to the results.

## Limits

One wholesaler-heavy retailer over two years; orders, not visits or contacts; intervals resample customers, not months, and
the swing shares rest on seven low-season months a year; three-month come-back windows from June to August reach into the
peak; revenue at stake is gross revenue and an upper figure; revenue counts only buyers with a customer number.
