nag_kernel_density_estim (g10bac) (PDF version)
g10 Chapter Contents
g10 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_kernel_density_estim (g10bac)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

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

2  Specification

#include <nag.h>
#include <nagg10.h>
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 , , x n , 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 = 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 + j-1 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 t , 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 ξ l  (see Jones and Lotwick (1984));
2. compute the FFT of the weights ξ l  to give Y l ;
3. compute ζ l = e - 1 2 h 2 s l 2 Y l  where s l = 2 π l / b-a ;
4. find the inverse FFT of ζ l  to give f ^ x .

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:     nIntegerInput
On entry: the number of observations in the sample, n .
Constraint: n>0 .
2:     x[n]const doubleInput
On entry: the n  observations, x i , for i=1,2,,n.
3:     windowdoubleInput
On entry: the window width, h .
Constraint: 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: low<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:
  • ns2 ;
  • 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, f ^ t l , for l=1,2,, n s .
8:     t[ns]doubleOutput
On exit: the points at which the estimate is calculated, t l , for l=1,2,, 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, high=value  while low=value . These arguments must satisfy high>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, n=value.
Constraint: n>0.
NE_INT_ARG_LT
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.
NE_REAL_ARG_LE
On entry, window must not be less than or equal to 0.0: window=value .

7  Accuracy

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

8  Parallelism and Performance

Not applicable.

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 n s log n s  as is the time for computing the inverse of the FFT.

10  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.

10.1  Program Text

Program Text (g10bace.c)

10.2  Program Data

Program Data (g10bace.d)

10.3  Program Results

Program Results (g10bace.r)


nag_kernel_density_estim (g10bac) (PDF version)
g10 Chapter Contents
g10 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2014