NAG CL Interface
g10bbc (kerndens_​gauss)

1 Purpose

g10bbc performs kernel density estimation using a Gaussian kernel.

2 Specification

#include <nag.h>
void  g10bbc (Integer n, const double x[], Nag_WindowType wtype, double *window, double *slo, double *shi, Integer ns, double smooth[], double t[], Nag_Boolean fcall, double rcomm[], NagError *fail)
The function may be called by the names: g10bbc, nag_smooth_kerndens_gauss or nag_kernel_density_gauss.

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.
  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=Nag_FALSE, n must be unchanged since the last call to g10bbc.
Constraint: n>0.
2: x[n] const double Input
On entry: xi, for i=1,2,,n.
If fcall=Nag_FALSE, x must be unchanged since the last call to g10bbc.
3: wtype Nag_WindowType Input
On entry: how the window width, h, is to be calculated:
wtype=Nag_WindowSupplied
h is supplied in window.
wtype=Nag_RuleOfThumb
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 g01amc. This is the "rule-of-thumb" suggested by Silverman (1990).
Suggested value: wtype=Nag_RuleOfThumb and window=1.0.
Constraint: wtype=Nag_WindowSupplied or Nag_RuleOfThumb.
4: window double * Input/Output
On entry: if wtype=Nag_WindowSupplied, then h, the window width. Otherwise, m, the multiplier used in the calculation of h.
Suggested value: window=1.0 and wtype=Nag_RuleOfThumb.
On exit: h, the window width actually used.
Constraint: window>0.0.
5: slo double * 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=Nag_FALSE, slo must be unchanged since the last call to g10bbc.
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 double * 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=Nag_FALSE, shi must be unchanged since the last call to g10bbc.
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=Nag_FALSE, ns must be unchanged since the last call to g10bbc.
Suggested value: ns=512.
Constraint: ns2.
8: smooth[ns] double Output
On exit: f^tl, for l=1,2,,ns, the ns values of the density estimate.
9: t[ns] double Output
On exit: tl, for l=1,2,,ns, the points at which the estimate is calculated.
10: fcall Nag_Boolean Input
On entry: if fcall=Nag_TRUE then the values of Yl are to be calculated by this call to g10bbc, 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.
11: rcomm[ns+20] double Communication Array
On entry: communication array, used to store information between calls to g10bbc.
If fcall=Nag_FALSE, rcomm must be unchanged since the last call to g10bbc.
On exit: the last ns elements of rcomm contain the fast Fourier transform of the weights of the discretized data, that is rcomm[l+19]=Yl, for l=1,2,,ns.
12: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_ILLEGAL_COMM
rcomm has been corrupted between calls.
NE_INT
On entry, n=value.
Constraint: n>0.
On entry, ns=value.
Constraint: ns2.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_PREV_CALL
On entry, n=value.
On entry at previous call, n=value.
Constraint: if fcall=Nag_FALSE, n must be unchanged since previous call.
On entry, ns=value.
On entry at previous call, ns=value.
Constraint: if fcall=Nag_FALSE, ns must be unchanged since previous call.
On entry, shi=value.
On exit from previous call, shi=value.
Constraint: if fcall=Nag_FALSE, shi must be unchanged since previous call.
On entry, slo=value.
On exit from previous call, slo=value.
Constraint: if fcall=Nag_FALSE, slo must be unchanged since previous call.
NE_REAL
On entry, window=value.
Constraint: window>0.0.
NW_POTENTIAL_PROBLEM
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.
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.

7 Accuracy

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

8 Parallelism and Performance

g10bbc is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g10bbc 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 function. 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 (g10bbce.c)

10.2 Program Data

Program Data (g10bbce.d)

10.3 Program Results

Program Results (g10bbce.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