-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfcnmultinormpdf.m
71 lines (57 loc) · 1.38 KB
/
fcnmultinormpdf.m
1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
% Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
function [pdf, lpdf] = fcnmultinormpdf(x, u, C)
%Multivariate normal distribution
%x = evaluation point (n x k)
%u = mean (k x 1)
%C = covariance matrix (k X k)
%DEFINE CONSTANTS
k = numel(u);
%GET DETERMINANT
scaling = mean(diag(C));
sdetC=abs(det(C/scaling)); %SCALED determinant of C
logdetC = log(sdetC) + scaling*k;
invC = C^-1;
%detC=abs(det(C));
%den = 1/((2*pi)^(k/2)*sqrt(detC)); %denominator
logden = - (log(2*pi)*(k/2) + logdetC/2);
%CHANGE u TO COLUMN
[rowsu, ~] = size(u);
if rowsu==1
u=u';
end
if numel(x)==numel(u) && all(size(x)==size(u))
dx1 = x-u;
lpdf0 = sum((dx1*invC).*dx1,2);
lpdf = (-1/2)*lpdf0 + logden;
pdf = exp(lpdf);
return
end
%RESHAPE x to 1D
sizex0 = numel(x);
if sizex0>1
sizex0 = size(x);
ne = numel(sizex0);
if numel(sizex0)>2
x = reshape(x, prod(sizex0(1:ne-1)), k);
elseif sizex0(ne)~=k
x = reshape(x, prod(sizex0), k);
end
end
% dx1 = zeros(size(x));
% for i = 1:k
% dx1(:,i) = x(:,i)-u(i);
% end
% lpdf0 = sum((dx1*invC).*dx1,2);
% lpdf = (-1/2)*lpdf0 + log(den);
for i = 1:k
x(:,i) = x(:,i)-u(i);
end
lpdf = (-1/2)*sum((x*invC).*x,2) + logden;
%RESHAPE FROM 1D TO ORIGINAL FORMAT
if numel(sizex0)>2
lpdf = reshape(lpdf, sizex0(1:ne-1));
elseif sizex0(ne)~=k
lpdf = reshape(lpdf, sizex0);
end
pdf = exp(lpdf);
end