# Variable kernel density estimation

In statistics, adaptive or "variable-bandwidth" kernel density estimation is a form of kernel density estimation in which the size of the kernels used in the estimate are varied depending upon either the location of the samples or the location of the test point. It is a particularly effective technique when the sample space is multi-dimensional. [1]

## Rationale

Given a set of samples, ${\displaystyle \lbrace {\vec {x}}_{i}\rbrace }$, we wish to estimate the density, ${\displaystyle P({\vec {x}})}$, at a test point, ${\displaystyle {\vec {x}}}$:

${\displaystyle P({\vec {x}})\approx {\frac {W}{nh^{D}}}}$
${\displaystyle W=\sum _{i=1}^{n}w_{i}}$
${\displaystyle w_{i}=K\left({\frac {{\vec {x}}-{\vec {x}}_{i}}{h}}\right)}$

where n is the number of samples, K is the "kernel", h is its width and D is the number of dimensions in ${\displaystyle {\vec {x}}}$. The kernel can be thought of as a simple, linear filter.

Using a fixed filter width may mean that in regions of low density, all samples will fall in the tails of the filter with very low weighting, while regions of high density will find an excessive number of samples in the central region with weighting close to unity. To fix this problem, we vary the width of the kernel in different regions of the sample space. There are two methods of doing this: balloon and pointwise estimation. In a balloon estimator, the kernel width is varied depending on the location of the test point. In a pointwise estimator, the kernel width is varied depending on the location of the sample.[1]

For multivariate estimators, the parameter, h, can be generalized to vary not just the size, but also the shape of the kernel. This more complicated approach will not be covered here.

## Balloon estimators

A common method of varying the kernel width is to make it inversely proportional to the density at the test point:

${\displaystyle h={\frac {k}{\left[nP({\vec {x}})\right]^{1/D}}}}$

where k is a constant. If we back-substitute the estimated PDF, and assuming a Gaussian kernel function, we can show that W is a constant:[2]

${\displaystyle W=k^{D}(2\pi )^{D/2}}$

A similar derivation holds for any kernel whose normalising function is of the order hD, although with a different constant factor in place of the (2 π)D/2 term. This produces a generalization of the k-nearest neighbour algorithm. That is, a uniform kernel function will return the KNN technique.[2]

There are two components to the error: a variance term and a bias term. The variance term is given as:[1]

${\displaystyle e_{1}={\frac {P\int K^{2}}{nh^{D}}}}$.

The bias term is found by evaluating the approximated function in the limit as the kernel width becomes much larger than the sample spacing. By using a Taylor expansion for the real function, the bias term drops out:

${\displaystyle e_{2}={\frac {h^{2}}{n}}\nabla ^{2}P}$

An optimal kernel width that minimizes the error of each estimate can thus be derived.

## Use for statistical classification

The method is particularly effective when applied to statistical classification. There are two ways we can proceed: the first is to compute the PDFs of each class separately, using different bandwidth parameters, and then compare them as in Taylor.[3] Alternatively, we can divide up the sum based on the class of each sample:

${\displaystyle P(j,{\vec {x}})\approx {\frac {1}{n}}\sum _{i=1,c_{i}=j}^{n}w_{i}}$

where ci is the class of the ith sample. The class of the test point may be estimated through maximum likelihood.

Many kernels, Gaussian for instance, are smooth. Consequently, estimates of joint or conditional probabilities are both continuous and differentiable. This makes it easy to search for a border between two classes by zeroing the difference between the conditional probabilities:

${\displaystyle R({\vec {x}})=P(2|{\vec {x}})-P(1|{\vec {x}})={\frac {P(2,{\vec {x}})-P(1,{\vec {x}})}{P(1,{\vec {x}})+P(2,{\vec {x}})}}}$

For example, we can use a one-dimensional root-finding algorithm to zero R along a line between two samples that straddle the class border. The border can be thus sampled as many times as necessary. The border samples along with estimates of the gradients of R determine the class of a test point through a dot-product:

${\displaystyle j=\arg {\underset {i}{\min }}|{\vec {b_{i}}}-{\vec {x}}|\,}$
${\displaystyle p=({\vec {x}}-{\vec {b_{j}}})\cdot \nabla _{\vec {x}}R|_{{\vec {x}}={\vec {b_{j}}}}\,}$
${\displaystyle c=(3+p/|p|)/2\,}$

where ${\displaystyle \lbrace {\vec {b_{i}}}\rbrace }$ sample the class border and c is the estimated class. The value of R, which determines the conditional probabilities, may be extrapolated to the test point:

${\displaystyle R({\vec {x}})\approx \tanh p\,}$

Two-class classifications are easy to generalize to multiple classes.