Sub topic 6
Reading a ROC curve: the diagonal, the corner, and trade-offs
This chapter assumes you can already compute a confusion matrix at one threshold. It also assumes you know how true positive rate (TPR) and false positive rate (FPR) are defined, and that you have seen the sweep that builds a ROC curve. Here the goal is to read a finished curve: what each region means, what the shape says about the model, and what moving along the curve costs you.
What a point on the curve is
Every point on a ROC curve is one threshold, shown as a pair (FPR, TPR).
- The horizontal axis is FPR: the share of actual negatives that the model wrongly flags as positive.
- The vertical axis is TPR: the share of actual positives that the model correctly flags.
The threshold is not drawn on the plot. It is a hidden parameter that moves you along the curve. A high threshold flags few examples, so you sit near the bottom left. A low threshold flags many, so you sit near the top right.
Both ends are fixed for any model and any data. At one end nothing is flagged, so there are no false positives and no true positives. At the other end everything is flagged, so every negative and every positive is caught. The model only controls the path between the endpoints.
Landmarks to know
| Location | Meaning |
|---|---|
| (0, 0) | The model flags nothing. TPR and FPR are both 0. |
| (1, 1) | The model flags everything. TPR and FPR are both 1. |
| (0, 1), top-left corner | Perfect classifier. Every positive is caught and no negative is flagged. |
| (1, 0), bottom-right corner | Every example is classified wrongly. The ranking is perfectly inverted. |
| Diagonal from (0, 0) to (1, 1) | No skill. The model flags positives and negatives at the same rate. |
The diagonal: no information
On the diagonal line TPR equals FPR. At any such point the model flags a fraction p of the positives and the same fraction p of the negatives. Knowing the model's decision tells you nothing about the true label.
A model that outputs random scores gives a curve that wanders close to the diagonal, and its AUC is near 0.5. With few examples the curve can stray from the diagonal by chance, so a small test set can make a useless model look slightly good.
A curve that is mostly below the diagonal means the model ranks in the wrong direction. Negating the scores mirrors the curve across the diagonal and turns AUC into 1 minus AUC. An AUC well below 0.5 usually points to a bug, such as a flipped sign, the wrong column of predict_proba, or swapped labels. It rarely means the model is truly terrible.
The following code shows all three cases.
import numpy as np
from sklearn.metrics import roc_auc_score
rng = np.random.default_rng(0)
n = 5000
y = rng.integers(0, 2, size=n)
random_score = rng.random(n)
informative = y + rng.normal(0, 1, size=n)
print("random: ", roc_auc_score(y, random_score))
print("informative:", roc_auc_score(y, informative))
print("negated: ", roc_auc_score(y, -informative))
squashed = 1 / (1 + np.exp(-informative))
print("same after sigmoid:", np.isclose(
roc_auc_score(y, informative), roc_auc_score(y, squashed)))The random scores give an AUC close to 0.5. The informative scores give about 0.76. The negated scores give exactly 1 minus the informative value. The last line prints True.
That last check shows a useful property. The ROC curve depends only on how the scores rank the examples, not on their scale. Any strictly increasing transformation of the scores, such as a sigmoid, leaves the curve unchanged. A well-calibrated probability and an uncalibrated score with the same ordering have identical ROC curves.
The corner: how close is close enough
The closer a curve hugs the top-left corner, the better the model separates the classes. Two curves can both be "near the corner" but differ where it matters.
- A curve that rises steeply and then flattens reaches high TPR at very low FPR. This suits tasks where false alarms are expensive.
- A curve that rises slowly at first and then climbs near the right edge only reaches high TPR by accepting many false positives.
Look at where the curve is steep, not only at how much area lies under it. If your application can only tolerate an FPR of 0.01, the part of the curve at FPR greater than 0.01 is irrelevant to you.
Steps, ties, and diagonal segments
Real ROC curves built from finite data are staircases. Each time the threshold passes a single example, the curve moves up by 1/P if that example is positive, or right by 1/N if it is negative. P and N are the numbers of positives and negatives.
When a positive and a negative share the same score, the threshold cannot separate them. The curve then moves up and right in a single step, which draws a diagonal segment. A model that gives every example the same score has only one such step, from (0, 0) to (1, 1). Models with coarse outputs, such as shallow decision trees or a rounded probability, produce curves with few points and long diagonal segments.
Here is a first look at the numbers for the four-example set.
import numpy as np
from sklearn.metrics import roc_curve
y_true = np.array([0, 0, 1, 1])
y_score = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = roc_curve(y_true, y_score)
for f, t, th in zip(fpr, tpr, thresholds):
print(f"threshold={th:.2f} FPR={f:.2f} TPR={t:.2f}")The points are (0, 0), (0, 0.5), (0.5, 0.5), (0.5, 1) and (1, 1). In recent scikit-learn versions the first threshold is printed as inf. It stands for "flag nothing", which is why the curve starts at (0, 0). Older versions used the maximum score plus one.
Read the staircase from the bottom. The highest score, 0.8, is a positive, so the curve goes up. The next score, 0.4, is a negative, so it goes right. Then 0.35 is a positive and the curve goes up again. The last score, 0.1, is a negative and finishes the path to (1, 1). The single negative that outscores a positive (0.4 above 0.35) causes the one horizontal step in the middle, and that lowers AUC to 0.75.
Trade-offs along the curve
Moving along the curve to the right buys more TPR and costs more FPR. The slope of the curve at a point tells you the price. A steep section gives a lot of extra TPR for a small increase in FPR. A flat section gives little TPR for the same extra FPR. For a smooth curve, the slope at a threshold is the ratio of how dense the positive scores are there to how dense the negative scores are, which is the likelihood ratio. A well-behaved model has a curve that is steep on the left and flattens toward the right.
Note that the slope is the marginal trade-off, not the whole story. To judge a point, convert the rates to counts, because FPR is a fraction of negatives and the negatives may be far more numerous than the positives.
Different applications want different points on the same curve.
- A screening test for a serious condition tolerates false positives and wants high TPR.
- An automatic account-blocking system wants very low FPR, because each false positive hurts a real user.
- If errors cost about the same, a point far above the diagonal is a reasonable default. A later chapter covers choosing a threshold with a stated criterion.
Common reading errors
- Treating the curve as showing the threshold. Two models can reach the same point at very different thresholds, so never compare thresholds across models by position.
- Treating AUC near 0.5 as a failed pipeline when the test set has only a few dozen examples. The sampling noise is large.
- Treating a curve below the diagonal as a bad model rather than first checking the score direction.
- Drawing a smooth line through a staircase from few examples. The smoothness suggests precision the data does not have.
Exercise
A model has a test-set AUC of 0.18. Name the most likely cause, give a quick way to confirm it, and say what AUC you would expect after the fix.
Show answer
The model ranks in the wrong direction, most often from a sign error, labels swapped, or using the wrong column of predictproba. Confirm by computing rocauc_score(y, -score). It should equal 1 - 0.18 = 0.82. After fixing the bug at its source, expect about 0.82.
Exercise
On a test set with 1,000 positives and 9,000 negatives, a threshold gives the point (FPR 0.10, TPR 0.60). How many true positives and false positives does it produce, and what is its precision? Why can this look good on a ROC plot but poor in practice?
Show answer
True positives = 0.60 × 1,000 = 600. False positives = 0.10 × 9,000 = 900. Precision = 600 / (600 + 900) = 0.40. The point sits well above the diagonal, but because negatives are nine times as common as positives, a 10% FPR creates more false alarms than true detections. The ROC plot does not show this because FPR and TPR each ignore the class ratio.