# Design: fraud decisions when the labels arrive late

Declared before any number from the test period (hours 24 to 48) was computed or read. The declaration time and this
file's sha256 are in `design-declaration.json`. Anything added after that point is labelled as added later, with why.

## Question

When fraud labels arrive late, how much does a model trained and monitored on only the labels it has lose, against one
trained on full labels? Which decision threshold and which monitoring rule hold up when the truth is incomplete?

## Data

- The credit card fraud data of the Machine Learning Group of the Université Libre de Bruxelles (ULB), as published on
  OpenML (dataset 1597, `creditcard`), parquet file `https://data.openml.org/datasets/0000/1597/dataset_1597.pq`,
  sha256 `b7efcb35a428bbe22347a05d2437d9177bab07ce61e51214a17bec584ad9496d`. The fetch script refuses any other bytes.
- 284,807 card transactions by European cardholders over two days, 492 of them fraud. Features V1 to V28 are PCA
  components; `Time` is seconds since the first transaction; `Amount` is the transaction amount; `Class` is 1 for fraud.
- The data covers only two days. Every delay below is simulated on the data's own clock, in hours. In real card
  portfolios, chargebacks and confirmed fraud reports arrive days to weeks after the transaction; here the same shape is
  compressed so that a typical label arrives about one decision window after its transaction. This is an assumption.
- The currency of `Amount` is not documented. Money on the page is read as euros (an assumption).
- No transaction row is committed or published: the code fetches the file and every published file holds aggregates.

## 1. Time split

- Hour of a row = `Time / 3600`. Hours 0 to 24 are the development period; hours 24 to 48 are the test period.
- The test period is six decision windows of four hours: [24, 28), [28, 32), [32, 36), [36, 40), [40, 44), [44, 48].
- At the start of each window (decision time t), every arm is retrained on all rows with hour < t, using only the label
  view that arm would have at time t, and then scores every transaction in the window.
- Features: V1 to V28 and log(1 + Amount). No time-of-day feature, so a model cannot learn "recent rows are clean".

## Label delay (assumption)

- Each fraud is never reported with probability q. Otherwise it is reported after a delay D drawn from a log-normal
  distribution with median m hours and log-scale sigma 0.6. Legitimate rows produce no report.
- At time t, a row carries a fraud label only if it is fraud, it is reported, and its hour plus D is at most t.
- Base case: m = 4 hours (one decision window), q = 10%.
- Maturity horizon H = the 95th percentile of D = m x exp(1.645 x 0.6), 10.7 hours in the base case. A row older than H
  has as complete a label as it will ever get.

## 2. Arms

Every arm uses the same two model families: gradient boosting (scikit-learn `HistGradientBoostingClassifier`,
200 iterations, learning rate 0.05, 15 leaves, L2 1.0, no early stopping) and a logistic baseline (standardised
features, `LogisticRegression`, C = 0.1).

- **Full labels (the ceiling):** the true class of every training row. No team has this at decision time; it is the
  reference every other arm is scored against.
- **Naive:** labels that have arrived by t; every unreported fraud counts as legitimate.
- **Drop recent:** rows older than H with their arrived labels, plus every row whose fraud label has arrived, whatever
  its age. Recent rows without a label are dropped.
- **Weighted (the declared correction):** the naive view, with each arrived fraud weighted by 1 / G(age), capped at 10,
  where G is the log-normal distribution function of D. G is the timing of reports among frauds that do get reported,
  which a team can estimate from its own history; the correction does not know q, the share never reported.
- Training sample for every arm: every row labelled fraud in that arm's view, plus a seeded 10% random sample of the other
  rows with weight 10. If an arm's view holds no fraud label, it has no model and raises no alerts.

## 3. Decision

Declared costs, all assumptions:

- A missed fraud costs L = 100% of its amount.
- Each alert costs R = 5 euros to review.
- At most K = 100 alerts per four-hour window (600 a day).

Rules, applied to each window with each arm's model:

- **Cost rule (primary):** expected loss = predicted fraud probability x amount x L. Alert when the expected loss is at
  least R; if more than K transactions qualify, alert the K with the highest expected loss.
- **Fill the cap (comparison):** alert the K transactions with the highest expected loss, whatever the threshold.

Reported per arm, model, rule and window, never pooled across windows:

- alerts; frauds caught; fraud value caught and its share of the window's fraud value;
- precision of the alerts, and precision at the cap (precision of the K transactions with the highest expected loss);
- total cost = L x missed fraud value + R x alerts, and its excess over the full-labels arm (same model, same rule);
- shortfall in fraud value caught against the full-labels arm: 1 - caught by the arm / caught with full labels.

Intervals: per window, 1,000 bootstrap resamples of that window's transactions (seeded), 95% percentile intervals for
each arm's shortfall and excess cost. The delay draw is a second source of noise: every scenario is run with 5 seeds
(delay draw, training sample and model seed), and the page reports the spread across seeds.

## 4. Monitoring

- Monitored model: the naive-arm gradient boosting model trained at hour 24, left in place for hours 24 to 48, cost rule.
- Hourly checks at c = 25, 26, ..., 48. At each check, three estimates of the precision of its alerts:
  - **True:** alerts in [c - 8, c) against full labels (unknowable at the time; the yardstick).
  - **Naive:** alerts in [c - 8, c) against the labels arrived by c, unarrived counted as legitimate.
  - **Matured:** alerts in [c - H - 8, c - H), from hour 24 on, against the labels arrived by c. Undefined when there
    are no such alerts yet; the monitor then waits.
- Retrain rule: trigger a retrain when the estimated precision falls below a floor. The floor is half the development
  precision P_dev: the precision of the cost-rule alerts of a gradient boosting model trained on hours 0 to 16 with full
  labels and scored on hours 16 to 24. P_dev is computed by the `dev` stage and written to its results file before the
  test stages run.
- Scored per monitor: checks where it fires, false alarms (it fires and the true-precision rule does not), misses (the
  true-precision rule fires and it does not), checks where it waits, and the mean absolute gap to the true precision.
- Run for the base case and for every sweep cell (seed spread reported).

## 5. Sweep

- m in {1, 2, 4, 6, 8} hours, q in {0%, 10%, 25%, 50%}: 20 cells, 5 seeds each, both models, both rules.
- Costs on the base case scores: R in {2, 5, 10} euros and K in {50, 100, 200} alerts per window.
- A flip: in a window, the best arm other than full labels (highest mean value caught over seeds, gradient boosting, cost
  rule) differs from the base case's best arm in that window. The page shows the cells where it flips.

## Predictions (written before the test run)

1. Under the cost rule, the naive arm catches less fraud value than full labels in every window of the base case,
   mainly because its probabilities are too low (unreported fraud is learned as legitimate), so fewer transactions clear
   the review cost.
2. Under fill the cap, the naive arm's shortfall is smaller than under the cost rule in most windows: ranking survives
   missing labels better than calibration does.
3. The naive monitor reads precision far below the truth in every check, and fires more false alarms than the matured
   monitor.
4. The weighted arm closes part of the naive arm's gap when q is small, and less as q grows, since it cannot see frauds
   that are never reported.
5. Drop recent is the worst arm when m is long, because it throws away most of the recent data.

Each prediction is scored on the page as held or not.

## Reproduction

`python fetch.py` downloads and checks the file; `python -m fdl.run all --data <file>` runs every stage; `python -m
fdl.report` rebuilds `results/results.json`, `results/page-numbers.json` and `index.html` from the stage files.
