I am implementing a function for calculating the likelihood of a point on multivariate gaussian distribution. I want to test it with extreme cases.
function [P,E] = gauss_pdf(X,M,S)
% In:
% X - Dx1 value or N values as DxN matrix
% M - Dx1 mean of distibution or N values as DxN matrix.
% S - DxD covariance matrix
%
% Out:
% P - Probability of X.
% E - Negative logarithm of P
if size(M,2) == 1
DX = X-repmat(M,1,size(X,2));
E = 0.5*sum(DX.*(S\DX),1);
d = size(M,1);
E = E + 0.5 * d * log(2*pi) + 0.5 * log(det(S));
P = (1 / sqrt(2*pi*det(S))) * exp(-E);
elseif size(X,2) == 1
DX = repmat(X,1,size(M,2))-M;
E = 0.5*sum(DX.*(S\DX),1);
d = size(M,1);
E = E + 0.5 * d * log(2*pi) + 0.5 * log(det(S));
P = (1 / sqrt(2*pi*det(S))) * exp(-E);
else
DX = X-M;
E = 0.5*DX'*(S\DX);
d = size(M,1);
E = E + 0.5 * d * log(2*pi) + 0.5 * log(det(S));
P = (1 / sqrt(2*pi*det(S))) * exp(-E);
end
I do not get errors, however I use this piece of code in extreme cases as well, how can I be sure that it is fine?
Aucun commentaire:
Enregistrer un commentaire