Skip to content

Learning from dataLesson 3 of 6

Average your neighbors

Local averaging and the curse of dimensionality

We want to estimate the mean output at input xx. If several observations have exactly that input, we can average their outputs:

f^(x)=1DxiDxyi,Dx={i:xi=x}.\hat f(x)=\frac{1}{|D_x|}\sum_{i\in D_x}y_i,\qquad D_x=\{i:x_i=x\}.

D_x is the set of rows whose input equals x. For an ideal continuously distributed input, a fixed target x has probability zero of matching any of a finite number of sampled rows. Rounded measurements can repeat, but exact matches may still be too scarce. An empty set has no sample average. So widen the target: take rows within a distance r of x.

f^(x)=1Dr(x)iDr(x)yi,Dr(x)={i:xixr}.\hat f(x)=\frac{1}{|D_r(x)|}\sum_{i\in D_r(x)}y_i,\qquad D_r(x)=\{i:\lVert x_i-x\rVert\le r\}.

‖x_i − x‖ is the distance between two input lists; for a single number it is just |x_i − x|. r is the window’s radius. Average the outputs of nearby examples, provided the neighborhood contains at least one example.

This assumes the conditional mean changes slowly enough nearby that neighbors have similar means. Otherwise their outputs tell you little about your target. With several features, also scale them sensibly: a distance dominated by dollars may ignore a meaningful change measured in another unit.

Dots within r of x₀ light up; their average is the estimate there. Trace it everywhere and compare to the rule.

The radius affects the estimate in two ways:

windowneighborsthe estimate
too smalla few, or nonethe average of a couple of noisy y’s — jumps around from one dataset to the next
too bigmanyincludes dots whose x is far away, where the rule is different — the bends get blurred

These effects lead to the distinction between bias and variance.

For this calculation, suppose the N dots are uniformly distributed in a unit-radius ball and the radius-r neighborhood is centered at the ball’s center, with 0 ≤ r ≤ 1. The fraction that land in a window is the window’s share of the space:

dimensionthe spacethe windowwindow’s share
p = 1a segment of length 2a segment of length 2rr
p = 2a disc of area π·1²a disc of area π·r²
p = 3a ball of volume k₃·1³a ball of volume k₃·r³
any pvolume k_p·1^pvolume k_p·r^pr^p

k_p is whatever constant makes the volume formula work in p dimensions; it cancels. Each extra dimension multiplies the share by r again. Expected neighbors: N · r^p. A thousand dots and a window of radius 1/10:

pshare r^pexpected neighbors
11/10100
21/10010
31/1,0001
51/100,0000.01
101/10,000,000,0000.0000001

Ten inputs, and this small centered window is almost always empty. Turn it around: to have 10 neighbors on average you need r=(10/N)1/pr=(10/N)^{1/p} — 0.01 in one dimension, 0.1 in two, 0.22 in three, 0.40 in five, 0.63 in ten. By p = 10 the radius reaches about 63% of the full radius just to capture 1% of the volume. The neighborhood reaches far away even though it contains little probability mass. That is the curse of dimensionality: high-dimensional spaces can require enormous datasets to provide enough genuinely local examples. The volume calculation depends on the sampling distribution and the neighborhood location; near a boundary or in nonuniform data it changes.

Count how many of 1,000 sampled points fall within radius r of the center as the dimension changes.
How much of a cube fits inside its inscribed ball?

The course’s practice set uses a cube of side 2 with a ball of radius 1 inside it, touching every face. The ball’s share of the cube is πp/2/(2pΓ(p/2+1))\pi^{p/2}/\bigl(2^p\Gamma(p/2+1)\bigr) — Γ is the factorial extended to fractions, so Γ(p/2 + 1) is “(p/2)!”:

p12345681012
ball ÷ cube10.7850.5240.3080.1640.0810.0160.00250.0003

Fill the cube with dots and by p = 10 only one in 400 sits inside the ball: most of the cube’s volume lies outside the inscribed ball. And two random dots in the cube are typically √(2p/3) apart — each coordinate’s gap has average square 2/3, and squares add across coordinates — so distances grow like √p. This is the root-mean-square distance, and distances concentrate around this scale in high dimensions. Lower-dimensional structure in real data can make the effective problem much smaller.

Local averaging can need too much data when inputs fill many dimensions. Another approach is to fit a model with shared parameters.

Definition

Read the full glossary entry →