Skip to content
Lambdian

Kernel density estimation

Estimate a smooth probability density from samples by summing kernels; choose the bandwidth well.

Animated explanation

The idea

A histogram estimates a distribution by counting points in bins, but the picture changes with the bin width and where the bins start, and it is blocky. Kernel density estimation fixes both: put a small smooth bump (a kernel, usually a Gaussian) on top of every data point and add the bumps up. Where points crowd together the bumps pile up into a hill.

The width of each bump, the bandwidth hh, is the one decision that matters. Too narrow and the estimate is a row of spikes; too wide and real features blur away. KDE gives you a smooth density for visualising distributions, for anomaly scores (low density = unusual), for sampling, and as the engine inside mean shift.

Bandwidth selection

Rules of thumb (Scott's, Silverman's) work for roughly bell-shaped data. For multi-modal data, choose hh by cross-validated log-likelihood: leave points out, evaluate the density there, keep the hh that predicts held-out points best. The kernel shape matters far less than hh.

Limits

Evaluating the density costs O(n)O(n) per query (tree methods help). In more than a handful of dimensions, KDE needs exponentially more data (the curse of dimensionality), so for high-dimensional densities use GMMs, normalising flows or other parametric models.

The algorithm, step by step

  1. 1
    Choose a kernel K (Gaussian) and bandwidth h (rule of thumb or cross-validation).
  2. 2
    For a query x, compute K((x−xi)/h)K((x - x_i)/h) for every training point.
  3. 3
    Average and divide by h (or by det⁡H1/2\det H^{1/2} in d dimensions).
  4. 4
    Use the density for plots, anomaly scores (low density), or sampling (pick a point, add kernel noise).

The maths

Estimator

f^h(x)=1nh∑iK(x−xih)\hat f_h(x) = \frac{1}{nh}\sum_i K\Big(\frac{x-x_i}{h}\Big)

Gaussian kernel

K(u)=12πe−u2/2K(u) = \frac{1}{\sqrt{2\pi}}e^{-u^2/2}

Silverman's rule

h=0.9min⁡(σ^,IQR/1.34) n−1/5h = 0.9\min(\hat\sigma, \mathrm{IQR}/1.34)\,n^{-1/5}

Bias–variance in h

MISE(h)≈h44μ2(K)2 ⁣∫f′′2+R(K)nh\mathrm{MISE}(h) \approx \frac{h^4}{4}\mu_2(K)^2\!\int f''^2 + \frac{R(K)}{nh}

Strengths and weaknesses

Strengths

  • Smooth, assumption-light density estimate
  • One parameter (h) with good selection methods
  • Useful for visualisation, anomaly detection and sampling

Weaknesses

  • Bandwidth choice drives the result
  • O(n) per query; whole dataset must be stored
  • Poor in high dimensions

Reach for it when…

Low-dimensional continuous data where you want the shape of the distribution or a density-based anomaly score.

Check your understanding

Question 1

Q1Your KDE shows a separate spike on every data point. You should…

Question 2

Q2Compared with a histogram, a KDE…

Question 3

Q3What is the best way to choose h for multi-modal data?

Lambdian Lab

KDE and bandwidth by cross-validation

Gaussian KDE in any dimension with a scalar bandwidth h.

  • log_density(X_train, X_query, h): log of the estimated density at each query point (use log-sum-exp for stability).
  • silverman(x): Silverman's rule for 1-D data (std with ddof=1).
  • best_bandwidth(X, hs, k, seed): k-fold cross-validated total log-likelihood; return the best h.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.