S14 · Lesson 20 · Evaluating models
Held-out data and k-fold cross-validation, the confusion matrix with precision, recall and F1, the base-rate effect on a positive result, ROC curves and the area under them, and how precisely a test set measures accuracy
A fault detector that never raises an alarm is 99% accurate on a line where 1% of boards are faulty, and completely useless. Judging a model needs the right numbers, measured on data it never trained on. This lesson covers the bookkeeping that every practitioner uses: cross-validation to estimate performance honestly, the confusion matrix and its ratios for imbalanced problems, the ROC curve for comparing detectors across all thresholds, and the error bars a finite test set puts on any of them.
- A detector's score is Gaussian with unit variance, centered at 0 for good boards and at d for faulty ones. A board is flagged when its score exceeds the threshold t.
- The true positive rate (recall) is and the false positive rate . Sweeping t trades one for the other; the area under the ROC curve, , summarizes the whole trade.
- Watch the precision at a 1% fault rate: even a good detector raises mostly false alarms unless t is high.
Predict
A test catches 90% of faulty boards and passes 98% of good ones, and 0.5% of boards are faulty. What's its precision?
Out of 10,000 boards, 50 are faulty and 45 of them fail; 9,950 are good and 2% of them, 199, also fail. Of the 244 alarms, 45 are real: a precision of 18.4%, though the test's recall is 90% and its specificity 98%. Lesson 2's base-rate effect sets the precision of every detector of rare events.
Name it
Held-out data
A model's error on its own training data is optimistic. Estimate performance on data it never saw: a held-out test set, or k-fold cross-validation, which splits the data into k parts, trains k times on k − 1 of them and tests on the remaining one, and averages. Each fit uses a fraction (k − 1)/k of the data. Model choices (λ, basis size) are made by cross-validation, and the test set is used once at the end.
The confusion matrix
With true and false positives and negatives (TP, FP, TN, FN):
- precision TP/(TP + FP): of the alarms, how many are real;
- recall (sensitivity, true positive rate) TP/(TP + FN): of the real cases, how many are caught;
- specificity TN/(TN + FP), and the false positive rate 1 − specificity;
- F1, the harmonic mean of precision and recall, .
By Bayes' theorem, precision at prevalence π is .
ROC curves
Plotting the true positive rate against the false positive rate as the threshold sweeps gives the ROC curve. The area under it, AUC, is the probability that a random positive scores higher than a random negative: 0.5 for guessing, 1 for perfect. For Gaussian scores of equal variance separated by d standard deviations, AUC = .
Error bars
An accuracy p measured on n independent test examples has standard error : to tell 90% from 91% reliably takes thousands of examples.
Worked example
A detector on 1000 boards gives TP = 40, FP = 10, FN = 20, TN = 930. Find accuracy, precision, recall, F1 and specificity.
Show the solution
- Accuracy. against 0.94 for a detector that never fires.
- Precision and recall.
- F1.
- Specificity.
Answer accuracy 0.97, precision 0.80, recall 0.667, F1 0.727 and specificity 0.989.
Worked example
What AUC do score separations of d = 1 and d = 2 give? And what's the standard error of a 90% accuracy measured on 500 examples?
Show the solution
- AUC. For d = 1 and d = 2:
- Standard error. so the true accuracy is roughly 90 ± 2.7% at two standard errors.
Answer the AUCs are 0.760 for d = 1 and 0.921 for d = 2, and the standard error is 0.0134, so the accuracy is roughly 90 ± 2.7%.
Worked example
Two classifiers are tested on the same 1000 examples. A is wrong and B right on 30; A right and B wrong on 15; the rest they agree on. Is B significantly better?
Show the solution
- Use the disagreements. Only the disagreements carry information. If the two were equally good, each of the 45 disagreements would be a coin flip.
- McNemar's statistic.
- Compare the threshold. That's above the 3.84 that a chi-square variable with one degree of freedom exceeds 5% of the time, so B's advantage of 1.5 points is significant at the 5% level.
- Why keep the pairing. Comparing the two accuracies as if from separate test sets would waste the pairing and could call it noise.
Answer yes: McNemar's statistic is 4.36, above 3.84, so B is significantly better at the 5% level.
Check
A classifier has 30 true positives, 3 false positives and 40 false negatives. What's its precision?
Check
With 30 true positives, 3 false positives and 40 false negatives, what's the F1 score?
Check
A test has sensitivity 0.9 and specificity 0.9, and the condition has prevalence 0.1. What fraction of positive results are true?
Check
Positive and negative scores are Gaussian with equal variance and means 1.5 standard deviations apart. What's the AUC?
Check
A classifier scores 0.9 accuracy on 100 independent test examples. What's the standard error of that estimate?
Check
5-fold cross-validation runs on 1000 examples. How many examples does each fit train on?
Check
A classifier's accuracy is about 0.95, and the test set must measure it with a standard error of 0.5 percentage points.
(a)
How many independent test examples does that take?
(b)
How wide is the 95% confidence interval's half-width then, in percentage points?
Check
A confusion matrix has T true positives, F false positives, N false negatives and M true negatives.
Step 1
What is the accuracy?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the specificity?
Step 3
What is the balanced accuracy (the mean of recall and specificity)?
Explain why
Why can accuracy be a misleading measure for a fault detector, and what should be reported instead?
When faults are rare, a detector that labels everything as good achieves an accuracy equal to the fraction of good units, which can be 99% or higher while catching nothing. Accuracy is dominated by the large negative class, so it barely changes when the detector misses real faults. Precision and recall measure what matters on the rare class: how many alarms are real and how many faults are caught. An ROC curve or its area shows the whole trade-off between detection and false alarms across thresholds, independently of the base rate, and the chosen operating point should then be judged by the precision it gives at the real prevalence.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.