-
Notifications
You must be signed in to change notification settings - Fork 0
/
mon.py
402 lines (325 loc) · 10.8 KB
/
mon.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from numba import jit
import time
import numpy as np
from scipy.stats import invwishart
from math import floor
###
#Convenience
###
@jit
def catrnd(p):
cp = p.cumsum(axis = 1)
N = p.shape[0]
q = np.zeros((N,), dtype = 'int64')
for n in np.arange(N):
q[n] = np.searchsorted(cp[n,:], np.random.rand())
return q
def compsize(q,K):
u, c = np.unique(q, return_counts = True)
qN = np.zeros((K,), dtype = 'int64')
qN[u] = c
return qN
@jit
def monrnd(mu, Sigma, q):
N = q.shape[0]
R = mu.shape[1]
x = np.zeros((N,R))
ch = np.linalg.cholesky(Sigma)
for n in np.arange(N):
x[n,:] = mu[q[n],:] + ch[q[n],:,:] @ np.random.randn(R,)
return x
###
#Probabilities
###
@jit
def mvnpdf_norm(x, mu, Sigma):
xS = (x - mu).T
f = np.exp(-0.5 * (xS * np.linalg.solve(Sigma, xS)).sum(axis = 0))
k = mu.shape[0]
con = np.sqrt((2 * np.pi)**k * np.linalg.det(Sigma))
return f / con
@jit
def mvnlpdf_norm(x, mu, Sigma):
xS = (x - mu).T
f = -0.5 * (xS * np.linalg.solve(Sigma, xS)).sum(axis = 0)
k = mu.shape[0]
con = (k / 2) * np.log(2 * np.pi) + 0.5 * np.sum(np.log(np.diag(Sigma)))
return f - con
@jit
def monlpdf_q(x, mu, Sigma, q):
N = x.shape[0]
f = np.zeros((N,))
for n in np.arange(N):
f[n] = mvnlpdf_norm(x[n,:], mu[q[n],:], Sigma[q[n],:,:])
return f
@jit
def monpdf(x, mu, Sigma, pi):
K = pi.shape[0]
f = 0
for k in np.arange(K):
f += pi[k] * mvnpdf_norm(x, mu[k,:], Sigma[k,:,:])
return f
###
#MCMC
###
def next_eta(alpha, qN):
cqN = np.cumsum(qN[::-1])[::-1]
eta = np.random.beta(1 + qN[:-1], alpha + cqN[1:])
return eta
def next_pi(alpha, qN, K, s, method):
if method == 'f':
pi = np.random.dirichlet(alpha + qN)
elif method == 'dp':
eta = next_eta(alpha, qN)
etaC = 1 - eta
etaC[etaC < 1e-300] = 1e-300
cumprodEtaC = np.cumprod(etaC)
pi = np.ones((K,))
pi[:-1] = eta
pi[1:] *= cumprodEtaC
pi /= pi.sum()
try:
alphaU = np.random.gamma(s[0] + K - 1, 1 / (s[1] - np.log(etaC).sum()))
except:
alphaU = alpha
if alphaU > 0.3: alpha = alphaU
else:
assert False, 'Method not supported!'
return alpha, pi
def next_q(x, mu, Sigma, pi, nInd, K):
phi = np.zeros((nInd, K))
for k in np.arange(K):
phi[:,k] = mvnpdf_norm(x, mu[k,:], Sigma[k,:,:])
numer = pi.reshape((1, K)) * phi
denom = numer.sum(axis = 1).reshape((nInd, 1))
p = numer / denom
q = catrnd(p)
qN = compsize(q,K)
return q, qN
def next_mu(x, Sigma, mu0, Si0Inv, nInd, R):
SigmaInv = np.linalg.inv(Sigma)
mu_SiInv = Si0Inv + nInd * SigmaInv
mu_mu = np.linalg.solve(mu_SiInv,
Si0Inv @ mu0 + SigmaInv @ np.sum(x, axis = 0))
mu = mu_mu + np.linalg.solve(np.linalg.cholesky(mu_SiInv).T,
np.random.randn(R,))
return mu
def next_Sigma(x, mu, nu, iwDiagA, diagCov, nInd, R):
xS = np.array(x.reshape((nInd, R)) - mu.reshape((1, R))).reshape((nInd, R))
Sigma = np.array(invwishart.rvs(nu + nInd + R - 1, 2 * nu * np.diag(iwDiagA) + xS.T @ xS)).reshape((R, R))
if diagCov: Sigma = np.diag(np.diag(Sigma))
return Sigma
def next_iwDiagA(Sigma, nu, invASq, R):
iwDiagA = np.random.gamma((nu + R) / 2, 1 / (invASq + nu * np.diag(np.linalg.inv(Sigma))))
return iwDiagA
def next_g0_k(mu0, Si0, nu, invASq, R):
mu_k = mu0 + np.linalg.cholesky(Si0) @ np.random.randn(R,)
iwDiagA_k = np.random.gamma(1 / 2, 1 / invASq)
Sigma_k = np.array(invwishart.rvs(nu + R - 1, 2 * nu * np.diag(iwDiagA_k)).reshape((R, R)))
return mu_k, Sigma_k, iwDiagA_k
def next_theta(
x, mu, Sigma, iwDiagA, mu0, Si0, Si0Inv,
nu, invASq, diagCov,
q, qN, nInd, K, R):
for k in np.arange(K):
if qN[k] > 0:
x_k = np.array(x[q == k,:])
mu[k,:] = next_mu(x_k, Sigma[k,:,:], mu0, Si0Inv, qN[k], R)
iwDiagA[k,:] = next_iwDiagA(Sigma[k,:,:], nu, invASq, R)
Sigma[k,:,:] = next_Sigma(x_k, mu[k,:], nu, iwDiagA[k,:], diagCov, qN[k], R)
else:
mu[k,:], Sigma[k,:,:], iwDiagA[k,:] = next_g0_k(mu0, Si0, nu, invASq, R)
return mu, Sigma, iwDiagA
def gibbs(
mcmc_iterBurn, mcmc_iterSample, method,
x, K,
mu0, Si0, nu, A, diagCov, alpha, s):
###
#Precomputations
###
nInd, R = x.shape
###
#Storage
###
mu_store = np.zeros((mcmc_iterSample, K, R))
Sigma_store = np.zeros((mcmc_iterSample, K, R, R))
pi_store = np.zeros((mcmc_iterSample, K))
x_star_store = np.zeros((mcmc_iterSample, nInd, R))
###
#MCMC
###
invASq = np.ones((R,)) / A**2
Si0Inv = np.linalg.inv(Si0)
mu = np.zeros((K, R))
Sigma = np.zeros((K, R, R))
iwDiagA = np.zeros((K, R))
for k in np.arange(K):
mu[k,:], Sigma[k,:,:], iwDiagA[k,:] = next_g0_k(mu0, Si0, nu, invASq, R)
if method == 'f':
pi = np.random.dirichlet(alpha * np.ones((K,)))
elif method == 'dp':
eta = np.random.beta(1, alpha, (K - 1,))
etaC = 1 - eta
cumprodEtaC = np.cumprod(etaC)
pi = np.ones((K,))
pi[:-1] = eta
pi[1:] *= cumprodEtaC
pi /= pi.sum()
else:
assert False, 'Method not supported!'
q, qN = next_q(x, mu, Sigma, pi, nInd, K)
j = -1
for i in np.arange(mcmc_iterBurn + mcmc_iterSample):
#Sample
alpha, pi = next_pi(alpha, qN, K, s, method)
q, qN = next_q(x, mu, Sigma, pi, nInd, K)
mu, Sigma, iwDiagA = next_theta(
x, mu, Sigma, iwDiagA, mu0, Si0, Si0Inv,
nu, invASq, diagCov,
q, qN, nInd, K, R)
#Evaluate density on grid
x_star = monrnd(mu, Sigma,
np.random.choice(np.arange(K), size = nInd, replace = True, p = pi))
#Store
if (i + 1) > mcmc_iterBurn:
j += 1
mu_store[j,:,:] = mu
Sigma_store[j,:,:,:] = Sigma
pi_store[j,:] = pi
x_star_store[j,:,:] = x_star
#Display progess
if ((i + 1) % 100) == 0:
print('Iteration: ' + str(i + 1))
return mu_store, Sigma_store, pi_store, x_star_store
###
#Density simulation
###
#Bivariate
def monpdf_bi_sim(mu_store, Sigma_store, pi_store, r_x, r_y, step):
g_xx, g_yy = np.meshgrid(np.arange(r_x[0], r_x[1], step),
np.arange(r_y[0], r_y[1], step))
g_x = g_xx.reshape((-1,))
g_y = g_yy.reshape((-1,))
g_xy = np.stack((g_x, g_y), axis = 1)
z = 0
nDraws = mu_store.shape[0]
for i in np.arange(nDraws):
z += monpdf(g_xy, mu_store[i,:,:], Sigma_store[i,:,:,:], pi_store[i,:])
#Display progess
if ((i + 1) % 100) == 0:
print('Iteration: ' + str(i + 1))
z /= nDraws
g_zz = z.reshape((g_xx.shape[0], g_xx.shape[1]))
return g_xx, g_yy, g_zz
###
#If main: test
###
if __name__ == "__main__":
import matplotlib.pyplot as plt
import seaborn as sns
from skewnormal import skewnormallogistic_rnd
###
#Generate data
###
nInd = 1000
R = 2
DGP = 2
if DGP == 1:
#DGP 1:
nComp = 2
#pComp = np.array([0.2, 0.1, 0.3, 0.3, 0.1])
pComp = np.array([0.25, 0.25, 0.5])
mu = [np.array([ 1.3, 1.3, 0.3]),
np.array([ 0.3, 1.3, 0.8]),
np.array([ 0.8, 0.8, 0.3])]
sd = [np.array([0.08, 0.08, 0.10]),
np.array([0.08, 0.08, 0.10]),
np.array([0.20, 0.20, 0.10])]
Corr = [np.array([[1.0, 0.6, 0.2],
[0.6, 1.0, 0.3],
[0.2, 0.3, 1.0]]),
np.array([[1.0, 0.2, 0.4],
[0.2, 1.0, 0.3],
[0.4, 0.3, 1.0]]),]
idxCorr = np.array([0, 0, 1])
Si = [np.diag(sd[i]) @ Corr[idxCorr[i]] @ np.diag(sd[i]) for i in np.arange(nComp)]
ch = [np.linalg.cholesky(Si[i]) for i in np.arange(nComp)]
q = np.random.choice(np.arange(nComp), size = nInd, replace = True, p = pComp)
x = np.zeros((nInd, R))
for n in np.arange(nInd):
x[n,:] = mu[q[n]] + ch[q[n]] @ np.random.randn(R, )
elif DGP == 2:
#DGP 2:
x0 = skewnormallogistic_rnd(0, 1, 50, nInd)
x1 = skewnormallogistic_rnd(0, 1, 50, nInd)
x = np.stack((x0, x1), axis = 1)
elif DGP == 3:
#DGP 3:
nComp = 3
pComp = np.array([0.25, 0.25, 0.5])
loc = [np.array([ 1, -2]),
np.array([-2, -2]),
np.array([ 1, 1])]
scale = [np.array([1, 1]),
np.array([1, 1]),
np.array([1, 1])]
alpha = [np.array([ 40, 80]),
np.array([ 70, 70]),
np.array([-50, -50])]
q = np.random.choice(np.arange(nComp), size = nInd, replace = True, p = pComp)
x = np.zeros((nInd, R))
for n in np.arange(nInd):
for r in np.arange(R):
x[n,r] = skewnormallogistic_rnd(loc[q[n]][r], scale[q[n]][r], alpha[q[n]][r])
###
#MCMC
###
K = 100
alpha = 1
s = (2, 2)
mu0 = np.zeros((R,))
Si0 = 1 * np.eye(R)
nu = 2
A = 1.04
diagCov = False
mcmc_iterBurn = 5000
mcmc_iterSample = 5000
method = 'dp'
tic = time.time()
mu_store, Sigma_store, pi_store, x_star_store = gibbs(
mcmc_iterBurn, mcmc_iterSample, method,
x, K,
mu0, Si0, nu, A, diagCov, alpha, s)
toc = time.time() - tic
print(toc)
###
#Plot: univariate
###
fig = plt.figure()
num_bins = 6 * 6
rng = [(-3, 4), (-3, 2)]
for i in np.arange(R):
ax = plt.subplot(2, R, i + 1)
#True
data = x[:,i]
n, bins, patches = ax.hist(data, num_bins, rng[i], density = 1)
#Estimated
data = x_star_store[:,:,i].reshape((-1,))
n, bins, patches = ax.hist(data, num_bins, rng[i], density = 1)
ax.grid()
plt.show()
###
#Plot: bivariate
###
r_x = (-2.5, 2.5)
r_y = (-2.5, 2.5)
step = 0.05
g_xx, g_yy, g_zz = monpdf_bi_sim(mu_store, Sigma_store, pi_store, r_x, r_y, step)
fig = plt.figure()
ax = plt.subplot()
ax = sns.kdeplot(x[:,0], x[:,1],
cmap= "Blues" , shade = True, shade_lowest = False, linestyles = "-")
ax.contour(g_xx, g_yy, g_zz, 10)
plt.xlim(r_x); plt.ylim(r_y)
ax.set(title = 'DP-MON', xlabel = 'X1', ylabel = 'X2')
plt.show()