Skip to content
Kudos AI
Lire en français
Supervised Learning

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.

10 min readKudos AI

Prerequisites: Logistic Regression and Classification

Two Gaussian densities crossing at the Bayes boundary with the overlap shaded as the error floor, the boundary sliding as the priors change, and a KNN decision boundary going from jagged to flat as k rises past its best value.

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 P(Y=j∣X=x)P(Y = j \mid X = x) for every xx, 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 xx the chance of being wrong is 1−max⁡jP(Y=j∣X=x)1 - \max_j P(Y = j \mid X = x), and no rule makes that smaller at that point. Optimal everywhere means optimal on average.

What it leaves behind,

1−E[max⁡jP(Y=j∣X)],1 - E\left[\max_j P(Y = j \mid X)\right],

is the Bayes error rate - the classification counterpart of irreducible error, non-zero because the classes genuinely overlap.

Two Gaussian classes on the line, N(−1.25,1)N(-1.25, 1) and N(1.25,1)N(1.25, 1), equally likely. The densities are mirror images so the boundary is the midpoint x=0x = 0, and the error rate is Φ(−1.25)=0.105650\Phi(-1.25) = 0.105650. 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.

class 0class 1x*
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, π1=0.7\pi_1 = 0.7. The boundary solves π1f1(x)=π2f2(x)\pi_1 f_1(x) = \pi_2 f_2(x):

x=σ2log⁡(π1/π2)+(μ22−μ12)/2μ2−μ1=+0.338919.x = \frac{\sigma^2 \log(\pi_1/\pi_2) + (\mu_2^2 - \mu_1^2)/2}{\mu_2 - \mu_1} = +0.338919 .

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.

priorboundaryBayes errorerror keeping the boundary at 0
0.5 / 0.50.0000000.1056500.105650
0.7 / 0.3+0.3389190.0935650.105650
0.9 / 0.1+0.8788900.0504960.105650

Ignoring the prior costs 0.0120840.012084 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 P(Y∣X)P(Y \mid X). k-nearest-neighbours estimates it as directly as possible - take the kk 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 0.0927080.092708, fitted on 200 training points and scored on 20,000:

kk135915254575125199
test error0.13820.11550.10300.09760.09720.09770.09770.10360.12310.5049

The ends are the lesson. At k=1k = 1 the training error is exactly zero while the test error, 0.1382000.138200, is the worst in the table apart from the degenerate k=199k = 199 - the clearest demonstration available that training error estimates nothing. At k=199k = 199 out of 200, almost the whole sample votes on every prediction, the classifier stops depending on xx 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 0.0971500.097150 at k=15k = 15 - 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 P(Y∣X)P(Y \mid X). Discriminant analysis is generative - it models P(X∣Y=k)P(X \mid Y = k) as Gaussian for each class, along with the priors, and inverts with Bayes' theorem.

Assume every class shares one covariance Σ\Sigma. Taking logs and dropping what does not depend on kk leaves log⁡πk−12(x−μk)⊤Σ−1(x−μk)\log \pi_k - \frac{1}{2}(x - \mu_k)^\top \Sigma^{-1}(x - \mu_k). Expand it: the term x⊤Σ−1xx^\top \Sigma^{-1} x never mentions kk, so it cancels when classes are compared, and what survives,

δk(x)=x⊤Σ−1μk−12μk⊤Σ−1μk+log⁡πk,\delta_k(x) = x^\top \Sigma^{-1} \mu_k - \tfrac{1}{2}\mu_k^\top \Sigma^{-1}\mu_k + \log \pi_k ,

is linear in xx. The linearity is a consequence of the shared covariance, not an independent modelling choice. Give each class its own Σk\Sigma_k and the quadratic term stays: the boundary curves. That is QDA, at KK times the covariance parameters.

E. When the shared assumption fails, and when it saves you

Class A at the origin with correlation +0.75+0.75; class B at (1.5,1.5)(1.5, 1.5) with correlation −0.75-0.75. No single covariance describes both, so the optimal boundary genuinely curves, and integrating the mixture gives a floor of 0.0927080.092708. On 200 training points, 200,000 test points:

test error
LDA0.118955
QDA0.093480
Bayes floor0.092708

The pooled covariance explains it: averaging +0.75+0.75 against −0.75-0.75 gives an off-diagonal of −0.046809-0.046809, near zero, describing neither class. QDA estimates +0.685607+0.685607 and −0.722339-0.722339 separately and lands 0.00080.0008 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 0.2442110.244211, averaged over 400 runs:

training pointsLDAQDAgap
200.2844790.312687+0.028208
500.2598540.268450+0.008595
1000.2520590.255246+0.003187
5000.2456300.246275+0.000645
20000.2447350.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 n=20n = 20. 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 0.0934800.093480 decomposes as:

predicted Bpredicted A
actually B96,2883,899
actually A14,79785,016

Sensitivity 0.9610830.961083, specificity 0.8517530.851753. It is markedly better at one job than the other.

Before blaming the fit, compute what the Bayes rule does class by class: sensitivity 0.9528450.952845, specificity 0.8617380.861738. 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:

thresholderror ratesensitivityspecificity
0.50.0934800.9610830.851753
0.30.1019050.9833510.812519
0.20.1104800.9905980.788064
0.10.1264150.9959280.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 UU statistic on the raw scores agree here to floating-point precision - the equivalence is an identity, not an approximation. Across all thresholds, QDA scores 0.9652690.965269 against LDA's 0.9334880.933488.

Because only the ordering matters, AUC is blind to calibration: a model that ranks perfectly while reporting every probability between 0.980.98 and 0.990.99 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.

FPRTPR

Confusion matrix on 200,000 test points

predicted Bpredicted A
actually B95,2774,723
actually A13,81886,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 0.0120840.012084 at a 0.7/0.3 split and doubled the error at 0.9/0.1.
  • In k-nearest-neighbours, kk is the flexibility dial: zero training error at k=1k = 1, the class prior at k≈nk \approx n, 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.

Related reading

7 min readSupervised Learning

Logistic Regression and Classification

Why a straight line cannot model a probability, how the logistic function fixes it, and what the coefficients mean in log-odds, with a gradient-ascent step and a converged fit computed and checked numerically.

StatisticsMachine LearningOptimization
6 min readUnsupervised Learning

The Direction That Changes When You Change Units

Twelve people, two measurements, and three different first principal components: in millimetres the answer is almost pure height, in metres almost pure weight, and in centimetres an even blend - with the correlation fixed at 0.9500 throughout. What that says about what PCA maximises, why a proportion of variance explained of 99.999% can be a statement about metres rather than about people, and what standardising actually chooses.

Machine LearningStatistics
4 min readTime Series

A Score That Loses to Doing Nothing

A five-nearest-neighbour model scores 0.9983 under random five-fold cross-validation on a random walk, a series whose increments are by construction unpredictable. Evaluated forward in time it scores 0.6559 with an RMSE 12.44 times larger, and loses to carrying the last observed value forward. The split, not the model, produced the first number.

StatisticsMachine Learning
← Back to all articles