Daniel@0: function CPD = maximize_params(CPD, temp) Daniel@0: % MAXIMIZE_PARAMS Set the params of a CPD to their ML values (Gaussian) Daniel@0: % CPD = maximize_params(CPD, temperature) Daniel@0: % Daniel@0: % Temperature is currently ignored. Daniel@0: Daniel@0: if ~adjustable_CPD(CPD), return; end Daniel@0: Daniel@0: CPD1 = struct(new_maximize_params(CPD)); Daniel@0: CPD2 = struct(old_maximize_params(CPD)); Daniel@0: assert(approxeq(CPD1.mean, CPD2.mean)) Daniel@0: assert(approxeq(CPD1.cov, CPD2.cov)) Daniel@0: assert(approxeq(CPD1.weights, CPD2.weights)) Daniel@0: Daniel@0: CPD = new_maximize_params(CPD); Daniel@0: Daniel@0: %%%%%%% Daniel@0: function CPD = new_maximize_params(CPD) Daniel@0: Daniel@0: if CPD.clamped_mean Daniel@0: cl_mean = CPD.mean; Daniel@0: else Daniel@0: cl_mean = []; Daniel@0: end Daniel@0: Daniel@0: if CPD.clamped_cov Daniel@0: cl_cov = CPD.cov; Daniel@0: else Daniel@0: cl_cov = []; Daniel@0: end Daniel@0: Daniel@0: if CPD.clamped_weights Daniel@0: cl_weights = CPD.weights; Daniel@0: else Daniel@0: cl_weights = []; Daniel@0: end Daniel@0: Daniel@0: [ssz psz Q] = size(CPD.weights); Daniel@0: Daniel@0: prior = repmat(CPD.cov_prior_weight*eye(ssz,ssz), [1 1 Q]); Daniel@0: [CPD.mean, CPD.cov, CPD.weights] = ... Daniel@0: Mstep_clg('w', CPD.Wsum, 'YY', CPD.WYYsum, 'Y', CPD.WYsum, 'YTY', [], ... Daniel@0: 'XX', CPD.WXXsum, 'XY', CPD.WXYsum, 'X', CPD.WXsum, ... Daniel@0: 'cov_type', CPD.cov_type, 'clamped_mean', cl_mean, ... Daniel@0: 'clamped_cov', cl_cov, 'clamped_weights', cl_weights, ... Daniel@0: 'tied_cov', CPD.tied_cov, ... Daniel@0: 'cov_prior', prior); Daniel@0: Daniel@0: Daniel@0: %%%%%%%%%%% Daniel@0: Daniel@0: function CPD = old_maximize_params(CPD) Daniel@0: Daniel@0: Daniel@0: if ~adjustable_CPD(CPD), return; end Daniel@0: Daniel@0: %assert(approxeq(CPD.nsamples, sum(CPD.Wsum))); Daniel@0: assert(~any(isnan(CPD.WXXsum))) Daniel@0: assert(~any(isnan(CPD.WXYsum))) Daniel@0: assert(~any(isnan(CPD.WYYsum))) Daniel@0: Daniel@0: [self_size cpsize dpsize] = size(CPD.weights); Daniel@0: Daniel@0: % Append 1s to the parents, and derive the corresponding cross products. Daniel@0: % This is used when estimate the means and weights simultaneosuly, Daniel@0: % and when estimatting Sigma. Daniel@0: % Let x2 = [x 1]' Daniel@0: XY = zeros(cpsize+1, self_size, dpsize); % XY(:,:,i) = sum_l w(l,i) x2(l) y(l)' Daniel@0: XX = zeros(cpsize+1, cpsize+1, dpsize); % XX(:,:,i) = sum_l w(l,i) x2(l) x2(l)' Daniel@0: YY = zeros(self_size, self_size, dpsize); % YY(:,:,i) = sum_l w(l,i) y(l) y(l)' Daniel@0: for i=1:dpsize Daniel@0: XY(:,:,i) = [CPD.WXYsum(:,:,i) % X*Y Daniel@0: CPD.WYsum(:,i)']; % 1*Y Daniel@0: % [x * [x' 1] = [xx' x Daniel@0: % 1] x' 1] Daniel@0: XX(:,:,i) = [CPD.WXXsum(:,:,i) CPD.WXsum(:,i); Daniel@0: CPD.WXsum(:,i)' CPD.Wsum(i)]; Daniel@0: YY(:,:,i) = CPD.WYYsum(:,:,i); Daniel@0: end Daniel@0: Daniel@0: w = CPD.Wsum(:); Daniel@0: % Set any zeros to one before dividing Daniel@0: % This is valid because w(i)=0 => WYsum(:,i)=0, etc Daniel@0: w = w + (w==0); Daniel@0: Daniel@0: if CPD.clamped_mean Daniel@0: % Estimating B2 and then setting the last column (the mean) to the clamped mean is *not* equivalent Daniel@0: % to estimating B and then adding the clamped_mean to the last column. Daniel@0: if ~CPD.clamped_weights Daniel@0: B = zeros(self_size, cpsize, dpsize); Daniel@0: for i=1:dpsize Daniel@0: if det(CPD.WXXsum(:,:,i))==0 Daniel@0: B(:,:,i) = 0; Daniel@0: else Daniel@0: % Eqn 9 in table 2 of TR Daniel@0: %B(:,:,i) = CPD.WXYsum(:,:,i)' * inv(CPD.WXXsum(:,:,i)); Daniel@0: B(:,:,i) = (CPD.WXXsum(:,:,i) \ CPD.WXYsum(:,:,i))'; Daniel@0: end Daniel@0: end Daniel@0: %CPD.weights = reshape(B, [self_size cpsize dpsize]); Daniel@0: CPD.weights = B; Daniel@0: end Daniel@0: elseif CPD.clamped_weights % KPM 1/25/02 Daniel@0: if ~CPD.clamped_mean % ML estimate is just sample mean of the residuals Daniel@0: for i=1:dpsize Daniel@0: CPD.mean(:,i) = (CPD.WYsum(:,i) - CPD.weights(:,:,i) * CPD.WXsum(:,i)) / w(i); Daniel@0: end Daniel@0: end Daniel@0: else % nothing is clamped, so estimate mean and weights simultaneously Daniel@0: B2 = zeros(self_size, cpsize+1, dpsize); Daniel@0: for i=1:dpsize Daniel@0: if det(XX(:,:,i))==0 % fix by U. Sondhauss 6/27/99 Daniel@0: B2(:,:,i)=0; Daniel@0: else Daniel@0: % Eqn 9 in table 2 of TR Daniel@0: %B2(:,:,i) = XY(:,:,i)' * inv(XX(:,:,i)); Daniel@0: B2(:,:,i) = (XX(:,:,i) \ XY(:,:,i))'; Daniel@0: end Daniel@0: CPD.mean(:,i) = B2(:,cpsize+1,i); Daniel@0: CPD.weights(:,:,i) = B2(:,1:cpsize,i); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % Let B2 = [W mu] Daniel@0: if cpsize>0 Daniel@0: B2(:,1:cpsize,:) = reshape(CPD.weights, [self_size cpsize dpsize]); Daniel@0: end Daniel@0: B2(:,cpsize+1,:) = reshape(CPD.mean, [self_size dpsize]); Daniel@0: Daniel@0: % To avoid singular covariance matrices, Daniel@0: % we use the regularization method suggested in "A Quasi-Bayesian approach to estimating Daniel@0: % parameters for mixtures of normal distributions", Hamilton 91. Daniel@0: % If the ML estimate is Sigma = M/N, the MAP estimate is (M+gamma*I) / (N+gamma), Daniel@0: % where gamma >=0 is a smoothing parameter (equivalent sample size of I prior) Daniel@0: Daniel@0: gamma = CPD.cov_prior_weight; Daniel@0: Daniel@0: if ~CPD.clamped_cov Daniel@0: if CPD.cov_prior_entropic % eqn 12 of Brand AI/Stat 99 Daniel@0: Z = 1-temp; Daniel@0: % When temp > 1, Z is negative, so we are dividing by a smaller Daniel@0: % number, ie. increasing the variance. Daniel@0: else Daniel@0: Z = 0; Daniel@0: end Daniel@0: if CPD.tied_cov Daniel@0: S = zeros(self_size, self_size); Daniel@0: % Eqn 2 from table 2 in TR Daniel@0: for i=1:dpsize Daniel@0: S = S + (YY(:,:,i) - B2(:,:,i)*XY(:,:,i)); Daniel@0: end Daniel@0: %denom = CPD.nsamples + gamma + Z; Daniel@0: denom = CPD.nsamples + Z; Daniel@0: S = (S + gamma*eye(self_size)) / denom; Daniel@0: if strcmp(CPD.cov_type, 'diag') Daniel@0: S = diag(diag(S)); Daniel@0: end Daniel@0: CPD.cov = repmat(S, [1 1 dpsize]); Daniel@0: else Daniel@0: for i=1:dpsize Daniel@0: % Eqn 1 from table 2 in TR Daniel@0: S = YY(:,:,i) - B2(:,:,i)*XY(:,:,i); Daniel@0: %denom = w(i) + gamma + Z; Daniel@0: denom = w(i) + Z; Daniel@0: S = (S + gamma*eye(self_size)) / denom; Daniel@0: CPD.cov(:,:,i) = S; Daniel@0: end Daniel@0: if strcmp(CPD.cov_type, 'diag') Daniel@0: for i=1:dpsize Daniel@0: CPD.cov(:,:,i) = diag(diag(CPD.cov(:,:,i))); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: check_covars = 0; Daniel@0: min_covar = 1e-5; Daniel@0: if check_covars % prevent collapsing to a point Daniel@0: for i=1:dpsize Daniel@0: if min(svd(CPD.cov(:,:,i))) < min_covar Daniel@0: disp(['resetting singular covariance for node ' num2str(CPD.self)]); Daniel@0: CPD.cov(:,:,i) = CPD.init_cov(:,:,i); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: