Contents
06The precision-recall curve
6.1Why precision depends on the prevalence
Precision mixes the two classes, and the mixing weight is the prevalence. This is just Bayes’ rule.
Proposition 3 (Precision from the rates). For a fixed threshold with rates \mathrm{TPR} and \mathrm{FPR}, on data with prevalence \pi (the share of positives),
By Bayes’ rule, p(y=1\mid\hat y=1)=p(\hat y=1\mid y=1)p(y=1)/p(\hat y=1), and by the law of total probability p(\hat y=1)=p(\hat y=1\mid y=1)\,\pi+p(\hat y=1\mid y=0)(1-\pi). Substitute \mathrm{TPR} and \mathrm{FPR} from (6). Equivalently, with counts: with n_+ positives and n_- negatives, \mathrm{TP}=n_+\mathrm{TPR} and \mathrm{FP}=n_-\mathrm{FPR}; dividing numerator and denominator of \mathrm{TP}/(\mathrm{TP}+\mathrm{FP}) by n=n_++n_- gives (10).
Murphy (2022, eq. 5.28) writes the same thing as P=\mathrm{TPR}/(\mathrm{TPR}+\mathrm{FPR}/r) with r=n_+/n_-. When \pi is tiny, \mathrm{FPR}\cdot(1-\pi) is a small rate times a huge crowd, and it swamps \mathrm{TPR}\cdot\pi. At the threshold \tau=0.5 (flag when p\ge0.5) our model has \mathrm{TPR}=0.559 and \mathrm{FPR}=0.000322; with \pi=0.00151, (10) gives 72.4\%, exactly the measured precision. In a world where half the transactions were fraud, the same rates would give 99.94\%; at 1 fraud in 10{,}000, only 14.8\% (Figure 5b).
Worked example 5 (A diagnostic test, Murphy, 2022, §2.3.1). A COVID-19 test with sensitivity 87.5\% (\mathrm{TPR}) and specificity 97.5\% (\mathrm{FPR}=2.5\%). Where 10\% of people are infected, as in New York City in spring 2020, a positive result means infection with probability 0.875\times0.1/(0.875\times0.1+0.025\times0.9)=79.5\%. Where only 1\% are infected, the same test gives 26\%: of 100{,}000 people, 875 of the 1{,}000 infected and 2{,}475 of the 99{,}000 healthy test positive, and 875/3{,}350=0.26. The test did not change; the prevalence did. This is the base-rate fallacy, and it is precisely the gap between recall (p(\hat y=1\mid y=1)) and precision (p(y=1\mid\hat y=1)).
The fallacy is not only a student’s mistake. Gigerenzer and Hoffrage (1995), adapting a problem of Eddy (1982), give the numbers for routine breast cancer screening of women aged forty: prevalence 1\%, \mathrm{TPR}=80\%, \mathrm{FPR}=9.6\%. By (10) the precision of a positive mammogram is 0.01\times0.8/(0.01\times0.8+0.99\times0.096)=7.8\%: of 1{,}000 women, 8 of the 10 with cancer and 95 of the 990 without test positive, and 8/103=7.8\%. They report Eddy’s finding that 95 of 100 physicians estimated this probability at 70 to 80\%, confusing it with the recall.
6.2The curve and its area
The precision–recall curve plots precision against recall as the threshold sweeps (Murphy, 2022, §5.1.4). Walk down the sorted list again; this time, each time a fraud is reached, record the recall so far (k/n_+ for the k-th fraud) and the precision so far (frauds seen over transactions seen). Figure 5a shows our model: the first 12 transactions in the ranking are all frauds, so the curve starts at precision 1; it stays above 0.7 up to a recall of about 0.6, then falls steeply, and the last fraud is reached only at rank 137{,}466.
The standard summary is average precision, the mean of the precisions recorded at each fraud:
where (R_k,P_k) are the points of the curve; recall grows by 1/n_+ at each positive and not at all at a negative, which gives the second form. AP is the area under the step-shaped curve. On our test day \mathrm{AP}=0.643. Random scores would give \mathrm{AP}\approx\pi=0.0015 (an actual draw of random scores gave 0.00157, with AUC 0.49): every flag is fraud with probability \pi whatever the threshold. So the random baseline of the PR curve is the horizontal line at the prevalence, not the diagonal, and 0.64 should be read against 0.0015, not against 0.5.
Worked example 6 (AP by hand). The seven transactions of Worked example 3, labels F F L F L L F. The precisions at the four frauds are 1/1, 2/2, 3/4 and 4/7, so \mathrm{AP}=(1+1+0.75+0.571)/4=0.830, while \mathrm{AUC}=0.667. AP weights the top of the list most: the two frauds at the top earn full marks, and the late fraud costs little. AUC treats every (fraud, legitimate) pair alike, wherever it sits in the list.
Two variants appear in practice. The interpolated precision at recall r is the highest precision at any recall \ge r, which removes the wiggles; its average is 0.645 here (Murphy, 2022, §5.1.4.2). And many libraries report the area under the curve with straight lines between the points (the trapezoid rule), 0.643 here; Davis and Goadrich (2006) show that straight-line interpolation in precision–recall space is incorrect and can be optimistic, because precision does not change linearly between two thresholds. For ranked retrieval, precision at K answers the user’s question directly: our top 100 transactions contain 81 frauds, the top 200 contain 134.
6.3Same ROC, different worlds
To see the prevalence at work, keep the model and its ROC curve and change only \pi. Applying (10) at every point of the ROC curve with \pi=0.5 gives the dashed curve of Figure 5a and \mathrm{AP}=0.971. A real check: take all 211 frauds and 211 random legitimate transactions, 200 times; the average precision is 0.971\pm0.002 and the AUC stays 0.958\pm0.003. Same model, same ranking quality, same AUC, but an average precision that moves from 0.64 to 0.97 with the prevalence. The PR curve answers the question the bank actually faces, “if I act on these alarms, how often am I right?”, in the world where the model will be used.
6.4How the two curves are related
ROC and PR curves are two views of the same confusion matrices. For a fixed data set, each ROC point corresponds to exactly one PR point (given n_+ and n_-, the pair (\mathrm{FPR},\mathrm{TPR}) determines \mathrm{TP}, \mathrm{FP} and \mathrm{FN}), and Davis and Goadrich (2006) prove that one curve dominates another in ROC space if and only if it dominates in PR space. So if one model is better everywhere, both curves agree. They disagree when curves cross: the PR curve stretches the low-false-positive region, where the precision of a rare-positive problem is decided, and squeezes the rest. Saito and Rehmsmeier (2015) reach the same practical conclusion in a simulation study: with imbalanced data, the precision–recall plot is more informative than the ROC plot. The rule of thumb is therefore: ROC-AUC to compare rankings when the classes are not too unbalanced or when you care about all operating points; the precision–recall curve and average precision when positives are rare and false alarms matter.
Pause and think. A fraud team retrains its model on a month with twice as many frauds. The AUC is unchanged, but the average precision goes up. Did the model improve?
Show the answer
Not necessarily. The AUC, which does not depend on prevalence, says the ranking is just as good. Average precision rises when positives become more common, even for the same model, as equation (10) shows. Compare average precision only on data with the same prevalence.