Part VII — Model Evaluation, Validation & Feature Engineering · Chapter 2

ROC curves, AUC & PR curves

Hook

Precision and recall both depend on wherever you happen to draw the line between "predict positive" and "predict negative." What if you don't want to commit to one threshold at all — what if you want to know how good the model's ranking is, independent of where anyone eventually decides to cut it?

Intuition
FPR=0.33, TPR=1.00, accuracy=0.67

Drag the decision threshold. Every value traces out one point on the ROC curve — short for Receiver Operating Characteristic — the true-positive rate against the false-positive rate at that exact cutoff. Slide it all the way and you retrace the entire curve, one threshold at a time.

Formalize

Sweep the threshold from 11 down to 00, plotting (FPR,TPR)(\text{FPR}, \text{TPR}) at each stop:

TPR=TPTP+FN=recall,FPR=FPFP+TN\text{TPR} = \frac{TP}{TP+FN} = \text{recall}, \qquad \text{FPR} = \frac{FP}{FP+TN}
  • TPR — true-positive rate: the fraction of actual positives the model catches at this threshold; the same quantity as recall.
  • FPR — false-positive rate: the fraction of actual negatives the model wrongly flags as positive at this threshold.
  • TPTP, FNFN — true positives and false negatives at this threshold, the counts TPR is built from.
  • FPFP, TNTN — false positives and true negatives at this threshold, the counts FPR is built from.
  1. Two extremes bound every curve

    A model that separates classes perfectly traces a curve that shoots straight up to (0,1)(0,1) before moving right at all. A model no better than a coin flip traces the diagonal exactly. Every real model's curve falls somewhere between the two.

  2. AUC condenses the whole sweep into one number

    The area under this curve (AUC) is the probability a random positive example gets a higher score than a random negative one — one number that summarizes the entire threshold sweep at once.

Play
AUC = 0.889 — a random guesser's diagonal would score exactly 0.5

The curve bows up and to the left of the diagonal — better than chance, but not the sharp right angle a perfect separator would trace. That's exactly what the one overlapping score pair in this dataset should produce: mostly correct ranking, with one pair out of place.

Worked example

Three positives (scores 0.9,0.7,0.40.9, 0.7, 0.4) and three negatives (scores 0.6,0.3,0.10.6, 0.3, 0.1):

  1. Two points at zero false positives
    • At threshold 0.90.9: TPR=1/3=1/3, FPR=0=0.
    • At threshold 0.70.7: TPR=2/3=2/3, FPR=0=0 — both real positives outrank every negative so far.
  2. The one overlapping pair costs a false positive
    • At threshold 0.60.6, the highest-scoring negative gets swept in too: TPR stays at 2/32/3, but FPR jumps to 1/31/3.
    • One step later, at 0.40.4, the last positive is caught: TPR reaches 11, FPR still 1/31/3.
  3. Integrating the whole curve

    AUC equals the fraction of (positive, negative) pairs where the positive scores higher — check all 3×3=93\times3=9 pairs:

    • 0.90.9 beats all three negatives (0.6,0.3,0.10.6,0.3,0.1): 33 wins
    • 0.70.7 beats all three negatives: 33 wins
    • 0.40.4 beats 0.30.3 and 0.10.1, but loses to 0.60.6: 22 wins

    Total: 3+3+2=83+3+2=8 correctly-ranked pairs out of 99, so AUC=8/90.889\text{AUC}=8/9\approx0.889 — meaningfully better than a coin flip's 0.50.5, but short of a perfect 1.01.0, exactly because of that one overlapping pair (0.40.4 losing to 0.60.6).

Checkpoint

Drag the threshold until every positive is caught (TPR=1) with the fewest possible false alarms.

FPR=0.00, TPR=0.33
Drag the threshold to try it
Summary
AUC=P(random positive scores higher than random negative)\text{AUC} = P(\text{random positive scores higher than random negative})

AUC answers a question accuracy, precision, and recall all dodge: how good is the model's ranking, before anyone commits to a specific cutoff? That makes it the right tool for comparing models that might eventually be deployed at different thresholds for different purposes — a fraud system tuned conservative in one country and aggressive in another can still be compared fairly by the same AUC, even though their accuracies at their own chosen thresholds would look completely different.