www.gusucode.com > stats 源码程序 matlab案例代码 > stats/ComputePosteriorProbabilitiesForGaussianMixtureVariatesExample.m

    %% Compute Posterior Probabilities for Gaussian Mixture Variates
%%
% Generate data from a mixture of two bivariate Gaussian distributions
% using the |mvnrnd| function.

% Copyright 2015 The MathWorks, Inc.

MU1 = [2 2];
SIGMA1 = [2 0; 0 1];
MU2 = [-2 -1];
SIGMA2 = [1 0; 0 1];
rng(1); % For reproducibility
X = [mvnrnd(MU1,SIGMA1,1000);mvnrnd(MU2,SIGMA2,1000)];

scatter(X(:,1),X(:,2),10,'.')
hold on
%%
% Fit a two-component Gaussian mixture model.
obj = fitgmdist(X,2);
h = ezcontour(@(x,y)pdf(obj,[x y]),[-8 6],[-8 6]);
%%
% Compute posterior probabilities of the components.
P = posterior(obj,X);

delete(h)
scatter(X(:,1),X(:,2),10,P(:,1))
hb = colorbar;
ylabel(hb,'Component 1 Probability')