Consistency of Nearest Neighbors
Cover and Hart’s 2× Bayes bound and Stone’s theorem.
Nearest neighbors was the first rule proved to learn every classification problem. Cover and Hart showed in 1967 that one neighbor comes within a factor of two of the Bayes error; Stone showed in 1977 that neighbors, with growing slowly, reach it for every distribution. This lesson proves both, shows that no rule can promise a rate on every problem, and maps the rates that hold under assumptions.
What you’ll learn
- Prove that the 1-NN error converges to for every distribution on , and derive the -class Cover–Hart bound.
- State Stone's theorem and prove it from the plug-in inequality, a variance bound and Stone's geometric lemma.
- Prove that no classification rule converges to the Bayes error at a universal rate.
- State the rates of k-NN under smoothness and margin conditions, and explain why the right notion of smoothness involves the marginal distribution.
- Describe when nearest neighbors are consistent in general metric spaces, and why some interpolating neighbor rules are consistent.
Setting, and the nearest neighbor converges
Let take values in with , and write . The Bayes rule has risk , the Bayes error. A sequence of rules , each trained on i.i.d. examples, is consistent for a distribution if , and universally consistent if this holds for every distribution of . Write for the -th nearest sample point to , with distance ties broken at random.
The nearest neighbor converges. If lies in the support of , every ball has positive mass , so . The support has -measure one, hence in probability, and almost surely since the distance is non-increasing in . The same argument shows that whenever : the number of sample points in is binomial with mean .
Write the left side as a sum over sample points, and use that are exchangeable to swap the roles of the query and the sample point :
Given , the counting fact says is among the nearest neighbors of at most of the points ; by symmetry each is equally likely to be one of them, so the conditional probability is at most :
Given the sample, the labels and are independent, so with the conditional error is . It differs from by , so
If were continuous, the right side would vanish by bounded convergence, since . In general, pick a continuous, compactly supported with (such functions are dense in ) and split:
So the limit superior is at most for every . The bounds follow pointwise from and Jensen's inequality, with , as in the Descent lesson.
Many classes. With classes and , the same argument gives , the probability that two independent labels drawn at disagree. Let be the Bayes conditional error. The smaller probabilities sum to , so by Cauchy–Schwarz their squares sum to at least , and
Taking expectations and using gives . The lower bound holds because .
A three-class problem has Bayes error . What is the Cover–Hart upper bound on the asymptotic 1-NN error?
Show the answer
0.34
. With two classes the same would give : more classes leave more room for the neighbor's label to disagree.
Fixed k is not enough; Stone's theorem
For fixed odd , the same reasoning gives the limit , where is the probability that a majority of independent votes disagrees with an independent label:
It decreases toward as grows, but for fixed it stays strictly above wherever or . Devroye (1981) showed that, for every distribution and odd , with of order .
| 1 | 3 | 5 | 7 | 15 | 25 | ||
|---|---|---|---|---|---|---|---|
| 0.180 | 0.122 | 0.107 | 0.102 | 0.100 | 0.100 | 0.100 | |
| 0.420 | 0.386 | 0.365 | 0.350 | 0.320 | 0.307 | 0.300 |
Plug-in inequality. k-NN is the plug-in rule with . Where and disagree, and lie on opposite sides of , so there:
Variance. Let . Given the inputs, the are independent Bernoulli variables with these means, so
Bias. For continuous, compactly supported , because when . For general , approximate by such a in and transfer the error to the neighbors with Stone's lemma:
Combine: , so . Both conditions were used: kills the variance, kills the bias. Nothing about the distribution was assumed, which is exactly why no rate came out.
Which choice of makes the k-NN rule universally consistent on ?
- for all
- , a vote of the whole sample
Show the answer
C.
and , so both conditions of Stone's theorem hold. A fixed leaves the variance of the vote; proportional to keeps the neighborhoods from shrinking, leaving bias.
No universal rate
Stone's theorem says k-NN eventually learns anything. It is natural to ask how fast. The answer, for every rule whatsoever, is: arbitrarily slowly.
Let take countably many values with probabilities , and let the label be a deterministic function, , so . A rule has no information about when is missing from the sample, which happens with probability .
Draw the labels as independent fair coins. On every unseen point, any rule is then wrong with probability , so averaged over ,
Atoms with survive the factor (for ), so the bound is at least an eighth of the total mass on atoms lighter than . Spreading enough mass over ever smaller atoms makes this exceed for every . Averaging shows that for each some labeling is bad; a more careful choice of a single that is bad for all simultaneously completes the proof.
The lesson is the classification version of the no-free-lunch principle (No Free Lunch & Inductive Bias): any guarantee on the speed of learning must restrict the distribution. Consistency is a property of a rule; a rate is a property of a rule and a class of problems.
Think first The proof used a discrete . Does a continuous input, say with a smooth density on , escape the theorem?
No. A density alone does not prevent from oscillating faster and faster at finer scales: replace each atom by a tiny interval and let the label flip between intervals. What breaks the construction is a bound on how fast can change, a smoothness assumption, which is exactly what the rates below assume.
Rates under smoothness and margin conditions
Assume (i) is Hölder with exponent : ; and (ii) has a density bounded away from zero on a regular compact support in . Then the -th neighbor lies within distance of order , so the bias term in Stone's proof is of order while the variance term is at most . Balancing them:
The plug-in inequality is pessimistic: errors in cost nothing where is far from . Tsybakov's margin condition with exponent ,
says that little mass sits near the decision boundary. Under (i), (ii) and this condition, plug-in rules achieve excess risk of order , and no classifier does better uniformly over these classes (Audibert & Tsybakov, 2007). When the exponent exceeds : faster than the of agnostic learning over a fixed class.
Chaudhuri and Dasgupta (2014) showed that k-NN attains these rates, and argued that the usual assumptions, smoothness measured by radius plus a density bounded below, miss how nearest neighbors adapt to the local scale of the data. They measure smoothness by probability mass instead: the average of over a ball differs from at its center by at most a power of the ball's -mass. That notion suits nearest neighbors, which adapt to where the data are, and it yields finite-sample bounds that also cover discrete distributions, as well as universal consistency in every metric measure space where a Lebesgue differentiation theorem holds.
| Assumptions | Excess risk of k-NN with the best | Source |
|---|---|---|
| none | , at no guaranteed rate | Stone (1977); DGL ch. 7 |
| -Hölder, density bounded below | plug-in inequality, as derived above | |
| plus margin exponent | , minimax | Audibert & Tsybakov (2007); Chaudhuri & Dasgupta (2014) |
| twice differentiable, regularity conditions | exact leading-order regret, optimal weights | Samworth (2012) |
Take Lipschitz () in dimensions. By what rate does the k-NN excess risk shrink without a margin condition, and with margin exponent ?
- without, with
- without, with
- in both cases
- in both cases: the margin condition only changes constants
Show the answer
B. without, with
, and with the margin condition . Little mass near the boundary means estimation errors in rarely flip a decision.
Beyond Euclidean space, and what is open
General metric spaces. Stone's lemma is a fact about cones in , and it fails in infinite dimensions. In a separable metric space, k-NN is consistent under a Besicovitch-type differentiation condition (Cérou & Guyader, 2006), which holds in finite dimension but not in every space of functions. Without such structure, k-NN and its variants are not universally consistent in every metric space. Hanneke, Kontorovich, Sabato and Weiss (2021) gave a sample-compression learner built on 1-NN that is universally strongly consistent in every essentially separable metric space, exactly the spaces in which any universally consistent learner exists.
Interpolation. The 1-NN rule fits its training set perfectly and is not consistent: its error tends to . Yet perfect fitting is not the culprit. Belkin, Hsu and Mitra (2018) showed that weighted nearest-neighbor schemes whose weights grow without bound as the distance goes to zero interpolate the training data and are nevertheless consistent, or nearly so, with optimal rates under standard assumptions: an early, transparent example of the phenomenon studied in Benign Overfitting.
Key takeaways
- For every distribution on , the 1-NN error tends to , between and ; with classes the upper bound is .
- Stone's theorem: k-NN with and is universally consistent. The proof combines the plug-in inequality, a variance bound and Stone's geometric lemma.
- No rule has a universal rate: for any sequence some distribution with keeps the error above . Rates need assumptions.
- Under Hölder- smoothness, a density bounded below and margin exponent , k-NN achieves the minimax rate ; measuring smoothness by probability mass removes the density assumption.
- In general metric spaces k-NN can fail to be consistent; a compression-based 1-NN learner is consistent wherever any learner can be, and suitably weighted interpolating neighbor rules can be consistent.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In the proof of Stone's theorem, what does each condition on control?
- controls the bias; controls the variance
- controls the variance of the vote; makes the neighborhoods shrink, controlling the bias
- Both control the computational cost
- They ensure the distribution has a density
Show the answer
B. controls the variance of the vote; makes the neighborhoods shrink, controlling the bias
The variance of the vote is at most , which vanishes as . The bias vanishes because the -th neighbor converges to the query when , so the averaged values approach .
Labels are independent of the inputs, with everywhere. What is the asymptotic error of the 1-NN rule?
- 0.30
- 0.21
- 0.42
- 0.60
Show the answer
C. 0.42
, while . Here is constant, so Jensen's inequality is an equality and the Cover–Hart upper bound is attained.
What does the slow-rates theorem imply for a practitioner who wants a guarantee like "error within 1% of the Bayes error after examples"?
- Such a guarantee holds for k-NN with , since it is universally consistent
- Such a guarantee is impossible for any rule, under any assumptions
- It requires a deterministic labeling, so
- It can hold only for a restricted class of distributions, for example under smoothness and margin conditions
Show the answer
D. It can hold only for a restricted class of distributions, for example under smoothness and margin conditions
For every rule there are distributions on which convergence is slower than any prescribed sequence, even with . Guarantees on speed must therefore assume something, such as Hölder smoothness of and a margin condition, and they hold only for distributions satisfying it.
Why can't the proof of Stone's theorem be copied verbatim to an infinite-dimensional space of functions?
- Its key geometric fact, that a point is a -nearest neighbor of at most others, relies on covering with finitely many cones, which fails in infinite dimensions
- Distances cannot be computed between functions
- The Bayes error is undefined in infinite dimensions
- Infinite-dimensional spaces have no probability measures
Show the answer
A. Its key geometric fact, that a point is a -nearest neighbor of at most others, relies on covering with finitely many cones, which fails in infinite dimensions
Stone's lemma needs the finite cone-covering number , which grows exponentially with and is infinite in infinite dimensions. Consistency then needs a substitute, such as a Besicovitch-type differentiation property, or a different learner altogether, as in Hanneke et al.'s compression-based rule.
Further reading
- T. M. Cover & P. E. Hart, "Nearest neighbor pattern classification," IEEE Transactions on Information Theory 13(1), 21–27 (1967).
- C. J. Stone, "Consistent nonparametric regression," Annals of Statistics 5(4), 595–620 (1977).
- L. Devroye, L. Györfi & G. Lugosi, A Probabilistic Theory of Pattern Recognition (Springer, 1996), chapters 5–7 and 11.
- K. Chaudhuri & S. Dasgupta, "Rates of convergence for nearest neighbor classification," Advances in Neural Information Processing Systems 27 (2014).
- S. Hanneke, A. Kontorovich, S. Sabato & R. Weiss, "Universal Bayes consistency in metric spaces," Annals of Statistics 49(4), 2129–2150 (2021).