forked from ermongroup/BCD-Nets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivergences.py
241 lines (191 loc) · 7.69 KB
/
divergences.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import jax.numpy as np
from tensorflow_probability.substrates.jax.distributions import (
MultivariateNormalFullCovariance as normal,
kl_divergence,
)
def gaussian_precision_kl(theta, theta_hat):
"""Computes KL divergence between Gaussians with precisions theta, theta_hat
Assumes mean zero for the Gaussians.
Args:
theta: d x d precision matrix
theta_hat: d x d precision matrix
Returns:
kl: KL divergence
"""
dim = np.shape(theta)[0]
theta_0_logdet = np.linalg.slogdet(theta)[1]
theta_hat_logdet = np.linalg.slogdet(theta_hat)[1]
# Not sure how stable this term is going to be :/
trace_term = np.trace(theta_hat @ np.linalg.inv(theta))
return 0.5 * (theta_0_logdet - theta_hat_logdet - dim + trace_term)
def gaussian_precision_wasserstein(theta, theta_hat):
"""Computes Wasserstein distance between Gaussians with precisions theta, theta_hat
Assumes mean zero for the Gaussians.
Horribly hacky implementation at the moment...
Args:
theta: d x d precision matrix
theta_hat: d x d precision matrix
Returns:
kl: KL divergence
"""
return gaussian_square_wasserstein(np.linalg.inv(theta), np.linalg.inv(theta_hat))
def precision_wasserstein_loss(true_noise, true_W, est_noise, est_W):
"""Computes the wasserstein loss to the true parameters
Args:
true_noise: (d,)-shape vector of noise variances
true_W: (d,d)-shape adjacency matrix
est_noise: (d,)-shape vector of estimated noise variances
est_W: (d,d)-shape adjacency matrix
Returns:
w_square: induced squared Wasserstein distance
"""
dim = len(true_noise)
true_cov = (
(np.eye(dim) - true_W) @ (np.diag(1.0 / true_noise)) @ (np.eye(dim) - true_W).T
)
est_cov = (
(np.eye(dim) - est_W) @ (np.diag(1.0 / est_noise)) @ (np.eye(dim) - est_W).T
)
return gaussian_precision_wasserstein(true_cov, est_cov)
def precision_wasserstein_sample_loss(x_prec, est_noise, est_W):
"""Computes the wasserstein loss to the true parameters
Args:
true_noise: (d,)-shape vector of noise variances
true_W: (d,d)-shape adjacency matrix
est_noise: (d,)-shape vector of estimated noise variances
Returns:
w_square: induced squared Wasserstein distance
"""
dim = len(est_noise)
est_prec = (
(np.eye(dim) - est_W) @ (np.diag(1.0 / est_noise)) @ (np.eye(dim) - est_W).T
)
return gaussian_precision_wasserstein(x_prec, est_prec)
def gaussian_kl(theta, theta_hat):
"""Computes KL divergence between Gaussians with covariances theta, theta_hat
Assumes mean zero for the Gaussians.
Args:
theta: d x d covariance matrix
theta_hat: d x d covariance matrix
Returns:
kl: KL divergence
"""
dim, _ = np.shape(theta)
theta_dist = normal(loc=np.zeros(dim), covariance_matrix=theta)
theta_hat_dist = normal(loc=np.zeros(dim), covariance_matrix=theta_hat)
kl = kl_divergence(theta_dist, theta_hat_dist)
return kl
def precision_kl_loss(true_noise, true_W, est_noise, est_W):
"""Computes the KL divergence to the true parameters
Args:
true_noise: (d,)-shape vector of noise variances
true_W: (d,d)-shape adjacency matrix
est_noise: (d,)-shape vector of estimated noise variances
est_W: (d,d)-shape adjacency matrix
Returns:
w_square: induced squared Wasserstein distance
"""
dim = len(true_noise)
true_prec = (
(np.eye(dim) - true_W) @ (np.diag(1.0 / true_noise)) @ (np.eye(dim) - true_W).T
)
est_prec = (
(np.eye(dim) - est_W) @ (np.diag(1.0 / est_noise)) @ (np.eye(dim) - est_W).T
)
return gaussian_precision_kl(true_prec, est_prec)
def precision_kl_sample_loss(x_prec, est_noise, est_W):
"""Computes the KL divergence to the sample distribution
Args:
x_cov: (d,d)-shape sample covariance matrix
est_noise: (d,)-shape vector of estimated noise variances
est_W: (d,d)-shape estimated adjacency matrix
Returns:
w_square: induced KL divergence
"""
dim = len(est_noise)
est_prec = (
(np.eye(dim) - est_W) @ (np.diag(1.0 / est_noise)) @ (np.eye(dim) - est_W).T
)
return gaussian_precision_kl(x_prec, est_prec)
def my_sqrtm(X):
"""Computes the matrix square root of X
Does this by diagonalizing X.
The method in scipy is probably more stable, not
requiring a matrix inverse, but not implemented at the
moment. Can probably also not use the matrix inverse
and use solve instead.
Args:
X: An n x n symmetric and PSD matrix
Returns:
sqrt_X: An n x n matrix such that sqrt_X @ sqrt_X = X
"""
vals, vectors = np.linalg.eigh(X)
# We have vectors.inv @ X @ vectors = sqrt(vals)^2
# so X = vectors @ sqrt(vals)^2 @ vectors.inv
# X = (vectors @ sqrt(vals) @ vectors.inv)^2
return vectors @ np.diag(np.sqrt(vals)) @ np.linalg.inv(vectors)
def gaussian_square_wasserstein(theta, theta_hat):
"""Computes square of Wasserstein distance between Gaussians
Assumes mean zero for the Gaussians.
Args:
theta: d x d covariance matrix
theta_hat: d x d covariance matrix
Returns:
dist: Wasserstein_2 distance
"""
T_sqrt = my_sqrtm(theta)
inner_sqrt = my_sqrtm(T_sqrt @ theta_hat @ T_sqrt)
return np.trace(theta + theta_hat - 2 * inner_sqrt)
def wasserstein_loss(true_noise, true_W, est_noise, est_W):
"""Computes the wasserstein loss to the true parameters
Args:
true_noise: (d,)-shape vector of noise variances
true_W: (d,d)-shape adjacency matrix
est_noise: (d,)-shape vector of estimated noise variances
est_W: (d,d)-shape adjacency matrix
Returns:
w_square: induced squared Wasserstein distance
"""
dim = len(true_noise)
true_cov = (np.eye(dim) + true_W) @ np.diag(true_noise) @ (np.eye(dim) + true_W).T
est_cov = (np.eye(dim) + est_W) @ np.diag(est_noise) @ (np.eye(dim) + est_W).T
return gaussian_square_wasserstein(true_cov, est_cov)
def wasserstein_sample_loss(x_cov, est_noise, est_W):
"""Computes the wasserstein loss to the true parameters
Args:
true_noise: (d,)-shape vector of noise variances
true_W: (d,d)-shape adjacency matrix
est_noise: (d,)-shape vector of estimated noise variances
Returns:
w_square: induced squared Wasserstein distance
"""
dim = len(est_noise)
est_cov = (np.eye(dim) + est_W) @ np.diag(est_noise) @ (np.eye(dim) + est_W).T
return gaussian_square_wasserstein(x_cov, est_cov)
def kl_loss(true_noise, true_W, est_noise, est_W):
"""Computes the KL divergence to the true parameters
Args:
true_noise: (d,)-shape vector of noise variances
true_W: (d,d)-shape adjacency matrix
est_noise: (d,)-shape vector of estimated noise variances
est_W: (d,d)-shape adjacency matrix
Returns:
w_square: induced squared Wasserstein distance
"""
dim = len(true_noise)
true_cov = (np.eye(dim) + true_W) @ np.diag(true_noise) @ (np.eye(dim) + true_W).T
est_cov = (np.eye(dim) + est_W) @ np.diag(est_noise) @ (np.eye(dim) + est_W).T
return gaussian_kl(true_cov, est_cov)
def kl_sample_loss(x_cov, est_noise, est_W):
"""Computes the KL divergence to the sample distribution
Args:
x_cov: (d,d)-shape sample covariance matrix
est_noise: (d,)-shape vector of estimated noise variances
est_W: (d,d)-shape estimated adjacency matrix
Returns:
w_square: induced KL divergence
"""
dim = len(est_noise)
est_cov = (np.eye(dim) + est_W) @ np.diag(est_noise) @ (np.eye(dim) + est_W).T
return gaussian_kl(x_cov, est_cov)