diff _misc/probability/.svn/text-base/KLDiv.m.svn-base @ 8:b5b38998ef3b

added all that other stuff
author matthiasm
date Fri, 11 Apr 2014 15:54:25 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/_misc/probability/.svn/text-base/KLDiv.m.svn-base	Fri Apr 11 15:54:25 2014 +0100
@@ -0,0 +1,35 @@
+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
+
+
+
+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(:)))
+   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)]);
+    dist =  sum(P.*log(P./repmat(Q,[size(P,1) 1])),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)]);
+    dist =  sum(P.*log(P./Q),2);
+end
+
+% resolving the case when P(i)==0
+dist(isnan(dist))=0;
+