# Online retail customer segments (part four)

What changes when a team builds customer segments by clustering instead of the study's hand-built recency-and-value
rule? Part four 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 `customer-segments.html` for the page.

It fits 168 segmentations at each of two decision dates (four feature sets, three scalings, four algorithms with their
distances, 2 to 8 segments), measures how stable they are over 100 resamples of the customers, reads off the win-back
list a team would pick from each, and scores whether the segments separate who orders again in the next 180 days, a
period the clustering never saw, better than the rule's six cells.

## How the holdout period was kept out

1. `DESIGN-segments.md` and `protocol-segments.json` set the features, scalings, algorithms, the range of segment counts,
   how the count is chosen, the stability measure, the holdout split, the win-back rule and fifteen predictions with
   their bands before any clustering was fitted. `segments_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 1 June 2011 and the lines from 1 June 2011 to two separate files. `fit`, `stability`
   and `collect` read only the first and filter each date's features to lines before that date; they write
   `results-segments-fit.json`, which holds segments, profiles, silhouettes, stability and the win-back lists, and no
   outcome.
3. `score` reads the outcome only after `require_fit_committed()` passes: `results-segments-fit.json` must be committed
   unmodified and the per-customer segment file must match the digest it records. It writes `results-segments.json` with
   the out-of-fold scores, the list measures and each prediction scored as held, missed or untestable.

What the gates prove: when the outcome was read, the protocol and the fitted segments were committed and unchanged. For
the 1 December 2010 date the outcome window lies inside the first file; there the separation is the date filter and the
order of the commits. The first part's published figures at both dates were known before the design (see its section
"What was already known").

## Reproduce

Prerequisites as for the [main study](README.md) (Linux, Python 3.12.3, uv 0.11.7, curl). This part also needs
scikit-learn and SciPy, pinned with hashes in `requirements-segments.lock`, which keeps every pin of `requirements.lock`.

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

`make segments` verifies the download, prepares the two line files, fits every segmentation at both dates, runs the
stability resamples (each combination writes a done-marker, so a stopped run resumes) and writes
`results-segments-fit.json`. Outside a git checkout the commit checks are skipped and only the hashes are checked.
`make segments-score` scores the holdout period, renders `customer-segments.html` and runs the tests. Run the numeric
steps with `OPENBLAS_NUM_THREADS=1` to reproduce the fits exactly.

## Files

- `retail/segments.py`: features, scalings, the four clusterers, stability, the win-back rule and the out-of-fold
  scores, as pure functions.
- `segments_pipeline.py`: the prepare, fit, stability, collect and score stages, the split and both gates.
- `render_segments.py`: the page.
- `protocol-segments.json`, `results-segments-fit.json`, `results-segments.json`: the protocol, the segments (no
  outcome) and the scores.
- `tests/test_segments.py`, `tests/test_render_segments.py`: constructed fixtures whose answers follow from their
  construction, the pins, and every number on the page traced to the results.

## Limits

One wholesaler-heavy retailer, two decision dates; segments and win-back lists of fewer than 11 customers are withheld in every
public file, and the worked example's counts are shown in bands (both changes made after the results, with a third that
added intervals for every clustering at 5 to 8 segments; see DESIGN-segments.md section 12); the win-back rule that turns segments into a list is one declared rule among many a team could use; intervals
resample customers with the segments held fixed, so they show sampling of customers, not refitting.
