Generate random variates that follow a mixture of two bivariate Gaussian distributions by using the mvnrnd function. Fit a Gaussian mixture model (GMM) to the generated data by using the fitgmdist function, 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.
Generate an equal number of random variates from each component, and combine the two sets of random variates.
The combined data set X contains random variates following a mixture of two bivariate Gaussian distributions.
Fit a two-component GMM to X.
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
fitgmdist fits a GMM to X using two mixture components. The means of Component 1 and Component 2 are [-2.9617,-4.9727] and [0.9539,2.0261], which are close to mu2 and mu1, respectively.
Compute the Mahalanobis distance of each point in X to each component of gm.
Plot X by using scatter and use marker color to visualize the Mahalanobis distance to Component 1.