P = posterior(obj,X)
[P,nlogl] = posterior(obj,X)
P = posterior(obj,X) returns the posterior probabilities of each of the k components in the Gaussian mixture distribution defined by obj for each observation in the data matrix X. X is n-by-d, where n is the number of observations and d is the dimension of the data. obj is an object created by gmdistribution or fit. P is n-by-k, with P(I,J) the probability of component J given observation I.
posterior treats NaN values as missing data. Rows of X with NaN values are excluded from the computation.
[P,nlogl] = posterior(obj,X) also returns nlogl, the negative log-likelihood of the data.
Generate data from a mixture of two bivariate Gaussian distributions using the mvnrnd function:
MU1 = [1 2]; SIGMA1 = [2 0; 0 .5]; MU2 = [-3 -5]; SIGMA2 = [1 0; 0 1]; 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 = gmdistribution.fit(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')