Given a sample of
$n$ observations,
${x}_{1},{x}_{2},\dots ,{x}_{n}$, from a distribution with unknown density function,
$f\left(x\right)$, an estimate of the density function,
$\hat{f}\left(x\right)$, may be required. The simplest form of density estimator is the histogram. This may be defined by:
where
${n}_{j}$ is the number of observations falling in the interval
$a+(j1)h$ to
$a+jh$,
$a$ is the lower bound to the histogram,
$b={n}_{s}h$ is the upper bound and
${n}_{s}$ is the total number of intervals. The value
$h$ is known as the window width. To produce a smoother density estimate a kernel method can be used. A kernel function,
$K\left(t\right)$, satisfies the conditions:
The kernel density estimator is then defined as
The choice of
$K$ 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
$h$. The larger the value of
$h$ the smoother the density estimate. The value of
$h$ can be chosen by examining plots of the smoothed density for different values of
$h$ or by using crossvalidation 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
$a$ to
$b$ the following steps are required.

(i)Discretize the data to give ${n}_{s}$ equally spaced points ${t}_{l}$ with weights ${\xi}_{l}$ (see Jones and Lotwick (1984)).

(ii)Compute the FFT of the weights ${\xi}_{l}$ to give ${Y}_{l}$.

(iii)Compute ${\zeta}_{l}={e}^{\frac{1}{2}{h}^{2}{s}_{l}^{2}}{Y}_{l}$ where ${s}_{l}=2\pi l/(ba)$.

(iv)Find the inverse FFT of ${\zeta}_{l}$ to give $\hat{f}\left(x\right)$.
To compute the kernel density estimate for further values of
$h$ 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:
$\mathbf{n}$ – Integer
Input

On entry:
$n$, the number of observations in the sample.
If
${\mathbf{fcall}}=0$,
n must be unchanged since the last call to
g10bbf.
Constraint:
${\mathbf{n}}>0$.

2:
$\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) array
Input

On entry:
${x}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,n$.
If
${\mathbf{fcall}}=0$,
x must be unchanged since the last call to
g10bbf.

3:
$\mathbf{wtype}$ – Integer
Input

On entry: how the window width,
$h$, is to be calculated:
 ${\mathbf{wtype}}=1$
 $h$ is supplied in window.
 ${\mathbf{wtype}}=2$
 $h$ is to be calculated from the data, with
where ${q}_{75}{q}_{25}$ is the interquartile range and $\sigma $ the standard deviation of the sample, $x$, and $m$ is a multipler supplied in window. The $25\%$ and $75\%$ quartiles, ${q}_{25}$ and ${q}_{75}$, are calculated using g01amf. This is the "ruleofthumb" suggested by Silverman (1990).
Suggested value:
${\mathbf{wtype}}=2$ and ${\mathbf{window}}=1.0$.
Constraint:
${\mathbf{wtype}}=1$ or $2$.

4:
$\mathbf{window}$ – Real (Kind=nag_wp)
Input/Output

On entry: if ${\mathbf{wtype}}=1$, then $h$, the window width. Otherwise, $m$, the multiplier used in the calculation of $h$.
Suggested value:
${\mathbf{window}}=1.0$ and ${\mathbf{wtype}}=2$.
On exit: $h$, the window width actually used.
Constraint:
${\mathbf{window}}>0.0$.

5:
$\mathbf{slo}$ – Real (Kind=nag_wp)
Input/Output

On entry: if
${\mathbf{slo}}<{\mathbf{shi}}$ then
$a$, the lower limit of the interval on which the estimate is calculated. Otherwise,
$a$ and
$b$, the lower and upper limits of the interval, are calculated as follows:
where
$h$ is the window width.
For most applications $a$ should be at least three window widths below the lowest data point.
If
${\mathbf{fcall}}=0$,
slo must be unchanged since the last call to
g10bbf.
Suggested value:
${\mathbf{slo}}=3.0$ and ${\mathbf{shi}}=0.0$ which would cause $a$ and $b$ to be set $3$ window widths below and above the lowest and highest data points respectively.
On exit: $a$, the lower limit actually used.

6:
$\mathbf{shi}$ – Real (Kind=nag_wp)
Input/Output

On entry: if
${\mathbf{slo}}<{\mathbf{shi}}$ then
$b$, the upper limit of the interval on which the estimate is calculated. Otherwise a value for
$b$ is calculated from the data as stated in the description of
slo and the value supplied in
shi is not used.
For most applications $b$ should be at least three window widths above the highest data point.
If
${\mathbf{fcall}}=0$,
shi must be unchanged since the last call to
g10bbf.
Suggested value:
${\mathbf{shi}}=0.0$ and ${\mathbf{slo}}=3.0$ which would cause $a$ and $b$ to be set $3$ window widths below and above the lowest and highest data points respectively.
On exit: $b$, the upper limit actually used.

7:
$\mathbf{ns}$ – Integer
Input

On entry:
${n}_{s}$, the number of points at which the estimate is calculated.
If
${\mathbf{fcall}}=0$,
ns must be unchanged since the last call to
g10bbf.
Suggested value:
${\mathbf{ns}}=512$.
Constraint:
${\mathbf{ns}}\ge 2$.

8:
$\mathbf{smooth}\left({\mathbf{ns}}\right)$ – Real (Kind=nag_wp) array
Output

On exit: $\hat{f}\left({t}_{\mathit{l}}\right)$, for $\mathit{l}=1,2,\dots ,{n}_{s}$, the ${n}_{s}$ values of the density estimate.

9:
$\mathbf{t}\left({\mathbf{ns}}\right)$ – Real (Kind=nag_wp) array
Output

On exit: ${t}_{\mathit{l}}$, for $\mathit{l}=1,2,\dots ,{n}_{s}$, the points at which the estimate is calculated.

10:
$\mathbf{fcall}$ – Integer
Input

On entry: if
${\mathbf{fcall}}=1$ then the values of
${Y}_{l}$ are to be calculated by this call to
g10bbf, otherwise it is assumed that the values of
${Y}_{l}$ were calculated by a previous call to this routine and the relevant information is stored in
rcomm.
Constraint:
${\mathbf{fcall}}=0$ or $1$.

11:
$\mathbf{rcomm}\left({\mathbf{ns}}+20\right)$ – Real (Kind=nag_wp) array
Communication Array

On entry: communication array, used to store information between calls to
g10bbf.
If
${\mathbf{fcall}}=0$,
rcomm must be unchanged since the last call to
g10bbf.
On exit: the last
ns elements of
rcomm contain the fast Fourier transform of the weights of the discretized data, that is
${\mathbf{rcomm}}\left(\mathit{l}+20\right)={Y}_{\mathit{l}}$, for
$\mathit{l}=1,2,\dots ,{n}_{s}$.

12:
$\mathbf{ifail}$ – Integer
Input/Output

On entry:
ifail must be set to
$0$,
$\mathrm{1}$ or
$1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $\mathrm{1}$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value
$\mathrm{1}$ or
$1$ is recommended. If message printing is undesirable, then the value
$1$ is recommended. Otherwise, the value
$\mathrm{1}$ is recommended since useful values can be provided in some output arguments even when
${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit.
When the value $\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit:
${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see
Section 6).
If on entry
${\mathbf{ifail}}=0$ or
$\mathrm{1}$, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
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 routine. Please also consult the
Users' Note for your implementation for any additional implementationspecific information.
Data is read from a file and the density estimated. The first $20$ values are then printed.
This plot shows the estimated density function for the example data for several window widths.