Main Content

mvksdensity

Kernel smoothing function estimate for multivariate data

Description

f = mvksdensity(x,pts,'Bandwidth',bw) computes a probability density estimate of the sample data in the n-by-d matrix x, evaluated at the points in pts using the required name-value pair argument value bw for the bandwidth value. The estimation is based on a product Gaussian kernel function.

For univariate or bivariate data, use ksdensity instead.

example

f = mvksdensity(x,pts,'Bandwidth',bw,Name,Value) returns any of the previous output arguments, using additional options specified by one or more Name,Value pair arguments. For example, you can define the function type that mvksdensity evaluates, such as probability density, cumulative probability, or survivor function. You can also assign weights to the input values.

example

Examples

collapse all

Load the Hald cement data.

load hald

The data measures the heat of hardening for 13 different cement compositions. The predictor matrix ingredients contains the percent composition for each of four cement ingredients. The response matrix heat contains the heat of hardening (in cal\g) after 180 days.

Estimate the kernel density for the first three observations in ingredients.

xi = ingredients(1:3,:);
f = mvksdensity(ingredients,xi,'Bandwidth',0.8);

Load the Hald cement data.

load hald

The data measures the heat of hardening for 13 different cement compositions. The predictor matrix ingredients contains the percent composition for each of four cement ingredients. The response matrix heat contains the heat of hardening (in cal/g) after 180 days.

Create an array of points at which to estimate the density. First, define the range and spacing for each variable, using a similar number of points in each dimension.

gridx1 = 0:2:22;
gridx2 = 20:5:80;
gridx3 = 0:2:24;
gridx4 = 5:5:65;

Next, use ndgrid to generate a full grid of points using the defined range and spacing.

[x1,x2,x3,x4] = ndgrid(gridx1,gridx2,gridx3,gridx4);

Finally, transform and concatenate to create an array that contains the points at which to estimate the density. This array has one column for each variable.

x1 = x1(:,:)';
x2 = x2(:,:)';
x3 = x3(:,:)';
x4 = x4(:,:)';
xi = [x1(:) x2(:) x3(:) x4(:)];

Estimate the density.

f = mvksdensity(ingredients,xi,...
	'Bandwidth',[4.0579 10.7345 4.4185 11.5466],...
	'Kernel','normpdf');

View the size of xi and f to confirm that mvksdensity calculates the density at each point in xi.

size_xi = size(xi)
size_xi = 1×2

       26364           4

size_f = size(f)
size_f = 1×2

       26364           1

Input Arguments

collapse all

Sample data for which mvksdensity returns the probability density estimate, specified as an n-by-d matrix of numeric values. n is the number of data points (rows) in x, and d is the number of dimensions (columns).

Data Types: single | double

Points at which to evaluate the probability density estimate f, specified as a matrix with the same number of columns as x. The returned estimate f and pts have the same number of rows.

Data Types: single | double

Value for the bandwidth of the kernel-smoothing window, specified as a scalar value or d-element vector. d is the number of dimensions (columns) in the sample data x. If bw is a scalar value, it applies to all dimensions.

If you specify 'BoundaryCorrection' as 'log'(default) and 'Support' as either 'positive' or a two-row matrix, mvksdensity converts bounded data to be unbounded by using log transformation. The value of bw is on the scale of the transformed values.

Silverman's rule of thumb for the bandwidth is

bi=σi{4(d+2)n}1(d+4),i=1,2,...,d,

where d is the number of dimensions, n is the number of observations, and σi is the standard deviation of the ith variate [4].

Example: 'Bandwidth',0.8

Data Types: single | double

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'Kernel','triangle','Function,'cdf' specifies that mvksdensity estimates the cdf of the sample data using the triangle kernel function.

Boundary correction method, specified as the comma-separated pair consisting of 'BoundaryCorrection' and either 'log' or 'reflection'.

ValueDescription
'log'

mvksdensity converts bounded data to be unbounded by using one of the following transformations. Then, it transforms back to the original bounded scale after density estimation.

  • If you specify 'Support','positive', then mvksdensity applies log(xj) for each dimension, where xj is the jth column of the input argument x.

  • If you specify 'Support' as a two-row matrix consisting of the lower and upper limits for each dimension, then mvksdensity applies log((xj-Lj)/(Uj-xj)) for each dimension, where Lj and Uj are the lower and upper limits of the jth dimension, respectively.

The value of bw is on the scale of the transformed values.

'reflection'

mvksdensity augments bounded data by adding reflected data near the boundaries, then it returns estimates corresponding to the original support. For details, see Reflection Method.

mvksdensity applies boundary correction only when you specify 'Support' as a value other than 'unbounded'.

Example: 'BoundaryCorrection','reflection'

Function to estimate, specified as the comma-separated pair consisting of 'Function' and one of the following.

ValueDescription
'pdf'Probability density function
'cdf'Cumulative distribution function
'survivor'Survivor function

Example: 'Function','cdf'

Type of kernel smoother, specified as the comma-separated pair consisting of 'Kernel' and one of the following.

ValueDescription
'normal' Normal (Gaussian) kernel
'box'Box kernel
'triangle'Triangular kernel
'epanechnikov'Epanechnikov kernel

You can also specify a kernel function that is a custom or built-in function. Specify the function as a function handle (for example, @myfunction or @normpdf) or as a character vector or string scalar (for example, 'myfunction' or 'normpdf'). The software calls the specified function with one argument that is an array of distances between data values and locations where the density is evaluated, normalized by the bandwidth in that dimension. The function must return an array of the same size containing the corresponding values of the kernel function.

mvksdensity applies the same kernel to each dimension.

Example: 'Kernel','box'

Support for the density, specified as the comma-separated pair consisting of 'support' and one of the following.

ValueDescription
'unbounded'Allow the density to extend over the whole real line
'positive'Restrict the density to positive values
2-by-d matrixSpecify the finite lower and upper bounds for the support of the density. The first row contains the lower limits and the second row contains the upper limits. Each column contains the limits for one dimension of x.

'Support' can also be a combination of positive, unbounded, and bounded variables specified as [0 -Inf L; Inf Inf U].

Example: 'Support','positive'

Data Types: single | double | char | string

Weights for sample data, specified as the comma-separated pair consisting of 'Weights' and a vector of length size(x,1), where x is the sample data.

Example: 'Weights',xw

Data Types: single | double

Output Arguments

collapse all

Estimated function values, returned as a vector. f and pts have the same number of rows.

More About

collapse all

References

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

[2] Hill, P. D. “Kernel estimation of a distribution function.” Communications in Statistics – Theory and Methods. Vol. 14, Issue 3, 1985, pp. 605-620.

[3] Jones, M. C. “Simple boundary correction for kernel density estimation.” Statistics and Computing. Vol. 3, Issue 3, 1993, pp. 135-146.

[4] Silverman, B. W. Density Estimation for Statistics and Data Analysis. Chapman & Hall/CRC, 1986.

[5] Scott, D. W. Multivariate Density Estimation: Theory, Practice, and Visualization. John Wiley & Sons, 2015.

Extended Capabilities

expand all

Version History

Introduced in R2016a