Kernel smoothing¶
Kernel smoothing is a non parametric estimation method of the probability density function of a distribution.
Introduction¶
Let be a random variable with probability density function
.
Given a sample of independent observations
of
and any point
, the kernel smoothing estimator provides
an approximation
of
.
In dimension 1, the kernel smoothed probability density function has the following expression,
where K is the univariate kernel, n the sample size and
the univariate random sample with
([wand1994] eq. 2.2 page 12):
(1)¶
The kernel K is a function satisfying:
The parameter h is called the bandwidth.
Multivariate kernels¶
In dimension , the kernel may be defined as a product kernel
,
as follows where
([wand1994] eq. 2.2 page 91):
The kernel smoothed probability density function in dimension d is:
where is the d-variate random sample
which components are denoted
.
In the multivariate case, the bandwidth is the vector
.
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 is
(see [scott2015] eq. page 40, [wand1994] pages 14-21):
It can be proved that the mean squared error is the sum of the variance and the squared bias:
where the bias is:
The MSE depends on the point where the density is evaluated. The mean integrated squared error (MISE) is (see [scott2015] eq. page 41):
Finally, the asymptotic mean integrated squared error (AMISE),
denoted is defined as the two first terms
in the Taylor series expansion of
when
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)¶
where the rugosity of the kernel is:
and the second raw moment of the kernel is:
In the equation (2), the expression is the rugosity of
the second derivative of the density probability function
that
we wish to approximate.
Since, by hypothesis, the true density
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):
where:
Therefore:
(3)¶
Several methods exist to evaluate the optimal bandwidth based on different approximations of
:
Silverman’s rule in dimension 1,
the plug-in bandwidth selection,
Scott’s rule in dimension d.
Efficiency of a kernel¶
Let 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):
where is Epanechnikov’s kernel.
The AMISE error is proportional to the efficiency (see [scott2015]
eq. 6.25 page 151):
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 |
||||
---|---|---|---|---|
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. .
If the density p is normal with standard deviation
,
then the term
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)¶
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)¶
Choice for the standard deviation¶
We estimate the standard deviation by its sample
counterpart
, evaluated from the sample
:
(6)¶
The estimator of the true standard deviation can
be estimated using the sample standard deviation based
on the sample
.
This is:
where is the sample mean:
Another method is to use the standardized interquartile range ([wand1994] page 60):
where is the quantile function of the
standard normal distribution and
and
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 .
We use the estimator
of
, using a kernel
density estimator of the
-th derivative of the
density.
The estimator is (see [wand1994] page 64):
(7)¶
where is the estimator based on the kernel
K.
Deriving equation (1) leads to:
(8)¶
and then the estimator is defined as:
(9)¶
We note that depends of the parameter h which can be
taken in order to minimize the Asymptotic Mean Square Error (AMSE) criteria
evaluated between
and
.
The optimal parameter h is:
(10)¶
The previous equation states that the bandwidth required
to compute
depends on
.
But to compute
, we need
, etc.
The goal of the method is to break this infinite set of equations at some point
by providing a pilot bandwidth.
The
-stage plug-in bandwidth method uses
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
as a function of
. Let
be the function defined by the equation:
(11)¶
The term
is approximated by its estimator defined in (9) evaluated with its optimal parameter
defined in (10):
(12)¶
which leads to the approximation:
(13)¶
The equation (3) and (12) lead to:
(14)¶
Let
be the function defined by the equation:
(15)¶
The equation (14) depends on both terms
and
which are evaluated with their estimators defined in (9) respectively with their AMSE optimal parameters
and
(see eq. (10)). It leads to the expressions:
(16)¶
In order to evaluate
and
, we assume that the density p is normal with a variance
which is approximated by the empirical variance of the sample, which leads to:
(17)¶
Finally, thanks to the equations (11), (13), (15), (16) and (17),
the optimal bandwidth of the STE rule, , is solution of the equation:
(18)¶
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 in the equation (9).
Let
be the probability density function of the standard Gaussian distribution.
We have:
when , with a fast decrease to zero.
Let
the cut-off value.
The evaluation is as follows:
Hence, only the most significant values in the evaluation of
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 and
be two kernels.
The equation (2) implies:
(19)¶
Scott (see [scott2015] table 6.2 page 152) notices that:
for many pairs of common kernels. Hence the equation (19) implies the equivalent kernel rescaling equation (see [scott2015] eq. 6.30 page 154):
(20)¶
Consider for example the normal kernel .
Since
,
then equation (20) implies:
(21)¶
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)¶
A mixed rule for a large sample¶
When the sample size 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
.
Finally, we combine two different samples, that is to say the
large sample size
and a smaller sample size for which the
STE rule can be evaluated.
The equation (3) implies that:
where K is a given kernel and is the
optimal AMISE bandwidth for the kernel K.
We notice that the previous ratio is independent from the sample
size
.
Let
be a small sample size.
Hence, the ratio is the same if we consider the sample size
or the sample size
.
We apply this equation to the normal kernel, approximate the
AMISE optimal bandwidth by the STE rule and use the sample
sizes
and
.
We get:
which implies:
The equation (21) leads to the bandwidth of the STE rule for the kernel K:
We substitute the expression of in the
previous equation and get the mixed rule:
(23)¶
Scott rule (dimension d)¶
In this section, we consider the general case where the
random vector has dimension .
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
to estimate it.
Therefore the optimal bandwidth vector
with respect to the
AMISE criteria is given by the normal reference rule (see [scott2015] eq.
6.43 page 164):
(24)¶
where is the standard deviation of the i-th component of the sample
, and
the standard deviation of the 1D kernel K.
Scott’ method is a simplification of Silverman’s rule, based on the fact that the coefficient
remains in
when the dimension d varies (see [scott2015] page 164).
Thus, Scott fixed it to 1:
(25)¶
This leads to Scott’s rule (see [scott2015] eq. 6.44 page 164):
(26)¶
Finally, the equations (26) and (22) applied in each direction i imply:
(27)¶
for .
Boundary treatment¶
In this section, we consider a random variable i.e. .
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
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, [jones1993]). 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
and
,
both sub-samples are transformed into their symmetric samples with respect their respective boundary: its results two samples within the range
and
,
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
(conditional PDF).
Log-transform treatment¶
In this section, we consider a random variable i.e. . This treatment is highly suited to skewed distributions,
which are all challenging for kernel smoothing. See [charpentier2015] to get more details.
Let be some independent random variates, identically distributed according to
.
The log-transform treatment maps each into
as follows:
where
is the skewness of
with
,
and
the shift scale.
Once a kernel smoothed distribution has been fitted on the transformed data, the fitted distribution of
is built as
where
is distributed according to the kernel smoothed distribution.
Given a sample from
, we denote by
,
and
for
.
We build the kernel smoothing distribution of
using
which pdf is
and cdf
.
We recover the pdf and cdf of as follows:
We note that this transformation also embeds a treatment of the boundaries as the finite lower bound in case of positive skewness
or the finite upper bound in case of negative skewness is rejected to infinity. Thus, there is no more boundary effect on the
-sample.
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 |
---|---|
Histogram |
|
Kernel smoothing |
|
Parametric |
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.