S1420 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 Q(t−d) and the false positive rate Q(t). Sweeping t trades one for the other; the area under the ROC curve, Φ(d/2), 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?

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, 2PR/(P+R)=2TP/(2TP+FP+FN).

By Bayes' theorem, precision at prevalence π is sens⋅πsens⋅π+(1−spec)(1−π).

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 = Φ(d/2).

Error bars

An accuracy p measured on n independent test examples has standard error p(1−p)/n: 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
  1. Accuracy. accuracy=(40+930)/1000=0.97 against 0.94 for a detector that never fires.
  2. Precision and recall. precision=40/50=0.80recall=40/60=0.667
  3. F1. F1=2×0.8×0.6671.467=0.727
  4. Specificity. specificity=930/940=0.989

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
  1. AUC. For d = 1 and d = 2: AUC1=Φ(1/2)=0.760AUC2=Φ(2/2)=0.921
  2. Standard error. SE=0.9×0.1/500=0.0134 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
  1. Use the disagreements. Only the disagreements carry information. If the two were equally good, each of the 45 disagreements would be a coin flip.
  2. McNemar's statistic. χ2=(|30−15|−1)245=4.36
  3. 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.
  4. 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°.

Explain why

Why can accuracy be a misleading measure for a fault detector, and what should be reported instead?

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.