01 / 21

Kernel Density
Estimation

Learn how to estimate the probability density function of a continuous random variable from observed data using smooth kernel functions.

Statistics

What is Kernel Density Estimation?

Kernel Density Estimation (KDE) is a non-parametric method for estimating the probability density function of a random variable. Instead of assuming data follows a known distribution (like the normal), KDE lets the data speak for itself.

Why Not Just Use a Histogram?

Histogram Issues
Depends on bin width and bin placement. Different choices yield different shapes. Inherently discontinuous.
KDE Advantages
Produces a smooth, continuous estimate. No arbitrary bin edges. Converges to the true density as \( n \to \infty \).

Visual Intuition: Building KDE

\(x\)
\(f(x)\)
\(x_1\)
\(x_2\)
\(x_3\)
\(x_4\)
\(x_5\)
KDE kernels

The KDE Formula

Given observations \( x_1, x_2, \ldots, x_n \), the kernel density estimator is:

\( K(\cdot) \)
The kernel function: a symmetric, non-negative function that integrates to 1.
\( h \)
The bandwidth: controls how wide each kernel is. The most critical parameter.
\( n \)
The sample size: more data points produce a more accurate estimate.

Common Kernel Functions

Kernel Formula \( K(u) \) Support
Gaussian \( \frac{1}{\sqrt{2\pi}} e^{-u^2/2} \) \( (-\infty, \infty) \)
Epanechnikov \( \frac{3}{4}(1 - u^2) \) \( |u| \leq 1 \)
Uniform \( \frac{1}{2} \) \( |u| \leq 1 \)
Triangular \( 1 - |u| \) \( |u| \leq 1 \)

The Role of Bandwidth \( h \)

\(x\)
True density Small h Large h Good h
Too Small
Undersmoothed: noisy, spiky, high variance.
Too Large
Oversmoothed: blurred, misses features, high bias.
Just Right
Optimal: balances bias and variance.

Bias-Variance Trade-off

The bandwidth \( h \) controls the fundamental trade-off in KDE. The Mean Integrated Squared Error (MISE) decomposes as:

Large \( h \)
High bias (over-smoothing), low variance.
Small \( h \)
Low bias, high variance (under-smoothing, spiky).

KDE vs Histogram vs Parametric

Property KDE Histogram Parametric
Smoothness Smooth Discontinuous Smooth
Assumptions Minimal Minimal Strong
Tuning Bandwidth \( h \) Bin width Model choice
Convergence \( O(n^{-4/5}) \) \( O(n^{-2/3}) \) \( O(n^{-1}) \)
Flexibility High Moderate Low

Choosing Bandwidth \( h \)

1
Silverman's Rule of Thumb: \( h = 0.9 \cdot \min\!\left(\hat{\sigma},\, \frac{\text{IQR}}{1.34}\right) \cdot n^{-1/5} \). Fast, good for unimodal data.
2
Least Squares Cross-Validation (LSCV): Minimizes the integrated squared error. Data-driven, can be noisy for small \( n \).
3
Plug-in Methods (Sheather-Jones): Estimates the unknown functionals in the optimal MISE formula. Generally the most reliable automatic selector.

How to Apply KDE: Step by Step

1
Collect your data: Obtain the sample \( x_1, x_2, \ldots, x_n \).
2
Choose a kernel: Usually the Gaussian kernel \( K(u) = \frac{1}{\sqrt{2\pi}} e^{-u^2/2} \).
3
Select bandwidth \( h \): Use Silverman's rule, cross-validation, or a plug-in method.
4
Evaluate: For each query point \( x \), compute \( \hat{f}_h(x) = \frac{1}{nh}\sum_{i=1}^{n} K\!\left(\frac{x - x_i}{h}\right) \).
5
Plot and interpret: The resulting curve is your estimated density. Look for modes, skewness, and spread.

Example 1: Three Data Points

Suppose we observe \( x_1 = 1,\; x_2 = 3,\; x_3 = 4 \) and use a Gaussian kernel with \( h = 1 \). What is \( \hat{f}_h(2.5) \)?

where \( \phi(\cdot) \) is the standard normal PDF.

Example 2: Silverman's Rule

Given \( n = 100 \) observations with \( \hat{\sigma} = 2.5 \) and \( \text{IQR} = 3.2 \). Find the rule-of-thumb bandwidth.

The suggested bandwidth is \( h \approx 0.856 \).

Example 3: Bandwidth Effect on Shape

\( h = 0.3 \)
\( h = 0.8 \)
\( h = 2.0 \)

Common Mistakes

Ignoring Bandwidth
Using the default \( h \) without considering data characteristics. Always inspect the result and compare bandwidths.
Forgetting Normalization
Omitting the \( \frac{1}{nh} \) factor, so the result does not integrate to 1 and is not a valid density.
Boundary Bias
Applying standard KDE to bounded data (e.g., positive-only) without correction. The estimate "leaks" probability into impossible regions.

Summary: Key Concepts

Concept Key Idea
KDE Formula \( \hat{f}_h(x) = \frac{1}{nh}\sum K\!\left(\frac{x-x_i}{h}\right) \)
Kernel \( K \) Symmetric bump; choice matters little
Bandwidth \( h \) Critical: controls bias-variance trade-off
MISE Rate \( O(n^{-4/5}) \) for optimal \( h \)
Silverman's Rule \( h = 0.9\,A\,n^{-1/5} \), fast default
Best Practice Visualize with multiple \( h \) values

Question 1 of 5

In the KDE formula \( \hat{f}_h(x) = \frac{1}{nh}\sum_{i=1}^{n} K\!\left(\frac{x - x_i}{h}\right) \), what does \( h \) control?

Question 2 of 5

If the bandwidth \( h \) is too large, the KDE estimate will be:

Question 3 of 5

Which kernel is theoretically optimal in the sense of minimizing the Mean Integrated Squared Error (MISE)?

Question 4 of 5

Silverman's rule of thumb for bandwidth is \( h = 0.9 \cdot A \cdot n^{-1/5} \). What is \( A \)?

Question 5 of 5

The optimal MISE convergence rate for KDE with a second-order kernel is: