ksdensity - Kernel smoothing density estimate

Syntax

[f,xi] = ksdensity(x)
f = ksdensity(x,xi)
ksdensity(...)
ksdensity(ax,...)
[f,xi,u] = ksdensity(...)
[...] = ksdensity(...,param1,val1,param2,val2,...)

Description

[f,xi] = ksdensity(x) computes a probability density estimate of the sample in the vector x. f is the vector of density values evaluated at the points in xi. The estimate is based on a normal kernel function, using a window parameter ('width') that is a function of the number of points in x. The density is evaluated at 100 equally spaced points that cover the range of the data in x.

f = ksdensity(x,xi) specifies the vector xi of values, where the density estimate is to be evaluated.

ksdensity(...) without output arguments produces a plot of the results.

ksdensity(ax,...) plots into axes ax instead of gca.

[f,xi,u] = ksdensity(...) also returns the width of the kernel-smoothing window.

[...] = ksdensity(...,param1,val1,param2,val2,...) specifies parameter/value pairs to control the density estimation. Valid parameter strings and their possible values are as follows:

'censoring'

A logical vector of the same length as x, indicating which entries are censoring times. Default is no censoring.

'kernel'

The type of kernel smoother to use. Choose the value as 'normal' (default), 'box', 'triangle', or 'epanechnikov'.

Alternatively, you can specify some other function, as a function handle or as a string, e.g., @normpdf or 'normpdf'. The function must take a single argument that is an array of distances between data values and places where the density is evaluated. It must return an array of the same size containing corresponding values of the kernel function.

'npoints'

The number of equally spaced points in xi. Default is 100.

'support'
  • 'unbounded' allows the density to extend over the whole real line (default).

  • 'positive' restricts the density to positive values.

  • A two-element vector gives finite lower and upper bounds for the support of the density.

'weights'

Vector of the same length as x, assigning weight to each x value.

'width'

The bandwidth of the kernel-smoothing window. The default is optimal for estimating normal densities, but you may want to choose a smaller value to reveal features such as multiple modes.

'function'

The function type to estimate, chosen from among 'pdf', 'cdf', 'icdf', 'survivor', or 'cumhazard' for the density, cumulative probability, inverse cumulative probability, survivor, or cumulative hazard functions, respectively.

In place of the kernel functions listed above, you can specify another kernel function by using @ (such as @normpdf) or quotes (such as 'normpdf'). The function must take a single argument that is an array of distances between data values and places where the density is evaluated, and return an array of the same size containing corresponding values of the kernel function. When the 'function' parameter value is 'pdf', this kernel function should return density values; otherwise, it should return cumulative probability values. Specifying a custom kernel when the 'function' parameter value is 'icdf' is an error.

If the 'support' parameter is 'positive', ksdensity transforms x using a log function, estimates the density of the transformed values, and transforms back to the original scale. If 'support' is a vector [L U], ksdensity uses the transformation log((X-L)/(U-X)). The 'width' parameter and u outputs are on the scale of the transformed values.

Examples

This example generates a mixture of two normal distributions and plots the estimated density.

x = [randn(30,1); 5+randn(30,1)];
[f,xi] = ksdensity(x); 
plot(xi,f); 

References

[1] Bowman, A. W., and A. Azzalini, Applied Smoothing Techniques for Data Analysis, Oxford University Press, 1997.

See Also

hist, @ (function handle)

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS