# Lifecycle accounting (design, declared before any result)

Second part of the online retail study. Question: on the same public transactions the win-back study uses, how much of
each month's change in active buyers comes from new, retained, returning and lapsed customers, and which of those flows is
worth acting on? Does a decomposition built on the first year predict where the second year's change comes from?

This file is committed before any first-year result is computed and before any second-year number is read. The
first-year statements are written after the first-year results, in `lifecycle-statements.json`, and committed (with their
hash pinned in `lifecycle_pipeline.py`) before the second year is loaded.

## What was already known before this design

The published win-back study on the same data. Its page and results report, among other things, that revenue peaks
every autumn in both years, the return rates of quiet customers at 1 December 2010 and 1 June 2011 (which use orders up
to late November 2011), and the cleaning counts. None of its figures is a monthly count of active, new, retained,
returning or lapsed buyers, a cohort retention rate or a lifecycle revenue flow. The autumn peak is used below to fix the
seasons; nothing else from it enters a statement.

## 1. Data and definitions

- Lines: the study's own cleaning, unchanged (`retail/clean.py` via `pipeline.read_sheets`): the two sheets combined at
  1 December 2010, exact duplicates dropped, non-product codes, zero or negative quantity or price excluded, cancellations
  netted on their own dates, lines without a customer number left out of customer measures.
- Month: calendar month of the invoice date. The months used are December 2009 to November 2011. December 2011 runs to
  the 9th only and is left out.
- Active buyer in a month: at least one non-cancelled product sale line in it. A month with only a cancellation is not
  active.
- Monthly revenue of a buyer: net product revenue in the month (sales minus cancellations on their dates), counted only
  in months where the buyer is active. Cancellations in a month where the buyer is not active are reported as a
  reconciliation total, not as a flow.
- Flows for month m (m from January 2010; December 2009 is the base month):
  - new: active in m, and m is the first active month in the data;
  - retained: active in m and in m - 1;
  - returning: active in m, not in m - 1, active in some month before m - 1;
  - lapsed: active in m - 1, not in m.
  Identity: actives(m) - actives(m - 1) = new + returning - lapsed.
- Left censoring: the data starts in December 2009, so in the first year "new" includes customers who bought before
  the data starts, and "returning" is undercounted. The sum new + returning, the retained and lapsed flows, and the
  quick ratio do not depend on this. Statements about the split between new and returning say so.

## 2. Monthly growth accounting

- Net change = new + returning - lapsed; quick ratio = (new + returning) / lapsed.
- Swing share: for a set of months, the share of the variance of monthly net change carried by each flow,
  Cov(flow, net) / Var(net), with lapsed entered as minus lapsed. The three shares sum to 1. "The flow that explains most
  of the change" is the one with the largest swing share.
- Season totals are ratios of sums over the season's months (for example returning share of actives = all returning
  buyer-months / all active buyer-months in the season).
- Lapse rate: lapsed in m / actives in m - 1, as a ratio of sums over the season.
- Cohort retention: buyers grouped by first active month (acquisition month); retention at age a is the share of the
  cohort active in month first + a. December 2009 is not a cohort (it holds everyone already buying). Curves are shown
  for every observed age; statements use ages 1 to 3 for cohorts acquired in low-season months.
- Revenue version: revenue(m) - revenue(m - 1) = new revenue + returning revenue + expansion - contraction - lapsed
  revenue, where expansion and contraction are the month-on-month changes of retained buyers and lapsed revenue is the
  lapsed buyer's revenue in m - 1. Revenue quick ratio = (new + returning + expansion) / (lapsed + contraction). Retained
  revenue share = revenue of retained buyers / revenue of all active buyers.

## 3. Build set and test set

- Year 1 (build): flows January to November 2010 (December 2009 is the base). Year 2 (test): flows December 2010 to
  November 2011. Like-for-like comparisons use the same calendar months in both years.
- Enforcement: `lifecycle_pipeline.py prepare` writes the first-year and second-year lines to two separate files.
  `build` opens only the first-year file and also filters to dates before 1 December 2010. The second-year file is opened
  only by `load('full')`, which calls `require_pinned()`: it refuses unless `lifecycle-statements.json` hashes to
  `STATEMENTS_SHA256` and, inside a git checkout, is committed and unmodified. `STATEMENTS_SHA256` is empty in this
  commit and is set only in the commit that adds the statements.
- Year-1 values in the final results are recomputed from the first-year file and must equal the pinned
  `results-lifecycle-year1.json`; a test checks it.

## 4. Seasons

Fixed here, before any monthly flow is computed, from the gift-ware calendar and the autumn revenue peak the win-back
study already reports:
- peak: September, October, November (holiday buying);
- turn: December and January (the peak unwinding: their flows are mostly peak buyers stopping and restarting);
- low season: February to August.
A lifecycle statement is about the low season unless it names the peak. A swing that only appears in peak or turn months
is never read as a lifecycle finding.

## 5. Which flow deserves intervention

Three lifecycle moments a contact could target, each counted in low-season months (February to August) of each year:
- first order: a buyer's first active month is m; came back = active in one of the next three months (the second order);
- lapse: active in m - 1, not in m; came back = active in one of the next three months;
- quiet: active in m - 3, not in m - 2, m - 1 or m (three months without an order, the win-back study's 90-day gap in
  months); came back = active in one of the next three months.
For each: events per month, the share that came back with no contact recorded (the data has no contacts), spend per
returner (net revenue in the three-month window of those who came back), and revenue at stake per month = events that
did not come back x spend per returner. Ranking: by revenue at stake per month; ties within 10% are broken by the lower
came-back share (fewer contacts wasted on buyers who return anyway). Each candidate is paired with the test it would
need: a randomized 50/50 holdout among that moment's events, sized with the study's `minimum_detectable_lift` on three
months of events at the second-year came-back share. The win-back holdout already exists on the main page and is linked,
not redesigned.

## 6. Intervals

Every share, rate and ratio is reported with a 95% percentile bootstrap interval over customers: 2,000 resamples of the
year's customers with replacement (all customers with a sale in the months used), seeds `SEEDS` in `lifecycle_pipeline.py` for year 1 and
for year 2. Counts are reported as counts. The customer bootstrap does not capture month-to-month variation; with seven
low-season months a year, statements about the swing rest on seven months and say so.

## 7. Statements and scoring

- Statements are written from year-1 results only, each as a claim on one measure defined above: a range (the year-2
  value lies within [lo, hi]), a comparison (one year-2 measure above another), or a ranking (the year-2 largest).
- Score on year 2: for each statement, the share p of the 2,000 year-2 resamples in which the claim is true.
  holds if p >= 0.95; fails if p <= 0.05; unclear otherwise, with the reason (the estimate and its interval).
- Statements are reported with their score whatever it is. A statement may not be edited, dropped or added after the
  second year is read; any later idea is labelled as added after the results and never counted in the score.

## 8. Outputs

`results-lifecycle-year1.json` (first year, pinned), `lifecycle-statements.json` (statements, pinned),
`results-lifecycle.json` (both years, cohort tables, scores, intervention ranking), and the page `lifecycle-cohorts.html`
rendered by `render_lifecycle.py`, linked from the main page with one sentence.

## Notes added after the results

Everything above this section is the declared design, unchanged except for two things: the heading, which carried the
declaration date (the study's version history dates the file), and section 6, which named the two resampling seeds as
numbers that read like dates; they are now referred to by name (`SEEDS` in `lifecycle_pipeline.py`), with the same values.

- Section 1 says an active buyer has a "non-cancelled" product sale line. The code counts any product sale line in the
  month, even one cancelled later; the cancellation only lowers revenue. The results follow the code.
- Section 5 follows each event for three months, so events in June to August are followed into the September to November
  peak. Added after the results, in `results-lifecycle-added.json`: the same measures for February to May events only,
  where the window ends by August, and the come-back share by event month.
- What `require_pinned()` 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.
- Also added after the results: a start-of-data test (the second year measured with no history before December 2010), the
  number of distinct buyers for the missed-month test, the share of lines without a customer number, and intervals on the
  retention table by month of first order.
