| 1 |
""" |
| 2 |
|
| 3 |
All implementations are based upon the following: |
| 4 |
http://www.fil.ion.ucl.ac.uk/~wpenny/publications/densities.ps |
| 5 |
""" |
| 6 |
|
| 7 |
import numpy as np |
| 8 |
from scipy.special import gammaln, digamma |
| 9 |
|
| 10 |
from . import _utils |
| 11 |
|
| 12 |
|
| 13 |
def kl_dirichlet(q, p): |
| 14 |
""" |
| 15 |
KL Divergence between two dirichlet distributions |
| 16 |
|
| 17 |
KL(q || p) = ln [gamma(q)/gamma(p)] - sum [ ln [gamma(q_j)/gamma(p_j)] |
| 18 |
- (q_j - p_j) (digamma(q_j) - digamma(p_j)] |
| 19 |
""" |
| 20 |
q = np.asarray(q) |
| 21 |
p = np.asarray(p) |
| 22 |
qsum = q.sum() |
| 23 |
psum = p.sum() |
| 24 |
return (gammaln(qsum) - gammaln(psum) |
| 25 |
- np.sum(gammaln(q) - gammaln(p)) |
| 26 |
+ np.einsum("i,i->", (q - p), (digamma(q) - digamma(qsum)))) |
| 27 |
|
| 28 |
|
| 29 |
def kl_normal_distribution(mean_q, variance_q, mean_p, variance_p): |
| 30 |
"""KL Divergence between two normal distributions.""" |
| 31 |
result = ((np.log(variance_p / variance_q)) / 2 |
| 32 |
+ ((mean_q - mean_p)**2 + variance_q) / (2 * variance_p) |
| 33 |
- .5) |
| 34 |
assert result >= 0, result |
| 35 |
return result |
| 36 |
|
| 37 |
|
| 38 |
def kl_multivariate_normal_distribution(mean_q, covar_q, mean_p, covar_p): |
| 39 |
""" |
| 40 |
KL Divergence of two Multivariate Normal Distribtuions |
| 41 |
|
| 42 |
q(x) = Normal(x; mean_q, variance_q) |
| 43 |
p(x) = Normal(x; mean_p, variance_p) |
| 44 |
""" |
| 45 |
|
| 46 |
|
| 47 |
mean_q = np.asarray(mean_q) |
| 48 |
covar_q = np.asarray(covar_q) |
| 49 |
mean_p = np.asarray(mean_p) |
| 50 |
covar_p = np.asarray(covar_p) |
| 51 |
|
| 52 |
|
| 53 |
precision_p = np.linalg.inv(covar_p) |
| 54 |
|
| 55 |
mean_diff = mean_q - mean_p |
| 56 |
D = mean_q.shape[0] |
| 57 |
|
| 58 |
|
| 59 |
return .5 * (_utils.logdet(covar_p) - _utils.logdet(covar_q) |
| 60 |
+ np.trace(precision_p @ covar_q) |
| 61 |
+ mean_diff @ precision_p @ mean_diff |
| 62 |
- D) |
| 63 |
|
| 64 |
|
| 65 |
def kl_gamma_distribution(b_q, c_q, b_p, c_p): |
| 66 |
""" |
| 67 |
KL Divergence between two gamma distributions |
| 68 |
|
| 69 |
q(x) = Gamma(x; b_q, c_q) |
| 70 |
p(x) = Gamma(x; b_p, c_p) |
| 71 |
""" |
| 72 |
result = ((b_q - b_p) * digamma(b_q) |
| 73 |
- gammaln(b_q) + gammaln(b_p) |
| 74 |
+ b_p * (np.log(c_q) - np.log(c_p)) |
| 75 |
+ b_q * (c_p-c_q) / c_q) |
| 76 |
assert result >= 0, result |
| 77 |
return result |
| 78 |
|
| 79 |
|
| 80 |
def kl_wishart_distribution(dof_q, scale_q, dof_p, scale_p): |
| 81 |
""" |
| 82 |
KL Divergence between two Wishart Distributions |
| 83 |
|
| 84 |
q(x) = Wishart(R|dof_q, scale_q) |
| 85 |
p(x) = Wishart(R|dof_p, scale_p) |
| 86 |
|
| 87 |
Definition from: |
| 88 |
Shihao Ji, B. Krishnapuram, and L. Carin, |
| 89 |
"Variational Bayes for continuous hidden Markov models and its |
| 90 |
application to active learning," IEEE Transactions on Pattern |
| 91 |
Analysis and Machine Intelligence, vol. 28, no. 4, pp. 522–532, |
| 92 |
Apr. 2006, doi: 10.1109/TPAMI.2006.85. |
| 93 |
""" |
| 94 |
scale_q = np.asarray(scale_q) |
| 95 |
scale_p = np.asarray(scale_p) |
| 96 |
D = scale_p.shape[0] |
| 97 |
return ((dof_q - dof_p)/2 * _E(dof_q, scale_q) |
| 98 |
- D * dof_q / 2 |
| 99 |
+ dof_q / 2 * np.trace(scale_p @ np.linalg.inv(scale_q)) |
| 100 |
|
| 101 |
+ _logZ(dof_p, scale_p) |
| 102 |
- _logZ(dof_q, scale_q)) |
| 103 |
|
| 104 |
|
| 105 |
def _E(dof, scale): |
| 106 |
r""" |
| 107 |
$L(a, B) = \int \mathcal{Wishart}(\Gamma; a, B) \log |\Gamma| d\Gamma$ |
| 108 |
""" |
| 109 |
return (-_utils.logdet(scale / 2) |
| 110 |
+ digamma((dof - np.arange(scale.shape[0])) / 2).sum()) |
| 111 |
|
| 112 |
|
| 113 |
def _logZ(dof, scale): |
| 114 |
D = scale.shape[0] |
| 115 |
return ((D * (D - 1) / 4) * np.log(np.pi) |
| 116 |
- dof / 2 * _utils.logdet(scale / 2) |
| 117 |
+ gammaln((dof - np.arange(scale.shape[0])) / 2).sum()) |