Exemplo n.º 1
0
def test_model_target_log_prob_fn():
    model = pm.Model()

    @model.define
    def simple(cfg):
        ed.Normal(0., 1., name='normal')

    model.target_log_prob_fn()
Exemplo n.º 2
0
def test_model_session():
    model = pm.Model()

    @model.define
    def simple(cfg):
        ed.Normal(0., 1., name='normal')

    assert isinstance(model.session, tf.Session)
Exemplo n.º 3
0
def test_model_definition_type1():
    model = pm.Model(name="testName")

    @model.define
    def simple(cfg):
        ed.Normal(0., 1., name='normal')

    assert 'normal' in model.variables
    assert [] == model.variables['normal'].shape.as_list()
    assert model.name == "testName"
Exemplo n.º 4
0
def test_variables():
    model = pm.Model()

    @model.define
    def simple(cfg):
        ed.Normal(0., 1., name='normal')

    assert len(model.variables) == 1
    assert len(model.unobserved) == 1
    assert "normal" in model.variables
Exemplo n.º 5
0
def test_sample():
    model = pm.Model()

    @model.define
    def sample(cfg):
        mu = ed.Normal(0., 1., name="mu")

    trace = pm.sample(model)

    assert 0. == pytest.approx(trace["mu"].mean(), 1)
    assert 1. == pytest.approx(trace["mu"].std(), 1)
Exemplo n.º 6
0
def test_model_log_prob_fn():
    model = pm.Model()

    @model.define
    def simple(cfg):
        mu = ed.Normal(0., 1., name="mu")

    log_prob_fn = model.target_log_prob_fn()

    with tf.Session():
        assert -0.91893853 == pytest.approx(log_prob_fn(0).eval(), 0.00001)
Exemplo n.º 7
0
def test_model_observe():

    model = pm.Model()

    @model.define
    def simple(cfg):
        ed.Normal(0., 1., name='normal')

    model.observe(normal=1)

    assert len(model.observed) == 1
    assert not model.unobserved
Exemplo n.º 8
0
def test_model_config():
    model = pm.Model()

    assert model.cfg == {}

    model = pm.Model(var1=123)

    @model.define
    def simple(cfg):
        assert cfg["var1"] == 123

    model = pm.Model(var1=123)

    @model.define
    def simple(cfg):
        pass

    model = model.configure(var1=12)

    @model.define
    def simple(cfg):
        assert cfg["var1"] == 12
Exemplo n.º 9
0
import numpy as np
import pymc4 as pm4
import tensorflow as tf  # For Random Variable operation
from tensorflow_probability import edward2 as ed  # For defining random variables

J = 8  # No. of schools
y = np.array([28., 8., -3., 7., -1., 1., 18., 12.])
sigma = np.array([15., 10., 16., 11., 9., 11., 10., 18.])

pymc4_non_centered_eight = pm4.Model(num_schools=J, y=y, sigma=sigma)


@pymc4_non_centered_eight.define
def process(cfg):
    mu = ed.Normal(loc=0., scale=10., name="mu")
    log_tau = ed.Normal(loc=5., scale=1., name="log_tau")
    theta_prime = ed.Normal(loc=tf.zeros(cfg.num_schools),
                            scale=tf.ones(cfg.num_schools),
                            name="theta_prime")
    theta = mu + tf.exp(log_tau) * theta_prime
    y = ed.Normal(loc=theta, scale=np.float32(cfg.sigma), name="y")
    return y


pymc4_non_centered_eight.observe(y=y)

pymc4_trace = pm4.sample(pymc4_non_centered_eight)

pymc4_theta = pymc4_trace['mu'][:, np.newaxis] + np.exp(
    pymc4_trace['log_tau'])[:, np.newaxis] * pymc4_trace['theta_prime']