Kernel smoothing

Kernel smoothing is a non parametric estimation method of the probability density function of a distribution.

Introduction

Let X be a random variable with probability density function p. Given a sample of independent observations x_1, ..., x_n of X and any point x \in \Rset, the kernel smoothing estimator provides an approximation \widehat{p}(x) of p(x).

In dimension 1, the kernel smoothed probability density function \widehat{p} has the following expression, where K is the univariate kernel, n the sample size and (X_1, \cdots, X_n) \in \Rset^n the univariate random sample with \forall i, \, \, X_i \in \Rset ([wand1994] eq. 2.2 page 12):

(1)\widehat{p}(x) = \frac{1}{nh}\sum_{i=1}^{n} K\left(\frac{x-X_i}{h}\right)

The kernel K is a function satisfying:

\int K(x)\, dx=1.

The parameter h is called the bandwidth.

Multivariate kernels

In dimension d>1, the kernel may be defined as a product kernel K_d, as follows where \vect{x} = (x_1, \cdots, x_d)\in \Rset^d ([wand1994] eq. 2.2 page 91):

K_d(\vect{x}) = \prod_{j=1}^{d} K(x_j).

The kernel smoothed probability density function in dimension d is:

\widehat{p}(\vect{x})
= \frac{1}{n \prod_{j=1}^{d}h_j} \sum_{i=1}^{n} K_d\left(\frac{x_1 - (X_{i})_1 }{h_1}, \dots, \frac{x_d - (X_{i})_d}{h_d}\right)

where (\vect{X}_1, \cdots, \vect{X}_n) is the d-variate random sample which components are denoted \vect{X}_i = (X_{i1}, \dots, X_{id}).

In the multivariate case, the bandwidth is the vector \vect{h} = (h_1, \cdots, h_d).

Asymptotic error and asymptotically optimal bandwidth

The quality of the approximation may be controlled by the AMISE (Asymptotic Mean Integrated Square error) criteria defined as follows. By definition, the mean squared error (MSE) at point \vect{x} is (see [scott2015] eq. page 40, [wand1994] pages 14-21):

\operatorname{MSE}(\widehat{p}, \vect{x})
= \mathbb{E}_\vect{X}\left[\left(\widehat{p}(\vect{x}) - p(\vect{x})\right)^2 \right].

It can be proved that the mean squared error is the sum of the variance and the squared bias:

\operatorname{MSE}(\widehat{p}, \vect{x})
= \operatorname{Var}_\vect{X}\left[\widehat{p}(\vect{x})\right]
+ \left(\operatorname{Bias}\left(\widehat{p}(\vect{x})\right)\right)^2

where the bias is:

\operatorname{Bias}\left(\widehat{p}(\vect{x})\right)
= \mathbb{E}_\vect{X}\left[\widehat{p}(\vect{x})\right] - p(\vect{x}).

The MSE depends on the point where the density is evaluated. The mean integrated squared error (MISE) is (see [scott2015] eq. page 41):

\operatorname{MISE}(\widehat{p})
= \mathbb{E}_\vect{X}\left[||\widehat{p} - p||^2_{L_2}\right]   = \int \, \operatorname{MSE}(\widehat{p}, \vect{x}) \, d\vect{x}  \\

Finally, the asymptotic mean integrated squared error (AMISE), denoted \operatorname{AMISE}(\widehat{p}) is defined as the two first terms in the Taylor series expansion of \operatorname{MISE}(\widehat{p}) when n tends to infinity.

The quality of the estimation essentially depends on the value of the bandwidth h. In dimension 1, the bandwidth that minimizes the AMISE criteria is (see [wand1994] eq 2.13 page 22):

(2)h_{\operatorname{AMISE}}(K)
= \left( \frac{R(K)}{\mu_2(K)^2 R\left(p^{(2)}\right)}\right)^{\frac{1}{5}}n^{-\frac{1}{5}}

where the rugosity of the kernel is:

R(K) = \int K(\vect{x})^2\, d\vect{x}

and the second raw moment of the kernel is:

\mu_2(K) = \int \vect{x}^2K(\vect{x})\, d\vect{x} = \sigma_K^2.

In the equation (2), the expression R\left(p^{(2)}\right) is the rugosity of the second derivative of the density probability function p that we wish to approximate. Since, by hypothesis, the true density p is unknown, its second derivative is also unknown. Hence the equation (2) cannot be used directly to compute the bandwidth.

We have ([wand1994] page 67):

R\left(p^{(r)}\right) = (-1)^r\Psi_{2r}

where:

\Psi_r(p)
= \int p^{(r)}p(x)\, dx = \mathbb{E}_\vect{X}\left[p^{(r)}\right].

Therefore:

(3)h_{\operatorname{AMISE}}(K)
= \left( \frac{R(K)}{\mu_2(K)^2\Psi_4(p)}\right)^{\frac{1}{5}}n^{-\frac{1}{5}}

Several methods exist to evaluate the optimal bandwidth h_{\operatorname{AMISE}}(K) based on different approximations of \Psi_4:

  • Silverman’s rule in dimension 1,

  • the plug-in bandwidth selection,

  • Scott’s rule in dimension d.

Efficiency of a kernel

Let K be a kernel. We may be interested if a particular kernel may be able to reduce the estimation error. The efficiency of a kernel is (see [scott2015] page 151):

\operatorname{eff}(k)
= \frac{\sigma_k R(k)}{\sigma_{k_E} R(k_E)}

where k_E is Epanechnikov’s kernel. The AMISE error is proportional to the efficiency (see [scott2015] eq. 6.25 page 151):

\operatorname{AMISE} \propto \operatorname{eff}(k)

The next table presents several kernels available in the library and their associated variance, rugosity and efficiency. We see that the best kernel is Epanechnikov’s kernel. We also see that there is not much difference between the different kernels. This is one of the reasons why the normal kernel is often used.

Kernel

\operatorname{Var}(k)

R(k)

\sigma_k R(k)

\operatorname{eff}(k)

Epanechnikov

0.2000

0.6000

0.2683

100.00 %

Biweight

0.1429

0.7143

0.2700

99.39 %

Quartic

0.1429

0.7143

0.2700

99.39 %

Triweight

0.1111

0.8159

0.2720

98.67 %

Triangular

0.1667

0.6667

0.2722

98.59 %

Normal

1.0000

0.2821

0.2821

95.12 %

Uniform

0.3333

0.5000

0.2887

92.95 %

Logistic

3.2899

0.1667

0.3023

88.76 %

Table 1. Efficiency of several order 2 kernels.

Silverman’s rule (dimension 1)

In this section, we consider a random variable i.e. d = 1. If the density p is normal with standard deviation \sigma, then the term \Psi_4 can be exactly evaluated. By definition, the Silverman rule for the bandwidth is the optimal bandwidth of the AMISE criteria when the true density p is normal (see [silverman1986] page 45):

(4)h^{Silver}(K)
  := h^{p = normal}_{\operatorname{AMISE}}(K)
  = \left( \frac{8\sqrt{\pi} R(K)}{3\mu_2(K)^2}\right)^{\frac{1}{5}}
  \sigma n^{-\frac{1}{5}}.

The Silverman rule is based on the hypothesis that the true density p is close to the normal density, even if the density p is not necessarily normal.

The equation (4) is accurate when the density is not far from a normal one. In the special case where we use the normal kernel, the Silverman rule is (see [silverman1986] eq 3.28 page 45):

(5)h^{Silver}(\mathcal{N})
  = 1.06 \sigma n^{-\frac{1}{5}}.

Choice for the standard deviation

We estimate the standard deviation \sigma by its sample counterpart \hat{\sigma}, evaluated from the sample (x_1, \dots, x_n):

(6)h^{Silver}(K)
  = \left( \frac{8\sqrt{\pi} R(K)}{3\mu_2(K)^2}\right)^{\frac{1}{5}}
  \hat{\sigma} n^{-\frac{1}{5}}

The estimator \hat{\sigma} of the true standard deviation can be estimated using the sample standard deviation based on the sample (x_1, \dots, x_n). This is:

\hat{\sigma}
= \sqrt{\frac{1}{n - 1} \sum_{i = 1}^n (x_i - \bar{x})^2 }

where \bar{x} is the sample mean:

\bar{x}
= \frac{1}{n} \sum_{i = 1}^n x_i.

Another method is to use the standardized interquartile range ([wand1994] page 60):

\widehat{\sigma}_{\operatorname{IQR}}
= \frac{\widehat{q}(3/4) - \widehat{q}(1/4)}{\Phi^{-1}(3/4) - \Phi^{-1}(1/4)}

where \Phi^{-1} is the quantile function of the standard normal distribution and \widehat{q}(3/4) and \widehat{q}(1/4) are the sample quartiles at levels 75% and 25% respectively. The previous estimator is robust against outliers that might be in the sample.

Plug-in bandwidth selection method (dimension 1)

The plug-in bandwidth selection method improves the estimation of the rugosity of the second derivative of the density. Instead of making the Gaussian assumption, the method uses a kernel smoothing method in order to make an approximation of higher derivatives of the density. This method is due to [sheather1991] who used ideas from [park1990]. The algorithm is presented in [wand1994], page 74 under the “Solve the equation rule” name. The implementation uses ideas from [raykar2006], but the fast selection is not implemented.

The equation (3) requires the evaluation of the quantity \Psi_4. We use the estimator \hat{\Psi}_r of \Psi_r, using a kernel density estimator of the r-th derivative of the density. The estimator is (see [wand1994] page 64):

(7)\hat{\Psi}_r(K)
  = \frac{1}{n}\sum_{i=1}^{n} \widehat{p}^{(r)}(X_i)

where \hat{\Psi}_r(K) is the estimator based on the kernel K. Deriving equation (1) leads to:

(8)\widehat{p}^{(r)}(x)
  = \frac{1}{nh^{r+1}}\sum_{i=1}^{n} K^{(r)}\left(\frac{x-X_i}{h}\right)

and then the estimator \hat{\Psi}_r(h) is defined as:

(9)\hat{\Psi}_r(h)
  = \frac{1}{n^2h^{r+1}}\sum_{i=1}^{n}\sum_{j=1}^{n} K^{(r)}\left(\frac{X_i-X_j}{h}\right)

We note that \hat{\Psi}_r(h) depends of the parameter h which can be taken in order to minimize the Asymptotic Mean Square Error (AMSE) criteria evaluated between \Psi_r and \hat{\Psi}_r(h). The optimal parameter h is:

(10)h^{(r)}_{\operatorname{AMSE}}
  = \left(\frac{-2K^{(r)}(0)}{\mu_2(K) \Psi_{r+2}}\right)^{\frac{1}{r+3}}n^{-\frac{1}{r+3}}

The previous equation states that the bandwidth h^{(r)} required to compute \widehat{p}^{(r)} depends on \Psi_{r+2}. But to compute \Psi_{r+2}, we need h^{(r + 2)}, etc. The goal of the method is to break this infinite set of equations at some point by providing a pilot bandwidth. The \ell-stage plug-in bandwidth method uses \ell different intermediate bandwidths before evaluating the final one.

In this document, we present the two stage solve-the-equation plug-in method.

  • The equation (3) defines h_{\operatorname{AMISE}}(K) as a function of \Psi_4. Let t be the function defined by the equation:

    (11)h_{\operatorname{AMSE}}(K) = t(\Psi_4).

  • The term \Psi_4 is approximated by its estimator defined in (9) evaluated with its optimal parameter h^{(4)}_{\operatorname{AMSE}} defined in (10):

    (12)h^{(4)}_{\operatorname{AMSE}}
  = \left(\frac{-2K^{(4)}(0)}{\mu_2(K)\Psi_{6}}\right)^{\frac{1}{7}}n^{-\frac{1}{7}}

    which leads to the approximation:

    (13)\hat{\Psi}_4 \left(h^{(4)}_{\operatorname{AMSE}}\right) \approx  \Psi_4

  • The equation (3) and (12) lead to:

    (14)h^{(4)}_{\operatorname{AMSE}}
  = \left( \frac{-2K^{(4)}(0)\mu_2(K)\Psi_4}{R(K)\Psi_{6}}\right) ^{\frac{1}{7}}h_{\operatorname{AMISE}}(K)^{\frac{5}{7}}.

    Let \ell be the function defined by the equation:

    (15)h^{(4)}_{\operatorname{AMSE}}
  = \ell(h_{\operatorname{AMISE}}(K)).

  • The equation (14) depends on both terms \Psi_4 and \Psi_6 which are evaluated with their estimators defined in (9) respectively with their AMSE optimal parameters g_1 and g_2 (see eq. (10)). It leads to the expressions:

    (16)g_1 & = \left(\frac{-2K^{(4)}(0)}{\mu_2(K)\Psi_{6}}\right)^{\frac{1}{7}}n^{-\frac{1}{7}}\\
g_2 & = \left(\frac{-2K^{(6)}(0)}{\mu_2(K)\Psi_{8}}\right)^{\frac{1}{7}}n^{-\frac{1}{9}}

  • In order to evaluate \Psi_6 and \Psi_8, we assume that the density p is normal with a variance \sigma^2 which is approximated by the empirical variance of the sample, which leads to:

    (17)\hat{\Psi}_6 & = \frac{-15}{16\sqrt{\pi}}\hat{\sigma}^{-7}\\
\hat{\Psi}_8 & = \frac{105^{\strut}}{32\sqrt{\pi}}\hat{\sigma}^{-9}

Finally, thanks to the equations (11), (13), (15), (16) and (17), the optimal bandwidth of the STE rule, h^{\operatorname{STE}}, is solution of the equation:

(18)h^{\operatorname{STE}}
  = t \circ \hat{\Psi}_4 \circ \ell (h^{\operatorname{STE}})

This equation does not necessarily have a close form expression and an numerical method must be used in general.

A cut-off value can be used to define the function \widehat{\psi_r} in the equation (9). Let \phi be the probability density function of the standard Gaussian distribution. We have:

\phi(x) \rightarrow 0

when |x|\rightarrow +\infty, with a fast decrease to zero. Let t> 0 the cut-off value. The evaluation is as follows:

\widetilde{\phi}(x)=
\begin{cases}
\phi(x) & \textrm{ if } |x| \leq t, \\
0 & \textrm{ otherwise}.
\end{cases}

Hence, only the most significant values in the evaluation of \hat{\psi_r} are taken into account, which improves the speed while slightly decreasing the accuracy.

Rescaling a bandwidth

In this section, we show that, if the optimal bandwidth is known for the normal kernel, then it can be computed for any kernel K using a rescaling equation.

Let K_1 and K_2 be two kernels. The equation (2) implies:

(19)\frac{h_{\operatorname{AMISE}}(K_1)}{h_{\operatorname{AMISE}}(K_2)}=\frac{\sigma_{K_2}}{\sigma_{K_1}}
  \left(\frac{\sigma_{K_1}R(K_1)}{\sigma_{K_2}R(K_2)}\right)^{1/5}.

Scott (see [scott2015] table 6.2 page 152) notices that:

\frac{\sigma_{K_1}R(K_1)}{\sigma_{K_2}R(K_2)} \in [1, 1.86]

for many pairs of common kernels. Hence the equation (19) implies the equivalent kernel rescaling equation (see [scott2015] eq. 6.30 page 154):

(20)h_{\operatorname{AMISE}}(K_1) \approx h_{\operatorname{AMISE}}(K_2)\frac{\sigma_{K_2}}{\sigma_{K_1}}

Consider for example the normal kernel K_2 = \mathcal{N}(0,1). Since \sigma_{K_1} = \sigma_{\mathcal{N}(0,1)} = 1, then equation (20) implies:

(21)h_{\operatorname{AMISE}}(K) \approx h_{\operatorname{AMISE}}(\mathcal{N})\frac{1}{\sigma_{K}}

We will use the previous equation in the derivation of the mixed rule presented in the next section. The previous equation applied to the Silverman rule implies:

(22)h^{Silver}(K) \approx h^{Silver}(\mathcal{N})\frac{1}{\sigma_{K}}

A mixed rule for a large sample

When the sample size n is large, the solve-the-equation (STE) rule cannot be applied because of its CPU cost. In this case, we use a method which combines the STE rule and the Silverman rule, which is less costly. Moreover, we combine these rules using two different kernels, namely the kernel K given by the user and the normal kernel \mathcal{N}(0, 1). Finally, we combine two different samples, that is to say the large sample size n and a smaller sample size for which the STE rule can be evaluated.

The equation (3) implies that:

\frac{h_{\operatorname{AMISE}}(K)}{h^{Silver}(K)}
= \left(\frac{\Psi_4(\mathcal{N})}{\Psi_4(p)}\right)^{1/5}

where K is a given kernel and h_{\operatorname{AMISE}}(K) is the optimal AMISE bandwidth for the kernel K. We notice that the previous ratio is independent from the sample size n. Let n_t \ll n be a small sample size. Hence, the ratio is the same if we consider the sample size n or the sample size n_t. We apply this equation to the normal kernel, approximate the AMISE optimal bandwidth by the STE rule and use the sample sizes n and n_t. We get:

\frac{h^{n, STE}(\mathcal{N})}{h^{n, Silver}(\mathcal{N})}
\approx \frac{h^{n_t, STE}(\mathcal{N})}{h^{n_t, Silver}(\mathcal{N})}

which implies:

h^{n, STE}(\mathcal{N})
\approx \frac{h^{n_t, STE}(\mathcal{N})}{h^{n_t, Silver}(\mathcal{N})}
h^{n, Silver}(\mathcal{N})

The equation (21) leads to the bandwidth of the STE rule for the kernel K:

h^{n, STE}(K)
\approx h^{n, STE}(\mathcal{N}) \frac{1}{\sigma_{K}}.

We substitute the expression of h^{n, STE} in the previous equation and get the mixed rule:

(23)h^{n, STE}(K)
\approx \frac{h^{n_t, STE}(\mathcal{N})}{h^{n_t, Silver}(\mathcal{N})}
h^{n, Silver}(\mathcal{N}) \frac{1}{\sigma_{K}}.

Scott rule (dimension d)

In this section, we consider the general case where the random vector has dimension d. The Scott rule is a simplification of the Silverman rule generalized to the dimension d which is optimal when the density p is normal with independent components. In all the other cases, it gives an empirical rule that gives good result when the density p is not far from the normal one. For examples, the Scott bandwidth may appear too large when p presents several maximum.

The Silverman rule given in dimension 1 in equation (4) can be generalized in dimension d as follows. We make the assumption that the density p is normal with independent components, in dimension d and that we use the normal kernel \mathcal{N}(0,1) to estimate it. Therefore the optimal bandwidth vector \vect{h} with respect to the AMISE criteria is given by the normal reference rule (see [scott2015] eq. 6.43 page 164):

(24)\vect{h}^{Silver}(\mathcal{N}) = \left(\left(\frac{4}{d+2}\right)^{1 / (d + 4)}\hat{\sigma}_i^n n^{-1 / (d + 4)}\right)_i

where \hat{\sigma}_i^n is the standard deviation of the i-th component of the sample (\vect{X}_1, \cdots, \vect{X}_n), and \sigma_K the standard deviation of the 1D kernel K.

Scott’ method is a simplification of Silverman’s rule, based on the fact that the coefficient \left(\frac{4}{d+2}\right)^{1 / (d + 4)} remains in [0.924, 1.059] when the dimension d varies (see [scott2015] page 164). Thus, Scott fixed it to 1:

(25)\left(\frac{4}{d+2}\right)^{1 / (d + 4)} \approx 1.

This leads to Scott’s rule (see [scott2015] eq. 6.44 page 164):

(26)\vect{h}^{Silver}(\mathcal{N}) \approx \left( \hat{\sigma}_i^n n^{-1 / (d + 4)}\right)_i

Finally, the equations (26) and (22) applied in each direction i imply:

(27)\vect{h}^{Scott}
  = \left(\frac{\hat{\sigma}_i^n}{\sigma_K}n^{-1 / (d + 4)}\right)_i

for i = 1, ..., d.

Boundary treatment

In this section, we consider a random variable i.e. d = 1. Assume that the domain of definition of the density is bounded. Then one of the problems of kernel smoothing is that it may produce a non zero density estimate even in the regions where we know it is zero. This is known as the boundary bias problem (see [silverman1986] page 29). The reason is that a subpart of the kernel windows does not contain any observation ([wand1994] page 127). In this case, for some observation x_i near the boundary, the density may be underestimated if the kernel sets a positive weight outside the domain ([chacon2018] page 73).

There are several methods to solve this problem. One of the methods is to apply a transformation to the data (see [chacon2018] page 73). Another method is to use boundary kernels (see [chacon2018] page 76, [scott2015] page 157).

In dimension 1, the boundary effects may be taken into account using a reflection or mirroring method (see [silverman1982] page 31). the boundaries are automatically detected from the sample (with the min and max functions) and the kernel smoothed PDF is corrected in the boundary areas to remain within the boundaries, according to the mirroring technique:

  • the Scott bandwidth is evaluated from the sample: h

  • two sub-samples are extracted from the initial sample, containing all the points within the range [min, min + h[ and ]max-h, max],

  • both sub-samples are transformed into their symmetric samples with respect their respective boundary: its results two samples within the range ]min-h, min] and [max, max+h[,

  • a kernel smoothed PDF is built from the new sample composed with the initial one and the two new ones, with the previous bandwidth h,

  • this last kernel smoothed PDF is truncated within the initial range [min, max] (conditional PDF).

Conclusion

The next table presents a summary of histogram, kernel smoothing and parametric methods. It appears that the kernel density estimator has an AMISE error which is quite close to the parametric rate.

Method

Optimal \operatorname{AMISE}

Histogram

\propto n^{-\frac{2}{3}}

Kernel smoothing

\propto n^{-\frac{4}{5}}

Parametric

\propto n^{-1}

Table 2. The AMISE error depending on the method to estimate the density, from the least to the most accurate.

The next table compare the different estimators of the bandwidth that we have presented so far. The best method is the STE rule, but this can be costly to evaluate if the sample is large. In this case the mixed rule can be used. If this rule is still too large, then the Silverman rule can be used and might be satisfactory if the true density p is not too far away from the normal distribution (i.e. unimodal and symmetric). Otherwise, the Silverman rule may produce a too large bandwidth, leading to oversmoothing.

Method

Assumption

Cost

Accuracy

Silverman

Normal assumption

Low

If p not far from normal

Mixed

Normal assumption

Moderate

Intermediate

Solve-the-equation (STE)

Normal assumption

High

Accurate

Table 3. Different estimators of the bandwidth from the least to the most accurate.