LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
RelativeEntropy.m
1% re = RelativeEntropy(p1, p2)
2%
3% Returns the relative entropy (aka Kullback–Leibler
4% divergence) of two vectors.
5%
6% Parameters
7% ----------
8% p1 : vector, length M
9% The first vector
10% p2 : vector, length M
11% The second vector
12%
13% Returns
14% -------
15% re : double
16% The relative entropy calculated as
17% `re=\sum_{i=1}^M p1_i |\log(p1_i/p2_i)|`
18
19function re = RelativeEntropy (p1, p2)
20
21 re = 0;
22 for i=1:length(p1)
23 if p1(i) > 0.0
24 re = re + p1(i)*abs(log(p1(i)/p2(i)));
25 end
26 end
27end
28