comparison toolboxes/FullBNT-1.0.7/KPMstats/mixgauss_Mstep.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 [mu, Sigma] = mixgauss_Mstep(w, Y, YY, YTY, varargin)
2 % MSTEP_COND_GAUSS Compute MLEs for mixture of Gaussians given expected sufficient statistics
3 % function [mu, Sigma] = Mstep_cond_gauss(w, Y, YY, YTY, varargin)
4 %
5 % We assume P(Y|Q=i) = N(Y; mu_i, Sigma_i)
6 % and w(i,t) = p(Q(t)=i|y(t)) = posterior responsibility
7 % See www.ai.mit.edu/~murphyk/Papers/learncg.pdf.
8 %
9 % INPUTS:
10 % w(i) = sum_t w(i,t) = responsibilities for each mixture component
11 % If there is only one mixture component (i.e., Q does not exist),
12 % then w(i) = N = nsamples, and
13 % all references to i can be replaced by 1.
14 % YY(:,:,i) = sum_t w(i,t) y(:,t) y(:,t)' = weighted outer product
15 % Y(:,i) = sum_t w(i,t) y(:,t) = weighted observations
16 % YTY(i) = sum_t w(i,t) y(:,t)' y(:,t) = weighted inner product
17 % You only need to pass in YTY if Sigma is to be estimated as spherical.
18 %
19 % Optional parameters may be passed as 'param_name', param_value pairs.
20 % Parameter names are shown below; default values in [] - if none, argument is mandatory.
21 %
22 % 'cov_type' - 'full', 'diag' or 'spherical' ['full']
23 % 'tied_cov' - 1 (Sigma) or 0 (Sigma_i) [0]
24 % 'clamped_cov' - pass in clamped value, or [] if unclamped [ [] ]
25 % 'clamped_mean' - pass in clamped value, or [] if unclamped [ [] ]
26 % 'cov_prior' - Lambda_i, added to YY(:,:,i) [0.01*eye(d,d,Q)]
27 %
28 % If covariance is tied, Sigma has size d*d.
29 % But diagonal and spherical covariances are represented in full size.
30
31 [cov_type, tied_cov, clamped_cov, clamped_mean, cov_prior, other] = ...
32 process_options(varargin,...
33 'cov_type', 'full', 'tied_cov', 0, 'clamped_cov', [], 'clamped_mean', [], ...
34 'cov_prior', []);
35
36 [Ysz Q] = size(Y);
37 N = sum(w);
38 if isempty(cov_prior)
39 %cov_prior = zeros(Ysz, Ysz, Q);
40 %for q=1:Q
41 % cov_prior(:,:,q) = 0.01*cov(Y(:,q)');
42 %end
43 cov_prior = repmat(0.01*eye(Ysz,Ysz), [1 1 Q]);
44 end
45 %YY = reshape(YY, [Ysz Ysz Q]) + cov_prior; % regularize the scatter matrix
46 YY = reshape(YY, [Ysz Ysz Q]);
47
48 % Set any zero weights to one before dividing
49 % This is valid because w(i)=0 => Y(:,i)=0, etc
50 w = w + (w==0);
51
52 if ~isempty(clamped_mean)
53 mu = clamped_mean;
54 else
55 % eqn 6
56 %mu = Y ./ repmat(w(:)', [Ysz 1]);% Y may have a funny size
57 mu = zeros(Ysz, Q);
58 for i=1:Q
59 mu(:,i) = Y(:,i) / w(i);
60 end
61 end
62
63 if ~isempty(clamped_cov)
64 Sigma = clamped_cov;
65 return;
66 end
67
68 if ~tied_cov
69 Sigma = zeros(Ysz,Ysz,Q);
70 for i=1:Q
71 if cov_type(1) == 's'
72 % eqn 17
73 s2 = (1/Ysz)*( (YTY(i)/w(i)) - mu(:,i)'*mu(:,i) );
74 Sigma(:,:,i) = s2 * eye(Ysz);
75 else
76 % eqn 12
77 SS = YY(:,:,i)/w(i) - mu(:,i)*mu(:,i)';
78 if cov_type(1)=='d'
79 SS = diag(diag(SS));
80 end
81 Sigma(:,:,i) = SS;
82 end
83 end
84 else % tied cov
85 if cov_type(1) == 's'
86 % eqn 19
87 s2 = (1/(N*Ysz))*(sum(YTY,2) + sum(diag(mu'*mu) .* w));
88 Sigma = s2*eye(Ysz);
89 else
90 SS = zeros(Ysz, Ysz);
91 % eqn 15
92 for i=1:Q % probably could vectorize this...
93 SS = SS + YY(:,:,i)/N - mu(:,i)*mu(:,i)';
94 end
95 if cov_type(1) == 'd'
96 Sigma = diag(diag(SS));
97 else
98 Sigma = SS;
99 end
100 end
101 end
102
103 if tied_cov
104 Sigma = repmat(Sigma, [1 1 Q]);
105 end
106 Sigma = Sigma + cov_prior;