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