Unit 5, lesson 31

Precision, Recall, and Classification Metrics

What accuracy, precision, recall and the F1 score measure at one threshold, and how the ROC curve, ROC AUC, the precision-recall curve and average precision compare score rankings across thresholds.

  • Video 31
  • Decisions, Metrics and Calibration

A classifier for delayed deliveries reports an accuracy of 9090 percent. Whether that is good depends on how common delays are, which errors the classifier makes and which threshold produced its decisions. If only 22 deliveries in 1010 are delayed, a rule that never predicts a delay already reaches 8080 percent while catching nothing. This lesson defines the metrics that look at the positive class directly at one threshold, and the curves and summaries that compare score rankings across all thresholds.

Key terms

Accuracy

Definition For a binary decision rule evaluated on n≥1n \ge 1 examples, with counts TP, FP, TN and FN from its confusion matrix, the accuracy is

accuracy=TP+TNn,\text{accuracy} = \frac{\text{TP} + \text{TN}}{n},

the fraction of decisions that are correct. Let π=n1/n\pi = n_1/n be the sample prevalence, the share of actual positives among the nn examples. When both classes are present, the accuracy equals π\pi times the sensitivity plus 1−π1 - \pi times the specificity, so it is a weighted average of the two class rates with weights set by the class shares. A rule that always predicts the more common class has accuracy equal to the larger of π\pi and 1−π1 - \pi.

In plain words Accuracy counts every correct decision equally, so the larger class dominates it. When positives are rare, a rule can reach high accuracy by ignoring them, and two rules of very different use can have the same accuracy. It is a fair first number only next to the prevalence and the accuracy of the majority rule.

Example In a constructed evaluation set of ten deliveries, deliveries 11 and 55 were delayed and the other eight were on time. Delayed is the positive class, so the sample prevalence is 2/10=0.22/10 = 0.2. The rule that always predicts on time is right for the eight on-time deliveries, an accuracy of 0.80.8, and it catches neither delay. Two models score the same deliveries, with a higher score meaning stronger evidence of delay. The scores are constructed rankings, not checked probabilities.

deliverylabelmodel Amodel B
11110.950.950.860.86
22000.850.850.960.96
33000.750.750.660.66
44000.650.650.560.56
55110.550.550.760.76
66000.450.450.460.46
77000.350.350.360.36
88000.250.250.260.26
99000.150.150.160.16
1010000.050.050.060.06

At the threshold 0.70.7, with a score equal to the threshold counted as positive, model A selects deliveries 11, 22 and 33. That gives 11 true positive, 22 false positives, 11 false negative and 66 true negatives, so its accuracy is 7/107/10.

Precision and recall

Definition For a binary decision rule with counts TP, FP and FN,

precision=TPTP+FP,recall=TPTP+FN.\begin{aligned} \text{precision} &= \frac{\text{TP}}{\text{TP} + \text{FP}}, \\[0.4em] \text{recall} &= \frac{\text{TP}}{\text{TP} + \text{FN}}. \end{aligned}

Precision is defined only when the rule makes at least one positive decision, and recall only when there is at least one actual positive. Recall is the same quantity as sensitivity. When the evaluation examples are a random sample from the population of interest, precision estimates P(Y=1∣Y^=1)P(Y = 1 \mid \hat{Y} = 1), also called the positive predictive value, and recall estimates P(Y^=1∣Y=1)P(\hat{Y} = 1 \mid Y = 1). Suppose a rule has sensitivity ss and false positive rate ff in a population where the positive class has prevalence π\pi. Its precision there is

sπsπ+f(1−π),\frac{s\pi}{s\pi + f(1 - \pi)},

provided the denominator is positive. So precision changes with the prevalence even when the rule and its two rates stay the same.

In plain words Precision looks along the alerts and asks how many of them are real. Recall looks along the actual positives and asks how many were caught. The two have different denominators, and a rule can do well on one and badly on the other. A rule that raises no alerts has no precision at all, neither 00 nor 11, and any number reported in that case is a software convention rather than an observed ratio. Because precision mixes the behavior of the rule with how common positives are, a precision measured where positives are common does not carry over to a setting where they are rare.

Example Model A at the threshold 0.70.7 raises three alerts and one of them is a real delay, so its precision is 1/31/3. It catches one of the two delays, so its recall is 1/21/2. Model B at the same threshold selects deliveries 11, 22 and 55, with precision 2/32/3 and recall 11. For a rule with sensitivity 0.90.9 and false positive rate 0.050.05, the precision is 0.45/0.475≈0.9470.45/0.475 \approx 0.947 at prevalence 0.50.5 but only 0.045/0.0925≈0.4860.045/0.0925 \approx 0.486 at prevalence 0.050.05.

F1 score

Definition The F1 score of a binary decision rule is the harmonic mean of its precision and recall,

F1=2 prec⋅recprec+rec=2 TP2 TP+FP+FN.\begin{aligned} F_1 &= \frac{2\,\text{prec} \cdot \text{rec}}{\text{prec} + \text{rec}} \\[0.4em] &= \frac{2\,\text{TP}}{2\,\text{TP} + \text{FP} + \text{FN}}. \end{aligned}

The two forms agree whenever TP≥1\text{TP} \ge 1. The count form is defined whenever 2 TP+FP+FN≥12\,\text{TP} + \text{FP} + \text{FN} \ge 1, and it gives 00 when there are no true positives but some errors, even though the first form is then undefined. When there are no actual positives and no alerts, the F1 score is undefined. Like any harmonic mean of two positive numbers, it lies between them and is never larger than their ordinary average.

In plain words The F1 score is high only when precision and recall are both high, because the harmonic mean is pulled toward the smaller of the two. It ignores true negatives and weighs a false alarm and a miss equally. It is a convenient single number for comparing rules on the positive class, not a substitute for the costs of an application, and it is not automatically the right target whenever positives are rare.

Example Model A at the threshold 0.70.7 has

F1=2⋅13⋅1213+12=2⋅12+2+1=25.\begin{aligned} F_1 &= \frac{2 \cdot \tfrac13 \cdot \tfrac12}{\tfrac13 + \tfrac12} \\[0.4em] &= \frac{2 \cdot 1}{2 + 2 + 1} = \frac{2}{5}. \end{aligned}

Model B at the same threshold has F1=4/(4+1+0)=4/5F_1 = 4/(4 + 1 + 0) = 4/5. The always on time rule has no precision, recall 00 and, by the count form, F1=0/(0+0+2)=0F_1 = 0/(0 + 0 + 2) = 0.

ROC curve

Definition Let a score s(x)s(\mathbf{x}) be evaluated on examples with n1≥1n_1 \ge 1 actual positives and n0≥1n_0 \ge 1 actual negatives, and for each threshold tt let the rule decide positive when s(x)≥ts(\mathbf{x}) \ge t. The true positive rate and false positive rate at tt are

TPR(t)=TP(t)/n1,FPR(t)=FP(t)/n0.\begin{aligned} \text{TPR}(t) &= \text{TP}(t)/n_1, \\[0.4em] \text{FPR}(t) &= \text{FP}(t)/n_0. \end{aligned}

The empirical ROC curve, short for receiver operating characteristic, plots the true positive rate against the false positive rate. It joins with straight segments the point (0,0)(0, 0), for a threshold above every score, and the points (FPR(t),TPR(t))\bigl(\text{FPR}(t), \text{TPR}(t)\bigr) for each distinct score tt taken in decreasing order. It ends at (1,1)(1, 1). Each positive admitted moves the curve up by 1/n11/n_1 and each negative moves it right by 1/n01/n_0. Examples with tied scores are admitted together, so a tie between classes appears as one diagonal segment.

In plain words Slide the threshold down from above the top score. Each time it passes a positive the curve steps up, and each time it passes a negative it steps right. A perfect ranking climbs straight to the top left corner (0,1)(0, 1) before moving right, and a score that gives every example the same value runs along the diagonal from (0,0)(0, 0) to (1,1)(1, 1). The curve shows every threshold at once, so it describes the ranking. On its own it does not say which threshold to use, since that depends on costs and prevalence.

Example Ranked by model A, the labels read positive, then three negatives, then a positive, then five negatives. The curve rises to (0,1/2)(0, 1/2), moves right in three steps of 1/81/8 to (3/8,1/2)(3/8, 1/2), rises to (3/8,1)(3/8, 1) and runs right to (1,1)(1, 1). Model B ranks one negative first and then both positives, so its curve moves right to (1/8,0)(1/8, 0), rises to (1/8,1)(1/8, 1) and runs right to (1,1)(1, 1).

ROC AUC

Definition The ROC AUC is the area under the empirical ROC curve. Let P\mathcal{P} index the n1≥1n_1 \ge 1 actual positives and N\mathcal{N} the n0≥1n_0 \ge 1 actual negatives, with scores sis_i. The area equals

AUC=1n1n0∑i∈P∑j∈Nhij,\text{AUC} = \frac{1}{n_1 n_0} \sum_{i \in \mathcal{P}} \sum_{j \in \mathcal{N}} h_{ij},

where

hij={1,si>sj,1/2,si=sj,0,si<sj.h_{ij} = \begin{cases} 1, & s_i > s_j, \\ 1/2, & s_i = s_j, \\ 0, & s_i < s_j. \end{cases}

So the AUC is the probability that the score ranks a randomly chosen positive above a randomly chosen negative, with ties counted as one half, when the two are drawn independently and uniformly from the actual positives and the actual negatives of the evaluation set. It is undefined when the evaluation set contains only one class. It equals 11 when every positive scores above every negative and 1/21/2 for a constant score, and a strictly increasing transformation of the scores leaves it unchanged.

In plain words The AUC compares the scores of one positive and one negative at a time and asks how often the positive comes out on top. It measures ranking only. An AUC of 0.8750.875 does not mean that 87.587.5 percent of the decisions are correct at some threshold, and it says nothing about whether the scores are calibrated probabilities or about the costs of errors. Each axis of the ROC curve is a rate within one class, so when positives are rare the AUC can look high while the precision at every threshold is low.

Example The first positive of model A outranks all eight negatives and its second outranks five, so its AUC is (8+5)/16=13/16=0.8125(8 + 5)/16 = 13/16 = 0.8125. Each positive of model B outranks seven negatives, which gives 14/16=0.87514/16 = 0.875. The areas under the two curves agree, 12⋅38+1⋅58=1316\tfrac12 \cdot \tfrac38 + 1 \cdot \tfrac58 = \tfrac{13}{16} for model A and 1⋅78=0.8751 \cdot \tfrac78 = 0.875 for model B.

Precision-recall curve and average precision

Definition For the same threshold rules, the precision-recall curve plots precision against recall at each distinct score used as a threshold. Thresholds that select nothing have no precision, and the point (0,1)(0, 1) that plotting software often adds at the left end is a drawing convention. Let t1>t2>⋯>tmt_1 > t_2 > \cdots > t_m be the distinct scores, let RkR_k and PkP_k be the recall and the precision of the rule with threshold tkt_k, and set R0=0R_0 = 0. With n1≥1n_1 \ge 1, the average precision is

AP=∑k=1m(Rk−Rk−1) Pk,\text{AP} = \sum_{k=1}^{m} (R_k - R_{k-1})\, P_k ,

the sum of each gain in recall times the precision at which it is reached. When no scores are tied, it is the mean, over the actual positives, of the precision of the rule whose threshold is that positive’s score. This sum is not the trapezoidal area under the precision-recall curve, which interpolates between points and can give a different value, so a report should state which summary it uses.

In plain words The precision-recall curve follows the alerts. Going down the ranking, it records what fraction of the cases selected so far are positive at each level of recall. Admitting a negative leaves the recall unchanged and never raises the precision, while admitting a positive raises the recall and never lowers the precision, so the curve has a sawtooth shape. Average precision rewards placing positives near the top. Its baseline is the prevalence rather than one half, since a constant score has average precision equal to the share of positives. ROC AUC and average precision answer different questions and can rank two models in opposite orders.

Example Model A reaches recall 1/21/2 with its top case, at precision 11, and recall 11 with its top five cases, at precision 2/52/5. Its average precision is 12⋅1+12⋅25=0.7\tfrac12 \cdot 1 + \tfrac12 \cdot \tfrac25 = 0.7. Model B reaches recall 1/21/2 at precision 1/21/2 and recall 11 at precision 2/32/3, so its average precision is 14+13=712≈0.583\tfrac14 + \tfrac13 = \tfrac{7}{12} \approx 0.583. Model B has the larger ROC AUC and model A the larger average precision. Model A puts a delay at the very top, while model B places both delays above more of the negatives. A constant score would give average precision 1/51/5 here, the prevalence.

Check yourself

  1. A rule is evaluated on 4040 cases, 66 of which have the event. It raises 1010 alerts, 44 of them for real events. Find the four counts, the accuracy, precision, recall, specificity and F1 score, and compare the accuracy with that of the rule that never raises an alert.
Show the answer

Of the 1010 alerts, 44 are true positives and 66 are false positives, so TP=4\text{TP} = 4 and FP=6\text{FP} = 6. The other 6−4=26 - 4 = 2 events are missed, so FN=2\text{FN} = 2, and the remaining 34−6=2834 - 6 = 28 negatives are true negatives. The accuracy is (4+28)/40=0.8(4 + 28)/40 = 0.8, the precision is 4/10=0.44/10 = 0.4, the recall is 4/6=2/34/6 = 2/3, the specificity is 28/34≈0.82428/34 \approx 0.824 and the F1 score is 8/(8+6+2)=0.58/(8 + 6 + 2) = 0.5.

The rule that never raises an alert is right on all 3434 negatives, an accuracy of 34/40=0.8534/40 = 0.85, higher than the rule’s 0.80.8. It has recall 00 and no precision. Accuracy prefers the useless rule here. A more useful report states the positive class, the prevalence 6/40=0.156/40 = 0.15 and the threshold, chosen beforehand on validation data, and gives the four counts with precision and recall, together with the cost of alerts and misses when it is known.

  1. Three positives have scores 0.90.9, 0.60.6 and 0.40.4, and four negatives have scores 0.70.7, 0.40.4, 0.20.2 and 0.10.1. Find the ROC AUC by comparing pairs, with ties counted as one half, and the average precision, with tied scores admitted together.
Show the answer

There are 3×4=123 \times 4 = 12 pairs. The positive at 0.90.9 outranks all four negatives. The positive at 0.60.6 outranks three and loses to 0.70.7. The positive at 0.40.4 loses to 0.70.7, ties with 0.40.4 and outranks two, for 2.52.5. The AUC is

4+3+2.512=1924≈0.792.\frac{4 + 3 + 2.5}{12} = \frac{19}{24} \approx 0.792 .

For the average precision, the threshold 0.90.9 gives recall 1/31/3 at precision 11. The threshold 0.70.7 admits a negative, so recall stays at 1/31/3. The threshold 0.60.6 gives recall 2/32/3 at precision 2/32/3. The threshold 0.40.4 admits a positive and a negative together, giving recall 11 at precision 3/53/5. So

AP=13(1+23+35)=3445≈0.756.\begin{aligned} \text{AP} &= \tfrac13 \bigl(1 + \tfrac23 + \tfrac35\bigr) \\[0.4em] &= \tfrac{34}{45} \approx 0.756 . \end{aligned}

Putting the tied positive ahead of the tied negative would give 10/1210/12 and 29/3629/36, credit for an ordering the score does not make.

  1. Model A’s scores are replaced by their squares after a software change. A colleague says that the ROC AUC and average precision are unchanged, so the rule that alerts when the score is at least 0.70.7 behaves as before. Which part of the claim is right?
Show the answer

Squaring is strictly increasing on nonnegative numbers, so the ranking and its ties are unchanged, and the AUC stays 13/1613/16 and the average precision 0.70.7. The decisions at a fixed threshold do change. The three highest squared scores are 0.90250.9025, 0.72250.7225 and 0.56250.5625, so the rule at 0.70.7 now alerts on deliveries 11 and 22 only. Its precision becomes 1/21/2 instead of 1/31/3, with recall still 1/21/2. To reproduce the old decisions, the threshold must move to 0.72=0.490.7^2 = 0.49. Ranking quality, performance at a chosen threshold and calibration are separate properties, and each needs its own check.

How this lesson connects