www.gusucode.com > stats 源码程序 matlab案例代码 > stats/FitAGaussianMixtureModelToDataExample.m
%% Fit a Gaussian Mixture Model to Data % This example shows how to simulate data from a multivariate normal % distribution, and then fit the data to a Gaussian mixture model (GMM) % using <docid:stats_ug.bt8x9gq fitgmdist>. To create a known, or fully % specified, GMM object, see <docid:stats_ug.bus2n9t>. %% % |fitgmdist| requires a matrix of data (|X|) and the number of components % in the GMM (|k|). To create a useful GMM, you must choose |k| carefully. % Too few components fails to model the data accurately (i.e., underfitting % to the data). Too many components leads to an over-fit model with % singular covariance matrices. %% % Simulate data from a mixture of two bivariate Gaussian distributions using <docid:stats_ug.f1130139 mvnrnd>. % Copyright 2015 The MathWorks, Inc. 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)]; figure; scatter(X(:,1),X(:,2),10,'.') %% % Fit a two-component GMM. Plot the pdf of the fitted % GMM. options = statset('Display','final'); gm = fitgmdist(X,2,'Options',options); gmPDF = @(x,y)pdf(gm,[x y]); hold on h = ezcontour(gmPDF,[-8 6],[-8 6]); title('Scatter Plot and PDF Contour') hold off %% % Display the estimates for mu, sigma, and mixture proportions ComponentMeans = gm.mu ComponentCovariances = gm.Sigma MixtureProportions = gm.PComponents %% % Fit the data to four models, each with an increasing number of % components. AIC = zeros(1,4); gm = cell(1,4); for k = 1:4 gm{k} = fitgmdist(X,k); AIC(k)= gm{k}.AIC; end %% % Display the number of components that minimizes the AIC. [minAIC,numComponents] = min(AIC); numComponents %% % The two-component model minimizes the AIC. %% % Display the two-component GMM. gm2 = gm{numComponents} %% % Both the Akaike and Bayes information are negative log-likelihoods for % the data with penalty terms for the number of estimated parameters. You % can use them to determine an appropriate number of components for a model % when the number of components is unspecified.