def test_missing_blanket(self): N = 10 z = rvs.Bernoulli(p=0.75, sample_shape=N) z_cond = ed.complete_conditional(z) self.assertIsInstance(z_cond, rvs.Bernoulli) with self.test_session() as sess: p_val = sess.run(z_cond.p) self.assertAllClose(p_val, 0.75 + np.zeros(N, np.float32))
def test_basic_bernoulli(self): N = 10 z = rvs.Bernoulli(probs=0.75, sample_shape=N) z_cond = ed.complete_conditional(z, [z]) self.assertIsInstance(z_cond, rvs.Bernoulli) with self.test_session() as sess: p_val = sess.run(z_cond.probs) self.assertAllClose(p_val, 0.75 + np.zeros(N, np.float32))
def test_beta_bernoulli(self): x_data = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1]) a0 = 0.5 b0 = 1.5 pi = rvs.Beta(a=a0, b=b0) x = rvs.Bernoulli(p=pi, sample_shape=10) pi_cond = ed.complete_conditional(pi, [pi, x]) self.assertIsInstance(pi_cond, rvs.Beta) with self.test_session() as sess: a_val, b_val = sess.run([pi_cond.a, pi_cond.b], {x: x_data}) self.assertAllClose(a_val, a0 + x_data.sum()) self.assertAllClose(b_val, b0 + (1 - x_data).sum())
For Part II, we would like you to hand in your code, and the runs that produce the two answers""" import tensorflow as tf print("tensorflow version: %s" % tf.__version__) import edward as ed print("edward version: %s" % ed.__version__) import edward.models as edm import edward.inferences as edi matplotlib inline config InlineBackend.figure_format = 'retina' import matplotlib.pyplot as plt rain = edm.Bernoulli(probs=0.2) p_sprinkler = tf.where(tf.cast(rain, tf.bool), 0.01, 0.4) sprinkler = edm.Bernoulli(probs=p_sprinkler) p_grass_wet = tf.where(tf.cast(rain, tf.bool), tf.where(tf.cast(sprinkler, tf.bool), 0.99, 0.8), tf.where(tf.cast(sprinkler, tf.bool), 0.9, 0.00000001)) grass_wet = edm.Bernoulli(probs=p_grass_wet) with tf.Session(): plt.hist([grass_wet.eval() for _ in range(1000)]); q_rain = edm.Bernoulli(probs=tf.nn.sigmoid(tf.Variable(tf.random_normal([])))) ed.get_session() inf = edi.KLpq({rain: q_rain}, data={grass_wet: tf.constant(1, dtype=tf.int32)}) inf.run(n_samples=50) print(q_rain.probs.eval())