Comparing Classifiers, and What Accuracy Hides
The Bayes classifier nothing can beat and the error floor it leaves behind, k-nearest-neighbours as a nonparametric imitation with k as the flexibility dial, discriminant analysis and why a shared covariance forces a straight line, and the confusion matrix, thresholds and ROC curve that a single accuracy figure conceals - every number computed on simulated data where the optimum is known.
Prerequisites: Logistic Regression and Classification
Comparing classifiers on real data has an awkward property: you never know how much room was left. A method scoring 12% error might be nearly optimal or might be leaving half the available accuracy on the table, and nothing in the data tells you which. This article works on simulated data instead, where the best possible classifier can be computed exactly - which turns vague comparisons into measurements.
A. The classifier that cannot be beaten
If you knew for every , the best you could do is assign each point to the class with the largest one. That is the Bayes classifier, and it is optimal for a reason almost too short to be a proof: at each the chance of being wrong is , and no rule makes that smaller at that point. Optimal everywhere means optimal on average.
What it leaves behind,
is the Bayes error rate - the classification counterpart of irreducible error, non-zero because the classes genuinely overlap.
Two Gaussian classes on the line, and , equally likely. The densities are mirror images so the boundary is the midpoint , and the error rate is . Integrating the mixture over four million grid points agrees to six decimals.
Interactive: the boundary nothing can beat
The shaded overlap is the error floor.
- Bayes error (the floor)
- 0.105650
- Boundary x*
- 0.000000
- Cost of using the midpoint
- 0.000000
With equal priors the boundary sits at the midpoint and the floor is the overlap of the two curves. Nothing can get under it: the classes genuinely occupy the same ground. Now drag the prior. Widen σ and the floor rises, because the floor is overlap and nothing else.
B. Priors move the boundary - which way?
Make class 1 more common, . The boundary solves :
It moves toward the rarer class, enlarging the common class's region. The opposite is a tempting guess - surely the abundant class needs less room? - and it is wrong: a point near zero is now more likely to be class 1 precisely because there is more class 1 to draw from.
| prior | boundary | Bayes error | error keeping the boundary at 0 |
|---|---|---|---|
| 0.5 / 0.5 | 0.000000 | 0.105650 | 0.105650 |
| 0.7 / 0.3 | +0.338919 | 0.093565 | 0.105650 |
| 0.9 / 0.1 | +0.878890 | 0.050496 | 0.105650 |
Ignoring the prior costs at 0.7 and more than doubles the error at 0.9. This is worth checking numerically rather than trusting the algebra: the derived boundary was confirmed optimal against a grid of 240,001 thresholds, which is exactly the check that catches a sign error - and did, on the first attempt at this article.
C. Imitating it by counting
Real data lacks . k-nearest-neighbours estimates it as directly as possible - take the closest training points and use their proportions - then applies the Bayes rule to that estimate. Nothing is fitted in advance; the training set is the model.
On a two-dimensional problem with Bayes error , fitted on 200 training points and scored on 20,000:
| 1 | 3 | 5 | 9 | 15 | 25 | 45 | 75 | 125 | 199 | |
|---|---|---|---|---|---|---|---|---|---|---|
| test error | 0.1382 | 0.1155 | 0.1030 | 0.0976 | 0.0972 | 0.0977 | 0.0977 | 0.1036 | 0.1231 | 0.5049 |
The ends are the lesson. At the training error is exactly zero while the test error, , is the worst in the table apart from the degenerate - the clearest demonstration available that training error estimates nothing. At out of 200, almost the whole sample votes on every prediction, the classifier stops depending on at all, and its error is essentially the class prior.
Between them is the U the bias-variance decomposition predicts, with a best value of at - within half a point of the floor, from a method that assumed nothing about the boundary's shape.
D. Modelling the classes instead of the boundary
Logistic regression is discriminative: it goes straight for . Discriminant analysis is generative - it models as Gaussian for each class, along with the priors, and inverts with Bayes' theorem.
Assume every class shares one covariance . Taking logs and dropping what does not depend on leaves . Expand it: the term never mentions , so it cancels when classes are compared, and what survives,
is linear in . The linearity is a consequence of the shared covariance, not an independent modelling choice. Give each class its own and the quadratic term stays: the boundary curves. That is QDA, at times the covariance parameters.
E. When the shared assumption fails, and when it saves you
Class A at the origin with correlation ; class B at with correlation . No single covariance describes both, so the optimal boundary genuinely curves, and integrating the mixture gives a floor of . On 200 training points, 200,000 test points:
| test error | |
|---|---|
| LDA | 0.118955 |
| QDA | 0.093480 |
| Bayes floor | 0.092708 |
The pooled covariance explains it: averaging against gives an off-diagonal of , near zero, describing neither class. QDA estimates and separately and lands above the floor.
Note the diagnosis. What identifies LDA's problem as bias is the gap to the floor, not the gap to QDA - without the benchmark you would know only that one method beat another, which is compatible with both being bad.
Now reverse it. A second problem whose true boundary really is linear, floor , averaged over 400 runs:
| training points | LDA | QDA | gap |
|---|---|---|---|
| 20 | 0.284479 | 0.312687 | +0.028208 |
| 50 | 0.259854 | 0.268450 | +0.008595 |
| 100 | 0.252059 | 0.255246 | +0.003187 |
| 500 | 0.245630 | 0.246275 | +0.000645 |
| 2000 | 0.244735 | 0.244889 | +0.000154 |
QDA is not wrong here - a quadratic model contains the linear one, so its bias is nil and both converge to the floor. It simply cannot afford its parameters at . The gap closes monotonically, which is the bias-variance trade-off appearing as a model-selection rule rather than a diagram.
Two of those three numbers are properties of the population rather than of a fit, and the figure below computes them: the Bayes floor at 0.092708, and the error of the line LDA converges to with unlimited data, 0.114143. The gap between them is LDA's bias, and no amount of data removes it - the 0.118955 measured above is that bias plus the estimation error of a 200-point fit. That line is not even the best straight one: the error-minimising line, parallel to LDA's but shifted towards B, makes 0.106416, because LDA assumes a covariance the classes do not share. Slide the correlation to zero and watch the curve settle onto the line and the bias go to nothing, which is the premise of the reversal below.
Interactive: what a straight boundary costs
Both error rates integrated from the population, not fitted to a sample.
- Bayes floor
- 0.092708
- LDA’s limiting line
- 0.114143
- Cost of LDA’s line
- 0.021435
- Pooled off-diagonal
- 0.000000
At a correlation of 0.75 in one class and -0.75 in the other, no single covariance describes both, so the optimal boundary genuinely curves. The floor is 0.092708 and the line LDA settles on with unlimited data manages 0.114143: a bias of 0.021435 that no amount of data removes. The lesson measures 0.118955 for LDA fitted on 200 points, which is this bias plus the estimation error of that fit. Note the pooled off-diagonal: averaging the two correlations gives exactly zero, so the pooled matrix claims the features are uncorrelated, which is true of neither class.
F. What one error rate conceals
Every figure above is a single number, and single numbers hide the shape of the errors. The QDA classifier's decomposes as:
| predicted B | predicted A | |
|---|---|---|
| actually B | 96,288 | 3,899 |
| actually A | 14,797 | 85,016 |
Sensitivity , specificity . It is markedly better at one job than the other.
Before blaming the fit, compute what the Bayes rule does class by class: sensitivity , specificity . The optimal classifier is asymmetric too, by almost the same margin. The two classes are equally spread, but A's long axis points at B (variance 1.75 along the line joining the centres) while B is narrow in that direction (0.25) - A's tail reaches into B's region, and the optimal rule gives away more of A in exchange. The imbalance belongs to the problem, not the model, and the way to tell is to compute the benchmark rather than reason about it.
G. The threshold is a decision, not a default
The threshold is not part of the model. Moving it changes the decision rule while every fitted parameter stays put:
| threshold | error rate | sensitivity | specificity |
|---|---|---|---|
| 0.5 | 0.093480 | 0.961083 | 0.851753 |
| 0.3 | 0.101905 | 0.983351 | 0.812519 |
| 0.2 | 0.110480 | 0.990598 | 0.788064 |
| 0.1 | 0.126415 | 0.995928 | 0.750784 |
The error rate says 0.5 is best, but the error rate weights a false positive and a false negative equally, and that weighting is an assumption. Where a missed positive costs ten times a false alarm, minimising the error rate is minimising the wrong quantity.
Sweeping the threshold and recording each (false positive rate, true positive rate) pair traces the ROC curve, whose area has an exact reading: the probability that a random positive outscores a random negative. Computing that area by the trapezoid rule and computing the Mann-Whitney statistic on the raw scores agree here to floating-point precision - the equivalence is an identity, not an approximation. Across all thresholds, QDA scores against LDA's .
Because only the ordering matters, AUC is blind to calibration: a model that ranks perfectly while reporting every probability between and has an AUC of 1.
The figure below is this problem's optimal rule, integrated rather than fitted, so its default row is the benchmark quoted above: 9.27% error, sensitivity 0.953, specificity 0.862. Move the threshold and nothing is refitted; only the line between yes and no moves, and the four cells rebalance. Then make class B one case in a hundred. Sensitivity and specificity do not budge, because neither one can see how rare the class is, while accuracy climbs toward specificity and precision falls through the floor.
Interactive: one model, every threshold
Nothing is refitted. Only the line between yes and no moves.
Confusion matrix on 200,000 test points
| predicted B | predicted A | |
|---|---|---|
| actually B | 95,277 | 4,723 |
| actually A | 13,818 | 86,182 |
- Sensitivity
- 95.3%
- Specificity
- 86.2%
- Error rate
- 9.3%
- AUC
- 0.9657
- Accuracy
- 90.7%
- Precision
- 87.3%
- Majority baseline
- 50.0%
how common class B is
The default threshold, and the error rate of 9.27% is the best this problem allows: no rule of any kind beats it, because these are the true posteriors. Note that it is still asymmetric, 95.3% against 86.2%. The optimal rule gives away nine points more of A than of B, so the lopsided confusion matrix the lesson starts from is a fact about the problem and not a defect of the fit. Move the threshold and watch which cell pays.
Key takeaways
- The Bayes classifier is optimal and needs the answer to compute, which makes simulated data the only place a method's remaining headroom is visible.
- Priors move the boundary toward the rarer class; ignoring them cost at a 0.7/0.3 split and doubled the error at 0.9/0.1.
- In k-nearest-neighbours, is the flexibility dial: zero training error at , the class prior at , and the best value in between.
- A shared covariance is what makes a discriminant boundary linear. Whether to share is decided by sample size as much as by truth.
- Diagnose bias against the Bayes floor, not against a competing method.
- Report the confusion matrix before the accuracy, choose the threshold from costs, and read AUC as ranking rather than calibration.
What's next
Every method here drew one boundary from one fitted model. Ensembles take a different route - fit many weak models and combine them - and the reason that works is a variance argument rather than a bias one.
References & further reading
- Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani, An Introduction to Statistical Learning, with Applications in R, Springer (Springer Texts in Statistics 103), 2013source ↗
- Stuart Russell, Peter Norvig, Artificial Intelligence: A Modern Approach, Pearson (3rd edition), 2010· Kudos AI reference library
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.