Products & Services Industries Academia Support User Community Company

Learn more about Statistics Toolbox   

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:

ParameterValue
'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.

For 'icdf', f=ksdensity(x,yi,...,'function','icdf') computes the estimated inverse CDF of the values in x, and evaluates it at the probability values specified in yi.

In place of the kernel functions listed above, you can specify another kernel function by using @ (such as @normpdf) or quotes (such as 'normpdf'). ksdensity calls the function with a single argument that is an array containing distances between data values in x and locations in xi where the density is evaluated. The function must return an array of the same size containing corresponding values of the kernel function. When the 'function' parameter value is 'pdf', this kernel function returns density values, otherwise it returns cumulative probability values. Specifying a custom kernel when the 'function' parameter value is 'icdf' returns 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

Generate 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); 

Generate a mixture of two normal distributions, and plot the estimated cumulative distribution at a specified set of values:

x = [randn(30,1); 5+randn(30,1)];
xi = linspace(-10,15,201);
f = ksdensity(x,xi,'function','cdf');
plot(xi,f);

Generate a mixture of two normal distributions, and plot the estimated inverse cumulative distribution function at a specified set of values:

x = [randn(30,1); 5+randn(30,1)];
yi = linspace(.01,.99,99);
g = ksdensity(x,yi,'function','icdf');
plot(yi,g);

References

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

See Also

hist, @ (function handle)

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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