Contents
03Your neighbors drift away
3.1The edge of a neighborhood
Local methods such as k-nearest neighbors predict from the training points near the query. Suppose we want a neighborhood that holds 10\% of the data. In one dimension, with uniform data on [0,1], an interval of length 0.1 will do. In d dimensions, a sub-cube with edge e has volume e^d, so holding a fraction r of uniform data needs e^d=r:
For r=0.1 the edge is 0.32 in two dimensions, 0.46 in three, 0.79 in ten and 0.98 in a hundred (Hastie et al., 2009, §2.5). In ten dimensions the “neighborhood” must span 79\% of the range of every feature; ESL rounds this to 0.80. Even 1\% of the data needs an edge of 0.63 in ten dimensions (Figure 2b). Such neighborhoods are not local any more: the prediction averages points that differ a lot from the query, which is bias in the language of Episode 2 (Murphy, 2022, §16.1.2).
3.2How far is the nearest neighbor?
A second view looks at the closest point itself. Place N points uniformly in the unit ball of \mathbb{R}^d and stand at the center. A single point lands within radius \rho with probability \rho^d, the volume ratio of the two balls, so all N points miss that ball with probability (1-\rho^d)^N. Setting this to \tfrac12 gives the median distance to the nearest point (Hastie et al., 2009, eq. 2.24):
With N=500 it is 0.0014 for d=1, 0.037 for d=2, 0.11 for d=3, 0.52 for d=10 and 0.94 for d=100. In ten dimensions the nearest of 500 points is typically more than halfway to the boundary of the ball. Since (1/2)^{1/N}\approx1-\ln2/N, the median is about (\ln2/N)^{1/d}: halving it takes 2^d times more data.
Pause and think. A 5-nearest-neighbor model is trained on 10{,}000 points with 20 features, roughly uniform. What fraction of the data does a neighborhood hold, and what edge length does a box holding it need?
Show the answer
5 of 10{,}000 is r=0.0005, and e_{20}(0.0005)=0.0005^{1/20}=0.68: the five “nearest” neighbors spread over about two thirds of the range of every feature. The model averages far-away points, so its predictions are biased toward the global average.