Kernel density estimation and bandwidth
Normalize a density smoother, derive its leading bias and variance, and separate interior asymptotics from boundary behavior.
On this page
The question for today
What you’ll understand
- Calculate a normalized kernel density estimate.
- Explain the leading bandwidth bias and variance orders.
- Recognize boundary bias and the limits of an asymptotic bandwidth formula.
A cumulative distribution can be estimated without smoothness, but estimating its density asks how probability mass changes over very short distances. Kernel smoothing spreads each observation into a small normalized bump. The bandwidth controls both the spatial resolution and the amount of random fluctuation. Narrow bumps resolve more detail while using fewer effective observations near a point. The resulting tradeoff is mathematical, not simply aesthetic, and its familiar formulas need hypotheses about the density, kernel, and location being considered.
Every observation contributes a normalized bump
For an iid sample from a Lebesgue density f on the real line, choose an integrable kernel K with integral one and bandwidth h>0. The estimate averages . If K is nonnegative, the estimate is nonnegative and integrates to one. The factor h inverse is essential: widening a bump lowers its height while preserving its probability mass. A symmetric kernel has zero first moment when that moment is finite, but symmetry alone does not supply finite second moments or square integrability.
The expectation of the estimate is the convolution of f with the rescaled kernel, so smoothing creates bias before sampling variability enters. A signed higher-order kernel can cancel additional moments and reduce certain bias terms, but its estimate can be negative. Thus unit integral and nonnegativity are distinct requirements. Calling every finite curve a density because it has a plausible shape overlooks both normalization and support. A density value also has inverse measurement units; changing the units of the observations must change its numerical height.
Leading orders explain bandwidth sensitivity
Take a bounded, nonnegative, symmetric, compactly supported kernel with integral one, positive second moment , and . At a fixed point where f is twice continuously differentiable in a neighborhood, Taylor expansion gives bias . With f continuous there, h tending to zero and nh tending to infinity, the variance is . These are asymptotic expansions, not exact finite-sample errors for every density.
For a density f in W²,²(R), the Sobolev space with two square-integrable weak derivatives, and the same compactly supported kernel, the leading mean integrated squared error balances a term proportional to h to the fourth power against a term proportional to one over nh. The optimizing order is n to the minus one-fifth in one dimension. Its constant depends on the unknown curvature of f, so the expression is an oracle guide rather than an implementable instruction by itself. Cross-validation and plug-in rules estimate different ingredients and can behave differently on finite samples.
Boundaries change the approximation problem
If a density is supported on a half-line and has a positive boundary value, a symmetric kernel spills mass outside the support. At the boundary its expectation may approach half the correct height rather than the height itself. The interior Taylor calculation cannot be used across a discontinuity in the density's zero extension. Reflection, boundary kernels, transformations, or local-polynomial density methods address particular boundary settings; none is justified merely by shrinking the bandwidth while retaining the same incorrect boundary argument.
Bandwidth controls a spatial scale, whereas increasing the sample size supplies more information at that scale. Comparing estimates at several bandwidths can reveal instability but does not distinguish noise from a genuine narrow feature without assumptions. Multivariate product kernels also pay a variance cost involving the product of bandwidths, which becomes severe as dimension grows. The normalization, effective local sample size, and applicable smoothness class should be stated before declaring a visually detailed estimate more accurate than a smooth one.
Work it through
A wider window need not lower a particular height
Use data 0,1,2,3 and the box kernel K(u)=1/2 for |u|≤1. Compute the density estimate at x=1 for bandwidths 0.75 and 1.25.
With h=0.75, only the observation at one is within the kernel window. Its kernel value is one-half, so the estimate is (1/2)/(4×0.75)=1/6.
With h=1.25, observations zero, one and two are inside the window while three is outside. The estimate is (3/2)/(4×1.25)=0.3. None of these distances lies exactly on a window boundary.
Every individual bump is shorter at the larger bandwidth, but more bumps contribute at this location. The total height increases here while the complete estimate remains normalized at both bandwidths. A pointwise increase does not contradict the overall smoothing effect.
The heights are 1/6 and 0.3. Bandwidth changes overlap and normalization together, not merely vertical scale.
The living notebook
Explore the model
Interactive lab · heat
Smoothing suppresses finer spatial oscillations
Pause and Reset, then set both controls to spatial mode n=1 and diffusivity κ=0.5. Take two Steps to time 1; predict amplitude exp(−0.5), about 0.606531. Reset, set n=2 and κ=0.5, and take two Steps; predict exp(−2), about 0.135335. Heat flow is an exact deterministic smoothing analogy. It does not fit a density, choose a bandwidth, or display sampling variance.
- Current amplitude
- 1
- Spatial L² norm
- 1.772
- Decay rate · κn²
- 3.6 /s
The exact periodic heat-equation mode is u(x,t) = exp(−κn²t) sin(nx), on [0,2π]. Playback advances physical time. The initial sine is shown separately, and the unnormalized L² norm is √π exp(−κn²t). This analytic instrument does not run a finite-difference solver or test discretization stability. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA smoothing operator can attenuate fine spatial structure faster than coarse structure.
Pause & reason
Check your understanding
Your turn
Try it yourself
For the Epanechnikov kernel K(u)=3(1−u²)/4 on [−1,1], calculate its second moment and squared integral. State why these two constants enter different parts of the leading risk.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Expand the even polynomials and integrate from zero to one, then double the result.
Compare with the solution
The second moment is (3/2)(1/3−1/5)=1/5. The squared integral is (9/8)(1−2/3+1/5)=3/5. The second moment multiplies the density curvature in the bias, so its square enters squared bias. The squared integral instead controls the leading variance of the localized random bump. Their roles cannot be exchanged even though both are kernel-dependent constants.
Keep the idea
Take it with you
- Bandwidth rescales bump height and width while preserving mass.
- Interior second-order smoothing balances squared bias h⁴ against variance 1/(nh).
- Boundary corrections and signed kernels require separate qualifications.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.