NAG FL Interface
g10bbf (kerndens_​gauss)

Settings help

FL Name Style:


FL Specification Language:


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 <nag.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)
The routine may be called by the names g10bbf or nagf_smooth_kerndens_gauss.

3 Description

Given a sample of n observations, x1,x2,,xn, from a distribution with unknown density function, f(x), 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-1)h 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, K(t), satisfies the conditions:
-K(t)dt=1  and  K(t)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
K(t)=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.
  1. (i)Discretize the data to give ns equally spaced points tl with weights ξl (see Jones and Lotwick (1984)).
  2. (ii)Compute the FFT of the weights ξl to give Yl.
  3. (iii)Compute ζl=e-12h2sl2Yl where sl=2πl/(b-a).
  4. (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 Integer Input
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: x(n) Real (Kind=nag_wp) array Input
On entry: xi, for i=1,2,,n.
If fcall=0, x must be unchanged since the last call to g10bbf.
3: wtype Integer Input
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× min(q75-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 = mini{xi}-slo×h b = maxi{xi}+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.
Suggested value: shi=0.0 and slo=3.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: b, the upper limit actually used.
7: ns Integer Input
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: smooth(ns) Real (Kind=nag_wp) array Output
On exit: f^(tl), for l=1,2,,ns, the ns values of the density estimate.
9: t(ns) Real (Kind=nag_wp) array Output
On exit: tl, for l=1,2,,ns, the points at which the estimate is calculated.
10: fcall Integer Input
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: rcomm(ns+20) Real (Kind=nag_wp) array Communication 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 rcomm(l+20)=Yl, for l=1,2,,ns.
12: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value −1 is recommended since useful values can be provided in some output arguments even when ifail0 on exit. 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:
Note: in some cases g10bbf may return useful information.
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, min(x)=value and max(x)=value.
Expected values of at least value and value for slo and shi.
slo should be at least three window widths below the lowest data point or shi should be at least three window widths above the highest data point. 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 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

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

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
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 nslog(ns), 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