A classifier for delayed deliveries reports an accuracy of percent. Whether that is good depends on how common delays are, which errors the classifier makes and which threshold produced its decisions. If only deliveries in are delayed, a rule that never predicts a delay already reaches 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 examples, with counts TP, FP, TN and FN from its confusion matrix, the accuracy is
the fraction of decisions that are correct. Let be the sample prevalence, the share of actual positives among the examples. When both classes are present, the accuracy equals times the sensitivity plus 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 and .
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 and were delayed and the other eight were on time. Delayed is the positive class, so the sample prevalence is . The rule that always predicts on time is right for the eight on-time deliveries, an accuracy of , 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.
| delivery | label | model A | model B |
|---|---|---|---|
At the threshold , with a score equal to the threshold counted as positive, model A selects deliveries , and . That gives true positive, false positives, false negative and true negatives, so its accuracy is .
Precision and recall
Definition For a binary decision rule with counts TP, FP and FN,
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 , also called the positive predictive value, and recall estimates . Suppose a rule has sensitivity and false positive rate in a population where the positive class has prevalence . Its precision there is
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 nor , 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 raises three alerts and one of them is a real delay, so its precision is . It catches one of the two delays, so its recall is . Model B at the same threshold selects deliveries , and , with precision and recall . For a rule with sensitivity and false positive rate , the precision is at prevalence but only at prevalence .
F1 score
Definition The F1 score of a binary decision rule is the harmonic mean of its precision and recall,
The two forms agree whenever . The count form is defined whenever , and it gives 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 has
Model B at the same threshold has . The always on time rule has no precision, recall and, by the count form, .
ROC curve
Definition Let a score be evaluated on examples with actual positives and actual negatives, and for each threshold let the rule decide positive when . The true positive rate and false positive rate at are
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 , for a threshold above every score, and the points for each distinct score taken in decreasing order. It ends at . Each positive admitted moves the curve up by and each negative moves it right by . 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 before moving right, and a score that gives every example the same value runs along the diagonal from to . 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 , moves right in three steps of to , rises to and runs right to . Model B ranks one negative first and then both positives, so its curve moves right to , rises to and runs right to .
ROC AUC
Definition The ROC AUC is the area under the empirical ROC curve. Let index the actual positives and the actual negatives, with scores . The area equals
where
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 when every positive scores above every negative and 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 does not mean that 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 . Each positive of model B outranks seven negatives, which gives . The areas under the two curves agree, for model A and 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 that plotting software often adds at the left end is a drawing convention. Let be the distinct scores, let and be the recall and the precision of the rule with threshold , and set . With , the average precision is
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 with its top case, at precision , and recall with its top five cases, at precision . Its average precision is . Model B reaches recall at precision and recall at precision , so its average precision is . 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 here, the prevalence.
Check yourself
- A rule is evaluated on cases, of which have the event. It raises alerts, 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 alerts, are true positives and are false positives, so and . The other events are missed, so , and the remaining negatives are true negatives. The accuracy is , the precision is , the recall is , the specificity is and the F1 score is .
The rule that never raises an alert is right on all negatives, an accuracy of , higher than the rule’s . It has recall and no precision. Accuracy prefers the useless rule here. A more useful report states the positive class, the prevalence 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.
- Three positives have scores , and , and four negatives have scores , , and . 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 pairs. The positive at outranks all four negatives. The positive at outranks three and loses to . The positive at loses to , ties with and outranks two, for . The AUC is
For the average precision, the threshold gives recall at precision . The threshold admits a negative, so recall stays at . The threshold gives recall at precision . The threshold admits a positive and a negative together, giving recall at precision . So
Putting the tied positive ahead of the tied negative would give and , credit for an ordering the score does not make.
- 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 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 and the average precision . The decisions at a fixed threshold do change. The three highest squared scores are , and , so the rule at now alerts on deliveries and only. Its precision becomes instead of , with recall still . To reproduce the old decisions, the threshold must move to . Ranking quality, performance at a chosen threshold and calibration are separate properties, and each needs its own check.