g10 Chapter Contents
g10 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_kernel_density_estim (g10bac)

## 1  Purpose

nag_kernel_density_estim (g10bac) performs kernel density estimation using a Gaussian kernel.

## 2  Specification

 #include #include
 void nag_kernel_density_estim (Integer n, const double x[], double window, double low, double high, Integer ns, double smooth[], double t[], NagError *fail)

## 3  Description

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 = 1 nh n j , a + j-1 h < x < a + jh , j = 1 , 2 , … , n s ,$
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 and $b={n}_{s}h$ 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, $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 = 1 nh ∑ i=1 n K x-x i h .$
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 = 1 2π e - t 2 / 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. discretize the data to give ${n}_{s}$ equally spaced points ${t}_{l}$ with weights ${\xi }_{l}$ (see Jones and Lotwick (1984));
2. compute the FFT of the weights ${\xi }_{l}$ to give ${Y}_{l}$;
3. 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. find the inverse FFT of ${\zeta }_{l}$ to give $\stackrel{^}{f}\left(x\right)$.

## 4  References

Jones M C and Lotwick H W (1984) Remark AS R50. A remark on algorithm AS 176 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:     nIntegerInput
On entry: the number of observations in the sample, $n$.
Constraint: ${\mathbf{n}}>0$.
2:     x[n]const doubleInput
On entry: the $n$ observations, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3:     windowdoubleInput
On entry: the window width, $h$.
Constraint: ${\mathbf{window}}>0.0$.
4:     lowdoubleInput
On entry: the lower limit of the interval on which the estimate is calculated, $a$. For most applications low should be at least three window widths below the lowest data point.
Constraint: ${\mathbf{low}}<{\mathbf{high}}$.
5:     highdoubleInput
On entry: the upper limit of the interval on which the estimate is calculated, $b$. For most applications high should be at least three window widths above the highest data point.
6:     nsIntegerInput
On entry: the number of points at which the estimate is calculated, ${n}_{s}$.
Constraints:
• ${\mathbf{ns}}\ge 2$;
• The largest prime factor of ns must not exceed 19, and the total number of prime factors of ns, counting repetitions, must not exceed 20.
7:     smooth[ns]doubleOutput
On exit: the ${n}_{s}$ values of the density estimate, $\stackrel{^}{f}\left({t}_{\mathit{l}}\right)$, for $\mathit{l}=1,2,\dots ,{n}_{s}$.
8:     t[ns]doubleOutput
On exit: the points at which the estimate is calculated, ${t}_{\mathit{l}}$, for $\mathit{l}=1,2,\dots ,{n}_{s}$.
9:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_REAL_ARG_LE
On entry, ${\mathbf{high}}=〈\mathit{\text{value}}〉$ while ${\mathbf{low}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{high}}>{\mathbf{low}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_C06_FACTORS
At least one of the prime factors of ns is greater than 19 or ns has more than 20 prime factors.
NE_G10BA_INTERVAL
On entry, the interval given by low to high does not extend beyond three window widths at either extreme of the dataset. This may distort the density estimate in some cases.
NE_INT_ARG_LE
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>0$.
NE_INT_ARG_LT
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.
NE_REAL_ARG_LE
On entry, window must not be less than or equal to 0.0: ${\mathbf{window}}=〈\mathit{\text{value}}〉$.

## 7  Accuracy

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

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.

## 9  Example

A sample of 1000 standard Normal (0,1) variates are generated using nag_rand_normal (g05skc) and the density estimated on 100 points with a window width of 0.1.

### 9.1  Program Text

Program Text (g10bace.c)

### 9.2  Program Data

Program Data (g10bace.d)

### 9.3  Program Results

Program Results (g10bace.r)