annotate toolboxes/FullBNT-1.0.7/netlab3.3/gmmactiv.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function a = gmmactiv(mix, x)
wolffd@0 2 %GMMACTIV Computes the activations of a Gaussian mixture model.
wolffd@0 3 %
wolffd@0 4 % Description
wolffd@0 5 % This function computes the activations A (i.e. the probability
wolffd@0 6 % P(X|J) of the data conditioned on each component density) for a
wolffd@0 7 % Gaussian mixture model. For the PPCA model, each activation is the
wolffd@0 8 % conditional probability of X given that it is generated by the
wolffd@0 9 % component subspace. The data structure MIX defines the mixture model,
wolffd@0 10 % while the matrix X contains the data vectors. Each row of X
wolffd@0 11 % represents a single vector.
wolffd@0 12 %
wolffd@0 13 % See also
wolffd@0 14 % GMM, GMMPOST, GMMPROB
wolffd@0 15 %
wolffd@0 16
wolffd@0 17 % Copyright (c) Ian T Nabney (1996-2001)
wolffd@0 18
wolffd@0 19 % Check that inputs are consistent
wolffd@0 20 errstring = consist(mix, 'gmm', x);
wolffd@0 21 if ~isempty(errstring)
wolffd@0 22 error(errstring);
wolffd@0 23 end
wolffd@0 24
wolffd@0 25 ndata = size(x, 1);
wolffd@0 26 a = zeros(ndata, mix.ncentres); % Preallocate matrix
wolffd@0 27
wolffd@0 28 switch mix.covar_type
wolffd@0 29
wolffd@0 30 case 'spherical'
wolffd@0 31 % Calculate squared norm matrix, of dimension (ndata, ncentres)
wolffd@0 32 n2 = dist2(x, mix.centres);
wolffd@0 33
wolffd@0 34 % Calculate width factors
wolffd@0 35 wi2 = ones(ndata, 1) * (2 .* mix.covars);
wolffd@0 36 normal = (pi .* wi2) .^ (mix.nin/2);
wolffd@0 37
wolffd@0 38 % Now compute the activations
wolffd@0 39 a = exp(-(n2./wi2))./ normal;
wolffd@0 40
wolffd@0 41 case 'diag'
wolffd@0 42 normal = (2*pi)^(mix.nin/2);
wolffd@0 43 s = prod(sqrt(mix.covars), 2);
wolffd@0 44 for j = 1:mix.ncentres
wolffd@0 45 diffs = x - (ones(ndata, 1) * mix.centres(j, :));
wolffd@0 46 a(:, j) = exp(-0.5*sum((diffs.*diffs)./(ones(ndata, 1) * ...
wolffd@0 47 mix.covars(j, :)), 2)) ./ (normal*s(j));
wolffd@0 48 end
wolffd@0 49
wolffd@0 50 case 'full'
wolffd@0 51 normal = (2*pi)^(mix.nin/2);
wolffd@0 52 for j = 1:mix.ncentres
wolffd@0 53 diffs = x - (ones(ndata, 1) * mix.centres(j, :));
wolffd@0 54 % Use Cholesky decomposition of covariance matrix to speed computation
wolffd@0 55 c = chol(mix.covars(:, :, j));
wolffd@0 56 temp = diffs/c;
wolffd@0 57 a(:, j) = exp(-0.5*sum(temp.*temp, 2))./(normal*prod(diag(c)));
wolffd@0 58 end
wolffd@0 59 case 'ppca'
wolffd@0 60 log_normal = mix.nin*log(2*pi);
wolffd@0 61 d2 = zeros(ndata, mix.ncentres);
wolffd@0 62 logZ = zeros(1, mix.ncentres);
wolffd@0 63 for i = 1:mix.ncentres
wolffd@0 64 k = 1 - mix.covars(i)./mix.lambda(i, :);
wolffd@0 65 logZ(i) = log_normal + mix.nin*log(mix.covars(i)) - ...
wolffd@0 66 sum(log(1 - k));
wolffd@0 67 diffs = x - ones(ndata, 1)*mix.centres(i, :);
wolffd@0 68 proj = diffs*mix.U(:, :, i);
wolffd@0 69 d2(:,i) = (sum(diffs.*diffs, 2) - ...
wolffd@0 70 sum((proj.*(ones(ndata, 1)*k)).*proj, 2)) / ...
wolffd@0 71 mix.covars(i);
wolffd@0 72 end
wolffd@0 73 a = exp(-0.5*(d2 + ones(ndata, 1)*logZ));
wolffd@0 74 otherwise
wolffd@0 75 error(['Unknown covariance type ', mix.covar_type]);
wolffd@0 76 end
wolffd@0 77