wolffd@0
|
1 function [mu, B] = clg_Mstep_simple(w, Y, YY, YTY, X, XX, XY)
|
wolffd@0
|
2 % CLG_MSTEP_SIMPLE Same as CLG_MSTEP, but doesn;t estimate Sigma, so is slightly faster
|
wolffd@0
|
3 % function [mu, B] = clg_Mstep_simple(w, Y, YY, YTY, X, XX, XY)
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % See clg_Mstep for details.
|
wolffd@0
|
6 % Unlike clg_Mstep, there are no optional arguments, which are slow to process
|
wolffd@0
|
7 % if this function is inside a tight loop.
|
wolffd@0
|
8
|
wolffd@0
|
9 [Ysz Q] = size(Y);
|
wolffd@0
|
10
|
wolffd@0
|
11 if isempty(X) % no regression
|
wolffd@0
|
12 %B = [];
|
wolffd@0
|
13 B2 = zeros(Ysz, 1, Q);
|
wolffd@0
|
14 for i=1:Q
|
wolffd@0
|
15 B(:,:,i) = B2(:,1:0,i); % make an empty array of size Ysz x 0 x Q
|
wolffd@0
|
16 end
|
wolffd@0
|
17 [mu, Sigma] = mixgauss_Mstep(w, Y, YY, YTY);
|
wolffd@0
|
18 return;
|
wolffd@0
|
19 end
|
wolffd@0
|
20
|
wolffd@0
|
21 N = sum(w);
|
wolffd@0
|
22 %YY = YY + cov_prior; % regularize the scatter matrix
|
wolffd@0
|
23
|
wolffd@0
|
24 % Set any zero weights to one before dividing
|
wolffd@0
|
25 % This is valid because w(i)=0 => Y(:,i)=0, etc
|
wolffd@0
|
26 w = w + (w==0);
|
wolffd@0
|
27
|
wolffd@0
|
28 Xsz = size(X,1);
|
wolffd@0
|
29 % Append 1 to X to get Z
|
wolffd@0
|
30 ZZ = zeros(Xsz+1, Xsz+1, Q);
|
wolffd@0
|
31 ZY = zeros(Xsz+1, Ysz, Q);
|
wolffd@0
|
32 for i=1:Q
|
wolffd@0
|
33 ZZ(:,:,i) = [XX(:,:,i) X(:,i);
|
wolffd@0
|
34 X(:,i)' w(i)];
|
wolffd@0
|
35 ZY(:,:,i) = [XY(:,:,i);
|
wolffd@0
|
36 Y(:,i)'];
|
wolffd@0
|
37 end
|
wolffd@0
|
38
|
wolffd@0
|
39 mu = zeros(Ysz, Q);
|
wolffd@0
|
40 B = zeros(Ysz, Xsz, Q);
|
wolffd@0
|
41 for i=1:Q
|
wolffd@0
|
42 % eqn 9
|
wolffd@0
|
43 if rcond(ZZ(:,:,i)) < 1e-10
|
wolffd@0
|
44 sprintf('clg_Mstep warning: ZZ(:,:,%d) is ill-conditioned', i);
|
wolffd@0
|
45 %probably because there are too few cases for a high-dimensional input
|
wolffd@0
|
46 ZZ(:,:,i) = ZZ(:,:,i) + 1e-5*eye(Xsz+1);
|
wolffd@0
|
47 end
|
wolffd@0
|
48 %A = ZY(:,:,i)' * inv(ZZ(:,:,i));
|
wolffd@0
|
49 A = (ZZ(:,:,i) \ ZY(:,:,i))';
|
wolffd@0
|
50 B(:,:,i) = A(:, 1:Xsz);
|
wolffd@0
|
51 mu(:,i) = A(:, Xsz+1);
|
wolffd@0
|
52 end
|