-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_kl_dirichlet.m
31 lines (28 loc) · 1.04 KB
/
spm_kl_dirichlet.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function [d] = spm_kl_dirichlet (lambda_q,lambda_p,log_tilde_pi)
% KL divergence between two Dirichlet densities
% FORMAT [d] = spm_kl_dirichlet (lambda_q,lambda_p,log_tilde_pi)
%
% Calculate KL (Q||P) = <log Q/P> where avg is wrt Q
% between two Dirichlet densities Q and P
%
% lambda_q Parameter vector of first density
% lambda_p Parameter vectpr of second density
% log_tilde_pi <log (pi)> where avg is over Q. If this argument
% isn't passed the routine will calculate it
%___________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Will Penny
% $Id: spm_kl_dirichlet.m 7382 2018-07-25 13:58:04Z karl $
if nargin < 3
m =length(lambda_q);
lambda_tot = sum(lambda_q);
dglt = psi(lambda_tot);
for s = 1:m,
log_tilde_pi(s) = psi(lambda_q(s)) - dglt;
end
end
d = gammaln(sum(lambda_q));
d = d+sum((lambda_q-lambda_p).*log_tilde_pi);
d = d-sum(gammaln(lambda_q));
d = d-gammaln(sum(lambda_p));
d = d+sum(gammaln(lambda_p));