NAG CL Interfaceg10bbc (kerndens_​gauss)

Settings help

CL Name Style:

1Purpose

g10bbc performs kernel density estimation using a Gaussian kernel.

2Specification

 #include
 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.

3Description

Given a sample of $n$ observations, ${x}_{1},{x}_{2},\dots ,{x}_{n}$, from a distribution with unknown density function, $f\left(x\right)$, an estimate of the density function, $\stackrel{^}{f}\left(x\right)$, 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 ${n}_{j}$ is the number of observations falling in the interval $a+\left(j-1\right)h$ to $a+jh$, $a$ is the lower bound to the histogram, $b={n}_{s}h$ is the upper bound and ${n}_{s}$ 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\left(t\right)$, 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 ${n}_{s}$ equally spaced points ${t}_{l}$ with weights ${\xi }_{l}$ (see Jones and Lotwick (1984)).
2. (ii)Compute the FFT of the weights ${\xi }_{l}$ to give ${Y}_{l}$.
3. (iii)Compute ${\zeta }_{l}={e}^{-\frac{1}{2}{h}^{2}{s}_{l}^{2}}{Y}_{l}$ where ${s}_{l}=2\pi l/\left(b-a\right)$.
4. (iv)Find the inverse FFT of ${\zeta }_{l}$ to give $\stackrel{^}{f}\left(x\right)$.
To compute the kernel density estimate for further values of $h$ only steps (iii) and (iv) need be repeated.

4References

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

5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations in the sample.
If ${\mathbf{fcall}}=\mathrm{Nag_FALSE}$, n must be unchanged since the last call to g10bbc.
Constraint: ${\mathbf{n}}>0$.
2: $\mathbf{x}\left[{\mathbf{n}}\right]$const double Input
On entry: ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
If ${\mathbf{fcall}}=\mathrm{Nag_FALSE}$, x must be unchanged since the last call to g10bbc.
3: $\mathbf{wtype}$Nag_WindowType Input
On entry: how the window width, $h$, is to be calculated:
${\mathbf{wtype}}=\mathrm{Nag_WindowSupplied}$
$h$ is supplied in window.
${\mathbf{wtype}}=\mathrm{Nag_RuleOfThumb}$
$h$ is to be calculated from the data, with
 $h=m× ( 0.9× min(q75-q25,σ) n0.2 )$
where ${q}_{75}-{q}_{25}$ is the inter-quartile range and $\sigma$ the standard deviation of the sample, $x$, and $m$ is a multipler supplied in window. The $25%$ and $75%$ quartiles, ${q}_{25}$ and ${q}_{75}$, are calculated using g01amc. This is the "rule-of-thumb" suggested by Silverman (1990).
Suggested value: ${\mathbf{wtype}}=\mathrm{Nag_RuleOfThumb}$ and ${\mathbf{window}}=1.0$.
Constraint: ${\mathbf{wtype}}=\mathrm{Nag_WindowSupplied}$ or $\mathrm{Nag_RuleOfThumb}$.
4: $\mathbf{window}$double * Input/Output
On entry: if ${\mathbf{wtype}}=\mathrm{Nag_WindowSupplied}$, then $h$, the window width. Otherwise, $m$, the multiplier used in the calculation of $h$.
Suggested value: ${\mathbf{window}}=1.0$ and ${\mathbf{wtype}}=\mathrm{Nag_RuleOfThumb}$.
On exit: $h$, the window width actually used.
Constraint: ${\mathbf{window}}>0.0$.
5: $\mathbf{slo}$double * Input/Output
On entry: if ${\mathbf{slo}}<{\mathbf{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 ${\mathbf{fcall}}=\mathrm{Nag_FALSE}$, slo must be unchanged since the last call to g10bbc.
Suggested value: ${\mathbf{slo}}=3.0$ and ${\mathbf{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: $\mathbf{shi}$double * Input/Output
On entry: if ${\mathbf{slo}}<{\mathbf{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 ${\mathbf{fcall}}=\mathrm{Nag_FALSE}$, shi must be unchanged since the last call to g10bbc.
Suggested value: ${\mathbf{shi}}=0.0$ and ${\mathbf{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: $\mathbf{ns}$Integer Input
On entry: ${n}_{s}$, the number of points at which the estimate is calculated.
If ${\mathbf{fcall}}=\mathrm{Nag_FALSE}$, ns must be unchanged since the last call to g10bbc.
Suggested value: ${\mathbf{ns}}=512$.
Constraint: ${\mathbf{ns}}\ge 2$.
8: $\mathbf{smooth}\left[{\mathbf{ns}}\right]$double Output
On exit: $\stackrel{^}{f}\left({t}_{\mathit{l}}\right)$, for $\mathit{l}=1,2,\dots ,{n}_{s}$, the ${n}_{s}$ values of the density estimate.
9: $\mathbf{t}\left[{\mathbf{ns}}\right]$double Output
On exit: ${t}_{\mathit{l}}$, for $\mathit{l}=1,2,\dots ,{n}_{s}$, the points at which the estimate is calculated.
10: $\mathbf{fcall}$Nag_Boolean Input
On entry: if ${\mathbf{fcall}}=\mathrm{Nag_TRUE}$ then the values of ${Y}_{l}$ are to be calculated by this call to g10bbc, otherwise it is assumed that the values of ${Y}_{l}$ were calculated by a previous call to this routine and the relevant information is stored in rcomm.
11: $\mathbf{rcomm}\left[{\mathbf{ns}}+20\right]$double Communication Array
On entry: communication array, used to store information between calls to g10bbc.
If ${\mathbf{fcall}}=\mathrm{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 ${\mathbf{rcomm}}\left[\mathit{l}+19\right]={Y}_{\mathit{l}}$, for $\mathit{l}=1,2,\dots ,{n}_{s}$.
12: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6Error 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.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_ILLEGAL_COMM
rcomm has been corrupted between calls.
NE_INT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}>0$.
On entry, ${\mathbf{ns}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ns}}\ge 2$.
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, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
On entry at previous call, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{fcall}}=\mathrm{Nag_FALSE}$, n must be unchanged since previous call.
On entry, ${\mathbf{ns}}=⟨\mathit{\text{value}}⟩$.
On entry at previous call, ${\mathbf{ns}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{fcall}}=\mathrm{Nag_FALSE}$, ns must be unchanged since previous call.
On entry, ${\mathbf{shi}}=⟨\mathit{\text{value}}⟩$.
On exit from previous call, ${\mathbf{shi}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{fcall}}=\mathrm{Nag_FALSE}$, shi must be unchanged since previous call.
On entry, ${\mathbf{slo}}=⟨\mathit{\text{value}}⟩$.
On exit from previous call, ${\mathbf{slo}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{fcall}}=\mathrm{Nag_FALSE}$, slo must be unchanged since previous call.
NE_REAL
On entry, ${\mathbf{window}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{window}}>0.0$.
NW_POTENTIAL_PROBLEM
On entry, ${\mathbf{slo}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{shi}}=⟨\mathit{\text{value}}⟩$.
On entry, $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{x}}\right)=⟨\mathit{\text{value}}⟩$ and $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{x}}\right)=⟨\mathit{\text{value}}⟩$.
Expected values of at least $⟨\mathit{\text{value}}⟩$ and $⟨\mathit{\text{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.

7Accuracy

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

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

The time for computing the weights of the discretized data is of order $n$, while the time for computing the FFT is of order ${n}_{s}\mathrm{log}\left({n}_{s}\right)$, as is the time for computing the inverse of the FFT.

10Example

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

10.1Program Text

Program Text (g10bbce.c)

10.2Program Data

Program Data (g10bbce.d)

10.3Program Results

Program Results (g10bbce.r)
This plot shows the estimated density function for the example data for several window widths.