G10 Chapter Contents
G10 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG10BAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G10BAF performs kernel density estimation using a Gaussian kernel.

## 2  Specification

 SUBROUTINE G10BAF ( N, X, WINDOW, SLO, SHI, NS, SMOOTH, T, USEFFT, FFT, IFAIL)
 INTEGER N, NS, IFAIL REAL (KIND=nag_wp) X(N), WINDOW, SLO, SHI, SMOOTH(NS), T(NS), FFT(NS) LOGICAL USEFFT

## 3  Description

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, $\stackrel{^}{f}\left(x\right)$, may be required. The simplest form of density estimator is the histogram. This may be defined by:
 $f^ x = 1nh nj , a + j-1 h < x < a + j h , j=1,2,…,ns ,$
where ${n}_{j}$ is the number of observations falling in the interval $a+\left(j-1\right)h$ to $a+jh$, $a$ is the lower bound to the histogram and $b={n}_{s}h$ is the upper bound. 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:
 $∫-∞∞Ktdt=1 and Kt≥0.$
The kernel density estimator is then defined as
 $f^x=1nh ∑i= 1nK x-xih .$
The choice of $K$ is usually not important but to ease the computational burden use can be made of the Gaussian kernel defined as
 $Kt=12πe-t2/2.$
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 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 $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/\left(b-a\right)$. (iv) Find the inverse FFT of ${\zeta }_{l}$ to give $\stackrel{^}{f}\left(x\right)$.
To compute the kernel density estimate for further values of $h$ only steps (iii) and (iv) need be repeated.

## 4  References

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

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the number of observations in the sample.
Constraint: ${\mathbf{N}}>0$.
2:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ observations, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3:     WINDOW – REAL (KIND=nag_wp)Input
On entry: $h$, the window width.
Constraint: ${\mathbf{WINDOW}}>0.0$.
4:     SLO – REAL (KIND=nag_wp)Input
On entry: $a$, 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.
Constraint: ${\mathbf{SLO}}<{\mathbf{SHI}}$.
5:     SHI – REAL (KIND=nag_wp)Input
On entry: $b$, 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, ${n}_{s}$.
Constraints:
• ${\mathbf{NS}}\ge 2$;
• The largest prime factor of NS must not exceed $19$, and the total number of prime factors of NS, counting repetitions, must not exceed $20$.
7:     SMOOTH(NS) – REAL (KIND=nag_wp) arrayOutput
On exit: the ${n}_{s}$ values of the density estimate, $\stackrel{^}{f}\left({t}_{\mathit{l}}\right)$, for $\mathit{l}=1,2,\dots ,{n}_{s}$.
8:     T(NS) – REAL (KIND=nag_wp) arrayOutput
On exit: the points at which the estimate is calculated, ${t}_{\mathit{l}}$, for $\mathit{l}=1,2,\dots ,{n}_{s}$.
9:     USEFFT – LOGICALInput
On entry: must be set to .FALSE. if the values of ${Y}_{l}$ 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. If ${\mathbf{USEFFT}}=\mathrm{.TRUE.}$ then the arguments N, SLO, SHI, NS and FFT must remain unchanged from the previous call to G10BAF with ${\mathbf{USEFFT}}=\mathrm{.FALSE.}$.
10:   FFT(NS) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{USEFFT}}=\mathrm{.TRUE.}$, FFT must contain the fast Fourier transform of the weights of the discretized data, ${\xi }_{\mathit{l}}$, for $\mathit{l}=1,2,\dots ,{n}_{s}$. Otherwise FFT need not be set.
On exit: the fast Fourier transform of the weights of the discretized data, ${\xi }_{\mathit{l}}$, for $\mathit{l}=1,2,\dots ,{n}_{s}$.
11:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. 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 $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}\le 0$, or ${\mathbf{NS}}<2$, or ${\mathbf{SHI}}\le {\mathbf{SLO}}$, or ${\mathbf{WINDOW}}\le 0.0$.
${\mathbf{IFAIL}}=2$
 On entry, G10BAF has been called with ${\mathbf{USEFFT}}=\mathrm{.TRUE.}$ but the routine has not been called previously with ${\mathbf{USEFFT}}=\mathrm{.FALSE.}$, or G10BAF has been called with ${\mathbf{USEFFT}}=\mathrm{.TRUE.}$ but some of the arguments N, SLO, SHI, NS have been changed since the previous call to G10BAF with ${\mathbf{USEFFT}}=\mathrm{.FALSE.}$.
${\mathbf{IFAIL}}=3$
On entry, at least one prime factor of NS is greater than $19$ or NS has more than $20$ prime factors.
${\mathbf{IFAIL}}=4$
On entry, the interval given by SLO to SHI does not extend beyond three window widths at either extreme of the dataset. This may distort the density estimate in some cases.

## 7  Accuracy

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 $n$, while the time for computing the FFT is of order ${n}_{s}\mathrm{log}\left({n}_{s}\right)$, as is the time for computing the inverse of the FFT.

## 9  Example

A sample of $1000$ standard Normal $\left(0,1\right)$ variates are generated using G05SKF and the density estimated on $100$ points with a window width of $0.1$. 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)