annotate toolboxes/FullBNT-1.0.7/KPMtools/collapse_mog.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [new_mu, new_Sigma, new_Sigma2] = collapse_mog(mu, Sigma, coefs)
wolffd@0 2 % COLLAPSE_MOG Collapse a mixture of Gaussians to a single Gaussian by moment matching
wolffd@0 3 % [new_mu, new_Sigma] = collapse_mog(mu, Sigma, coefs)
wolffd@0 4 %
wolffd@0 5 % coefs(i) - weight of i'th mixture component
wolffd@0 6 % mu(:,i), Sigma(:,:,i) - params of i'th mixture component
wolffd@0 7
wolffd@0 8 % S = sum_c w_c (S_c + m_c m_c' + m m' - 2 m_c m')
wolffd@0 9 % = sum_c w_c (S_c + m_c m_c') + m m' - 2 (sum_c m_c) m'
wolffd@0 10 % = sum_c w_c (S_c + m_c m_c') - m m'
wolffd@0 11
wolffd@0 12 new_mu = sum(mu * diag(coefs), 2); % weighted sum of columns
wolffd@0 13
wolffd@0 14 n = length(new_mu);
wolffd@0 15 new_Sigma = zeros(n,n);
wolffd@0 16 new_Sigma2 = zeros(n,n);
wolffd@0 17 for j=1:length(coefs)
wolffd@0 18 m = mu(:,j) - new_mu;
wolffd@0 19 new_Sigma = new_Sigma + coefs(j) * (Sigma(:,:,j) + m*m');
wolffd@0 20 new_Sigma2 = new_Sigma2 + coefs(j) * (Sigma(:,:,j) + mu(:,j)*mu(:,j)');
wolffd@0 21 end
wolffd@0 22 %assert(approxeq(new_Sigma, new_Sigma2 - new_mu*new_mu'))