๐Ÿงฎ Brain Teaser

Nearest Neighbor Is Never More Than Twice as Bad as Bayes

Field: ML / Statistics (nonparametric classification) Estimated difficulty: 2 / 5

Setup

Consider two-class classification. A feature vector XโˆˆRpX \in \mathbb{R}^p has a density, and the label is Yโˆˆ{0,1}Y \in \{0,1\}. Write ฮท(x)=P(Y=1โˆฃX=x)\eta(x) = P(Y=1 \mid X=x) for the posterior, and assume ฮท\eta is continuous.

  • The Bayes classifier predicts 11 exactly when ฮท(x)>1/2\eta(x) > 1/2. Its error rate is Rโˆ—=E[minโก(ฮท(X),โ€‰1โˆ’ฮท(X))].R^* = \mathbb{E}\big[\min(\eta(X),\, 1-\eta(X))\big].
  • The 1-nearest-neighbor (1-NN) classifier is trained on NN i.i.d. pairs (Xi,Yi)(X_i, Y_i). At a query point xx, it predicts the label of the training point closest to xx.

As Nโ†’โˆžN \to \infty, the nearest neighbor X(1)X_{(1)} of xx converges to xx. Take this as given.

Questions

(a) Show that the asymptotic 1-NN error rate is RNN=E[โ€‰2โ€‰ฮท(X)โ€‰(1โˆ’ฮท(X))โ€‰].R_{NN} = \mathbb{E}\big[\,2\,\eta(X)\,(1-\eta(X))\,\big].

(b) Prove the Coverโ€“Hart bounds Rโˆ—โ€…โ€Šโ‰คโ€…โ€ŠRNNโ€…โ€Šโ‰คโ€…โ€Š2Rโˆ—(1โˆ’Rโˆ—)โ€…โ€Šโ‰คโ€…โ€Š2Rโˆ—.R^* \;\le\; R_{NN} \;\le\; 2R^*(1-R^*) \;\le\; 2R^*.

(c) (Bonus) Now allow KK classes with posteriors pk(x)p_k(x). Show that RNNโ€…โ€Šโ‰คโ€…โ€ŠRโˆ—(2โˆ’KKโˆ’1โ€‰Rโˆ—).R_{NN} \;\le\; R^*\left(2 - \frac{K}{K-1}\,R^*\right).

Why this is interesting

The 1-NN rule has no model, no parameters, and no training. Yet with infinite data it is guaranteed to lose at most a factor of 2 against the best possible classifier, whatever the distribution. Informally: half of all the classification information in the world is contained in the label of your nearest neighbor. The proof is a short pointwise inequality followed by one application of Jensen's inequality.

Reference: Hastie, Tibshirani, Friedman, The Elements of Statistical Learning (2nd ed.), ยง13.3 (the 1-NN asymptotic error bound and the KK-class formula). Original result: T. Cover & P. Hart, "Nearest neighbor pattern classification," IEEE Trans. Inf. Theory, 1967.

nearest-neighborsBayes errorCover-HartJensen inequalitynonparametric classification

Answer: Nearest Neighbor Is Never More Than Twice as Bad as Bayes

Key Idea / Intuition

With infinite data, your nearest neighbor sits essentially on top of you. Its label is therefore an independent draw from the same coin ฮท(x)\eta(x) as your own label. So 1-NN errs exactly when two independent flips of the same biased coin disagree, which happens with probability 2ฮท(1โˆ’ฮท)2\eta(1-\eta).

Bayes, by contrast, errs with probability m=minโก(ฮท,1โˆ’ฮท)m = \min(\eta, 1-\eta). Since 2ฮท(1โˆ’ฮท)=2m(1โˆ’m)2\eta(1-\eta) = 2m(1-m) lies between mm and 2m2m, we get the pointwise bounds. The function mโ†ฆ2m(1โˆ’m)m \mapsto 2m(1-m) is concave, so Jensen's inequality averages the pointwise bound into the sharper global bound 2Rโˆ—(1โˆ’Rโˆ—)2R^*(1-R^*).


Formal Proof / Solution

(a) The asymptotic error

Fix a query point xx. Let YY be its true label and Yโ€ฒY' the label of its nearest neighbor X(1)X_{(1)}.

Given the feature values, the labels are drawn independently:

  • YโˆผBernoulli(ฮท(x))Y \sim \text{Bernoulli}(\eta(x)),
  • Yโ€ฒโˆผBernoulli(ฮท(X(1)))Y' \sim \text{Bernoulli}(\eta(X_{(1)})).

As Nโ†’โˆžN \to \infty, we have X(1)โ†’xX_{(1)} \to x. By continuity, ฮท(X(1))โ†’ฮท(x)\eta(X_{(1)}) \to \eta(x).

The classifier errs exactly when Yโ‰ Yโ€ฒY \neq Y'. So the conditional error converges to P(Yโ‰ Yโ€ฒ)=ฮท(x)โ€‰(1โˆ’ฮท(x))+(1โˆ’ฮท(x))โ€‰ฮท(x)=2ฮท(x)(1โˆ’ฮท(x)).P(Y \ne Y') = \eta(x)\,(1-\eta(x)) + (1-\eta(x))\,\eta(x) = 2\eta(x)(1-\eta(x)).

The conditional error is bounded by 11, so dominated convergence lets us take expectations over XX: RNN=E[2ฮท(X)(1โˆ’ฮท(X))].R_{NN} = \mathbb{E}\big[2\eta(X)(1-\eta(X))\big].

(b) The Coverโ€“Hart bounds

Let m(x)=minโก(ฮท(x),1โˆ’ฮท(x))โˆˆ[0,12]m(x) = \min(\eta(x), 1-\eta(x)) \in [0, \tfrac12]. By symmetry, 2ฮท(1โˆ’ฮท)=2m(1โˆ’m).2\eta(1-\eta) = 2m(1-m).

Lower bound. Since mโ‰ค12m \le \tfrac12, we have 1โˆ’mโ‰ฅ121-m \ge \tfrac12. Hence 2m(1โˆ’m)โ‰ฅm.2m(1-m) \ge m. Taking expectations gives RNNโ‰ฅE[m]=Rโˆ—R_{NN} \ge \mathbb{E}[m] = R^*.

This is also clear without calculation: no rule beats Bayes.

Upper bound. The function ฯ†(m)=2m(1โˆ’m)\varphi(m) = 2m(1-m) is concave. By Jensen's inequality, RNN=E[ฯ†(m(X))]โ‰คฯ†(E[m(X)])=2Rโˆ—(1โˆ’Rโˆ—).R_{NN} = \mathbb{E}[\varphi(m(X))] \le \varphi(\mathbb{E}[m(X)]) = 2R^*(1-R^*).

Finally, 2Rโˆ—(1โˆ’Rโˆ—)โ‰ค2Rโˆ—2R^*(1-R^*) \le 2R^* is trivial.

Remarks.

  • The pointwise bound alone, 2m(1โˆ’m)โ‰ค2m2m(1-m) \le 2m, already gives the factor 2.
  • Jensen sharpens it to 2Rโˆ—(1โˆ’Rโˆ—)2R^*(1-R^*). Equality holds when m(X)m(X) is almost surely constant, for example when noise is uniform across feature space.
  • When Rโˆ—=0R^* = 0 (separable classes), 1-NN is asymptotically perfect.
  • When Rโˆ—=12R^* = \tfrac12 (pure noise), both rules have error 12\tfrac12.

(c) KK classes

At a point xx, let the posteriors be p1,โ€ฆ,pKp_1, \dots, p_K. Let kโˆ—k^* be the index of the largest posterior, and set r=1โˆ’pkโˆ—r = 1 - p_{k^*}. Then rr is the Bayes error at xx.

The NN error at xx. By the same argument as in (a), the true label and the neighbor's label are independent draws from (pk)(p_k). Hence errNN(x)=1โˆ’โˆ‘kpk2.\text{err}_{NN}(x) = 1 - \sum_k p_k^2.

Bounding the sum of squares. The other Kโˆ’1K-1 probabilities sum to rr. By Cauchyโ€“Schwarz, โˆ‘kโ‰ kโˆ—pk2โ‰ฅr2Kโˆ’1.\sum_{k \ne k^*} p_k^2 \ge \frac{r^2}{K-1}. Therefore 1โˆ’โˆ‘kpk2โ€…โ€Šโ‰คโ€…โ€Š1โˆ’(1โˆ’r)2โˆ’r2Kโˆ’1โ€…โ€Š=โ€…โ€Š2rโˆ’KKโˆ’1โ€‰r2โ€…โ€Š=:โ€…โ€Šฯˆ(r).1 - \sum_k p_k^2 \;\le\; 1 - (1-r)^2 - \frac{r^2}{K-1} \;=\; 2r - \frac{K}{K-1}\,r^2 \;=:\; \psi(r).

Averaging. The function ฯˆ\psi is concave in rr, and E[r(X)]=Rโˆ—\mathbb{E}[r(X)] = R^*. By Jensen, RNNโ‰คE[ฯˆ(r(X))]โ‰คฯˆ(Rโˆ—)=Rโˆ—(2โˆ’KKโˆ’1Rโˆ—).R_{NN} \le \mathbb{E}[\psi(r(X))] \le \psi(R^*) = R^*\left(2 - \frac{K}{K-1}R^*\right).

For K=2K = 2 this reduces exactly to 2Rโˆ—(1โˆ’Rโˆ—)2R^*(1-R^*).

Moral. The whole proof rests on two observations. First, an asymptotic nearest neighbor is an independent copy of your own label. Second, the map from Bayes error to NN error is concave, so averaging over feature space can only help the bound.

Caveat. The result is asymptotic. In high dimensions, X(1)โ†’xX_{(1)} \to x happens extremely slowly (the curse of dimensionality), so finite-NN performance can be far worse than this bound suggests.

Source: Hastie, Tibshirani, Friedman โ€” The Elements of Statistical Learning (2nd ed.), ยง13.3; Cover & Hart (1967)

Type: ML/StatsSource: Hastie, Tibshirani, Friedman โ€” The Elements of Statistical Learning (2nd ed.), ยง13.3; Cover & Hart (1967)Edit on GitHub โ†—