Daniel@0: function [mu, Sigma] = mixgauss_Mstep(w, Y, YY, YTY, varargin) Daniel@0: % MSTEP_COND_GAUSS Compute MLEs for mixture of Gaussians given expected sufficient statistics Daniel@0: % function [mu, Sigma] = Mstep_cond_gauss(w, Y, YY, YTY, varargin) Daniel@0: % Daniel@0: % We assume P(Y|Q=i) = N(Y; mu_i, Sigma_i) Daniel@0: % and w(i,t) = p(Q(t)=i|y(t)) = posterior responsibility Daniel@0: % See www.ai.mit.edu/~murphyk/Papers/learncg.pdf. Daniel@0: % Daniel@0: % INPUTS: Daniel@0: % w(i) = sum_t w(i,t) = responsibilities for each mixture component Daniel@0: % If there is only one mixture component (i.e., Q does not exist), Daniel@0: % then w(i) = N = nsamples, and Daniel@0: % all references to i can be replaced by 1. Daniel@0: % YY(:,:,i) = sum_t w(i,t) y(:,t) y(:,t)' = weighted outer product Daniel@0: % Y(:,i) = sum_t w(i,t) y(:,t) = weighted observations Daniel@0: % YTY(i) = sum_t w(i,t) y(:,t)' y(:,t) = weighted inner product Daniel@0: % You only need to pass in YTY if Sigma is to be estimated as spherical. Daniel@0: % Daniel@0: % Optional parameters may be passed as 'param_name', param_value pairs. Daniel@0: % Parameter names are shown below; default values in [] - if none, argument is mandatory. Daniel@0: % Daniel@0: % 'cov_type' - 'full', 'diag' or 'spherical' ['full'] Daniel@0: % 'tied_cov' - 1 (Sigma) or 0 (Sigma_i) [0] Daniel@0: % 'clamped_cov' - pass in clamped value, or [] if unclamped [ [] ] Daniel@0: % 'clamped_mean' - pass in clamped value, or [] if unclamped [ [] ] Daniel@0: % 'cov_prior' - Lambda_i, added to YY(:,:,i) [0.01*eye(d,d,Q)] Daniel@0: % Daniel@0: % If covariance is tied, Sigma has size d*d. Daniel@0: % But diagonal and spherical covariances are represented in full size. Daniel@0: Daniel@0: [cov_type, tied_cov, clamped_cov, clamped_mean, cov_prior, other] = ... Daniel@0: process_options(varargin,... Daniel@0: 'cov_type', 'full', 'tied_cov', 0, 'clamped_cov', [], 'clamped_mean', [], ... Daniel@0: 'cov_prior', []); Daniel@0: Daniel@0: [Ysz Q] = size(Y); Daniel@0: N = sum(w); Daniel@0: if isempty(cov_prior) Daniel@0: %cov_prior = zeros(Ysz, Ysz, Q); Daniel@0: %for q=1:Q Daniel@0: % cov_prior(:,:,q) = 0.01*cov(Y(:,q)'); Daniel@0: %end Daniel@0: cov_prior = repmat(0.01*eye(Ysz,Ysz), [1 1 Q]); Daniel@0: end Daniel@0: %YY = reshape(YY, [Ysz Ysz Q]) + cov_prior; % regularize the scatter matrix Daniel@0: YY = reshape(YY, [Ysz Ysz Q]); Daniel@0: Daniel@0: % Set any zero weights to one before dividing Daniel@0: % This is valid because w(i)=0 => Y(:,i)=0, etc Daniel@0: w = w + (w==0); Daniel@0: Daniel@0: if ~isempty(clamped_mean) Daniel@0: mu = clamped_mean; Daniel@0: else Daniel@0: % eqn 6 Daniel@0: %mu = Y ./ repmat(w(:)', [Ysz 1]);% Y may have a funny size Daniel@0: mu = zeros(Ysz, Q); Daniel@0: for i=1:Q Daniel@0: mu(:,i) = Y(:,i) / w(i); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if ~isempty(clamped_cov) Daniel@0: Sigma = clamped_cov; Daniel@0: return; Daniel@0: end Daniel@0: Daniel@0: if ~tied_cov Daniel@0: Sigma = zeros(Ysz,Ysz,Q); Daniel@0: for i=1:Q Daniel@0: if cov_type(1) == 's' Daniel@0: % eqn 17 Daniel@0: s2 = (1/Ysz)*( (YTY(i)/w(i)) - mu(:,i)'*mu(:,i) ); Daniel@0: Sigma(:,:,i) = s2 * eye(Ysz); Daniel@0: else Daniel@0: % eqn 12 Daniel@0: SS = YY(:,:,i)/w(i) - mu(:,i)*mu(:,i)'; Daniel@0: if cov_type(1)=='d' Daniel@0: SS = diag(diag(SS)); Daniel@0: end Daniel@0: Sigma(:,:,i) = SS; Daniel@0: end Daniel@0: end Daniel@0: else % tied cov Daniel@0: if cov_type(1) == 's' Daniel@0: % eqn 19 Daniel@0: s2 = (1/(N*Ysz))*(sum(YTY,2) + sum(diag(mu'*mu) .* w)); Daniel@0: Sigma = s2*eye(Ysz); Daniel@0: else Daniel@0: SS = zeros(Ysz, Ysz); Daniel@0: % eqn 15 Daniel@0: for i=1:Q % probably could vectorize this... Daniel@0: SS = SS + YY(:,:,i)/N - mu(:,i)*mu(:,i)'; Daniel@0: end Daniel@0: if cov_type(1) == 'd' Daniel@0: Sigma = diag(diag(SS)); Daniel@0: else Daniel@0: Sigma = SS; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if tied_cov Daniel@0: Sigma = repmat(Sigma, [1 1 Q]); Daniel@0: end Daniel@0: Sigma = Sigma + cov_prior;