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 from an unknown continuous distribution, the kernel density estimator with kernel and bandwidth is the function . The kernel must satisfy and . Each scaled bump then has area , so is itself a valid probability density function. The method is nonparametric because it assumes no fixed shape, such as a normal curve.
Key Formula
Where:
- = Estimated density at the point
- = Number of data values
- = The -th data value, where one bump is centered
- = Bandwidth, which controls the width of each bump
- = 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 , which sets how wide each bump is. Software usually defaults to a Gaussian (normal-curve) kernel. The triangular kernel, for and 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 for (and otherwise) with bandwidth miles to estimate the density at miles.
Step 1: Scale each distance from by the bandwidth: .
Step 2: Apply the kernel. The value 10 lies more than one bandwidth away, so its kernel value is 0.
Step 3: Divide by .
Answer: per mile. At , only one kernel value is nonzero, which is , so 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 gives a spiky curve with a bump at every point. A very large blurs real features together. With the commute data above, shows two separate groups, but merges them into a single hump. Try several bandwidths and trust the features that persist.
