1function d = mvn_div_js(m1, m2, use_kl)
2%MVN_DIV_JS Compute the Jensen-Shannon (JS) divergence between two multivariate normals.
3%
4%   [d] = MVN_DIV_JS(m1, m2) computes the JS divergence between two
5%   multivariate normals. d is never negative.
6%
7%   The JS divergence is defined as:
8%       d = 0.5*KL(m1, m1+m2) + 0.5*KL(m2, m1+m2)
9
10%   (c) 2010-2011, Dominik Schnitzer, <dominik.schnitzer@ofai.at>
11%   http://www.ofai.at/~dominik.schnitzer/mvn
12%
13%   This file is part of the MVN Octave/Matlab Toolbox
14%   MVN is free software: you can redistribute it and/or modify
15%   it under the terms of the GNU General Public License as published by
16%   the Free Software Foundation, either version 3 of the License, or
17%   (at your option) any later version.
18%
19%   MVN is distributed in the hope that it will be useful,
20%   but WITHOUT ANY WARRANTY; without even the implied warranty of
21%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22%   GNU General Public License for more details.
23%
24%   You should have received a copy of the GNU General Public License
25%   along with MVN.  If not, see <http://www.gnu.org/licenses/>.
26
27    % Speedup:
28    %
29    %    m12 = mvn_bregmancentroid_kl_left([m1 m2]);
30    %
31    % using Cholesky & more Optimization
32
33    m12.m = 0.5*m1.m + 0.5*m2.m;
34    m12.cov = 0.5*(m1.cov + m1.m*m1.m') + 0.5*(m2.cov + m2.m*m2.m') ...
35        - m12.m*m12.m';
36    m12_chol = chol(m12.cov);
37    m12.logdet = 2*sum(log(diag(m12_chol)));
38
39    if ((nargin > 2) && (use_kl == 1))
40        m12_ui = m12_chol\eye(length(m12.m));
41        m12.icov = m12_ui*m12_ui';
42
43        d = 0.5*mvn_div_kl(m1, m12) + 0.5*mvn_div_kl(m2, m12);
44    else
45        % Speedup original (entropy):
46        %
47        % d = mvn_entropy(m12) - 0.5*mvn_entropy(m1) - 0.5*mvn_entropy(m2);
48        %
49        % faster:
50
51        d = 0.5*m12.logdet - 0.25*m1.logdet - 0.25*m2.logdet;
52    end
53
54    d = max(d, 0);
55end
56