init_GMM_timeBased.m 2.64 KB
Newer Older
Sylvain Calinon's avatar
Sylvain Calinon committed
1
function model = init_GMM_timeBased(Data, model)
2
% This function initializes the parameters of a Gaussian Mixture Model
3
% (GMM) by splitting the data into equal bins (time-based clustering).
Sylvain Calinon's avatar
Sylvain Calinon committed
4 5 6 7 8 9 10
% Inputs -----------------------------------------------------------------
%   o Data:     D x N array representing N datapoints of D dimensions.
%   o nbStates: Number K of GMM components.
% Outputs ----------------------------------------------------------------
%   o Priors:   1 x K array representing the prior probabilities of the
%               K GMM components.
%   o Mu:       D x K array representing the centers of the K GMM components.
11
%   o Sigma:    D x D x K array representing the covariance matrices of the
Sylvain Calinon's avatar
Sylvain Calinon committed
12 13
%               K GMM components.
%
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
% 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"
% }
%
% 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/>.

Sylvain Calinon's avatar
Sylvain Calinon committed
43

44
[nbVar, nbData] = size(Data);
45 46
%diagRegularizationFactor = 1E-2; %Optional regularization term
diagRegularizationFactor = 1E-8; %Optional regularization term
Sylvain Calinon's avatar
Sylvain Calinon committed
47

48 49
TimingSep = linspace(min(Data(1,:)), max(Data(1,:)), model.nbStates+1);

Sylvain Calinon's avatar
Sylvain Calinon committed
50
for i=1:model.nbStates
51 52 53 54
	idtmp = find( Data(1,:)>=TimingSep(i) & Data(1,:)<TimingSep(i+1));
	model.Priors(i) = length(idtmp);
	model.Mu(:,i) = mean(Data(:,idtmp)');
	model.Sigma(:,:,i) = cov(Data(:,idtmp)');
55
	%Optional regularization term to avoid numerical instability
56
	model.Sigma(:,:,i) = model.Sigma(:,:,i) + eye(nbVar)*diagRegularizationFactor;
Sylvain Calinon's avatar
Sylvain Calinon committed
57 58
end
model.Priors = model.Priors / sum(model.Priors);