Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinvtran committed Sep 26, 2016
1 parent 0d82e94 commit 188d04a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
9 changes: 6 additions & 3 deletions examples/normal_normal.py
Expand Up @@ -11,17 +11,20 @@

ed.set_seed(42)

# Normal-Normal with known variance
# 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_mu = tf.Variable(tf.random_normal([]))
qmu_sigma = tf.nn.softplus(tf.Variable(tf.random_normal([])))
qmu = Normal(mu=qmu_mu, sigma=qmu_sigma)

data = {x: np.array([0.0] * 50, dtype=np.float32)}

# analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140)
data = {x: x_data}
inference = ed.MFVI({mu: qmu}, data)

inference.initialize()
Expand Down
47 changes: 47 additions & 0 deletions examples/normal_normal_mh.py
@@ -0,0 +1,47 @@
#!/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_params = tf.Variable(tf.zeros([500]))
qmu = Empirical(params=qmu_params)

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.initialize()
for t in range(inference.n_iter):
info_dict = inference.update()
inference.print_progress(t, info_dict)

# 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()

0 comments on commit 188d04a

Please sign in to comment.