Categories &

Functions List

Function Reference: ksdensity

statistics: f = ksdensity (x)
statistics: f = ksdensity (x, pts)
statistics: [f, xi] = ksdensity (…)
statistics: [f, xi, bw] = ksdensity (…)
statistics: […] = ksdensity (…, Name, Value)

Kernel smoothing density estimate.

f = ksdensity (x) computes a probability density estimate of the sample in the vector x, evaluated at 100 equally spaced points xi that span the range of the data. [f, xi] = ksdensity (x) also returns those points. Both are row vectors, whichever way x itself lies. When called without output arguments, the estimate is plotted instead.

f = ksdensity (x, pts) evaluates the estimate at the values in pts instead; f is then the same size as pts. For 'Function' equal to 'icdf' the entries of pts are probabilities in [0, 1].

[f, xi, bw] = ksdensity (…) additionally returns the bandwidth bw of the smoothing kernel.

The following Name-Value pairs are supported:

NameValue
'Kernel'The smoothing kernel: 'normal' (default), 'box', 'triangle', 'epanechnikov', or a function handle @(z) evaluating a kernel density at the standardized distance z.
'Bandwidth'The kernel bandwidth, a positive scalar. The default is the value that is optimal for estimating a normal density, bw = sigma × (4 / (3 × n)) ^ (1 / 5), with sigma a robust estimate of the standard deviation of x.
'Function'The function to estimate: 'pdf' (default), 'cdf', 'icdf', 'survivor', or 'cumhazard'.
'Weights'A vector of non-negative weights, one for each element of x. The default weights are all equal.
'NumPoints'The number of equally spaced points xi at which to evaluate the estimate when pts is not given. The default is 100.

Source Code: ksdensity

See also: hist, histc, ecdf

Source Code: ksdensity

Kernel density estimate of a small sample, with a histogram for reference

 x = [1 1.5 2 2 2.5 3 3.5 3.5 4 6];
 [f, xi] = ksdensity (x);
 hist (x, 6, 6 / numel (x));
 hold on;  plot (xi, f, 'r-', 'LineWidth', 2);  hold off;
plotted figure