Main Content

mahal

Mahalanobis distance to Gaussian mixture component

Description

example

d2= mahal(gm,X)returns the squared Mahalanobis distance of each observation inXto each Gaussian mixture component ingm.

Examples

collapse all

Generate random variates that follow a mixture of two bivariate Gaussian distributions by using themvnrndfunction. Fit a Gaussian mixture model (GMM) to the generated data by using thefitgmdistfunction, and then compute Mahalanobis distances between the generated data and the mixture components of the fitted GMM.

Define the distribution parameters (means and covariances) of two bivariate Gaussian mixture components.

rng('default')% For reproducibilitymu1 = [1 2];% Mean of the 1st componentsigma1 = [2 0; 0 .5];% Covariance of the 1st componentmu2 = [-3 -5];% Mean of the 2nd componentsigma2 = [1 0; 0 1];% Covariance of the 2nd component

Generate an equal number of random variates from each component, and combine the two sets of random variates.

r1 = mvnrnd(mu1,sigma1,1000); r2 = mvnrnd(mu2,sigma2,1000); X = [r1; r2];

The combined data setXcontains random variates following a mixture of two bivariate Gaussian distributions.

Fit a two-component GMM toX.

gm = fitgmdist(X,2)
gm = Gaussian mixture distribution with 2 components in 2 dimensions Component 1: Mixing proportion: 0.500000 Mean: -2.9617 -4.9727 Component 2: Mixing proportion: 0.500000 Mean: 0.9539 2.0261

fitgmdistfits a GMM toXusing two mixture components. The means ofComponent1andComponent2are[-2.9617,-4.9727]and[0.9539,2.0261], which are close tomu2andmu1, respectively.

Compute the Mahalanobis distance of each point inXto each component ofgm.

d2 = mahal(gm,X);

PlotXby usingscatterand use marker color to visualize the Mahalanobis distance toComponent1.

scatter(X(:,1),X(:,2),10,d2(:,1),'.')% Scatter plot with points of size 10c = colorbar; ylabel(c,'Mahalanobis Distance to Component 1')

Figure contains an axes object. The axes object contains an object of type scatter.

Input Arguments

collapse all

Gaussian mixture distribution, also called Gaussian mixture model (GMM), specified as agmdistributionobject.

You can create agmdistributionobject usinggmdistributionorfitgmdist. Use thegmdistributionfunction to create agmdistributionobject by specifying the distribution parameters. Use thefitgmdistfunction to fit agmdistributionmodel to data given a fixed number of components.

指定的数据,as ann-by-mnumeric matrix, wherenis the number of observations andmis the number of variables in each observation.

If a row ofXcontainsNaNs, thenmahalexcludes the row from the computation. The corresponding value ind2isNaN.

Data Types:single|double

Output Arguments

collapse all

Squared Mahalanobis distance of each observation inXto each Gaussian mixture component ingm, returned as ann-by-knumeric matrix, wherenis the number of observations inXandk混合组件的数量吗gm.

d2(i,j)is the squared distance of observationito thejth Gaussian mixture component.

More About

collapse all

Mahalanobis Distance

The Mahalanobis distance is a measure between a sample point and a distribution.

The Mahalanobis distance from a vectorxto a distribution with meanμand covarianceΣis

d = ( x μ ) 1 ( x μ ) ' .

This distance represents how farxis from the mean in number of standard deviations.

mahalreturns the squared Mahalanobis distanced2from an observation inXto a mixture component ingm.

Version History

Introduced in R2007b