Given a sample of
observations,
, from a distribution with unknown density function,
, an estimate of the density function,
, may be required. The simplest form of density estimator is the histogram. This may be defined by:
where
is the number of observations falling in the interval
to
,
is the lower bound to the histogram,
is the upper bound and
is the total number of intervals. The value
is known as the window width. To produce a smoother density estimate a kernel method can be used. A kernel function,
, satisfies the conditions:
The kernel density estimator is then defined as
The choice of
is usually not important but to ease the computational burden use can be made of the Gaussian kernel defined as
The smoothness of the estimator depends on the window width
. The larger the value of
the smoother the density estimate. The value of
can be chosen by examining plots of the smoothed density for different values of
or by using cross-validation methods (see
Silverman (1990)).
Silverman (1982) and
Silverman (1990) show how the Gaussian kernel density estimator can be computed using a fast Fourier transform (FFT). In order to compute the kernel density estimate over the range
to
the following steps are required.
(i) |
Discretize the data to give equally spaced points with weights (see Jones and Lotwick (1984)). |
(ii) |
Compute the FFT of the weights to give . |
(iii) |
Compute where . |
(iv) |
Find the inverse FFT of to give . |
To compute the kernel density estimate for further values of
only steps
(iii) and
(iv) need be repeated.
Jones M C and Lotwick H W (1984) Remark AS R50. A remark on algorithm AS 176. Kernel density estimation using the Fast Fourier Transform Appl. Statist. 33 120–122
Silverman B W (1982) Algorithm AS 176. Kernel density estimation using the fast Fourier transform Appl. Statist. 31 93–99
- 1:
– IntegerInput
-
On entry:
, the number of observations in the sample.
If
,
n must be unchanged since the last call to
nag_kernel_density_gauss (g10bbc).
Constraint:
.
- 2:
– const doubleInput
-
On entry:
, for
.
If
,
x must be unchanged since the last call to
nag_kernel_density_gauss (g10bbc).
- 3:
– Nag_WindowTypeInput
-
On entry: how the window width,
, is to be calculated:
- is supplied in window.
- is to be calculated from the data, with
where is the inter-quartile range and the standard deviation of the sample, , and is a multipler supplied in window. The and quartiles, and , are calculated using nag_double_quantiles (g01amc). This is the "rule-of-thumb" suggested by Silverman (1990).
Suggested value:
and .
Constraint:
or .
- 4:
– double *Input/Output
-
On entry: if , then , the window width. Otherwise, , the multiplier used in the calculation of .
Suggested value:
and .
On exit: , the window width actually used.
Constraint:
.
- 5:
– double *Input/Output
-
On entry: if
then
, the lower limit of the interval on which the estimate is calculated. Otherwise,
and
, the lower and upper limits of the interval, are calculated as follows:
where
is the window width.
For most applications should be at least three window widths below the lowest data point.
If
,
slo must be unchanged since the last call to
nag_kernel_density_gauss (g10bbc).
Suggested value:
and which would cause and to be set window widths below and above the lowest and highest data points respectively.
On exit: , the lower limit actually used.
- 6:
– double *Input/Output
-
On entry: if
then
, the upper limit of the interval on which the estimate is calculated. Otherwise a value for
is calculated from the data as stated in the description of
slo and the value supplied in
shi is not used.
For most applications should be at least three window widths above the highest data point.
If
,
shi must be unchanged since the last call to
nag_kernel_density_gauss (g10bbc).
On exit: , the upper limit actually used.
- 7:
– IntegerInput
-
On entry:
, the number of points at which the estimate is calculated.
If
,
ns must be unchanged since the last call to
nag_kernel_density_gauss (g10bbc).
Suggested value:
.
Constraint:
.
- 8:
– doubleOutput
-
On exit: , for , the values of the density estimate.
- 9:
– doubleOutput
-
On exit: , for , the points at which the estimate is calculated.
- 10:
– Nag_BooleanInput
-
On entry: if
then the values of
are to be calculated by this call to
nag_kernel_density_gauss (g10bbc), otherwise it is assumed that the values of
were calculated by a previous call to this routine and the relevant information is stored in
rcomm.
- 11:
– doubleCommunication Array
-
On entry: communication array, used to store information between calls to
nag_kernel_density_gauss (g10bbc).
If
,
rcomm must be unchanged since the last call to
nag_kernel_density_gauss (g10bbc).
On exit: the last
ns elements of
rcomm contain the fast Fourier transform of the weights of the discretized data, that is
, for
.
- 12:
– NagError *Input/Output
-
The NAG error argument (see
Section 3.7 in How to Use the NAG Library and its Documentation).
See
Jones and Lotwick (1984) for a discussion of the accuracy of this method.
Please consult the
x06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
Data is read from a file and the density estimated. The first values are then printed.
This plot shows the estimated density function for the example data for several window widths.