Mercurial > hg > mauch-mirex-2010
annotate _misc/probability/.svn/text-base/JSDiv.m.svn-base @ 9:4ea6619cb3f5 tip
removed log files
author | matthiasm |
---|---|
date | Fri, 11 Apr 2014 15:55:11 +0100 |
parents | b5b38998ef3b |
children |
rev | line source |
---|---|
matthiasm@8 | 1 function dist=JSDiv(P,Q) |
matthiasm@8 | 2 % Jensen-Shannon divergence of two probability distributions |
matthiasm@8 | 3 % dist = JSD(P,Q) Kullback-Leibler divergence of two discrete probability |
matthiasm@8 | 4 % distributions |
matthiasm@8 | 5 % P and Q are automatically normalised to have the sum of one on rows |
matthiasm@8 | 6 % have the length of one at each |
matthiasm@8 | 7 % P = n x nbins |
matthiasm@8 | 8 % Q = 1 x nbins |
matthiasm@8 | 9 % dist = n x 1 |
matthiasm@8 | 10 |
matthiasm@8 | 11 |
matthiasm@8 | 12 if size(P,2)~=size(Q,2) |
matthiasm@8 | 13 error('the number of columns in P and Q should be the same'); |
matthiasm@8 | 14 end |
matthiasm@8 | 15 |
matthiasm@8 | 16 % normalizing the P and Q |
matthiasm@8 | 17 Q = Q ./sum(Q); |
matthiasm@8 | 18 Q = repmat(Q,[size(P,1) 1]); |
matthiasm@8 | 19 P = P ./repmat(sum(P,2),[1 size(P,2)]); |
matthiasm@8 | 20 |
matthiasm@8 | 21 M = 0.5.*(P + Q); |
matthiasm@8 | 22 |
matthiasm@8 | 23 dist = 0.5.*KLDiv(P,M) + 0.5*KLDiv(Q,M); |