Exercises — ROC curve and AUC
Before we start, one shared picture of the two axes every ROC problem lives on.
Figure s01 — Anatomy of the ROC plot. A unit square: the horizontal axis is FPR (false-positive rate, 0 to 1), the vertical axis is TPR (true-positive rate, 0 to 1). A dashed white diagonal marks the random classifier (AUC = 0.5); a blue curve bulging toward the top-left marks a good classifier. Yellow dots mark the two fixed corners: where the threshold is 1 (nothing called positive) and where the threshold is 0 (everything called positive).

Recall The two rates in one breath
TPR (up the page) ::: Of all the truly-positive examples, the fraction we correctly flagged: . FPR (across the page) ::: Of all the truly-negative examples, the fraction we wrongly flagged: . The bottom-left corner means ::: threshold so high nothing is called positive. The top-right corner means ::: threshold so low everything is called positive.
Level 1 — Recognition
L1.1
A classifier's ROC curve lies exactly on the diagonal from to . What is its AUC, and what does this say about the model?
Recall Solution
The area under a straight diagonal of a unit square is a triangle: . So . This is a random classifier — it ranks a positive above a negative exactly half the time, no better than a coin flip.
L1.2
For a threshold , a model gives , , , . Compute TPR and FPR.
Recall Solution
This is a single point on the ROC curve — one dot for one threshold.
L1.3
Rank these AUC values from best to worst discrimination: .
Recall Solution
Intended answer (raw AUC, as reported): . Higher AUC = better as-is; sits below the random baseline , so as reported it is the worst.
A subtlety worth knowing: an AUC below 0.5 is worse than random but flippable — a model at becomes if you invert every prediction. So its usable discrimination is , which would rank third:
- Raw ranking (the intended answer): .
- Usable ranking (after allowing a flip): . Unless the question says "you may flip," give the raw ranking: .
Level 2 — Application
L2.1
Six examples, scores and labels below. Compute , , and the ROC point at threshold (predict positive if ).
| Ex | Score | Label |
|---|---|---|
| A | 0.90 | 1 |
| B | 0.70 | 0 |
| C | 0.60 | 1 |
| D | 0.50 | 1 |
| E | 0.40 | 0 |
| F | 0.20 | 0 |
Recall Solution
(A, C, D), (B, E, F). At , predicted-positive = scores = {A (0.90), B (0.70), C (0.60)}. Among those: A→label 1 (TP), C→label 1 (TP), B→label 0 (FP). So , . Point on the curve: .
L2.2
Using the Mann–Whitney U formula, compute the AUC for the six examples of L2.1. Here is the score of example and its true label, and is the indicator function (defined in the Notation box above — if the bracketed condition holds, else ):
Recall Solution
The outer sum runs over positives (label ), the inner over negatives (label ). Each term counts if the positive out-scores the negative, for a tie, otherwise. Positives: A(0.90), C(0.60), D(0.50). Negatives: B(0.70), E(0.40), F(0.20). Count, for each positive, how many negatives it out-scores (no ties here):
- A = 0.90: beats B(0.70), E(0.40), F(0.20) → 3
- C = 0.60: beats E(0.40), F(0.20), loses to B(0.70) → 2
- D = 0.50: beats E(0.40), F(0.20), loses to B(0.70) → 2
Total correctly-ordered pairs out of .
L2.3
A model always outputs the same score for every example (positives and negatives alike). What is its AUC? Use the tie term in the Mann–Whitney formula.
Recall Solution
Every positive–negative pair is a tie, so each contributes . A constant model has no ranking power → exactly the random baseline. This confirms why we assign to ties: it keeps a no-information model honestly at .
Level 3 — Analysis
L3.1
Take Worked Example 1 from the parent note (8 examples, AUC = 0.6875). Two of the ROC points are and . Explain geometrically why the curve moves straight up (FPR constant, TPR rising) between these two points, and which example causes it.
Figure s02 — Staircase steps. The 9-vertex ROC path of the parent's Worked Example 1, drawn in blue with dotted markers over the dashed random diagonal. A thick green segment highlights the up-step from to — caused by crossing a positive (example D), so TPR rises by while FPR is unchanged. A thick red segment highlights a right-step from to — caused by crossing a negative (example E), so FPR rises by while TPR is unchanged.

Recall Solution
Sorted scores: A(0.9,1), B(0.8,1), C(0.7,0), D(0.6,1), E(0.55,0), F(0.5,1), G(0.3,0), H(0.1,0). Lowering the threshold past D (score 0.6, label 1) turns one more true positive on. A true positive raises (so TPR climbs) but leaves untouched (so FPR stays put). Hence a vertical move up: . The rule: crossing a positive example → step up (height ); crossing a negative → step right (width ). Every ROC curve is a staircase of up-steps and right-steps.
L3.2
Same dataset. What is the maximum achievable TPR at FPR = 0 (i.e. before any false positive)? Which examples give it?
Recall Solution
Read the sorted list from the top until the first negative appears. The top two, A and B, are both positives; the third, C, is the first negative. So we catch 2 of 4 positives with zero false positives → TPR at FPR . Geometrically this is the highest point the curve reaches while still hugging the left edge .
L3.3
Model P and Model Q have the same AUC = 0.80 but their ROC curves cross. Model P bulges toward the top-left at low FPR; Model Q bulges near high TPR at high FPR. You need a low-false-alarm operating point. Which model do you pick, and why is AUC alone not enough?
Recall Solution
Pick Model P. AUC is one number summarising the whole curve; two curves with equal area can trade strength in different regions. At low FPR (the region you care about), P sits higher — more true positives caught per false alarm. Q's area is "earned" out at high FPR, useless to you. Lesson: when the operating region is fixed, compare the curves locally (partial AUC or the TPR at your target FPR), not the global AUC.
Level 4 — Synthesis
L4.1
A fraud dataset has frauds and legit transactions. At one threshold, , . (a) Compute TPR and FPR. (b) Compute Precision . (c) Explain why ROC "looks fine" but precision reveals the problem.
Recall Solution
(a) . . (b) . (c) On the ROC axes this point looks excellent — high catch rate, tiny FPR. But an FPR of on 9900 negatives is false alarms, dwarfing the true frauds. Precision : 6 out of 7 flagged transactions are innocent. ROC hides this because FPR divides by the huge negative count. This is precisely why the parent note's Mistake 2 recommends Precision-Recall Curve on imbalanced data.
L4.2
Combine ranking and area. Ten examples produce the ROC staircase with vertices Compute the AUC by the trapezoid sum .
Recall Solution
Only segments where FPR increases contribute area (vertical segments have zero width):
- : width → contributes .
- : .
- : .
- : .
L4.3
You have two thresholds giving points and . A downstream system needs Specificity . Recall Specificity . Which of the two thresholds is admissible, and what is its Sensitivity?
Recall Solution
Specificity .
- Point : Specificity ✓. Sensitivity (=TPR) .
- Point : Specificity ✗. Only the first threshold satisfies the constraint; it delivers Sensitivity 0.70 (see Sensitivity and Specificity). You trade some recall to meet the specificity floor.
Level 5 — Mastery
L5.1 (Degenerate curve)
A dataset has positives but zero negatives (). What happens to FPR, and can you draw an ROC curve?
Recall Solution
requires dividing by → undefined. With no negatives there is no x-axis to sweep; the ROC curve cannot be defined. AUC is likewise undefined. This is a data problem, not a model problem: ROC/AUC require both classes present. (Same collapse happens to TPR if .)
L5.2 (Perfect separation)
Positives all score above and negatives all score below . Sketch the ROC path and give the AUC. Explain via the Mann–Whitney interpretation.
Figure s03 — Perfect separation. The ROC path drawn in green, hugging the left edge then the top edge, with the enclosed unit square lightly shaded green (area = 1) over the dashed random diagonal. A yellow arrow marks that all positives are caught before any false positive appears, giving AUC = 1.

Recall Solution
As you lower the threshold from : you first sweep past all positives (curve climbs straight up the left edge to ) before touching a single negative, because the lowest positive still beats every negative. Then, continuing down, you sweep the negatives, moving right along the top edge to . Path: . This traces the top-left corner → area of the entire unit square . Mann–Whitney check: every positive out-scores every negative, so all pairs are correctly ordered → . Perfect separation ⇔ AUC .
L5.3 (Flipped model)
A model has AUC . (a) What single operation makes it useful? (b) What is the resulting AUC? (c) Why does this operation reflect the ROC curve about a specific line?
Recall Solution
(a) Negate the scores (replace each by , or equivalently swap the two predicted labels): every ranking reverses. (b) Reversing the order of every positive–negative pair turns each correctly-ordered pair into an incorrectly-ordered one and vice-versa, so the fraction correct becomes minus the old fraction: . (c) On the ROC plot, negating scores maps each point to — a 180° rotation about the centre (equivalently, reflection through that centre point). A curve that bulged below the diagonal now bulges above it. That symmetry is exactly why AUC and pair up.
L5.4 (Threshold count)
With examples all having distinct scores, how many distinct ROC vertices (including and ) can the curve have at most, and why?
Recall Solution
Sweeping the threshold below each distinct score produces one new vertex, plus the starting corner . With distinct scores you get crossings, giving vertices total (from to ). For (parent Worked Example 1): vertices — matching the 9-point path listed there. Each vertex is either an up-step (positive crossed) or a right-step (negative crossed).
Recall Self-test checklist (reveal after finishing)
Diagonal AUC ::: , a random classifier. AUC below 0.5 fix ::: negate scores → new AUC . Up-step vs right-step ::: crossing a positive steps up ; crossing a negative steps right . ROC undefined when ::: a class is empty ( or ). Ties in Mann–Whitney ::: count as each. Why ROC misleads on imbalance ::: FPR divides by the huge negative count, hiding large false-alarm counts.