NAG Library Routine Document

1Purpose

g10bbf performs kernel density estimation using a Gaussian kernel.

2Specification

Fortran Interface
 Subroutine g10bbf ( n, x, slo, shi, ns, t,
 Integer, Intent (In) :: n, wtype, ns, fcall Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(n) Real (Kind=nag_wp), Intent (Inout) :: window, slo, shi, rcomm(ns+20) Real (Kind=nag_wp), Intent (Out) :: smooth(ns), t(ns)
#include nagmk26.h
 void g10bbf_ (const Integer *n, const double x[], const Integer *wtype, double *window, double *slo, double *shi, const Integer *ns, double smooth[], double t[], const Integer *fcall, double rcomm[], Integer *ifail)

3Description

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, $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:
 $∫-∞∞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.

4References

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
Silverman B W (1990) Density Estimation Chapman and Hall

5Arguments

1:     $\mathbf{n}$ – IntegerInput
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) arrayInput
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}$ – IntegerInput
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
 $h=m× 0.9× minq75-q25,σ n0.2$
where ${q}_{75}-{q}_{25}$ is the inter-quartile 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 "rule-of-thumb" 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:
 $a = minixi-slo×h b = maxixi+slo×h$
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.
On exit: $b$, the upper limit actually used.
7:     $\mathbf{ns}$ – IntegerInput
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) arrayOutput
On exit: $\stackrel{^}{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) arrayOutput
On exit: ${t}_{\mathit{l}}$, for $\mathit{l}=1,2,\dots ,{n}_{s}$, the points at which the estimate is calculated.
10:   $\mathbf{fcall}$ – IntegerInput
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) arrayCommunication 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}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation 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, because for this routine the values of the output arguments may be useful even if ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}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).

6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Note: g10bbf may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{ifail}}=11$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>0$.
${\mathbf{ifail}}=12$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
On entry at previous call, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{fcall}}=0$, n must be unchanged since previous call.
${\mathbf{ifail}}=31$
On entry, ${\mathbf{wtype}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{wtype}}=1$ or $2$.
${\mathbf{ifail}}=41$
On entry, ${\mathbf{window}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{window}}>0.0$.
${\mathbf{ifail}}=51$
On entry, ${\mathbf{slo}}=〈\mathit{\text{value}}〉$.
On exit from previous call, ${\mathbf{slo}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{fcall}}=0$, slo must be unchanged since previous call.
${\mathbf{ifail}}=61$
On entry, ${\mathbf{slo}}=〈\mathit{\text{value}}〉$ and ${\mathbf{shi}}=〈\mathit{\text{value}}〉$.
On entry, $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{x}}\right)=〈\mathit{\text{value}}〉$ and $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{x}}\right)=〈\mathit{\text{value}}〉$.
Expected values of at least $〈\mathit{\text{value}}〉$ and $〈\mathit{\text{value}}〉$ for slo and shi.
All output values have been returned.
${\mathbf{ifail}}=62$
On entry, ${\mathbf{shi}}=〈\mathit{\text{value}}〉$.
On exit from previous call, ${\mathbf{shi}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{fcall}}=0$, shi must be unchanged since previous call.
${\mathbf{ifail}}=71$
On entry, ${\mathbf{ns}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ns}}\ge 2$.
${\mathbf{ifail}}=74$
On entry, ${\mathbf{ns}}=〈\mathit{\text{value}}〉$.
On entry at previous call, ${\mathbf{ns}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{fcall}}=0$, ns must be unchanged since previous call.
${\mathbf{ifail}}=101$
On entry, ${\mathbf{fcall}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{fcall}}=0$ or $1$.
${\mathbf{ifail}}=111$
rcomm has been corrupted between calls.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

7Accuracy

See Jones and Lotwick (1984) for a discussion of the accuracy of this method.

8Parallelism and Performance

g10bbf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g10bbf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
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 implementation-specific information.

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.

10Example

Data is read from a file and the density estimated. The first $20$ values are then printed.

10.1Program Text

Program Text (g10bbfe.f90)

10.2Program Data

Program Data (g10bbfe.d)

10.3Program Results

Program Results (g10bbfe.r)

This plot shows the estimated density function for the example data for several window widths.
© The Numerical Algorithms Group Ltd, Oxford, UK. 2017