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