-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKLDiv.m
38 lines (30 loc) · 1010 Bytes
/
KLDiv.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
function dist=KLDiv(P,Q)
% dist = KLDiv(P,Q) Kullback-Leibler divergence of two discrete probability
% distributions
% P and Q are automatically normalised to have the sum of one on rows
% have the length of one at each
% P = n x nbins
% Q = 1 x nbins or n x nbins(one to one)
% dist = n x 1
P(isnan(P))=0;
if size(P,2)~=size(Q,2)
error('the number of columns in P and Q should be the same');
end
if sum(~isfinite(P(:))) + sum(~isfinite(Q(:)))
% keyboard
error('the inputs contain non-finite values!')
end
% normalizing the P and Q
if size(Q,1)==1
Q = Q ./sum(Q);
P = P ./repmat(sum(P,2),[1 size(P,2)]);
temp = P.*log(P./repmat(Q,[size(P,1) 1]));
temp(isnan(temp))=0;% resolving the case when P(i)==0
dist = sum(temp,2);
elseif size(Q,1)==size(P,1)
Q = Q ./repmat(sum(Q,2),[1 size(Q,2)]);
P = P ./repmat(sum(P,2),[1 size(P,2)]);
temp = P.*log(P./Q);
temp(isnan(temp))=0; % resolving the case when P(i)==0
dist = sum(temp,2);
end