-
Notifications
You must be signed in to change notification settings - Fork 0
/
randSPDGauss.py
381 lines (291 loc) · 13 KB
/
randSPDGauss.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import numpy as np
import math
import utils
from tqdm import tqdm
# Somehow one cannot just do import scipy and call scipy.linalg.*
import scipy.linalg as la
from scipy.stats import ortho_group
from scipy.optimize import minimize_scalar
def prod_sinh_alternative(x, d):
"""Compute \Pi_{i<j} \sinh(|r_i - r_j|/2).
An alternative method relying entirely on numpy
manipulations, as opposed to python for loops.
"""
repeated_r = np.repeat(x, d, axis=0)
r_diff_matrix = (repeated_r.reshape((d, d), order="F")
- repeated_r.reshape((d, d), order="C"))
r_diff_vec = r_diff_matrix[np.triu_indices(d, k=1)]
r_vec = np.absolute(r_diff_vec)/2
r_vec = np.sinh(r_vec)
return np.prod(r_vec)
def prod_sinh(x, d):
"""Compute \Pi_{i<j} \sinh(|r_i - r_j|/2).
"""
p = 1
for i in range(d-1):
for j in range(i+1, d):
p = p * math.sinh(abs(x[i] - x[j])/2)
return p
def unifpdf_dim_p(x, a, b):
n = x.shape[0]
if (x < a).all() or (x > b).all():
return 0
return (1/(b-a))**n
def generate_ri_RW(gamma, d, N, rng, omit, delta=.01):
""" Generate random r_i samples (see equation (29) of Said et al., 2017)
Uses MH with random walk.
"""
def pdf(x):
return (math.exp(-1*np.sum(np.power(x, 2))/(2*(gamma**2))))*prod_sinh(x, d)
def proppdf(x, y): return unifpdf_dim_p(x - y, -delta, delta)
def proprnd(x): return x + rng.random(d)*2*delta - delta
r, _ = utils.mhsample(rng.random(d), N+omit, pdf, proppdf, proprnd, rng=rng)
return r[omit:] # Chop off the ommited samples.
def generate_ri_MALA(gamma, d, N, rng, tau, omit=1000):
""" Generate random r_i samples (see equation (29) of Said et al., 2017).
Uses Metropolis-Adjusted Langevin Algorithm.
"""
def compute_log_grad_pdf(x):
ret = -1*x.copy()/(gamma**2)
for k in range(d):
curr_diff = 0
for i in range(d):
if i < k and x[i] >= x[k]:
curr_diff += (-.5)*np.cosh((x[i] - x[k])/2)/np.sinh((x[i] - x[k])/2)
elif k < i and x[k] >= x[i]:
curr_diff += (.5)*np.cosh((x[k] - x[i])/2)/np.sinh((x[k] - x[i])/2)
elif i < k and x[i] < x[k]:
curr_diff += (-.5)*np.cosh((x[i] - x[k])/2)/np.sinh((x[i] - x[k])/2)
elif k < i and x[k] < x[i]:
curr_diff += (.5)*np.cosh((x[k] - x[i])/2)/np.sinh((x[k] - x[i])/2)
ret[k] += curr_diff
return ret
def pdf(x):
return (math.exp(-1*np.sum(np.power(x, 2))/(2*(gamma**2))))*prod_sinh(x, d)
def proppdf(x, y): return np.exp((-1/(4*tau))*np.linalg.norm(x - y - tau*compute_log_grad_pdf(y))**2)
def proprnd(x):
loggradient = compute_log_grad_pdf(x)
return x + tau*loggradient + ((2*tau)**.5)*rng.multivariate_normal(np.zeros(d), np.eye(d))
r, _ = utils.mhsample(rng.random(d), N+omit, pdf, proppdf, proprnd, rng=rng)
return r[omit:] # Chop off the ommited samples.
def generate_ri_ULA(gamma, d, N, rng, tau, omit=1000):
""" Generate random r_i samples (see equation (29) of Said et al., 2017).
Uses Unconstrained Langevin Algorithm.
"""
def compute_log_grad_pdf(x):
ret = -1*x.copy()/(gamma**2)
for k in range(d):
curr_diff = 0
for i in range(d):
if i < k and x[i] >= x[k]:
curr_diff += (-.5)*np.cosh((x[i] - x[k])/2)/np.sinh((x[i] - x[k])/2)
elif k < i and x[k] >= x[i]:
curr_diff += (.5)*np.cosh((x[k] - x[i])/2)/np.sinh((x[k] - x[i])/2)
elif i < k and x[i] < x[k]:
curr_diff += (-.5)*np.cosh((x[i] - x[k])/2)/np.sinh((x[i] - x[k])/2)
elif k < i and x[k] < x[i]:
curr_diff += (.5)*np.cosh((x[k] - x[i])/2)/np.sinh((x[k] - x[i])/2)
ret[k] += curr_diff
return ret
samples = [rng.random(d)]
for i in range(1, N+omit):
prev_step = samples[i-1]
next_step = prev_step + tau*compute_log_grad_pdf(prev_step) + ((2*tau)**.5)*rng.standard_normal(d)
samples.append(next_step)
return samples[omit:] # Chop off the ommited samples.
def generate_ri_Gibbs_MALA(gamma, d, N, rng, tau, omit, marginal_omit=100):
""" Generate random r_i samples (see equation (29) of Said et al., 2017).
Uses Gibbs sampler, where marginal distributions sampled with MALA.
marginal_omit: number of samples to omit when sampling the marginal distributions.
"""
def compute_log_grad_pdf(r, conds):
ret = -1*r/(gamma**2)
for cond in conds:
ret += (r-cond)/(2*abs(r - cond)*np.tanh(abs(r-cond)/2))
return ret
def pdf(r, conds):
ret = np.exp((-1*r**2)/(2*gamma**2))
for cond in conds:
ret *= np.sinh(abs(r - cond)/2)
return ret
def proppdf(x, y, conds): return np.exp((-1/(4*tau))*np.linalg.norm(x - y - tau*compute_log_grad_pdf(y, conds))**2)
def proprnd(x, conds):
loggradient = compute_log_grad_pdf(x, conds)
return x + tau*loggradient + ((2*tau)**.5)*rng.standard_normal()
samples = [rng.random(d).tolist()]
# Set disable=False if you want to see the progress of generating the
# ri samples:
for i in tqdm(range(1, N+omit), desc=" Sampling ri", disable=True):
prev_step = samples[i-1]
curr_sample = []
for j in range(d):
cond_before = curr_sample
cond_after = [] if j + 1 >= d else prev_step[j+1:]
conds = cond_before + cond_after
curr_pdf = lambda x: pdf(x, conds)
curr_proppdf = lambda x, y: proppdf(x, y, conds)
curr_proprnd = lambda x: proprnd(x, conds)
# Start at the mode of the marginal dist.,
# otherwise MCMC difficult to converge:
neg_pdf = lambda x: -1*curr_pdf(x)
mode_res = minimize_scalar(neg_pdf)
curr_s, _ = utils.mhsample(mode_res.x, 1+marginal_omit, curr_pdf, curr_proppdf, curr_proprnd, rng=rng)
curr_sample.append(curr_s[-1])
samples.append(curr_sample)
return samples[omit:] # Chop off the ommited samples.
def generate_ri_Gibbs_RW(gamma, d, N, rng, omit, delta=.01, marginal_omit=100):
""" Generate random r_i samples (see equation (29) of Said et al., 2017).
Uses Gibbs sampler, where marginal distributions sampled with Random Walk MCMC.
marginal_omit: number of samples to omit when sampling the marginal distributions.
"""
def pdf(r, conds):
ret = np.exp((-1*r**2)/(2*gamma**2))
for cond in conds:
ret *= np.sinh(abs(r - cond)/2)
return ret
def proppdf(x, y): return utils.unifpdf(y - x, -delta, delta)
def proprnd(x): return x + rng.random()*2*delta - delta
samples = [rng.random(d).tolist()]
# Set disable=False if you want to see the progress of generating the
# ri samples:
for i in tqdm(range(1, N+omit), desc=" Sampling ri", disable=True):
prev_step = samples[i-1]
curr_sample = []
for j in range(d):
cond_before = curr_sample
cond_after = [] if j + 1 >= d else prev_step[j+1:]
conds = cond_before + cond_after
curr_pdf = lambda x: pdf(x, conds)
# Start at the mode of the marginal dist.,
# otherwise MCMC difficult to converge:
neg_pdf = lambda x: -1*curr_pdf(x)
mode_res = minimize_scalar(neg_pdf)
curr_s, _ = utils.mhsample(mode_res.x, 1+marginal_omit, curr_pdf, proppdf, proprnd, rng=rng)
curr_sample.append(curr_s[-1])
samples.append(curr_sample)
return samples[omit:] # Chop off the ommited samples.
def generate_r_RW(sigma, num_samples, rng, omit):
''' Generate the radius component of random samples.
For more details, see, e.g., equation (24) of Said et al., 2017.
Uses MH with random walk.
'''
delta = .05
pdf = lambda x: np.exp(-.25*np.square(x)/(sigma**2)) * np.sinh(.5*np.abs(x))
proppdf = lambda x, y: utils.unifpdf(y - x, -delta, delta)
proprnd = lambda x: x + rng.random()*2*delta - delta
# The pdf of r is symmetric and bimodal, and hence generate sample starting from the 2 modes:
neg_pdf = lambda x: -1*pdf(x)
mode_res = minimize_scalar(neg_pdf)
starting_point = abs(mode_res.x) # Global maxima 1.
half_num_samples = int(num_samples/2)
r1, _ = utils.mhsample(starting_point, half_num_samples+omit, pdf, proppdf, proprnd, rng=rng)
r1 = r1[omit:]
starting_point = -1*starting_point # Global maxima 2.
r2, _ = utils.mhsample(starting_point, num_samples+half_num_samples+omit, pdf, proppdf, proprnd, rng=rng)
r2 = r2[omit:]
ret = np.concatenate((r1, r2))
rng.shuffle(ret)
return ret
# The following is a simpler approach where the RW starts from 1:
# r, _ = utils.mhsample(1, num_samples+omit, pdf, proppdf, proprnd, rng=rng)
# return r[omit:]
def generate_r_MALA(gamma, num_samples, rng, tau, omit):
''' Generate the radius component of random samples.
For more details, see, e.g., equation (24) of Said et al., 2017.
Uses Metropolis-Adjusted Langevin Algorithm
'''
def compute_log_grad_pdf(x):
return -1*x/(2*gamma**2) + .5*np.cosh(.5*x)/np.sinh(.5*x)
def proppdf(x, y): return np.exp((-1/(4*tau))*np.linalg.norm(x - y - tau*compute_log_grad_pdf(y))**2)
def proprnd(x):
loggradient = compute_log_grad_pdf(x)
return x + tau*loggradient + ((2*tau)**.5)*rng.standard_normal()
def pdf(x): return np.exp(-.25*np.square(x)/(gamma**2)) * np.sinh(.5*np.abs(x))
# The pdf of r is symmetric and bimodal, and hence generate sample starting from the 2 modes:
neg_pdf = lambda x: -1*pdf(x)
mode_res = minimize_scalar(neg_pdf)
starting_point = abs(mode_res.x) # Global maxima 1.
half_num_samples = int(num_samples/2)
r1, _ = utils.mhsample(starting_point, half_num_samples+omit, pdf, proppdf, proprnd, rng=rng)
r1 = r1[omit:]
starting_point = -1*starting_point # Global maxima 2.
r2, _ = utils.mhsample(starting_point, num_samples+half_num_samples+omit, pdf, proppdf, proprnd, rng=rng)
r2 = r2[omit:]
ret = np.concatenate((r1, r2))
rng.shuffle(ret)
return ret
# The following is a simpler approach where the Markov chain starts from 1:
# r, _ = utils.mhsample(1, num_samples+omit, pdf, proppdf, proprnd, rng=rng)
# r = r[omit:]
# return r
def randSPDGauss_p2(Ybar, gamma, N, rng=None, omit=None):
if rng is None:
rng = np.random.default_rng()
Y = np.zeros((2, 2, N))
# Generate log determinant, t, of
# Y. This has normal distribution with
# mean 0 and variance 2gamma^2:
t = (2*gamma**2)**.5*rng.standard_normal(N)
# Generate log of ratio of eigen
# values of Y, r. This uses MCMC:
omit = omit or 100000
r = generate_r_RW(gamma, N, rng, omit)
# r = generate_r_MALA(gamma, N, rng, .0001, omit)
# Recover a Id-centered samples from t and r:
for i in range(N):
theta = ortho_group.rvs(2, random_state=rng)
# t and r are the "t" and "rho" in the remarks preceding (31) of
# "Riemannian Gaussian Distributions on the Space of Symmetric Positive Definite Matrices"
# by Said et al..
r1 = (t[i] + r[i])/2
r2 = (t[i] - r[i])/2
D = np.diag([np.exp(r1), np.exp(r2)])
Y[:,:,i] = theta.T @ D @ theta
# Translate to center at Ybar:
# g = la.sqrtm(Ybar) #NOTE: It is unclear which square root scipy chooses.
g = utils.SPD_sqrt(Ybar)
for i in range(N):
Y[:,:,i] = g.T @ Y[:,:,i] @ g
return Y
def randSPDGauss(Ybar, gamma, N, rng=None, omit=None):
""" Generate N samples from a Gaussian SPD manifold with mean Ybar and dispersion gamma.
Ybar: numpy array
"""
if rng is None:
rng = np.random.default_rng()
p = Ybar.shape[0]
if p == 2:
return randSPDGauss_p2(Ybar, gamma, N, rng, omit)
Y = np.zeros((p, p, N))
omit = omit or 1000
# r = generate_ri_RW(gamma, p, N, rng, omit, delta=.006)
# r = generate_ri_MALA(gamma, p, N, rng, .0001, omit=1000)
# r = generate_ri_Gibbs_MALA(gamma, p, N, rng, .001, omit=1000)
r = generate_ri_Gibbs_RW(gamma, p, N, rng, omit)
for i in range(N):
theta = ortho_group.rvs(p, random_state=rng)
ri = np.array(r[i])
D = np.diag(np.exp(ri))
Y[:,:,i] = theta.T @ D @ theta
# g = la.sqrtm(Ybar) #NOTE: It is unclear which square root scipy chooses.
g = utils.SPD_sqrt(Ybar)
for i in range(N):
Y[:,:,i] = g.T @ Y[:,:,i] @ g
return Y
def main():
# Some sanity check:
from numpy.linalg import matrix_rank
for n in range(2, 6):
a = np.eye(n)
ret = randSPDGauss(a, 1, 3)
for i in range(3):
assert matrix_rank(ret[:,:,i]) == n
# Print some examples:
Ybar = np.eye(2)
gamma = .1
N = 10
samples = randSPDGauss(Ybar, gamma, N)
for i in range(N):
print(samples[:,:,i])
if __name__ == "__main__":
main()