EM_HDGMM.m 3.66 KB
Newer Older
1
function [model, GAMMA2] = EM_HDGMM(Data, model)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
% EM for High Dimensional Data Clustering (HDDC, HD-GMM) model proposed by Bouveyron (2007). 
%
% Writing code takes time. Polishing it and making it available to others takes longer! 
% If some parts of the code were useful for your research of for a better understanding 
% of the algorithms, please reward the authors by citing the related publications, 
% and consider making your own research available in this way.
%
% @article{Calinon15,
%   author="Calinon, S.",
%   title="A Tutorial on Task-Parameterized Movement Learning and Retrieval",
%   journal="Intelligent Service Robotics",
%   year="2015"
% }
% 
% @article{Bouveyron07,
% 	author = "Bouveyron, C. and Girard, S. and Schmid, C.",
% 	title = "High-dimensional data clustering",
% 	journal = "Computational Statistics and Data Analysis",
% 	year = "2007",
% 	volume = "52",
% 	number = "1",
% 	pages = "502--519"
% }
%
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
% 
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
% 
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
% 
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.

43 44 45 46 47 48 49

%Parameters of the EM iterations
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);

50
diagRegularizationFactor = 1E-8; %Regularization term is optional
51 52 53 54 55 56 57 58 59 60

%EM loop
for nbIter=1:nbMaxSteps
	fprintf('.');
	
	%E-step
	[Lik, GAMMA] = computeGamma(Data, model); %See 'computeGamma' function below
	GAMMA2 = GAMMA ./ repmat(sum(GAMMA,2), 1, nbData);
	
	%M-step
61
	%Update Priors
62 63
	model.Priors = sum(GAMMA,2)/nbData;
	
64
	%Update Mu
65 66
	model.Mu = Data * GAMMA2';
	
67
	%Update factor analyser parameters
68 69 70 71 72
	for i=1:model.nbStates
		%Compute covariance
		DataTmp = Data - repmat(model.Mu(:,i),1,nbData);
		S(:,:,i) = DataTmp * diag(GAMMA2(i,:)) * DataTmp' + eye(model.nbVar) * diagRegularizationFactor;

73
		%HDGMM update
74 75 76 77 78 79 80 81
		[V,D] = eig(S(:,:,i)); 
		[~,id] = sort(diag(D),'descend');
% 		model.D(:,:,i) = D(id(1:model.nbFA), id(1:model.nbFA));
% 		model.V(:,:,i) = V(:, id(1:model.nbFA)); 
		d = diag(D);
		model.D(:,:,i) = diag([d(id(1:model.nbFA)); repmat(mean(d(id(model.nbFA+1:end))), model.nbVar-model.nbFA, 1)]);
		model.V(:,:,i) = V(:,id); 
	
82
		%Reconstruct Sigma
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		model.Sigma(:,:,i) = model.V(:,:,i) * model.D(:,:,i) * model.V(:,:,i)' + eye(model.nbVar) * diagRegularizationFactor;
	end
	
	%Compute average log-likelihood
	LL(nbIter) = sum(log(sum(Lik,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
end
disp(['The maximum number of ' num2str(nbMaxSteps) ' EM iterations has been reached.']);
end

99

100 101 102 103 104 105 106 107
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Lik, GAMMA] = computeGamma(Data, model)
Lik = zeros(model.nbStates,size(Data,2));
for i=1:model.nbStates
	Lik(i,:) = model.Priors(i) * gaussPDF(Data, model.Mu(:,i), model.Sigma(:,:,i));
end
GAMMA = Lik ./ repmat(sum(Lik,1)+realmin, model.nbStates, 1);
end