๐Ÿงฎ Brain Teaser

Kernel Density Estimation: The Bandwidth Dilemma

You are estimating a probability density ff on R\mathbb{R} from nn i.i.d. samples X1,โ€ฆ,XnX_1, \ldots, X_n using a kernel density estimator (KDE):

f^h(x)=1nhโˆ‘i=1nKโ€‰โฃ(xโˆ’Xih)\hat{f}_h(x) = \frac{1}{nh} \sum_{i=1}^n K\!\left(\frac{x - X_i}{h}\right)

where KK is a symmetric kernel with โˆซK(u)โ€‰du=1\int K(u)\,du = 1, โˆซuK(u)โ€‰du=0\int u K(u)\,du = 0, and โˆซu2K(u)โ€‰du=ฯƒK2<โˆž\int u^2 K(u)\,du = \sigma_K^2 < \infty.

(a) Show that the pointwise bias of f^h(x)\hat{f}_h(x) is approximately

Bias[f^h(x)]โ‰ˆh2ฯƒK22fโ€ฒโ€ฒ(x)\text{Bias}[\hat{f}_h(x)] \approx \frac{h^2 \sigma_K^2}{2} f''(x)

for small hh.

(b) Without calculation, explain why the variance of f^h(x)\hat{f}_h(x) behaves as โˆผ1nh\sim \frac{1}{nh} for large nn.

(c) Hence, what is the optimal bandwidth hโˆ—h^* (in terms of nn) that minimizes the mean squared error (MSE), and what is the resulting rate at which the MSE decays?

The punchline: What fundamental statistical trade-off does this reveal, and why can't you simply take hโ†’0h \to 0?

kernel density estimationbias-variance tradeoffnonparametric statisticsbandwidth selectionMSE

Answer: KDE Bandwidth Bias-Variance Tradeoff

Key Idea / Intuition

KDE with bandwidth hh is essentially a local average of the data over a window of size hh. A large window forces the estimate to look like a flattened version of the truth (high bias, low variance), while a tiny window uses almost no data per point (low bias, high variance). The optimal hh balances these two forces, and the optimal rate reveals a fundamental limit on how fast you can learn a density nonparametrically.


Formal Proof / Solution

Part (a): Bias Computation

The expected value of the estimator at xx is:

E[f^h(x)]=1hโˆซKโ€‰โฃ(xโˆ’th)f(t)โ€‰dt\mathbb{E}[\hat{f}_h(x)] = \frac{1}{h} \int K\!\left(\frac{x-t}{h}\right) f(t)\, dt

Substitute u=(tโˆ’x)/hu = (t - x)/h, so t=x+hut = x + hu, dt=hโ€‰dudt = h\,du:

E[f^h(x)]=โˆซK(u)โ€‰f(x+hu)โ€‰du\mathbb{E}[\hat{f}_h(x)] = \int K(u)\, f(x + hu)\, du

Now expand f(x+hu)f(x + hu) via Taylor series around xx:

f(x+hu)=f(x)+hufโ€ฒ(x)+h2u22fโ€ฒโ€ฒ(x)+O(h3)f(x + hu) = f(x) + hu f'(x) + \frac{h^2 u^2}{2} f''(x) + O(h^3)

Integrate term by term against K(u)K(u):

E[f^h(x)]=f(x)โˆซK(u)โ€‰duโŸ=1+hfโ€ฒ(x)โˆซuK(u)โ€‰duโŸ=0+h22fโ€ฒโ€ฒ(x)โˆซu2K(u)โ€‰duโŸ=ฯƒK2+O(h3)\mathbb{E}[\hat{f}_h(x)] = f(x)\underbrace{\int K(u)\,du}_{=1} + h f'(x)\underbrace{\int u K(u)\,du}_{=0} + \frac{h^2}{2}f''(x)\underbrace{\int u^2 K(u)\,du}_{=\sigma_K^2} + O(h^3)

Therefore:

Bias[f^h(x)]=E[f^h(x)]โˆ’f(x)โ‰ˆh2ฯƒK22fโ€ฒโ€ฒ(x)\boxed{\text{Bias}[\hat{f}_h(x)] = \mathbb{E}[\hat{f}_h(x)] - f(x) \approx \frac{h^2 \sigma_K^2}{2} f''(x)}

The bias is O(h2)O(h^2): wider bandwidth โ‡’\Rightarrow more smoothing โ‡’\Rightarrow larger bias.


Part (b): Why Variance โˆผ1nh\sim \frac{1}{nh}?

The estimator f^h(x)\hat{f}_h(x) is an average of nn i.i.d. terms 1hKโ€‰โฃ(xโˆ’Xih)\frac{1}{h}K\!\left(\frac{x-X_i}{h}\right). By independence:

Var[f^h(x)]=1nVarโ€‰โฃ[1hKโ€‰โฃ(xโˆ’X1h)]\text{Var}[\hat{f}_h(x)] = \frac{1}{n}\text{Var}\!\left[\frac{1}{h}K\!\left(\frac{x - X_1}{h}\right)\right]

The term 1hKโ€‰โฃ(xโˆ’X1h)\frac{1}{h}K\!\left(\frac{x-X_1}{h}\right) has variance of order 1h2โ‹…h=1h\frac{1}{h^2} \cdot h = \frac{1}{h} (since the second moment of 1hK((xโˆ’t)/h)\frac{1}{h}K((x-t)/h) scales as 1h\frac{1}{h} by a change of variables). Thus:

Var[f^h(x)]โˆผf(x)โˆฅKโˆฅ22nh\text{Var}[\hat{f}_h(x)] \sim \frac{f(x)\|K\|_2^2}{nh}

Intuition: Each kernel window of width hh effectively uses only about nhnh of the nn data points. Averaging nhnh observations gives variance โˆผ1nh\sim \frac{1}{nh}.

Smaller hh โ‡’\Rightarrow fewer neighbors used โ‡’\Rightarrow higher variance. This is the direct opposite of the bias behavior.


Part (c): Optimal Bandwidth and MSE Rate

The pointwise MSE decomposes as:

MSE[f^h(x)]=Bias2+Varianceโ‰ˆh4ฯƒK44[fโ€ฒโ€ฒ(x)]2โŸโˆผh4+CnhโŸโˆผ(nh)โˆ’1\text{MSE}[\hat{f}_h(x)] = \text{Bias}^2 + \text{Variance} \approx \underbrace{\frac{h^4 \sigma_K^4}{4}[f''(x)]^2}_{\sim h^4} + \underbrace{\frac{C}{nh}}_{\sim (nh)^{-1}}

Minimize over hh by differentiating:

ddhโ€‰โฃ[Ah4+Bnh]=4Ah3โˆ’Bnh2=0\frac{d}{dh}\!\left[A h^4 + \frac{B}{nh}\right] = 4Ah^3 - \frac{B}{nh^2} = 0

โ‡’hโˆ—โˆnโˆ’1/5\Rightarrow h^* \propto n^{-1/5}

Substituting back, the optimal MSE rate is:

MSEโˆ—โˆผnโˆ’4/5\text{MSE}^* \sim n^{-4/5}

Comparison with parametric rates: In a parametric model, MSE decays as nโˆ’1n^{-1}. KDE only achieves nโˆ’4/5n^{-4/5}, which is strictly slower โ€” this is the price of not knowing the functional form of ff.


The Punchline: The Biasโ€“Variance Trade-off

You cannot take hโ†’0h \to 0 because:

  • As hโ†’0h \to 0: bias โ†’0\to 0 โœ“ but variance โ†’โˆž\to \infty โœ— (each point uses essentially no data).
  • As hโ†’โˆžh \to \infty: variance โ†’0\to 0 โœ“ but bias โ†’โˆž\to \infty โœ— (you're estimating a flat function regardless of ff).

The optimal hโˆ—โˆผnโˆ’1/5h^* \sim n^{-1/5} is a delicate balance. This trade-off is universal in nonparametric estimation: you pay a nโˆ’4/5n^{-4/5} rate rather than nโˆ’1n^{-1} because you must simultaneously reduce both bias and variance, and they pull in opposite directions.

This is the nonparametric analogue of biasโ€“variance decomposition, and it is one of the most fundamental ideas in statistical learning theory.

Source: The Elements of Statistical Learning, Ch. 6 (Hastie, Tibshirani, Friedman)

Type: ML/StatsSource: The Elements of Statistical Learning, Ch. 6 (Hastie, Tibshirani, Friedman)Edit on GitHub โ†—