www.gusucode.com > stats 源码程序 matlab案例代码 > stats/TwoComponentGaussianMixtureModelExample.m
%% Two-Component Gaussian Mixture Model %% % Generate the sample data from a mixture of two bivariate Gaussian % distributions. MU1 = [1 2]; SIGMA1 = [2 0; 0 .5]; MU2 = [-3 -5]; SIGMA2 = [1 0; 0 1]; rng(1) % For reproducibility X = [mvnrnd(MU1,SIGMA1,1000);mvnrnd(MU2,SIGMA2,1000)]; %% % Create a scatter plot of the data. scatter(X(:,1),X(:,2),10,'.') hold on %% % Next, fit a two-component Gaussian mixture model. options = statset('Display','final'); obj = fitgmdist(X,2,'Options',options); %% % Plot two-component probability density contours. h = ezcontour(@(x,y)pdf(obj,[x y]),[-8 6],[-8 6]); hold off %% % Display the parameter estimates. ComponentMeans = obj.mu ComponentCovariances = obj.Sigma MixtureProportions = obj.PComponents %% % The Akaike information is minimized by the two-component model. AIC = zeros(1,4); obj = cell(1,4); for k = 1:4 obj{k} = fitgmdist(X,k,'Options',options); AIC(k)= obj{k}.AIC; end [minAIC,numComponents] = min(AIC); numComponents %% % Display the model. model = obj{2} %% % Both the Akaike and Bayes information are negative log-likelihoods for % the data with penalty terms for the number of estimated parameters. They % are often used to determine an appropriate number of components for a % model when the number of components is unspecified.