2.6.8 · D4Model Evaluation & Selection

Exercises — Precision-recall tradeoff and curves

3,119 words14 min readBack to topic

This is a self-testing worksheet for the parent topic. Every problem has a fully worked solution hidden inside a collapsible callout — try first, then reveal. Exercises climb from L1 Recognition (do you know the words?) up to L5 Mastery (can you invent and defend a decision?).

Before we start, one shared reminder of the four counts we will use everywhere. Picture sorting every prediction into one of four boxes — look at the figure below as you read the definitions:

Figure — Precision-recall tradeoff and curves

Level 1 — Recognition

Goal: read the counts and plug into the definitions. No threshold sweeping yet.

Exercise 1.1 (L1)

A spam filter processes 200 emails. It flags 50 as spam. Of those 50, 40 really are spam. There were 60 spam emails in total. Compute precision and recall.

Recall Solution 1.1

"Flagged as spam" = predicted positive = 50 emails. Of those, 40 are correct, so Total actual spam = 60, we caught 40, so we missed Reading it back: when the filter shouts "spam" it is right of the time (precision), and it catches two-thirds of all real spam (recall).

Exercise 1.2 (L1)

For the same filter, which metric would you watch if your biggest fear is a real invoice landing in the spam folder (a false positive that hurts the user)?

Recall Solution 1.2

A real email wrongly sent to spam is a false positive (). The metric whose denominator is punished by is So you watch precision. Higher precision = fewer false alarms = fewer lost invoices.


Level 2 — Application

Goal: pick a threshold, build the confusion counts yourself, compute both metrics.

Exercise 2.1 (L2)

A model scores 8 patients (higher score = more likely diseased). Sorted:

Patient Score True label
A 0.92 Sick
B 0.81 Sick
C 0.70 Healthy
D 0.60 Sick
E 0.44 Healthy
F 0.35 Sick
G 0.22 Healthy
H 0.10 Healthy

Predict "sick" when score . Compute precision and recall. Total sick = 4.

Recall Solution 2.1

Predicted positive (score ): A, B, C, D.

  • A sick ✅, B sick ✅, C healthy ❌, D sick ✅.
  • So (A,B,D), (C).
  • Missed sick people: F (0.35) is below , so .

Exercise 2.2 (L2)

Same table. Now lower the threshold to . Recompute precision and recall, and state in one sentence which way each moved and why.

Recall Solution 2.2

Predicted positive (score ): A, B, C, D, E, F.

  • Sick among them: A, B, D, F → .
  • Healthy among them: C, E → .
  • Sick people missed: none left (). What moved: recall rose (we now catch every sick patient), precision fell (we let in one extra healthy person, E). Lowering traded precision for recall — the tradeoff in action. See 3.2.04-Decision-thresholds-and-calibration.

Level 3 — Analysis

Goal: reason about whole curves, baselines, and why ROC and PR disagree.

Exercise 3.1 (L3)

A dataset has 1000 samples: 20 positive, 980 negative. A model flags 100 as positive; of those, . Compute (a) precision, (b) recall, (c) FPR, and (d) explain why the ROC picture looks far rosier than the PR picture.

Recall Solution 3.1

Predicted positive = 100, correct = 16, so . Actual positives = 20, caught 16, so . True negatives .

  • (a)
  • (b)
  • (c)
  • (d) ROC plots recall () against FPR () — that looks like a great point (top-left-ish). But FPR divides by the huge , so 84 false alarms barely register. PR plots precision (), which ignores entirely and sees those 84 false alarms clearly. On imbalanced data the PR view is honest, the ROC view is flattering. Compare with 2.6.07-ROC-curves-and-AUC and 2.6.12-Class-imbalance-handling.

Exercise 3.2 (L3)

A fraud dataset is 3% fraud. Your model gets AUC-PR . Is that good? Compute the no-skill baseline and the lift over it.

Recall Solution 3.2

The PR no-skill baseline equals the positive-class fraction (a coin-flip classifier's expected precision is just the prevalence of positives): So AUC-PR is 8× better than random — genuinely good here, even though sounds low in isolation. Never compare AUC-PR to ; compare it to prevalence.

Exercise 3.3 (L3)

Given three PR operating points sorted by increasing recall — — estimate AUC-PR by the trapezoidal rule.

Recall Solution 3.3

Why the trapezoid rule here? We only know precision at a handful of thresholds — the true curve between them is unmeasured. We need a way to guess the area under those unknown gaps. The simplest honest guess is to connect each pair of known points with a straight line and measure the area of the resulting slab. That slab is a trapezoid (two vertical sides of heights , a horizontal base of width ), and a trapezoid's area is (average of the two heights) × width. This assumes precision varies roughly linearly between adjacent points — a reasonable, unbiased middle-ground compared to assuming it stays flat (over/under-estimates). So: First segment (): Second segment (): The purple shaded region in the figure below is these two trapezoids stacked side by side.

Look at the shaded area — each vertical dotted line marks a trapezoid boundary, and the mint dashed line shows the no-skill floor we compared against in Exercise 3.2:

Figure — Precision-recall tradeoff and curves

Level 4 — Synthesis

Goal: combine metrics, choose thresholds under a stated goal, connect to F-score.

Exercise 4.1 (L4)

Using Exercise 2.1's model, you must catch at least 90% of sick patients (recall ) while keeping precision as high as possible. Which threshold from do you pick? Show the recall/precision at your choice.

Recall Solution 4.1

Sweep thresholds on the 8-patient table (4 sick: A,B,D,F).

  • : positives {A}. . Recall . Fails recall.
  • : positives {A,B,C,D}. . Recall . Fails recall.
  • : positives {A,B,C,D,E}. Sick caught A,B,D → ; F(0.35) still out → . Recall . Fails recall.
  • : positives {A,B,C,D,E,F}. . Recall ✅, Precision .
  • : adds G(healthy). . Recall , Precision .

The only thresholds hitting recall are and . Between them, gives higher precision. Pick : recall , precision .

Exercise 4.2 (L4)

At the two recall-satisfying thresholds above ( and ), compute the F1 score and confirm it prefers the same threshold.

Recall Solution 4.2

is the harmonic mean of precision and recall — it punishes the lower of the two, so it favours balanced points. See 2.6.09-F1-score-and-Fbeta-score. At : : At : : is higher at (), agreeing with our precision-based choice.


Level 5 — Mastery

Goal: design and defend a decision where the "right" metric depends on real-world cost.

Exercise 5.1 (L5)

Two fraud models on the same 2%-fraud test set:

  • Model X: AUC-ROC , AUC-PR .
  • Model Y: AUC-ROC , AUC-PR .

Each missed fraud costs $1000 (a ); each false alarm costs $5 of analyst review (a ). Which model do you ship, and which curve did you trust — and why?

Recall Solution 5.1

Step 1 — which curve is trustworthy here? The set is positive: heavily imbalanced. ROC's FPR hides false positives behind a giant , so AUC-ROC over-praises both models. PR is the honest curve (see Exercise 3.1's logic). So compare AUC-PR: vs . Step 2 — baseline check. PR baseline . Model X lift ; Model Y lift . Both beat random, Y far more. Step 3 — cost lens. Missing fraud costs $1000, a false alarm only $5 — a 200:1 ratio. High precision (few false alarms) is cheap; high recall (few misses) is what saves money. Model Y's much larger AUC-PR means it sustains higher precision and recall across thresholds, so at any operating point matching X's recall it produces fewer false alarms, and at any precision it catches more fraud. Decision: ship Model Y, trusting AUC-PR over AUC-ROC because the class imbalance makes ROC optimistic. See 4.1.08-Cost-sensitive-learning.

Exercise 5.2 (L5)

For your chosen Model Y, suppose at some threshold: on transactions ( fraud) you get . Compute the total expected cost with the $1000/$5 figures above, and compare it to the "flag nothing" baseline.

Recall Solution 5.2

Missed fraud: . False alarms: . "Flag nothing" baseline: every fraud is missed, , : Model Y saves 200000 - 51500 = \148500$1500$) is trivial next to the fraud it prevents. This is why recall dominates the decision here.


Recall Self-check summary (reveal after finishing)

Metric divides by the "yes" you produced ::: Precision uses Metric divides by the "yes" that truly existed ::: Recall uses When precision is undefined ::: when (no positives predicted at all) When recall is undefined ::: when (no actual positives in the data) PR curve's random baseline ::: the fraction of positive samples (the prevalence) Why ROC flatters imbalanced data ::: FPR's denominator is dominated by the huge Why the trapezoid rule fits AUC-PR ::: it linearly connects known points and averages the two heights times the width Precision at recall = 1 ::: it drops to the prevalence, because nearly everything is predicted positive Constrained threshold choice ::: fix one metric as a floor, optimize the other among feasible thresholds Right metric under heavy imbalance and cost asymmetry ::: AUC-PR and expected cost, not AUC-ROC