Contents
05ROC curve and AUC
5.1Sweep the threshold
As before, the model flags a transaction (\hat y=1) when its score s(x), here the predicted fraud probability, is at least a threshold \tau; y=1 means the transaction really is fraud, y=0 that it is legitimate. Two rates, each computed inside one class (Murphy, 2022, eqs. 5.18–5.19):
The true positive rate is recall: the share of frauds flagged. The false positive rate is the share of legitimate transactions flagged, the false alarm rate; 1-\mathrm{FPR} is the specificity. Now sweep the threshold \tau from +\infty (nothing is flagged) down to -\infty (everything is flagged): each threshold gives one point (\mathrm{FPR},\mathrm{TPR}), from (0,0) (flag nothing) to (1,1) (flag everything). The path is the ROC curve (Figure 4a). Concretely, sort the test transactions by score from high to low and walk down the list: each fraud moves the curve up by 1/n_+, where n_+=211 is the number of frauds, and each legitimate transaction moves it right by 1/n_-, where n_-=139{,}810 is the number of legitimate transactions. A perfect ranking climbs straight to (0,1) and then runs right; a random ranking climbs as fast as it runs, along the diagonal.
Two properties follow at once. First, the curve depends only on the order of the scores: any strictly increasing transformation of the score s (squaring it, taking its logarithm) gives the same curve. Second, it does not depend on the prevalence, the share of frauds: \mathrm{TPR} is computed only among the frauds and \mathrm{FPR} only among the legitimate transactions (Murphy, 2022, §5.1.3.3), (Fawcett, 2006, §4). Each rate is a fraction inside one class, so it cannot see how big that class is compared with the other.
Worked example 2 (Make fraud ten times rarer). Imagine nine more copies of every legitimate transaction on the test day, so the frauds are ten times rarer: 211 frauds among 1{,}398{,}100 legitimate transactions, a prevalence of 0.015\% instead of 0.15\%. Keep the model and the threshold \tau=0.5. The frauds are untouched, so 118 of the 211 are still caught: \mathrm{TPR}=55.9\%. The false alarms grow tenfold, from 45 to 450, but the number of legitimate transactions grows tenfold too, so \mathrm{FPR}=450/1{,}398{,}100=45/139{,}810=0.032\%, unchanged. The same holds at every threshold, so every point of the ROC curve stays where it was, and the AUC stays 0.958. What the fraud team sees has changed a lot: 118 real frauds among 118+450=568 alarms, a precision of 20.8\% instead of 72.4\%, so four alarms in five are now false. The same happens all along the precision-recall curve of Chapter 6: at the point where \mathrm{FPR}=0.96\%, the 174 caught frauds now come with 13{,}480 false alarms instead of 1{,}348 (precision 1.3\% instead of 11.4\%), and the average precision falls from 0.64 to 0.42. This is what “ROC ignores how rare fraud is” means: the curve describes how the scores of each class are spread, not how many of each class the model will meet. Chapter 6 turns this into a formula.
5.2The area under the curve
The area under the ROC curve, AUC, summarizes the whole curve in one number: 1 for a perfect ranking, 0.5 for a random one. Ours is 0.958. Its meaning is the most-asked fact about it.
Proposition 2 (AUC is a probability, Hanley and McNeil, 1982). Let s^+ be the score of a randomly chosen positive and s^- that of a randomly chosen negative, drawn independently. Then the area under the empirical ROC curve is
Assume first that there are no ties. Walk down the sorted list. Each negative j moves the curve right by 1/n_-, and at that moment the height of the curve is the fraction of positives already passed, that is, with a higher score: \frac1{n_+}\sum_i\mathbb{1}[s_i>s_j]. The region under the curve is a set of rectangles, one per negative, of width 1/n_- and that height, so \mathrm{AUC}=\sum_j\frac1{n_-}\cdot\frac1{n_+}\sum_i\mathbb{1}[s_i>s_j], which is (7). A group of tied scores moves the curve diagonally; the area under that diagonal piece is a trapezoid, and it gives each tied (positive, negative) pair exactly half a unit. The double sum counts the fraction of all n_+n_- pairs in which the positive wins, which is the probability on the right.
The double sum is the Mann–Whitney statistic U divided by n_+n_- (Mann and Whitney, 1947), and it can be computed from ranks without forming any pair. Give all n scores their ranks 1,\dots,n from lowest to highest, and let R_+ be the sum of the ranks of the positives. The positive with the k-th smallest score among the positives has rank k plus the number of negatives below it, so R_+=\frac{n_+(n_++1)}{2}+U, and
On our test day there are 211\times139{,}810=29{,}499{,}910 pairs, U=28{,}265{,}092, and U/(n_+n_-)=0.958, the same as the area. Drawing 10{,}000 random pairs, the fraud wins 96.0\% of the time (Figure 4c). The AUC is also tied to the Gini coefficient used in credit scoring: \text{Gini}=2\,\mathrm{AUC}-1=0.916 (Hand and Till, 2001; Fawcett, 2006).
Worked example 3 (AUC by hand). Seven transactions sorted by score from high to low, with labels F F L F L L F (F: fraud, L: legitimate). There are 4\times3=12 (fraud, legitimate) pairs. The frauds at positions 1 and 2 beat all three legitimate ones (3+3); the fraud at position 4 beats the two below it (2); the last fraud beats none. \mathrm{AUC}=8/12=0.667. With ranks from the bottom, the frauds have ranks 7,6,4,1, so R_+=18 and U=18-4\cdot5/2=8, as before.
Worked example 4 (A radiologist’s ROC curve, Hanley and McNeil, 1982). A ROC curve does not need a model: any ordered rating will do. Hanley and McNeil (1982, Table I) report one reader’s ratings of computed tomography images of 109 patients, 58 normal and 51 abnormal, on a five-point scale from 1 (definitely normal) to 5 (definitely abnormal); Table 2 gives the counts. Calling an image abnormal when its rating is at least k gives one ROC point for each k: at k=5 the reader catches 33 of the 51 abnormal images (\mathrm{TPR}=0.647) with 2 of the 58 normal ones (\mathrm{FPR}=0.034); at k=4, \mathrm{TPR}=44/51=0.863 and \mathrm{FPR}=13/58=0.224. Joining the points with straight lines, which is the trapezoid rule and counts tied ratings as half, gives an area of 0.893, exactly the Wilcoxon statistic. With Q_1 and Q_2 estimated from the data, equation (9) below gives a standard error of 0.032, as in the paper; the closed-form Q_1, Q_2 give 0.0325.
| rating | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| normal images (58) | 33 | 6 | 6 | 11 | 2 |
| abnormal images (51) | 3 | 2 | 2 | 11 | 33 |
| \mathrm{TPR} if rating \ge k | 1.000 | 0.941 | 0.902 | 0.863 | 0.647 |
| \mathrm{FPR} if rating \ge k | 1.000 | 0.431 | 0.328 | 0.224 | 0.034 |
5.3What is a good AUC?
It depends on the field, and Swets (1988) collected real values. Computed tomography and chest X-ray reading reached areas of 0.90 to 0.98, mammography 0.80 to 0.90 (six radiologists reading 118 mammograms scored 0.81, and 0.87 with a reading aid), forecasts of rain 0.75 to 0.90, and information retrieval systems 0.75 to 0.95. As a rough guide he called areas from 0.5 to 0.7 low, from 0.7 to 0.9 useful for some purposes, and above 0.9 rather high. Our fraud model, at 0.958, is in the “rather high” range, and the next two subsections show why that is still not the whole story.
5.4How precise is an AUC?
Hanley and McNeil (1982) gave a standard error for the area, with A=\mathrm{AUC}:
Q_1 is the probability that two random positives both outscore one random negative, and Q_2 the probability that one positive outscores two negatives; the closed forms come from a model in which the scores are exponentially distributed, and they work well in practice. With A=0.958: Q_1=0.920, Q_2=0.938, and \mathrm{SE}=0.0096. A stratified bootstrap of the test day (resampling frauds and legitimate transactions separately, 300 times) gives 0.0101, with a 95\% interval from 0.938 to 0.976. When n_-\gg n_+, the last term dominates and \mathrm{SE}\approx\sqrt{(Q_2-A^2)/n_+}: the precision of the AUC is set by the rare class. Here it is the 211 frauds, not the 139{,}810 legitimate transactions, that limit what we know.
5.5Where ROC flatters
Read the curve at a false positive rate of 1\%: the true positive rate is 82.5\%, which looks excellent. But 1\% of 139{,}810 is a lot of transactions. At that point the model flags 174 frauds and 1{,}348 legitimate transactions: a precision of 11.4\%, so almost nine of every ten blocked cards belong to innocent customers. In Figure 4a all of this happens in a sliver at the left edge; only the logarithmic axis of Figure 4b shows it. Murphy (2022, §5.1.3.3) puts it this way: with severe imbalance a large change in the number of false positives barely changes the false positive rate, so all the action happens in the extreme left part of the curve. The very property that made ROC attractive, that it ignores the prevalence, hides the cost of rarity.
Pause and think. Two fraud models have AUC 0.95 and 0.93. Which do you deploy?
Show the answer
Not necessarily the first. AUC averages over all false positive rates, including ranges no bank would ever operate in (flagging 30\% of all payments). Compare the curves where you will actually operate, at the low false positive rates, or compare precision at the recall you need; two ROC curves can cross (Fawcett, 2006).