Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.A smooth histogram with no bins
Instead of dropping each measurement into a bin, place a small smooth bump centred on it, then add all the bumps up. The result is a smooth curve that rises where measurements cluster: a kernel density estimate. There is no bin edge to place, so the origin artefact of histograms disappears. One choice remains, and it matters a great deal: how wide each bump is, the bandwidth.
KDEs underlie violin plots, likelihood-based anomaly detection, smooth bootstrap resampling, mode finding (mean shift) and probabilistic models of sensor noise. Automated pipelines need a bandwidth chosen without a human looking at the plot, and the different selectors fail in different, predictable ways.
- 1. A KDE is built by…
- 2. Compared with the kernel's shape, the bandwidth matters…
- 3. A KDE avoids the histogram's…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.The estimator and rules of thumb by hand
Estimator. f̂(x) = (1/(nh)) Σᵢ K((x − xᵢ)/h), where K is a density with mean 0 and unit variance. f̂ integrates to 1 for any h.
AMISE. For a second-order kernel:
AMISE(h) = R(K)/(nh) + h⁴ μ₂(K)² R(f″)/4,
with R(g) = ∫g², μ₂ the kernel variance, and R(f″) the density’s curvature. For the Gaussian kernel R(K) = 1/(2√π) = 0.282 and μ₂ = 1.
Optimal bandwidth. h* = [R(K)/(μ₂² R(f″) n)]^(1/5), with AMISE* ∝ n^(−4/5), faster than the histogram’s n^(−2/3).
Normal reference. If f is normal with s.d. σ, R(f″) = 3/(8√π σ⁵), which gives h = (4/3)^(1/5) σ n^(−1/5) = 1.06 σ n^(−1/5). For n = 400 that is 0.320.
Silverman’s rule. h = 0.9 · min(σ̂, IQR/1.34) · n^(−1/5), deliberately smaller and robust to outliers so that it oversmooths multimodal data less: 0.272 for n = 400 and σ = 1.
The steps in plain English:
- Pick a kernel (Gaussian is fine) and standardise the data if comparing variables.
- Start with a plug-in bandwidth (Sheather–Jones); compare with Silverman and with cross-validation.
- If the data are bounded (positive, percentages), transform them or use boundary correction before trusting the edges.
- Overlay the estimate on a histogram and a rug plot, and check that claimed features survive nearby bandwidths.
- 1. The KDE's AMISE-optimal bandwidth scales as…
- 2. The normal-reference constant is about…
- 3. Silverman's 0.9 · min(σ, IQR/1.34) is designed to…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.The selectors and how they fail
Least-squares cross-validation (LSCV). It minimises an unbiased estimate of ISE − ∫f²:
LSCV(h) = ∫f̂² − (2/n) Σ f̂₋ᵢ(xᵢ).
It targets the right criterion, but its minimiser is very variable: its relative error decays only like n^(−1/10), and the criterion often has several local minima or picks too small an h.
Likelihood cross-validation. It maximises Σ log f̂₋ᵢ(xᵢ), a Kullback–Leibler criterion. One far outlier has almost no neighbours, so its leave-one-out density is nearly zero, and the criterion pushes h up to cover it. With heavy tails this badly oversmooths.
Plug-in (Sheather–Jones). Estimate the unknown R(f″) = ψ₄ with a pilot KDE of f″, whose own bandwidth comes from a normal-reference estimate of the next derivative (ψ₆, then ψ₈), and plug it into h*. Its relative error decays like n^(−5/14), much faster than LSCV, and it is the default in many statistics packages.
Kernel efficiency. Epanechnikov’s kernel minimises AMISE. The Gaussian kernel is about 95% as efficient and the uniform about 93%, so with equal-variance kernels the choice barely matters.
Exercise 1 (400 standard normal values; AMISE-optimal h = 0.320). Bandwidths, with ISE ×1000:
- Silverman 0.271 (2.02);
- LSCV 0.376 (2.01);
- likelihood CV 0.376 (2.01);
- plug-in 0.327 (1.92), closest to the optimum.
from scipy.stats import gaussian_kde
kde = gaussian_kde(x, bw_method="silverman") # or "scott", or a scalar factor
from statsmodels.nonparametric.kde import KDEUnivariate
k = KDEUnivariate(x); k.fit(bw="normal_reference") # statsmodels also offers cross-validated bandwidths via KDEMultivariate- 1. LSCV's main weakness is…
- 2. Likelihood CV oversmooths heavy-tailed data because…
- 3. Plug-in selectors estimate…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Rates. With the optimal h, KDE MISE is of order n^(−4/5) for twice-differentiable f and second-order kernels. Higher-order kernels, which take negative values, can reach n^(−2s/(2s+1)) for smoother densities, at the cost of non-positive estimates.
Boundary bias. For data on [0, ∞), the KDE leaks mass below 0 and underestimates near the boundary, with bias of order h rather than h². Fixes include reflection, boundary kernels, and estimating on a log scale and transforming back (with the Jacobian). The lognormal case in the exercises suffers from this.
Adaptive bandwidths. A single h oversmooths sharp peaks and undersmooths long tails. Abramson’s square-root law, hᵢ ∝ f̂(xᵢ)^(−½), uses wider kernels in sparse regions and reduces bias.
Interview questions.
- Which bandwidth selector should be the default? Sheather–Jones plug-in for univariate data, cross-checked with a rule of thumb. Avoid likelihood CV with heavy tails, and treat LSCV with care for small n.
- How do you estimate a density for a positive quantity such as a time to failure? Transform to log scale, estimate there, and back-transform with the Jacobian, or use reflection or boundary kernels. Otherwise the estimate puts mass on negative times.
- 1. At a boundary, the standard KDE's bias is of order…
- 2. Abramson's adaptive rule sets local bandwidths ∝…
- 3. Higher-order kernels can improve rates but…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Which selector, for which density?
History.
- M. Rosenblatt (1956) and E. Parzen (1962) introduced kernel density estimation.
- B. Silverman (1986) gave the rule of thumb.
- M. Rudemo (1982) and A. Bowman (1984) introduced least-squares cross-validation.
- S. Sheather and M. C. Jones (1991) introduced the solve-the-equation plug-in.
- M. P. Wand and M. C. Jones (1995) wrote the standard monograph.
Exercise 2. Integrated squared error ×1000, mean of 3 samples.
| density | n | Silverman | LSCV | likelihood CV | plug-in |
|---|---|---|---|---|---|
| normal | 100 | 7.91 | 7.99 | 6.33 | 7.48 |
| 500 | 2.32 | 2.60 | 2.26 | 2.19 | |
| bimodal | 100 | 58.4 | 21.86 | 21.7 | 22.36 |
| 500 | 25.73 | 3.37 | 3.53 | 3.30 | |
| lognormal | 100 | 25.93 | 19.89 | 52.76 | 20.75 |
| 500 | 13.58 | 7.11 | 41.53 | 7.62 | |
| t (3 d.f.) | 100 | 6.31 | 20.54 | 12.95 | 6.83 |
| 500 | 1.64 | 1.80 | 17.69 | 1.68 |
- The plug-in selector was within 30% of the best selector in every case. No other selector managed that.
- Silverman’s rule oversmoothed the bimodal density (25.7 against 3.3 at n = 500), despite its built-in caution.
- Likelihood CV failed badly on heavy and skewed tails (t₃: 17.7 against 1.7; lognormal: 41.5 against 7.6). A few extreme points dragged its bandwidth up.
- LSCV was excellent when it worked but unreliable at small n (t₃ at n = 100: 20.5, three times the plug-in’s error).
- The lognormal errors stay large for every selector, partly because of boundary bias at 0: a log transform would help more than any bandwidth rule.
KDE in AI, robotics and aerospace
- Sensor-noise models: non-Gaussian measurement-error densities for particle filters and likelihood-based fault detection.
- Reliability: smooth estimates of failure-time or load spectra densities, on log scales to respect positivity.
- Mean-shift tracking and clustering: modes of KDEs in feature space for object tracking and regime discovery.
References
- E. Parzen (1962). “On estimation of a probability density function and mode.” Annals of Mathematical Statistics 33(3).
- B. W. Silverman (1986). Density Estimation for Statistics and Data Analysis. Chapman and Hall.
- S. J. Sheather, M. C. Jones (1991). “A reliable data-based bandwidth selection method for kernel density estimation.” Journal of the Royal Statistical Society B 53(3).
- M. P. Wand, M. C. Jones (1995). Kernel Smoothing. Chapman and Hall.
- M. C. Jones, J. S. Marron, S. J. Sheather (1996). “A brief survey of bandwidth selection for density estimation.” Journal of the American Statistical Association 91(433).