AI from scratch · Chapter 12 · Stage 3: classic ML and judging models
Measuring a model
"It's 99% accurate" is the most common way an ML project fools its own team. A model's quality isn't one number: it depends on how rare the thing you're looking for is, where you draw the line between yes and no, and what each kind of mistake costs. This chapter is the vocabulary for saying what a classifier is actually good for.
1. A classifier gives scores, not answers
Say a model flags fraudulent card payments. For each payment it outputs a score; we say "fraud" when the score is over a threshold. Below are 10,000 payments: the honest ones' scores and the frauds' scores. A good model pushes them apart; they always overlap somewhere.
With fraud at 1%, a "model" that says never fraud scores 99% accuracy and catches nothing. Accuracy counts the easy majority; it hides the cases you built the model for. So we split the mistakes:
A classifier only ranks; a threshold turns the ranking into decisions, and accuracy hides what happens to the rare class.
1 payment in 100 is fraud. What accuracy (in %) does the model that always answers "not fraud" get?
2. Precision and recall
Recall (sensitivity): of all the frauds, how many did we flag? Precision: of everything we flagged, how much was really fraud? Move the threshold above: down, and recall rises while precision falls (we flag more honest people); up, the opposite. There's no setting that's free.
\[ \text{precision} = \frac{TP}{TP + FP} \qquad \text{recall} = \frac{TP}{TP + FN} \qquad F_1 = \frac{2\,\text{precision}\cdot\text{recall}}{\text{precision} + \text{recall}} \]
Now make fraud rarer with the middle slider and keep the threshold. Recall doesn't change (it's measured only on frauds) but precision collapses: the honest crowd is so large that even a small false-positive rate buries the real hits. This is the base-rate trap behind every "the test is 99% accurate, so…" mistake.
Recall asks how many real cases you caught, precision how many alarms were real; making the cases rarer crushes precision and leaves recall alone.
A screening model has a false-positive rate of 1% and catches 90% of cases. The condition affects 1 in 1,000. Roughly what fraction of flagged people actually have it?
- About 90%
- About 8%
- About 0.1%
3. Every threshold at once: ROC and precision-recall curves
Sweep the threshold from top to bottom and plot what you get at each setting. The ROC curve plots recall (true-positive rate) against the false-positive rate; the precision-recall curve plots what it sounds like. The dot is your current threshold.
The area under the ROC curve (AUC) has a lovely meaning: the chance that a random fraud scores higher than a random honest payment. 0.5 is a coin flip, 1.0 is perfect separation. It doesn't move when you change how rare fraud is (try it), which makes it good for comparing models and bad for predicting what users will see. For rare events, look at the precision-recall curve: that one shows the trap.
AUC is the chance a random positive outranks a random negative: a property of the ranking alone, blind to the threshold and the base rate.
The math: AUC as a ranking probability
With scores \(s^+_i\) for the \(n_+\) positives and \(s^-_j\) for the \(n_-\) negatives,
\[ \text{AUC} = \frac{1}{n_+ n_-} \sum_i \sum_j \mathbf{1}\left[s^+_i > s^-_j\right] \]
the fraction of (positive, negative) pairs ranked the right way round. Sort all scores once and it's a sum of ranks (the Mann–Whitney U statistic), so it costs \(O(n \log n)\), not \(n_+ n_-\). For two bell curves a distance \(d\) apart with the same spread, \(\text{AUC} = \Phi(d / \sqrt 2)\).
4. Choosing the threshold: what does a mistake cost?
The right threshold isn't a property of the model; it's a business decision. A missed fraud might cost $400; blocking an honest customer's card might cost $20 in support time and goodwill. Pick the costs, and the threshold that minimizes the expected cost follows.
When missing is expensive, the best threshold drops (flag more); when false alarms are expensive, it rises. Medical screening, spam filters and fraud teams all pick different points on the same curve.
The threshold is a business decision: it sits where the expected costs of the two mistakes balance.
Why the best threshold is at probability \(c_{\text{FP}} / (c_{\text{FP}} + c_{\text{FN}})\)
- Suppose the model is calibrated (section 5): a payment it gives probability \(p\) really is fraud a fraction \(p\) of the time.
- Flag it, and you pay the false-alarm cost when it's honest: on average \((1 - p)\, c_{\text{FP}}\).
- Let it through, and you pay the miss cost when it's fraud: on average \(p\, c_{\text{FN}}\).
- Flag when flagging is cheaper: \((1 - p)\, c_{\text{FP}} < p\, c_{\text{FN}}\), which rearranges to \(p > c_{\text{FP}} / (c_{\text{FP}} + c_{\text{FN}})\).
- With $20 and $400: flag anything above \(20/420 \approx 4.8\%\). A payment that's 95% likely honest still gets flagged, because one miss costs as much as 20 false alarms.
- The threshold depends only on the costs, not on the model. The model's job is to make \(p\) honest, which is why calibration matters even when nobody reads the probabilities.
5. Calibration: does 0.8 mean 80%?
Many models output a probability, and people act on it ("80% chance of rain, take an umbrella"). A model is calibrated if, among all the times it says 0.8, the thing happens 80% of the time. Modern neural networks are often overconfident: their 0.95s come true much less than 95% of the time. The fix from chapter 5: divide the scores by a temperature before the sigmoid, with T chosen on validation data.
Calibrated means the probabilities can be taken at their word; that's a separate skill from ranking well.
The bars show, for each bin of predicted probability, how often fraud really happened. On the diagonal, the model means what it says. The expected calibration error (ECE) is the average gap, weighted by how many predictions fell in each bin; with rare fraud nearly every prediction sits in the lowest bin, so look at the confident ones too (the readout), where overconfidence hurts. Temperature doesn't change the ranking, so the AUC stays exactly the same: calibration and discrimination are different properties.
Where it breaks: numbers that were right on the test set
- The base rate moves. Test sets are often balanced 50/50 for convenience. At recall 0.9 and a 1% false-positive rate, precision on that balanced set is \(0.45 / (0.45 + 0.005) \approx 99\%\); in production at 1 fraud in 1,000 it's 8% (section 2's question). Recall and AUC carry over, precision and calibration don't: recompute them at the real rate.
- AUC averages thresholds you'll never use. A fraud team can afford maybe 1% false alarms. Two models with the same AUC can differ a lot in that corner, so compare them at the operating point, not by the area.
- Small counts. Precision from 10 flagged payments moves 10 points with each one. The next lesson (Probability 4) says how far a measured rate can wobble and still be the same model.
6. What to report
- The base rate of the thing you predict, and a baseline (always-no, last week's rule) next to every number.
- Precision and recall at the threshold you'll ship, and the cost-based reason for that threshold.
- AUC or the PR curve to compare models independent of the threshold.
- Calibration, if anyone will read the probabilities.
- Slices: the same numbers per country, device, customer type. A model that's fine on average can fail a whole group.
All chapters: AI from scratch.
Last updated