KL divergence for Gamma densities FORMAT [d] = spm_kl_gamma (b_q,c_q,b_p,c_p) KL (Q||P) = <log Q/P> where avg is wrt Q b_q, c_q Parameters of first Gamma density b_p, c_p Parameters of second Gamma density %W% Will Penny %E%
0001 function [d] = spm_kl_gamma (b_q,c_q,b_p,c_p) 0002 % KL divergence for Gamma densities 0003 % FORMAT [d] = spm_kl_gamma (b_q,c_q,b_p,c_p) 0004 % 0005 % KL (Q||P) = <log Q/P> where avg is wrt Q 0006 % 0007 % b_q, c_q Parameters of first Gamma density 0008 % b_p, c_p Parameters of second Gamma density 0009 % 0010 % %W% Will Penny %E% 0011 0012 digamma_c_q=spm_digamma(c_q); 0013 d=(c_q-1)*digamma_c_q-log(b_q)-c_q-gammaln(c_q); 0014 d=d+gammaln(c_p)+c_p*log(b_p)-(c_p-1)*(digamma_c_q+log(b_q)); 0015 d=d+b_q*c_q/b_p; 0016 0017