-
Notifications
You must be signed in to change notification settings - Fork 2
/
metropolis_hastings_hybrid_kernels.py
393 lines (302 loc) · 12 KB
/
metropolis_hastings_hybrid_kernels.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def print_rejection_statistics(count_proposals, count_rejected):
# Rejections per each proposal
for idx in range(len(count_proposals)):
perc = 100 * count_rejected[idx] / count_proposals[idx]
print('Prop {} : Used {} : Rejected {}: Rej. Percent {:.2f} %'
.format(idx, count_proposals[idx], count_rejected[idx], perc))
# All rejections
perc = 100 * sum(count_rejected) / sum(count_proposals)
print('All : Used {} : Rejected {}: Rej. Percent {:.2f} %'
.format(sum(count_proposals), sum(count_rejected), perc))
def plot_individual_hist(sample, name, params=['a', 'b', 'c']):
# Number of parameters
n = len(sample[0][1])
# For each parameter
for idx in range(n):
# Hist sample
smp = np.array([elem[1][idx] for elem in sample])
plt.hist(smp, bins=20)
plt.title('Histogram of "{}" sample'.format(params[idx]))
plt.savefig('{}_{}.png'.format(name, params[idx]),
bbox_inches='tight', pad_inches=0)
plt.show()
def plot_individual_walk_mean(walk, burn_in, name,
params=['a', 'b', 'c']):
# Number of parameters
n = len(walk[0][1])
means = []
# For each parameter
for idx in range(n):
# Plot walk
X_wlk = [elem[0] for elem in walk]
Y_wlk = np.array([elem[1][idx] for elem in walk])
Y_mean = [np.mean(Y_wlk[:idx + 1]) for idx in range(len(Y_wlk))]
plt.plot(X_wlk, Y_mean, '-', alpha=1.0,
label='param {}'.format(params[idx]))
means.append(np.mean(Y_wlk))
# Format plot
plt.axvline(x=burn_in, color='r', label='Burn-in')
plt.legend(loc='upper right')
plt.savefig('{}_{}.png'.format(name, params[idx]),
bbox_inches='tight', pad_inches=0)
plt.show()
print('Converged to the following mean ')
print(means)
def plot_individual_walk(walk, rejected, burn_in,
name, params=['a', 'b', 'c']):
# Number of parameters
n = len(walk[0][1])
# For each parameter
for idx in range(n):
# Plot walk
X_wlk = [elem[0] for elem in walk]
Y_wlk = [elem[1][idx] for elem in walk]
plt.plot(X_wlk, Y_wlk, '-', alpha=1.0,
label='param {}'.format(params[idx]))
# Plot rejected
X_rej = [elem[0] for elem in rejected]
Y_rej = [elem[1][idx] for elem in rejected]
plt.plot(X_rej, Y_rej, 'x', alpha=.2, color='red',
label='rejected')
# Format plot
plt.axvline(x=burn_in, color='r', label='Burn-in')
plt.legend(loc='best')
plt.savefig('{}_{}.png'.format(name, params[idx]),
bbox_inches='tight', pad_inches=0)
plt.show()
def plot_walk(walk, rejected, posterior, name):
# Plot walk
X_wlk = [elem[0] for elem in walk]
Y_wlk = [elem[1] for elem in walk]
plt.plot(X_wlk, Y_wlk, '-o', alpha=0.4, color='blue',
label='accepted')
# Plot rejected
X_rej = [elem[1][0] for elem in rejected]
Y_rej = [elem[1][1] for elem in rejected]
plt.plot(X_rej, Y_rej, 'x', alpha=0.2, color='red',
label='rejected')
plt.legend(loc='upper right')
# Get max and min of walk
X_max = 1920 # np.max(X_wlk + X_rej)
X_min = 0 # np.min(X_wlk + X_rej)
Y_max = 1080 # np.max(Y_wlk + Y_rej)
Y_min = 0 # np.min(Y_wlk + Y_rej)
# Plot contour
X_lin = np.linspace(X_min, X_max, 100)
Y_lin = np.linspace(Y_min, Y_max, 100)
# Create grid
X, Y = np.meshgrid(X_lin, Y_lin, indexing='xy')
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y
# Evaluate posterior
Z = np.array([[posterior((cell[0], cell[1]))
for cell in row] for row in pos])
# Read image
img = mpimg.imread('../mini-vae/imgs/train_station.jpg')
plt.imshow(img)
# Plot contour map
plt.contour(X, Y, Z, 20, cmap='RdGy')
plt.savefig(name, bbox_inches='tight', pad_inches=0)
plt.show()
def plot_sample(sample, posterior, name, args,
params=['a', 'b'], start=None, interm=None):
# Plot sample
X_smp = [elem[0] for elem in sample]
Y_smp = [elem[1] for elem in sample]
plt.plot(X_smp, Y_smp, 'o', alpha=0.4, color='blue',
label='sample')
plt.legend(loc='upper right')
# Get max and min of sample
X_max = 1920 # np.max(X_smp)
X_min = 0 # np.min(X_smp)
Y_max = 1080 # np.max(Y_smp)
Y_min = 0 # np.min(Y_smp)
# Plot contour
X_lin = np.linspace(X_min, X_max, 100)
Y_lin = np.linspace(Y_min, Y_max, 100)
# Create grid
X, Y = np.meshgrid(X_lin, Y_lin, indexing='xy')
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y
# Evaluate posterior
Z = np.array([[posterior((cell[0], cell[1]))
for cell in row] for row in pos])
# Read image
img = mpimg.imread('../mini-vae/imgs/train_station.jpg')
plt.imshow(img)
# Plot start
plt.plot(start[0], start[1], marker='x',
color='red', mew=2, ms=10)
plt.annotate('start', xy=start, xytext=(10, -5),
textcoords='offset points', color='white')
# Plot intermediate
for idx, pt in enumerate(interm):
plt.plot(pt[0], pt[1], marker='x',
color='red', mew=2, ms=10)
plt.annotate('interm_{}'.format(idx), xy=pt, xytext=(10, -5),
textcoords='offset points', color='white')
# Plot contour map
plt.contour(X, Y, Z, 20, cmap='RdGy')
plt.xlabel(params[0])
plt.ylabel(params[1])
plt.savefig(name, bbox_inches='tight', pad_inches=0)
plt.show()
# Acceptance Ratio
def log_acceptance_ratio(x_p, x_t, log_posterior, log_proposal):
return min(0, log_posterior(x_p) + log_proposal(x_p, x_t) -
log_posterior(x_t) - log_proposal(x_t, x_p))
def metropolis_hastings_hybrid_kernels(sample_size,
x_initial,
log_posterior,
log_proposal,
step,
kernel_probs,
burn_in,
log_interval):
# Current step
t = 0
# Number of proposal
len_proposal = len(log_proposal)
# Initial uniform sample
x_t = x_initial
# Sample
sample = []
# Random walk
walk = []
rejected = []
# Counting statistics
count_proposals = [0] * len_proposal
count_rejected = [0] * len_proposal
# Init Burn-in count
burnt = 0
# Sample until desired number
while len(sample) < sample_size:
# Save random walk
walk.append(x_t)
# Select transition kernel
kernel_idx = np.random.choice(len_proposal, p=kernel_probs)
log_proposal_t = log_proposal[kernel_idx]
step_t = step[kernel_idx]
# Generate random step from proposed distribution
x_p = step_t(x_t)
# Calculate the acceptance ratio
rho = np.exp(log_acceptance_ratio(x_p, x_t,
log_posterior,
log_proposal_t))
# Random uniform sample
u = np.random.uniform()
if u < rho: # Accept
x_t = x_p
else: # Reject
rejected.append((t, x_p))
if burnt == burn_in:
count_rejected[kernel_idx] += 1
if burnt == burn_in: # Sample stage
sample.append(x_t)
count_proposals[kernel_idx] += 1
if len(sample) % log_interval == 0:
print('# Samples:', len(sample))
else: # Burn-in stage
burnt += 1
if burnt % log_interval == 0:
print('# Burnt:', burnt)
# Next step
t += 1
# Rejection statistics
print_rejection_statistics(count_proposals, count_rejected)
return (sample, walk, rejected)
def sample_from_normal_posterior(rho):
# Parser arguments
parser = argparse.ArgumentParser(
description='Metropolis_Hastings generator.')
parser.add_argument('--sample_size', '--size',
type=int, default=100, metavar='N',
help='The sample size of "random" numbers.')
parser.add_argument('--burn_in', '--burn',
type=int, default=0, metavar='N',
help='Number of samples to drop. (default: 0')
parser.add_argument('--sigma', '--sd',
type=float, default=.1, metavar='N',
help='Standard deviation of normal step (default: .1)')
parser.add_argument('--log-interval', '--li',
type=int, default=100, metavar='N',
help='interval to print current status')
args = parser.parse_args()
# Posterior distribution params
mu_1 = -.5
mu_2 = .5
sigma_1 = sigma_2 = 1
# Construct vectors
MU = [mu_1, mu_2]
SIGMA = [[sigma_1 ** 2, rho * sigma_1 * sigma_2],
[rho * sigma_1 * sigma_2, sigma_2 ** 2]]
# Posterior to sample with metropolis hastings
def posterior(x):
return stats.multivariate_normal.pdf(x, MU, SIGMA)
def log_posterior(x):
return stats.multivariate_normal.logpdf(x, MU, SIGMA)
# Hybrid kernel proposals
# X_1 | X_2 normal
def proposal_1(x, x_prime):
mean = mu_1 + rho * (sigma_1 / sigma_2) * (x[1] - mu_2)
sigma = (sigma_1 ** 2) * (1 - rho ** 2)
return stats.norm.logpdf(x_prime[1], mean, sigma)
def step_1(x):
mean = mu_1 + rho * (sigma_1 / sigma_2) * (x[1] - mu_2)
sigma = (sigma_1 ** 2) * (1 - rho ** 2)
return (stats.norm.rvs(mean, sigma), x[1])
# X_2 | X_1 normal
def proposal_2(x, x_prime):
mean = mu_2 + rho * (sigma_2 / sigma_1) * (x[0] - mu_1)
sigma = (sigma_2 ** 2) * (1 - rho ** 2)
return stats.norm.logpdf(x_prime[0], mean, sigma)
def step_2(x):
mean = mu_2 + rho * (sigma_2 / sigma_1) * (x[0] - mu_1)
sigma = (sigma_2 ** 2) * (1 - rho ** 2)
return (x[0], stats.norm.rvs(mean, sigma))
# Intial value for Metropolis-Hastings
x_init = (np.random.uniform(-1, 1), np.random.uniform(-1, 1))
# Sample using Metropolis-Hastings
(sample, walk, rejected) = \
metropolis_hastings_hybrid_kernels(args.sample_size,
x_init,
log_posterior,
[proposal_1, proposal_2],
[step_1, step_2],
[.5, .5],
args.burn_in,
args.log_interval)
# Plot sample
name = 'imgs/sample_normal_s={}_b={}_r={}.png'\
.format(args.sample_size, args.burn_in, rho)
plot_sample(sample, posterior, name)
name = 'imgs/walk_normal_s={}_b={}_r={}.png'\
.format(args.sample_size, args.burn_in, rho)
plot_walk(sample, rejected, posterior, name)
name = 'imgs/burn-in_normal_s={}_b={}_r={}.png'\
.format(args.sample_size, args.burn_in, rho)
plot_individual_walk_mean(list(enumerate(walk)), name)
plot_individual_hist(list(enumerate(sample)), '')
print('Kolmogorov-Smirnov test of normality :')
s0 = [elem[0] for elem in sample]
s1 = [elem[1] for elem in sample]
print(stats.kstest(s0, 'norm'))
print(stats.kstest(s1, 'norm'))
return sample
def main():
# Print format to 3 decimal spaces and fix seed
np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})
# Sample from posterior with different parameters
sample_from_normal_posterior(rho=0.8)
sample_from_normal_posterior(rho=0.99)
if __name__ == "__main__":
main()