NAG Library Routine Document
G10BAF performs kernel density estimation using a Gaussian kernel.
|SUBROUTINE G10BAF (
||N, X, WINDOW, SLO, SHI, NS, SMOOTH, T, USEFFT, FFT, IFAIL)
||N, NS, IFAIL
||X(N), WINDOW, SLO, SHI, SMOOTH(NS), T(NS), FFT(NS)
Given a sample of
, 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:
is the number of observations falling in the interval
is the lower bound to the histogram and
is the upper bound. 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)
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
the following steps are required.
||Discretize the data to give equally spaced points with weights (see Jones and Lotwick (1984)).
||Compute the FFT of the weights to give .
||Compute where .
||Find the inverse FFT of to give .
To compute the kernel density estimate for further values of
only steps (iii)
need be repeated.
Jones M C and Lotwick H W (1984) Remark AS R50. A remark on algorithm AS 176 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
Silverman B W (1990) Density Estimation Chapman and Hall
- 1: N – INTEGERInput
On entry: , the number of observations in the sample.
- 2: X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the observations,
, for .
- 3: WINDOW – REAL (KIND=nag_wp)Input
On entry: , the window width.
- 4: SLO – REAL (KIND=nag_wp)Input
, the lower limit of the interval on which the estimate is calculated. For most applications SLO
should be at least three window widths below the lowest data point.
- 5: SHI – REAL (KIND=nag_wp)Input
, the upper limit of the interval on which the estimate is calculated. For most applications SHI
should be at least three window widths above the highest data point.
- 6: NS – INTEGERInput
On entry: the number of points at which the estimate is calculated, .
- The largest prime factor of NS must not exceed , and the total number of prime factors of NS, counting repetitions, must not exceed .
- 7: SMOOTH(NS) – REAL (KIND=nag_wp) arrayOutput
On exit: the values of the density estimate,
, for .
- 8: T(NS) – REAL (KIND=nag_wp) arrayOutput
On exit: the points at which the estimate is calculated,
, for .
- 9: USEFFT – LOGICALInput
: must be set to .FALSE. if the values of
are to be calculated by G10BAF and to .TRUE. if they have been computed by a previous call to G10BAF and are provided in FFT
then the arguments N
must remain unchanged from the previous call to G10BAF with
- 10: FFT(NS) – REAL (KIND=nag_wp) arrayInput/Output
must contain the fast Fourier transform of the weights of the discretized data,
. Otherwise FFT
need not be set.
On exit: the fast Fourier transform of the weights of the discretized data,
, for .
- 11: IFAIL – INTEGERInput/Output
must be set to
. If you are unfamiliar with this parameter you should refer to Section 3.3
in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
. When the value is used it is essential to test the value of IFAIL on exit.
unless the routine detects an error or a warning has been flagged (see Section 6
6 Error Indicators and Warnings
If on entry
, explanatory error messages are output on the current error message unit (as defined by X04AAF
Errors or warnings detected by the routine:
|On entry,||G10BAF has been called with but the routine has not been called previously with ,|
|or||G10BAF has been called with but some of the arguments N, SLO, SHI, NS have been changed since the previous call to G10BAF with .|
On entry, at least one prime factor of NS
is greater than
has more than
On entry, the interval given by SLO
does not extend beyond three window widths at either extreme of the dataset. This may distort the density estimate in some cases.
See Jones and Lotwick (1984)
for a discussion of the accuracy of this method.
The time for computing the weights of the discretized data is of order
, while the time for computing the FFT
is of order
, as is the time for computing the inverse of the FFT
A sample of
variates are generated using G05SKF
and the density estimated on
points with a window width of
. The resulting estimate of the density function is plotted using G01AGF
9.1 Program Text
Program Text (g10bafe.f90)
9.2 Program Data
Program Data (g10bafe.d)
9.3 Program Results
Program Results (g10bafe.r)