productTPGMM.m 1.99 KB
Newer Older
1
function [Mu, Sigma] = productTPGMM(model, p)
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
% Compute the product of Gaussians for a task-parametrized model where the
% set of parameters are stored in the variable 'p'.
%
% 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/>.
33

34

35 36 37
%diagRegularizationFactor = 1E-6; %Optional regularization term
diagRegularizationFactor = 1E-4; %Optional regularization term

38 39
for i=1:model.nbStates
	%Reallocating
Milad Malekzadeh's avatar
Milad Malekzadeh committed
40 41
	SigmaTmp = zeros(model.nbVar);
	MuTmp = zeros(model.nbVar,1);
42 43 44 45
	%Product of Gaussians
	for m=1:model.nbFrames
		MuP = p(m).A * model.Mu(1:model.nbVars(m),m,i) + p(m).b;
		SigmaP = p(m).A * model.Sigma(1:model.nbVars(m),1:model.nbVars(m),m,i) * p(m).A' + eye(model.nbVar)*diagRegularizationFactor;
Milad Malekzadeh's avatar
Milad Malekzadeh committed
46
		SigmaTmp = SigmaTmp + inv(SigmaP);
Milad Malekzadeh's avatar
Milad Malekzadeh committed
47
		MuTmp = MuTmp + SigmaP\MuP;
Milad Malekzadeh's avatar
Milad Malekzadeh committed
48 49
	end
	Sigma(:,:,i) = inv(SigmaTmp);
Milad Malekzadeh's avatar
Milad Malekzadeh committed
50
	Mu(:,i) = Sigma(:,:,i) * MuTmp;
51
end