Daniel@0: function a = gmmactiv(mix, x) Daniel@0: %GMMACTIV Computes the activations of a Gaussian mixture model. Daniel@0: % Daniel@0: % Description Daniel@0: % This function computes the activations A (i.e. the probability Daniel@0: % P(X|J) of the data conditioned on each component density) for a Daniel@0: % Gaussian mixture model. For the PPCA model, each activation is the Daniel@0: % conditional probability of X given that it is generated by the Daniel@0: % component subspace. The data structure MIX defines the mixture model, Daniel@0: % while the matrix X contains the data vectors. Each row of X Daniel@0: % represents a single vector. Daniel@0: % Daniel@0: % See also Daniel@0: % GMM, GMMPOST, GMMPROB Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: % Check that inputs are consistent Daniel@0: errstring = consist(mix, 'gmm', x); Daniel@0: if ~isempty(errstring) Daniel@0: error(errstring); Daniel@0: end Daniel@0: Daniel@0: ndata = size(x, 1); Daniel@0: a = zeros(ndata, mix.ncentres); % Preallocate matrix Daniel@0: Daniel@0: switch mix.covar_type Daniel@0: Daniel@0: case 'spherical' Daniel@0: % Calculate squared norm matrix, of dimension (ndata, ncentres) Daniel@0: n2 = dist2(x, mix.centres); Daniel@0: Daniel@0: % Calculate width factors Daniel@0: wi2 = ones(ndata, 1) * (2 .* mix.covars); Daniel@0: normal = (pi .* wi2) .^ (mix.nin/2); Daniel@0: Daniel@0: % Now compute the activations Daniel@0: a = exp(-(n2./wi2))./ normal; Daniel@0: Daniel@0: case 'diag' Daniel@0: normal = (2*pi)^(mix.nin/2); Daniel@0: s = prod(sqrt(mix.covars), 2); Daniel@0: for j = 1:mix.ncentres Daniel@0: diffs = x - (ones(ndata, 1) * mix.centres(j, :)); Daniel@0: a(:, j) = exp(-0.5*sum((diffs.*diffs)./(ones(ndata, 1) * ... Daniel@0: mix.covars(j, :)), 2)) ./ (normal*s(j)); Daniel@0: end Daniel@0: Daniel@0: case 'full' Daniel@0: normal = (2*pi)^(mix.nin/2); Daniel@0: for j = 1:mix.ncentres Daniel@0: diffs = x - (ones(ndata, 1) * mix.centres(j, :)); Daniel@0: % Use Cholesky decomposition of covariance matrix to speed computation Daniel@0: c = chol(mix.covars(:, :, j)); Daniel@0: temp = diffs/c; Daniel@0: a(:, j) = exp(-0.5*sum(temp.*temp, 2))./(normal*prod(diag(c))); Daniel@0: end Daniel@0: case 'ppca' Daniel@0: log_normal = mix.nin*log(2*pi); Daniel@0: d2 = zeros(ndata, mix.ncentres); Daniel@0: logZ = zeros(1, mix.ncentres); Daniel@0: for i = 1:mix.ncentres Daniel@0: k = 1 - mix.covars(i)./mix.lambda(i, :); Daniel@0: logZ(i) = log_normal + mix.nin*log(mix.covars(i)) - ... Daniel@0: sum(log(1 - k)); Daniel@0: diffs = x - ones(ndata, 1)*mix.centres(i, :); Daniel@0: proj = diffs*mix.U(:, :, i); Daniel@0: d2(:,i) = (sum(diffs.*diffs, 2) - ... Daniel@0: sum((proj.*(ones(ndata, 1)*k)).*proj, 2)) / ... Daniel@0: mix.covars(i); Daniel@0: end Daniel@0: a = exp(-0.5*(d2 + ones(ndata, 1)*logZ)); Daniel@0: otherwise Daniel@0: error(['Unknown covariance type ', mix.covar_type]); Daniel@0: end Daniel@0: