Kernel Density Estimation

1 minute read

Published:

A quick introduction to the What, Why, and How of Kernel Density Estimation.

What?

Estimate the shape of a probability density function from a set of observations. Most importantly, the shape of the resulting function is continuous.

Why?

Other simpler methods like histograms estimate the shape of these functions by counting the number of observations inside each bucket. This results in “bumpy” approximations of the original function that produced the data.

How?

Histograms estimate the value of the function at any given point as

\[\hat{f}_X(x_0)=\frac{\#x_i \in \mathcal{N}(x_0)}{N\lambda}\]

where $\mathcal{N}(x_0)$ is the set of observations that fall into the same bucket as $x_0$, $\lambda$ is the width of the bucket and $N$ is the number of observations.

Histogram of N(0,1) with 5, 20, and 100 bins. The number of bins determines our bias-variance trade-off. More bins, more variance, less bins, more bias.

As we said before, these are bumpy estimations of $f_X$. This is mainly because we are weighting all observations $x_i$ inside the bucket equally without taking into account that some might be closer to $x_0$ than others. This could be kinda patched by using more and more bins, however, a much more elegant solution solves this problem.

Instead of having each neighboring point of $x_0$ contribute equally, we could use

\[\hat{f}_X(x_0)=\frac{1}{N\lambda}\sum_{i=1}^N K_\lambda(x_0, x_i)\]

where choosing a continuous function as our $K_\lambda(x_0, x_i)$ results in a continuous estimate of $f_X$.

A common choice for $K_\lambda$ is the Gaussian kernel

\[K_\lambda(x_0,x) = \phi \left(\frac{x-x_0}{\lambda}\right)\]

where each point’s weight decays like a $\mathcal{N}(0,\lambda^2)$, where $\lambda$ controls how fast.

Gaussian KDE of N(0,1) with increasing lambda. Again, lambda controls the bias-variance trade-off where a bigger lambda increases bias while a smaller one increases variance.

Main Takeaway

Kernel Density Estimation generalizes histograms by leveraging kernels to weight how much each observation contributes to estimating the density of each point.

References

Hastie, T., Tibshirani, R., Friedman, J. (2009). The Elements of Statistical Learning: Data Mining, Inference, and Prediction. 2nd ed. Springer. Section 6.6: Kernel Density Estimation and Classification.