G10BBF (PDF version)
G10 Chapter Contents
G10 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G10BBF

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.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

G10BBF performs kernel density estimation using a Gaussian kernel.

2  Specification

SUBROUTINE G10BBF ( N, X, WTYPE, WINDOW, SLO, SHI, NS, SMOOTH, T, FCALL, RCOMM, IFAIL)
INTEGER  N, WTYPE, NS, FCALL, IFAIL
REAL (KIND=nag_wp)  X(N), WINDOW, SLO, SHI, SMOOTH(NS), T(NS), RCOMM(NS+20)

3  Description

Given a sample of n observations, x1,x2,,xn, from a distribution with unknown density function, fx, an estimate of the density function, f^x, 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 nj is the number of observations falling in the interval a+j-1h to a+jh, a is the lower bound to the histogram, b=nsh is the upper bound and ns 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, Kt, satisfies the conditions:
-Ktdt=1  and  Kt0.  
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 ns equally spaced points tl with weights ξl (see Jones and Lotwick (1984)).
(ii) Compute the FFT of the weights ξl to give Yl.
(iii) Compute ζl=e-12h2sl2Yl where sl=2πl/b-a.
(iv) Find the inverse FFT of ζl to give f^x.
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. 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

5  Arguments

1:     N – INTEGERInput
On entry: n, the number of observations in the sample.
If FCALL=0, N must be unchanged since the last call to G10BBF.
Constraint: N>0.
2:     XN – REAL (KIND=nag_wp) arrayInput
On entry: xi, for i=1,2,,n.
If FCALL=0, X must be unchanged since the last call to G10BBF.
3:     WTYPE – INTEGERInput
On entry: how the window width, h, is to be calculated:
WTYPE=1
h is supplied in WINDOW.
WTYPE=2
h is to be calculated from the data, with
h=m× 0.9× minq75-q25,σ n0.2  
where q75-q25 is the inter-quartile range and σ the standard deviation of the sample, x, and m is a multipler supplied in WINDOW. The 25% and 75% quartiles, q25 and q75, are calculated using G01AMF. This is the "rule-of-thumb" suggested by Silverman (1990).
Suggested value: WTYPE=2 and WINDOW=1.0.
Constraint: WTYPE=1 or 2.
4:     WINDOW – REAL (KIND=nag_wp)Input/Output
On entry: if WTYPE=1, then h, the window width. Otherwise, m, the multiplier used in the calculation of h.
Suggested value: WINDOW=1.0 and WTYPE=2.
On exit: h, the window width actually used.
Constraint: WINDOW>0.0.
5:     SLO – REAL (KIND=nag_wp)Input/Output
On entry: if SLO<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 FCALL=0, SLO must be unchanged since the last call to G10BBF.
Suggested value: SLO=3.0 and 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:     SHI – REAL (KIND=nag_wp)Input/Output
On entry: if SLO<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 FCALL=0, SHI must be unchanged since the last call to G10BBF.
On exit: b, the upper limit actually used.
7:     NS – INTEGERInput
On entry: ns, the number of points at which the estimate is calculated.
If FCALL=0, NS must be unchanged since the last call to G10BBF.
Suggested value: NS=512.
Constraint: NS2.
8:     SMOOTHNS – REAL (KIND=nag_wp) arrayOutput
On exit: f^tl, for l=1,2,,ns, the ns values of the density estimate.
9:     TNS – REAL (KIND=nag_wp) arrayOutput
On exit: tl, for l=1,2,,ns, the points at which the estimate is calculated.
10:   FCALL – INTEGERInput
On entry: if FCALL=1 then the values of Yl are to be calculated by this call to G10BBF, otherwise it is assumed that the values of Yl were calculated by a previous call to this routine and the relevant information is stored in RCOMM.
Constraint: FCALL=0 or 1.
11:   RCOMMNS+20 – REAL (KIND=nag_wp) arrayCommunication Array
On entry: communication array, used to store information between calls to G10BBF.
If 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 RCOMMl+20=Yl, for l=1,2,,ns.
12:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 IFAIL0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry 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:
IFAIL=11
On entry, N=value.
Constraint: N>0.
IFAIL=12
On entry, N=value.
On entry at previous call, N=value.
Constraint: if FCALL=0, N must be unchanged since previous call.
IFAIL=31
On entry, WTYPE=value.
Constraint: WTYPE=1 or 2.
IFAIL=41
On entry, WINDOW=value.
Constraint: WINDOW>0.0.
IFAIL=51
On entry, SLO=value.
On exit from previous call, SLO=value.
Constraint: if FCALL=0, SLO must be unchanged since previous call.
IFAIL=61
On entry, SLO=value and SHI=value.
On entry, minX=value and maxX=value.
Expected values of at least value and value for SLO and SHI.
All output values have been returned.
IFAIL=62
On entry, SHI=value.
On exit from previous call, SHI=value.
Constraint: if FCALL=0, SHI must be unchanged since previous call.
IFAIL=71
On entry, NS=value.
Constraint: NS2.
IFAIL=74
On entry, NS=value.
On entry at previous call, NS=value.
Constraint: if FCALL=0, NS must be unchanged since previous call.
IFAIL=101
On entry, FCALL=value.
Constraint: FCALL=0 or 1.
IFAIL=111
RCOMM has been corrupted between calls.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
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.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

7  Accuracy

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

8  Parallelism 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.

9  Further Comments

The time for computing the weights of the discretized data is of order n, while the time for computing the FFT is of order nslogns, as is the time for computing the inverse of the FFT.

10  Example

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

10.1  Program Text

Program Text (g10bbfe.f90)

10.2  Program Data

Program Data (g10bbfe.d)

10.3  Program Results

Program Results (g10bbfe.r)

This plot shows the estimated density function for the example data for several window widths.
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 0 0.1 0.2 0.3 0.4 0.5 0.6 −6 −5 −4 −3 −2 −1 0 1 2 3 4 5 Density Estimate t Example Program Gaussian Kernel Density Estimation gnuplot_plot_1 window = 0.0941 gnuplot_plot_2 window = 0.1882 gnuplot_plot_3 window = 0.3764 gnuplot_plot_4 window = 0.7528

G10BBF (PDF version)
G10 Chapter Contents
G10 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2016