From 2918c7fc9a4db37f03a0a94d455e393b26fc5e72 Mon Sep 17 00:00:00 2001 From: Mo Chen Date: Mon, 7 Mar 2016 17:12:13 +0800 Subject: [PATCH] removed mixMnEm --- chapter09/mixBernEm.m | 2 +- chapter09/mixMnEm.m | 48 ------------------------------------------- 2 files changed, 1 insertion(+), 49 deletions(-) delete mode 100644 chapter09/mixMnEm.m diff --git a/chapter09/mixBernEm.m b/chapter09/mixBernEm.m index 6c94a40..65612e4 100644 --- a/chapter09/mixBernEm.m +++ b/chapter09/mixBernEm.m @@ -1,7 +1,7 @@ function [label, model, llh] = mixBernEm(X, k) % Perform EM algorithm for fitting the Bernoulli mixture model. % Input: -% X: d x n data matrix +% X: d x n binary (0/1) data matrix % k: number of cluster % Output: % label: 1 x n cluster label diff --git a/chapter09/mixMnEm.m b/chapter09/mixMnEm.m deleted file mode 100644 index d22b944..0000000 --- a/chapter09/mixMnEm.m +++ /dev/null @@ -1,48 +0,0 @@ -function [label, model, llh] = mixMnEm(X, k) -% Perform EM algorithm for fitting the multinomial mixture model. -% Input: -% X: d x n data matrix -% k: number of cluster -% Output: -% label: 1 x n cluster label -% model: trained model structure -% llh: loglikelihood -% Written by Mo Chen (sth4nth@gmail.com). -%% initialization -fprintf('EM for mixture model: running ... \n'); -n = size(X,2); -label = ceil(k*rand(1,n)); % random initialization -R = sparse(1:n,label,1,n,k,n); -tol = 1e-8; -maxiter = 500; -llh = -inf(1,maxiter); -for iter = 2:maxiter - model = maximization(X,R); - [R, llh(iter)] = expectation(X,model); - if abs(llh(iter)-llh(iter-1)) < tol*abs(llh(iter)); break; end; -end -[~,label(:)] = max(R,[],2); -llh = llh(2:iter); - -function [R, llh] = expectation(X, model) -mu = model.mu; -w = model.weight; -n = size(X,2); -logRho = X'*log(mu); -logRho = bsxfun(@plus,logRho,log(w)); -T = logsumexp(logRho,2); -llh = sum(T)/n; % loglikelihood -logR = bsxfun(@minus,logRho,T); -R = exp(logR); - -function model = maximization(X, R) -n = size(X,2); -nk = sum(R,1); -w = nk/n; -mu = bsxfun(@times, X*R, 1./nk); -d = size(X,1); -lambda = 1e-4; -prior = (1/d)*ones(d,1); -mu = bsxfun(@plus,(1-lambda)*mu,lambda*prior); -model.mu = mu; -model.weight = w; \ No newline at end of file