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

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