ROC and Precision–Recall Curves
Threshold sweeps, AUC interpretation, and why PR curves beat ROC under heavy imbalance.
Assumes you know
ROC and Precision–Recall Curves
Intuition first
A classifier outputs scores. Where you cut those scores into "yes" and "no" is a separate decision from how good the scores are.
Cut low and you catch almost everything but flag a lot of innocents. Cut high and everything you flag is real but you miss most cases. Neither setting tells you whether the ranking underneath is any good.
A curve sweeps the cut point across its whole range and plots what happens. If the scores rank positives above negatives well, the curve bows sharply towards the good corner regardless of where you eventually cut. If the scores are noise, the curve is a diagonal.
Two curves are in common use, and choosing between them matters far more than most people realise: on heavily imbalanced data they can tell completely different stories about the same model.
The ROC curve
Plots the true positive rate against the false positive rate as the threshold sweeps from high to low.
AUC is the area under this curve. It has an exact probabilistic meaning, which is the reason it is worth knowing.
AUC equals the probability of correct rankingAdvanced
Claim: AUC is the probability that a randomly chosen positive receives a higher score than a randomly chosen negative.
Sketch. Sweep the threshold from downwards. At threshold ,
The area under the ROC curve is . Substituting the distribution of negative scores for the measure turns the integral into
where is the density of scores on negatives. Ties contribute one half.
Two useful consequences fall out immediately:
- AUC is random ranking, and AUC below 0.5 means the scores are anti-correlated with the label — invert them and you have a useful model.
- AUC is invariant to any monotone transformation of the scores. Passing scores through a sigmoid, or squaring positive scores, leaves AUC untouched. It measures ranking only and says nothing about whether the numbers are calibrated probabilities.
The precision–recall curve
Plots precision against recall, again as the threshold sweeps.
Note what is absent: appears nowhere. That single fact explains why PR curves behave so differently on imbalanced data.
Why the choice matters on imbalanced data
FPR divides by the number of negatives. When negatives vastly outnumber positives, a large absolute number of false positives is a small FPR — so the ROC curve barely notices them. Precision divides by the number of predictions, so those same false positives destroy it.
Solved problem 1 · The same model, two very different stories
A dataset has 1,000 positives and 100,000 negatives — a 1% positive rate. At a particular threshold a model achieves and .
Compute the ROC coordinates and the precision, and explain what each suggests.
Step 1 — fill in the confusion matrix
Step 2 — ROC coordinates
The point sits close to the top-left corner. In ROC terms this looks excellent.
Step 3 — precision at the same threshold
Step 4 — reconcile the two views
Both numbers describe the identical 9,000 false positives.
ROC divides them by 100,000 negatives, giving a reassuring 0.09. Precision divides them by the 9,900 flagged cases, giving 0.09 — and here that means 91% of everything the model flags is wrong.
Operationally: a reviewer works through 9,900 cases to find 900 real ones, opening eleven files per genuine hit.
Step 5 — the baseline each metric is measured against
Random guessing gives AUC , so ROC has a fixed, class-balance-independent baseline.
The PR baseline is the positive rate itself, here . So precision of is about 9× better than random — genuinely informative, just not comfortable.
Answer
, , precision .
ROC says "near the perfect corner"; PR says "9 out of 10 alerts are false". Both are correct. Report PR when positives are rare and the cost of a false positive falls on a human.
Comparing the two curves
| ROC / AUC | Precision–Recall / AP | |
|---|---|---|
| Axes | TPR vs FPR | Precision vs Recall |
| Uses TN? | Yes | No |
| Baseline | 0.5 always | Positive rate — changes with balance |
| Insensitive to class balance | Yes | No |
| Good when | Classes roughly balanced; both errors matter | Positives rare; false positives costly |
| Misleads when | Heavy imbalance | Comparing across datasets with different balance |
Average precision (AP) summarises the PR curve as a weighted mean of precisions:
Computing both
import numpy as np
from sklearn.metrics import (
roc_auc_score, average_precision_score, roc_curve, precision_recall_curve,
)
rng = np.random.default_rng(0)
# 1% positive rate, mildly separable scores.
n_pos, n_neg = 1_000, 100_000
y_true = np.concatenate([np.ones(n_pos), np.zeros(n_neg)])
scores = np.concatenate([
rng.normal(1.6, 1.0, n_pos),
rng.normal(0.0, 1.0, n_neg),
])
auc = roc_auc_score(y_true, scores)
ap = average_precision_score(y_true, scores)
baseline_ap = y_true.mean()
print(f"ROC AUC {auc:.4f}")
print(f"Average precision {ap:.4f} (random baseline {baseline_ap:.4f})")
# Precision at the threshold giving 90% recall — the operational question.
prec, rec, thr = precision_recall_curve(y_true, scores)
i = np.argmin(np.abs(rec - 0.90))
print(f"at recall {rec[i]:.3f}: precision {prec[i]:.4f}, threshold {thr[min(i, len(thr)-1)]:.3f}")Run it and the pattern from the solved problem appears: AUC looks strong while average precision sits far lower, because AP is judged against a 0.0099 baseline rather than 0.5.
Exercise 1
Model P has AUC 0.91 and AP 0.34. Model Q has AUC 0.88 and AP 0.52. The positive rate is 3%. Which do you deploy for a fraud queue with limited reviewer capacity?
Show solutionHide solution
Model Q.
With a capacity-limited review queue, the quantity that matters is how many of the cases sent for review are genuine — precision at the operating threshold. AP summarises precision across thresholds, and Q's 0.52 against P's 0.34 means roughly half of Q's alerts are real versus a third of P's.
P's higher AUC reflects better ranking averaged over the whole threshold range, including regions of very high recall that a capacity-limited queue will never operate in. AUC weights all thresholds; the business only uses one.
Both beat the 0.03 baseline substantially, so both models are informative. The decision turns on the operating region, and in the high-precision region Q is better.
Worth doing before committing: plot both PR curves and compare precision at the exact recall your capacity implies, since AP is an average and the curves may cross.
Exercise 2
Explain why AUC is unchanged if you replace every predicted probability with , but at a fixed threshold of 0.5 does change.
Show solutionHide solution
Cubing is strictly increasing on , so it preserves the order of the scores. If then . Since AUC is the probability that a random positive outranks a random negative, and every pairwise comparison is unchanged, AUC is identical.
at a fixed threshold depends on which examples fall above the cut, not on their order. Cubing compresses values towards zero: a score of becomes , and becomes . An example that was above 0.5 is now below it.
So the confusion matrix changes, and with it precision, recall and — even though the model's ranking ability is untouched.
The general lesson: threshold-free metrics measure ranking, threshold-dependent metrics measure ranking and calibration. If a transformation changes your but not your AUC, the model did not get better or worse — the threshold simply no longer sits where you thought. That is what the calibration lesson addresses.
Next: Regression Metrics, where the target is continuous and the choice of metric decides which errors you are implicitly willing to make.