Skip to content
AI360Xpert
Beta

Kernel Density Estimation

Histograms jump with bin edges. KDE centers a smooth bump on every point and averages them into a curve with no bins to argue about.

Eight samples each contribute a dashed bump of width 0.3, and their average forms the smooth density with no bin edges
Eight samples each contribute a dashed bump of width 0.3, and their average forms the smooth density with no bin edges

Why Does This Exist?

Histograms lie by binning: shift the edges and modes appear or vanish, and high dimensions make bins explode exponentially. Parametric fits lie by shape: data are rarely exactly Gaussian. Analysts need a smooth, bin-free picture of "where the data concentrate" before modeling.

KDE hatfh(x)=frac1nhsumiK((x−Xi)/h)\\hat f_h(x) = \\frac{1}{nh}\\sum_i K((x - X_i)/h) delivers it: one kernel KK per point, averaged, with bandwidth hh setting bump width. Gaussian kernels are the default. It needs only descriptive statistics as background and pairs with resampling in bootstrap and resampling.

The one idea you need first

A density integrates to 1 with tall where data crowd. Each kernel bump integrates to 1/n1/n, so the average integrates to 1 automatically. Bandwidth is the only knob: small hh follows every point, large hh flattens everything.

Think of It Like This

Sand piles instead of buckets

A histogram sorts sand into buckets: move the bucket walls and the skyline changes. KDE drops one small sand pile on each grain and lets them merge into dunes.

Pile width is bandwidth: sugar-fine piles spike at every grain, gravel-wide piles flatten the desert. Where it stops: sand piles are physical and local, while KDE sums globally per query, which gets expensive on big nn without trees or FFT.

How It Actually Works

Bias shrinks as h2h^2 while variance shrinks as 1/(nh)1/(nh): the classic tradeoff, minimized near hsimn−1/5h \\sim n^{-1/5} in 1D with integrated squared error simn−4/5\\sim n^{-4/5}. Silverman's rule h=0.9min(sigma,IQR/1.34)n−1/5h = 0.9\\min(\\sigma, IQR/1.34)n^{-1/5} starts Gaussian-ish cases. Multivariate KDE needs full bandwidth matrices and degrades fast with dimension: effective samples shrink exponentially, so past ~5D prefer parametric or projection methods.

Worked example

Eight points [1.0,1.5,2.0,2.2,3.0,3.5,5.0,5.2][1.0, 1.5, 2.0, 2.2, 3.0, 3.5, 5.0, 5.2] with Gaussian h=0.3h = 0.3. At x=2.1x = 2.1, kernel contributions peak for the 2.0 and 2.2 neighbors: each contributes frac18frac10.3sqrt2piexp(−0.5(0.1/0.3)2)approx0.157\\frac{1}{8}\\frac{1}{0.3\\sqrt{2\\pi}}\\exp(-0.5(0.1/0.3)^2) \\approx 0.157, distant 5.0 contributes under 0.0010.001. Summing all eight gives hatf(2.1)approx0.42\\hat f(2.1) \\approx 0.42. With h=0.05h = 0.05 the same xx spikes above 1.0 between points and crashes between them; with h=2h = 2 it flattens near 0.12 everywhere.

Code

import numpy as np
x = np.array([1.0, 1.5, 2.0, 2.2, 3.0, 3.5, 5.0, 5.2])h = 0.3grid = np.array([2.1])dens = np.exp(-0.5 * ((grid[:, None] - x) / h) ** 2).sum(axis=1)dens = dens / (x.size * h * np.sqrt(2 * np.pi))print(round(float(dens[0]), 2))  # -> 0.42

The hand sum and the code agree at 0.42. Change hh to 0.05 or 2.0 and watch spike versus flatten.

Watch Out For

Trusting default bandwidths blindly

Silverman assumes near-Gaussian data and oversmooths multimodality: two real humps merge into one, and analysts conclude one segment. Undersmoothed defaults do the reverse, inventing modes from noise.

Plot several bandwidths across a decade of hh. If the mode count changes, the default decided your conclusion, not the data.

KDE on high dimensions or bounded edges

In 10D, neighborhoods are empty and bumps never overlap: the curve is nn spikes on a plain. On bounded [0,infty)[0, \\infty) data, symmetric kernels leak mass across the edge and understate near-zero density.

Cap KDE at low dimensions, use boundary correction or logs near edges, and switch families past ~5D. If most query points see one bump, dimensionality won.

The Quick Version

  • KDE averages one 1/n1/n-mass bump per point; no bins, one bandwidth knob.
  • Bias simh2\\sim h^2, variance sim1/(nh)\\sim 1/(nh); optimal hsimn−1/5h \\sim n^{-1/5} in 1D.
  • Eight-point example at h=0.3h=0.3 gives hatf(2.1)approx0.42\\hat f(2.1) \\approx 0.42.
  • Tiny hh spikes on noise, huge hh flattens real modes; scan a decade.
  • Past ~5 dimensions or on hard boundaries, correct or switch methods.
  • Naive queries cost O(n)O(n) each; use trees or FFT at scale.