EM_GMM.m 2.13 KB
Newer Older
Sylvain Calinon's avatar
Sylvain Calinon committed
1
function [model, GAMMA2, LL] = EM_GMM(Data, model)
2
% Training of a Gaussian mixture model (GMM) with an expectation-maximization (EM) algorithm.
Sylvain Calinon's avatar
Sylvain Calinon committed
3 4
%
% Author:	Sylvain Calinon, 2014
5
%         http://programming-by-demonstration.org/SylvainCalinon
Sylvain Calinon's avatar
Sylvain Calinon committed
6 7 8 9 10 11 12

%Parameters of the EM algorithm
nbMinSteps = 5; %Minimum number of iterations allowed
nbMaxSteps = 100; %Maximum number of iterations allowed
maxDiffLL = 1E-4; %Likelihood increase threshold to stop the algorithm
nbData = size(Data,2);

13 14
%diagRegularizationFactor = 1E-6; %Regularization term is optional, see Eq. (2.1.2) in doc/TechnicalReport.pdf
diagRegularizationFactor = 1E-4; %Regularization term is optional, see Eq. (2.1.2) in doc/TechnicalReport.pdf
Sylvain Calinon's avatar
Sylvain Calinon committed
15 16

for nbIter=1:nbMaxSteps
17 18 19 20 21 22 23 24 25 26 27 28
	fprintf('.');
	
	%E-step
	[L, GAMMA] = computeGamma(Data, model); %See 'computeGamma' function below
	GAMMA2 = GAMMA ./ repmat(sum(GAMMA,2),1,nbData);
	
	%M-step
	for i=1:model.nbStates
		%Update Priors, see Eq. (2.0.6) in doc/TechnicalReport.pdf
		model.Priors(i) = sum(GAMMA(i,:)) / nbData;
		
		%Update Mu, see Eq. (2.0.7) in doc/TechnicalReport.pdf
Sylvain Calinon's avatar
Sylvain Calinon committed
29
		model.Mu(:,i) = Data * GAMMA2(i,:)';
30 31
		
		%Update Sigma, see Eq. (2.0.8) in doc/TechnicalReport.pdf (regularization term is optional, see Eq. (2.1.2))
Sylvain Calinon's avatar
Sylvain Calinon committed
32 33
		DataTmp = Data - repmat(model.Mu(:,i),1,nbData);
		model.Sigma(:,:,i) = DataTmp * diag(GAMMA2(i,:)) * DataTmp' + eye(model.nbVar) * diagRegularizationFactor;
34 35 36 37 38 39 40 41 42 43 44
	end
	
	%Compute average log-likelihood
	LL(nbIter) = sum(log(sum(L,1))) / nbData;
	%Stop the algorithm if EM converged (small change of LL)
	if nbIter>nbMinSteps
		if LL(nbIter)-LL(nbIter-1)<maxDiffLL || nbIter==nbMaxSteps-1
			disp(['EM converged after ' num2str(nbIter) ' iterations.']);
			return;
		end
	end
Sylvain Calinon's avatar
Sylvain Calinon committed
45
end
46
disp(['The maximum number of ' num2str(nbMaxSteps) ' EM iterations has been reached.']);
Sylvain Calinon's avatar
Sylvain Calinon committed
47 48 49 50
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [L, GAMMA] = computeGamma(Data, model)
51 52 53 54 55 56
%See Eq. (2.0.5) in doc/TechnicalReport.pdf
L = zeros(model.nbStates,size(Data,2));
for i=1:model.nbStates
	L(i,:) = model.Priors(i) * gaussPDF(Data, model.Mu(:,i), model.Sigma(:,:,i));
end
GAMMA = L ./ repmat(sum(L,1)+realmin, model.nbStates, 1);
Sylvain Calinon's avatar
Sylvain Calinon committed
57 58 59 60
end