NAG Library Routine Document

g10bbf  (kerndens_gauss)

 Contents

    1  Purpose
    7  Accuracy

1
Purpose

g10bbf performs kernel density estimation using a Gaussian kernel.

2
Specification

Fortran Interface
Subroutine g10bbf ( n, x, wtype, window, slo, shi, ns, smooth, t, fcall, rcomm, ifail)
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)
C Header Interface
#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)

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
© The Numerical Algorithms Group Ltd, Oxford, UK. 2017