# One record per customer: design

The second part of the identity resolution study. Written and committed before any result was read: when this file
was committed, no matching, survivorship or steward rule had run on generated data, and the master-data sources below
had not been generated. Every constant here is also in `protocol-master-record.json`, which the code reads; the two are
committed together and frozen. A change after this commit goes in a section at the end headed
`## Amendment N: <what changed>`, committed before it is used, and the page reports it.

## 0. Question

The same customer sits in three systems that never agreed on an id: a CRM, a billing system and the product database.
Building one master record per customer takes deterministic keys first, then probabilistic matching, then rules for
which system wins each field (survivorship), with a steward queue for the pairs nobody is sure of. How wrong do the
customer count and revenue per customer get under each matching rule, how big is the steward queue at each threshold,
and how often does each survivorship rule keep the right name, email, phone and address?

## 1. Data: the same synthetic shop, extended (a labelled design step)

The first part's generator (`idr/generate.py`) is not changed. Its people, households, account holders, shared
accounts, visits and purchases on each seed are the starting point, taken from its baseline scenario with every rate
as declared there. A new module, `idr/mdm_generate.py`, draws everything new from a separate random stream
(`numpy.random.default_rng([seed, 7])`), so the first part's data stays byte-identical; a test pins that. Seeds 0 to 9.
Development and debugging use seed 100 only, which is not a reported seed.

Every rate below is an assumption, chosen to look like a mid-sized online shop that keeps a CRM, a billing system and
a product database. No real person, customer or company data is used; names come from fixed lists of common first
names and surnames, emails use reserved example domains, phone numbers use the fictional 555 exchange, and streets,
towns and regions are invented.

### What each person is (the truth)

- A legal first name and surname. 20 of the first names have a common short form (Robert and Bob). Members of a
  household share the surname with probability 0.8.
- A personal email; people who work at an office (55%, as in the first part) also have a work email.
- A mobile phone number.
- A home address per household (street number, street, town, postcode, region; five regions). 15% of households move
  once at a uniform day in the 90-day period; 30% of moves cross into another region.
- One payment card; 30% of people have a second. In households of 2 or more, with probability 0.15 the second member
  pays with the first member's card.
- In households of 2 or more, with probability 0.06 the first two members share a family email, used as described
  below.

The current truth for each field is the legal name, the personal email, the mobile number and the address at the
end of the period.

### Product database (accounts)

- One account per login id in the first part's data (every account holder with at least one visit). A shared account
  (the first part's two household members on one login) is one account, held in the first member's name.
- 5% of account holders also open a second account with a second personal email (same name). From its opening day,
  half of that person's logged-in purchases use it.
- Name as entered at signup: the full legal name (70%), the first name only (20%), or the short form plus surname
  (10%, people whose name has one; otherwise the full name).
- Email: the personal email, or the work email with probability 0.15 for office workers.
- Phone filled with probability 0.4. A saved address (the address at signup) with probability 0.6; after a move it is
  updated with probability 0.5.

### Billing (customers, orders, refunds)

- A purchase made while logged in goes to the account's billing customer, created at that account's first logged-in
  purchase; that billing customer carries the account id with probability 0.9 (the rest came through an older checkout
  path that stored no account id).
- A guest purchase (not logged in) types an email: the personal email (90%) or, for office workers, the work email;
  either of the two members who share a family email types it with probability 0.5. The typed email has a one-character typo
  with probability 0.03. Billing reuses an earlier guest customer only when the typed email matches exactly;
  otherwise it creates a new one.
- Name on the billing customer: the cardholder's legal name, in capitals with probability 0.5, with a middle initial
  with probability 0.3. Phone filled with probability 0.6. Address: the address at the latest order, written in a
  second format (street type abbreviated, unit placement moved) with probability 0.5. Card fingerprint of the card
  used.
- Order value: log-normal with median US$55 and log-scale spread 0.6. 6% of orders are refunded in full.
- Net revenue is order value minus refunds; it is the revenue measure throughout.

### CRM (contacts)

- Email subscribers: every person with at least one email-channel visit in the first part's data has a subscriber
  contact, created at their first visit: an email (the personal email; the work email with probability 0.2 for office
  workers; the family email with probability 0.3 where one exists) and the first name only, with probability 0.7.
- A billing-to-CRM sync: each billing customer is synced at its first order. With probability 0.7 the sync carries the
  billing id; otherwise it looks up a contact by exact email and attaches to it if found, or creates a new contact.
  A synced contact takes the billing name (title case), email, phone and address. After a move, a contact's address is
  updated with probability 0.4.
- Support: 8% of buyers raise a ticket after a purchase. The agent searches by the email the person gives (their
  personal email, or the work email with probability 0.3 for office workers): if a contact has it, the ticket attaches
  there; otherwise the agent creates a new contact with the name as typed (a one-character typo with probability
  0.05, the short form with probability 0.15), that email and the phone (probability 0.7). The contact is assigned to
  one of 12 support agents.

Every record keeps its true person (for a shared account or a family email, the person who opened it) and its last
update day. Every order keeps its true buyer. A true customer is a person who opened a record or placed an order.

## 2. Matching rules (the arms)

Each rule produces clusters of records; one cluster is one master customer.

1. **Deterministic keys.** Records are joined (connected components) when they share: an account id carried by a
   billing customer, a billing id carried by a CRM contact, an exact email after lower-casing and trimming, or an
   exact phone number after keeping digits only.
2. **Deterministic, then probabilistic, auto-linked at a threshold.** Start from the deterministic clusters.
   Candidate pairs are records in different clusters that share (a) the same surname and first initial after
   normalising, (b) the same postcode and street number, or (c) the same email local part. Blocks of more than 200
   records are skipped. Features and levels:
   - first name: exact (after normalising case and spacing, or both short forms of the same name), close (a short form
     against its full name, or Jaro-Winkler similarity 0.92 or more), different, missing;
   - surname: exact, close (Jaro-Winkler 0.92 or more), different, missing;
   - email: same local part on another domain, close local part (edit distance 2 or less), different, missing;
   - phone: different, missing (an equal phone was already joined);
   - address: same street number, street and postcode after normalising, same postcode only, different, missing;
   - card: same card, different card, not comparable (a pair that is not two billing customers).
   A missing or not-comparable level adds no evidence. Non-match probabilities (u) are each level's share among
   100,000 random record pairs. Match probabilities (m) and the prior are fitted by expectation maximisation over the
   candidate pairs with u held fixed (the usual unsupervised Fellegi-Sunter fit), started from m of 0.90 on the
   strongest level of each feature and the rest spread evenly, and a prior of 0.1, for at most 100 iterations or until
   the log-likelihood gains less than 1e-9. A pair links when its posterior is at or above the threshold; links join
   clusters transitively. Thresholds: 0.5, 0.8, 0.9, 0.95 and 0.99. The main threshold is 0.9.
3. **Deterministic, then probabilistic, with a steward queue.** Pairs at a posterior of 0.99 or more link
   automatically. Pairs from a lower bound up to 0.99 go to a steward queue; the steward links a pair when the two
   records belong to one person. The steward is simulated: each decision is right with probability 0.95, drawn from a
   separate stream (`default_rng([seed, 9])`). Lower bounds: 0.5, 0.8, 0.9 and 0.95. Steward accuracy is also run at
   0.85 and 1.0 on the main data setting.
4. **Perfect match (reference, not a rule).** Records clustered by their true person. It is right by construction on
   the count of record holders; it is shown because a shared account or a family email puts two people's orders on
   one record, which no matching rule can undo.

## 3. Survivorship rules (which system wins each field)

Applied to the clusters of rule 4 (so matching errors do not blur them) and to the clusters of rule 2 at 0.9.

1. **Most recent wins:** the non-missing value from the record updated last.
2. **Source priority:** name from billing, then product, then CRM; email from product, then billing, then CRM; phone
   from CRM, then billing, then product; address from billing, then CRM, then product. Within one source, most recent.
3. **Most frequent:** the value most records in the cluster carry after normalising; ties go to the most recent.

## 4. Measures, against the truth

- **Customer count error:** master customers minus true customers, as a share of true customers.
- **Merges and splits per 100 true customers**, as in the first part, with records as the unit: a merge is each extra
  true person inside one cluster, a split is each extra cluster one person's records are spread over. Count error is
  splits minus merges, minus the true customers who own no record (bookkeeping, not evidence).
- **Revenue per customer wrong by more than 10%:** for each true customer with positive net revenue, the cluster
  holding most of their net revenue is their master record; the share of those customers whose master record shows a
  net revenue more than 10% away from their true net revenue (a split shows too little, a merge too much).
- **Mean revenue per customer error:** total net revenue over master customers against total net revenue over true
  customers. Total revenue is the same on both sides, so this follows from the count error (bookkeeping); it is
  reported because it is the number a dashboard shows.
- **Steward queue:** pairs queued per 1,000 true customers at each lower bound, the share of queued pairs that are one
  person, and after the steward the same measures as above.
- **Link precision (rule 2):** the share of accepted probabilistic links that join one person, beside the model's own
  estimate (the mean posterior of the accepted links).
- **Field accuracy (survivorship):** per field, the share of master customers whose surviving value equals the current
  true value of the cluster's main person (the person who opened most of its records; ties to the lower id), compared
  after normalising case, spacing, punctuation, street-type abbreviations and middle initials. A short form against a
  legal first name counts as wrong.
- **Revenue per customer by region (survivorship):** each master customer's region is the region of its surviving
  address. Per region, net revenue per master customer against the truth (true customers by current region); reported
  as the mean absolute error over the five regions, with the share of master customers that have no address.
- **What it is worth (a declared assumption):** a mailed offer at US$1.50 per master customer, so each extra master
  customer per campaign costs US$1.50, and each merged person receives another person's offer.

## 5. Reporting

Per rule and threshold, never pooled across rules or data settings: the mean over the 10 seeds and the lowest and
highest seed. Seeds are the only intervals. Where a measure is right by construction, the page says so.

## 6. A declared sweep of the assumed noise (decided before any run)

The sizes depend on two sets of assumed rates, so both are swept on the same 10 seeds, with every rule:

- **Entry noise:** typo rates, the short-form and first-name-only shares, the second-format share, the work-email
  shares and the second-account share, all multiplied by 0.5, 1 (declared) and 2 (each capped at 0.95).
- **Sharing:** the shared-card and family-email rates multiplied by 0, 1 (declared) and 2.

That is 9 settings x 10 seeds; the 1 x 1 setting is the main run. The page gives each rule's range over the 9 settings
beside the main result.

## 7. Predictions (before any run)

1. On the main setting, the deterministic rule overcounts true customers by more than 5% in every seed.
2. On the main setting, the probabilistic rule at 0.9 has a smaller customer-count error than the deterministic rule
   in every seed, with at most 2 merges per 100 true customers on average.
3. On the main setting, the steward queue from 0.5 is at least 3 times the queue from 0.9, on average.
4. On the main setting, with the steward queue from 0.8, the share of paying customers whose revenue is more than 10%
   wrong is at most half the deterministic rule's share, on average.
5. On perfect-match clusters, source priority keeps the right email more often than most recent, and most recent
   keeps the right address more often than source priority (means over the 10 seeds).
6. On perfect-match clusters, the best survivorship rule for the address keeps revenue per customer by region within
   5% on average over the regions.
7. Even the perfect match leaves at least 1% of paying customers with revenue more than 10% wrong, on average.
8. At the 2 x entry-noise setting (sharing 1), the deterministic rule's count error is at least twice its error at the
   0.5 x setting, on average.

## 8. What would change the conclusion

- If the probabilistic rule at 0.9 makes more than 2 merges per 100 true customers, the page leads with the merges,
  not the count.
- If no threshold brings the share of paying customers with revenue more than 10% wrong under 5% without a steward
  queue above 50 pairs per 1,000 customers, the page says no threshold does.
- If the ranking of the survivorship rules on any field flips across the 9 data settings, the page says the ranking
  does not hold for that field.
- Rates outside section 6 (household sizes, the 0.9 share of logged purchases with an account id, the sync's 0.7) are
  not swept; the page says so.

## 9. Checks

- Unit tests on small tables written into the test code: the deterministic joins (each key type), the email and
  address normalisers, the comparison levels, the expectation-maximisation fit on a table with a known answer, the
  transitive clustering, merges and splits, the revenue measures, each survivorship rule, and the steward queue.
- A test that the first part's generator output is unchanged (a pinned hash on a small run).
- A seeded replay test that rebuilds this part's results file and page from the stored per-run aggregates and compares
  bytes, with the headline numbers pinned; one small seeded run checks the pipeline end to end.
- A number check that ties every number on the page to the results file, with a planted number as its positive
  control.

## 10. Order of work

1. This file and `protocol-master-record.json` committed.
2. Code written and debugged on development seed 100 only, then committed before seeds 0 to 9 run.
3. Seeds 0 to 9 run once on all 9 settings; the per-run aggregates are committed before the page is written.
4. The page is built from the results file. Nothing is run again after the results are read unless a numbered
   amendment above says so.

## Amendment 1: a second fit of the probabilistic model, and two added measures

Declared after the code ran on development seed 100 (not a reported seed) and before seeds 0 to 9 ran. What that run
showed, and nothing else was read from it beyond output formats and timings:

- The fit declared in section 2 set the prior to 1.0 within its iterations and gave every candidate pair a posterior
  of 1.0, so at every threshold every candidate pair linked and the steward queue was empty at every lower bound. The
  candidates are blocked on surname, postcode and email local part, so they agree on those features far more often
  than the random pairs that set u, and a free prior absorbs that difference.
- Revenue per customer by region was driven by master customers with no address at all (about a third, mostly email
  subscribers who never bought), the same under every survivorship rule.
- Many of the deterministic rule's splits share no block key with the rest of their person's records, so no pairwise
  rule can repair them.

Rules 2 and 3 stay exactly as declared and are reported. Added beside them, built for the first point and labelled so
on the page:

- **Rule 2s, the session fit:** the same candidates, levels and u. Match probabilities are fitted by expectation
  maximisation in three sessions, one per block rule, each over that rule's candidate pairs and only the features the
  rule does not block on (the surname-and-initial block: email, phone, address, card; the postcode-and-number block:
  first name, surname, email, phone, card; the email-local-part block: first name, surname, phone, address, card),
  with the same starting values and limits as section 2. Each session's own prior is free and discarded. Each
  feature's m is the mean over the sessions that fit it. The posterior uses the probability that two random records
  are one person, estimated as the record pairs inside deterministic clusters over all record pairs (a lower bound,
  since the deterministic rule misses matches). Same thresholds.
- **Rule 3s:** rule 3's steward queue on rule 2s's posteriors, with the same lower bounds, auto-link level, steward
  accuracies and draws.
- Survivorship is also scored on rule 2s's clusters at 0.9.

Added measures:

- **The best a pairwise rule can do on these candidates:** the deterministic clusters joined by every candidate pair
  that is one person (a steward who reviews every candidate pair and never errs). Its splits are the ones no candidate
  pair can reach.
- **Revenue per customer by region over paying customers only:** master customers and true customers with positive
  net revenue, so the measure reads the surviving address rather than who has one.

Predictions for the added arm and measures, before any reported run:

9. On the main setting, rule 2s at 0.9 has a smaller customer-count error than the deterministic rule in every seed.
10. On the main setting, rule 2s's own estimate of its precision at 0.9 is within 10 points of its true precision, on
    average.
11. On the main setting, rule 3s's queue from 0.5 is at least 3 times its queue from 0.9, on average.
12. On perfect-match clusters, the best survivorship rule for the address keeps paying-customer revenue per customer
    by region within 5% on average over the regions.
