Mathwords logoMathwords

Kernel Density Estimation — Definition, Formula & Example

Kernel density estimation (KDE) is a way to draw a smooth curve showing where data values are concentrated. You place a small bump, called a kernel, on every data point and add the bumps together.

Given a sample x1,…,xnx_1, \dots, x_n from an unknown continuous distribution, the kernel density estimator with kernel KK and bandwidth h>0h > 0 is the function 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). The kernel must satisfy K(u)≥0K(u) \ge 0 and ∫−∞∞K(u) du=1\int_{-\infty}^{\infty} K(u)\,du = 1. Each scaled bump then has area 1n\frac{1}{n}, so f^h\hat f_h is itself a valid probability density function. The method is nonparametric because it assumes no fixed shape, such as a normal curve.

Key Formula

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:
  • f^h(x)\hat f_h(x) = Estimated density at the point xx
  • nn = Number of data values
  • xix_i = The ii-th data value, where one bump is centered
  • hh = Bandwidth, which controls the width of each bump
  • KK = Kernel: a nonnegative function with total area 1, such as the Gaussian or triangular kernel

How It Works

Unlike a histogram, KDE has no bin edges, so the picture does not change when you shift where the bins start. Its main tuning choice is the bandwidth hh, which sets how wide each bump is. Software usually defaults to a Gaussian (normal-curve) kernel. The triangular kernel, K(u)=1−∣u∣K(u) = 1 - |u| for ∣u∣≤1|u| \le 1 and 00 otherwise, has area 1 and is easy to compute by hand. Read the result like any density curve: areas under it are probabilities, and its heights are not.

Worked Example

Problem: Four students commute 3, 4, 5, and 10 miles to school. Use the triangular kernel K(u)=1−∣u∣K(u) = 1 - |u| for ∣u∣≤1|u| \le 1 (and 00 otherwise) with bandwidth h=2h = 2 miles to estimate the density at x=4x = 4 miles.
Step 1: Scale each distance from x=4x = 4 by the bandwidth: ui=4−xi2u_i = \frac{4 - x_i}{2}.
u=4−32, 4−42, 4−52, 4−102=0.5, 0, −0.5, −3u = \tfrac{4-3}{2},\ \tfrac{4-4}{2},\ \tfrac{4-5}{2},\ \tfrac{4-10}{2} = 0.5,\ 0,\ -0.5,\ -3
Step 2: Apply the kernel. The value 10 lies more than one bandwidth away, so its kernel value is 0.
K(0.5)+K(0)+K(−0.5)+K(−3)=0.5+1+0.5+0=2K(0.5) + K(0) + K(-0.5) + K(-3) = 0.5 + 1 + 0.5 + 0 = 2
Step 3: Divide by nh=4⋅2=8nh = 4 \cdot 2 = 8.
f^2(4)=28=0.25\hat f_2(4) = \frac{2}{8} = 0.25
Answer: f^2(4)=0.25\hat f_2(4) = 0.25 per mile. At x=10x = 10, only one kernel value is nonzero, which is K(0)=1K(0) = 1, so f^2(10)=18=0.125\hat f_2(10) = \frac{1}{8} = 0.125 per mile. The estimate is twice as high in the cluster near 4 miles as at the lone 10-mile commute. The chart shows the full curve with its two separate bumps.

Visualization

Why It Matters

KDE is the smooth curve drawn by density plots and violin plots in tools such as Python's seaborn and R's ggplot2. In college statistics and data science courses, you use it to spot skewness or multiple peaks before choosing a model. A two-dimensional version produces hotspot maps, for example of crime locations or traffic accidents.

Common Mistakes

Mistake: Accepting the default bandwidth without checking how it shapes the curve.
Correction: A very small hh gives a spiky curve with a bump at every point. A very large hh blurs real features together. With the commute data above, h=2h = 2 shows two separate groups, but h=6h = 6 merges them into a single hump. Try several bandwidths and trust the features that persist.

Related Terms