forked from blei-lab/edward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
normal_normal_mh.py
43 lines (32 loc) · 967 Bytes
/
normal_normal_mh.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
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import edward as ed
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from edward.models import Empirical, Normal
ed.set_seed(42)
# DATA
x_data = np.array([0.0] * 50, dtype=np.float32)
# MODEL: Normal-Normal with known variance
mu = Normal(mu=0.0, sigma=1.0)
x = Normal(mu=tf.ones(50) * mu, sigma=1.0)
# INFERENCE
qmu = Empirical(params=tf.Variable(tf.zeros([1000])))
proposal_mu = Normal(mu=0.0, sigma=tf.sqrt(1.0 / 51.0))
# analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140)
data = {x: x_data}
inference = ed.MetropolisHastings({mu: qmu}, {mu: proposal_mu}, data)
inference.run()
# CRITICISM
# Check convergence with visual diagnostics.
sess = ed.get_session()
samples = sess.run(qmu.params)
# Plot histogram.
plt.hist(samples, bins='auto')
plt.show()
# Trace plot.
plt.plot(samples)
plt.show()