Mercurial > hg > mauch-mirex-2010
annotate _FullBNT/KPMtools/cross_entropy.m @ 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 kl = cross_entropy(p, q, symmetric) |
matthiasm@8 | 2 % CROSS_ENTROPY Compute the Kullback-Leibler divergence between two discrete prob. distributions |
matthiasm@8 | 3 % kl = cross_entropy(p, q, symmetric) |
matthiasm@8 | 4 % |
matthiasm@8 | 5 % If symmetric = 1, we compute the symmetric version. Default: symmetric = 0; |
matthiasm@8 | 6 |
matthiasm@8 | 7 tiny = exp(-700); |
matthiasm@8 | 8 if nargin < 3, symmetric = 0; end |
matthiasm@8 | 9 p = p(:); |
matthiasm@8 | 10 q = q(:); |
matthiasm@8 | 11 if symmetric |
matthiasm@8 | 12 kl = (sum(p .* log((p+tiny)./(q+tiny))) + sum(q .* log((q+tiny)./(p+tiny))))/2; |
matthiasm@8 | 13 else |
matthiasm@8 | 14 kl = sum(p .* log((p+tiny)./(q+tiny))); |
matthiasm@8 | 15 end |