NAG Library Routine Document

g10baf (withdraw_kerndens_gauss)

 Contents

    1  Purpose
    7  Accuracy

1
Purpose

g10baf performs kernel density estimation using a Gaussian kernel.

2
Specification

Fortran Interface
Subroutine g10baf ( n, x, window, slo, shi, ns, smooth, t, usefft, fft, ifail)
Integer, Intent (In):: n, ns
Integer, Intent (Inout):: ifail
Real (Kind=nag_wp), Intent (In):: x(n), window, slo, shi
Real (Kind=nag_wp), Intent (Inout):: fft(ns)
Real (Kind=nag_wp), Intent (Out):: smooth(ns), t(ns)
Logical, Intent (In):: usefft
C Header Interface
#include nagmk26.h
void  g10baf_ (const Integer *n, const double x[], const double *window, const double *slo, const double *shi, const Integer *ns, double smooth[], double t[], const logical *usefft, double fft[], 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 and b=nsh 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, 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.
Constraint: n>0.
2:     xn – Real (Kind=nag_wp) arrayInput
On entry: the n observations, xi, for i=1,2,,n.
3:     window – Real (Kind=nag_wp)Input
On entry: h, the window width.
Constraint: 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: slo<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, ns.
Constraint: ns2.
7:     smoothns – Real (Kind=nag_wp) arrayOutput
On exit: the ns values of the density estimate, f^tl, for l=1,2,,ns.
8:     tns – Real (Kind=nag_wp) arrayOutput
On exit: the points at which the estimate is calculated, tl, for l=1,2,,ns.
9:     usefft – LogicalInput
On entry: must be set to .FALSE. if the values of Yl 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 usefft=.TRUE. then the arguments n, slo, shi, ns and fft must remain unchanged from the previous call to g10baf with usefft=.FALSE..
10:   fftns – Real (Kind=nag_wp) arrayInput/Output
On entry: if usefft=.TRUE., fft must contain the fast Fourier transform of the weights of the discretized data, ξl, for l=1,2,,ns. Otherwise fft need not be set.
On exit: the fast Fourier transform of the weights of the discretized data, ξl, for l=1,2,,ns.
11:   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, if you are not familiar with this argument, the recommended value is 0. 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).
Errors or warnings detected by the routine:
ifail=1
On entry,n0,
orns<2,
orshislo,
orwindow0.0.
ifail=2
On entry,g10baf has been called with usefft=.TRUE. but the routine has not been called previously with usefft=.FALSE.,
org10baf has been called with usefft=.TRUE. but some of the arguments n, slo, shi, ns have been changed since the previous call to g10baf with usefft=.FALSE..
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.
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

g10baf is not thread safe and should not be called from a multithreaded user program. Please see Section 3.12.1 in How to Use the NAG Library and its Documentation for more information on thread safety.
g10baf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g10baf 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. The full estimated density function is shown in the accompanying plot.

10.1
Program Text

Program Text (g10bafe.f90)

10.2
Program Data

Program Data (g10bafe.d)

10.3
Program Results

Program Results (g10bafe.r)

GnuplotProduced by GNUPLOT 4.6 patchlevel 3 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 −5 −4 −3 −2 −1 0 1 2 3 4 5 Density Estimate t Example Program Plot of the Smoothed Density (window = 0.4) gnuplot_plot_1
© The Numerical Algorithms Group Ltd, Oxford, UK. 2017