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