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