about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/KPMtools/cross_entropy.m
diff options
context:
space:
mode:
Diffstat (limited to 'sourcecodes/bnt-master/KPMtools/cross_entropy.m')
-rw-r--r--sourcecodes/bnt-master/KPMtools/cross_entropy.m15
1 files changed, 15 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/KPMtools/cross_entropy.m b/sourcecodes/bnt-master/KPMtools/cross_entropy.m
new file mode 100644
index 00000000..1a7d2003
--- /dev/null
+++ b/sourcecodes/bnt-master/KPMtools/cross_entropy.m
@@ -0,0 +1,15 @@
+function kl = cross_entropy(p, q, symmetric)
+% CROSS_ENTROPY Compute the Kullback-Leibler divergence between two discrete prob. distributions
+% kl = cross_entropy(p, q, symmetric)
+%
+% If symmetric = 1, we compute the symmetric version. Default: symmetric = 0;
+
+tiny = exp(-700);
+if nargin < 3, symmetric = 0; end
+p = p(:);
+q = q(:);
+if symmetric
+  kl  = (sum(p .* log((p+tiny)./(q+tiny))) + sum(q .* log((q+tiny)./(p+tiny))))/2;
+else
+  kl  = sum(p .* log((p+tiny)./(q+tiny)));
+end