Exemplo n.º 1
0
def test_sample_auto_batching(vectorized_model_fixture, xla_fixture,
                              use_auto_batching_fixture):
    model, is_vectorized_model, core_shapes = vectorized_model_fixture
    num_samples = 10
    num_chains = 4
    if not is_vectorized_model and not use_auto_batching_fixture:
        with pytest.raises(Exception):
            pm.sample(
                model=model(),
                num_samples=num_samples,
                num_chains=num_chains,
                burn_in=1,
                step_size=0.1,
                xla=xla_fixture,
                use_auto_batching=use_auto_batching_fixture,
            )
    else:
        trace = pm.sample(
            model=model(),
            num_samples=num_samples,
            num_chains=num_chains,
            burn_in=1,
            step_size=0.1,
            xla=xla_fixture,
            use_auto_batching=use_auto_batching_fixture,
        )
        posterior = trace.posterior
        for rv_name, core_shape in core_shapes.items():
            assert posterior[rv_name].shape == (num_chains,
                                                num_samples) + core_shape
Exemplo n.º 2
0
def test_sampling(mixture, xla_fixture):
    model, n, k = mixture
    if xla_fixture:
        with pytest.raises(tf.errors.InvalidArgumentError):
            pm.sample(model, num_samples=100, num_chains=2, xla=xla_fixture)
    else:
        trace = pm.sample(model, num_samples=100, num_chains=2, xla=xla_fixture)
        if n == 1:
            assert trace.posterior["mixture/means"].shape == (2, 100, k)
        else:
            assert trace.posterior["mixture/means"].shape == (2, 100, n, k)
Exemplo n.º 3
0
def test_sampling_log_likelihood(vectorized_model_fixture):
    model, is_vectorized_model, core_shapes = vectorized_model_fixture
    num_samples = 10
    num_chains = 4
    trace = pm.sample(
        model=model(),
        num_samples=num_samples,
        num_chains=num_chains,
        burn_in=1,
        step_size=0.1,
        include_log_likelihood=True,
    )

    if is_vectorized_model:
        # only one log likeliood matrix
        assert trace.log_likelihood["model/x"].shape == (num_chains,
                                                         num_samples)

    else:
        state, _ = pm.initialize_sampling_state(model())
        assert trace.log_likelihood["model/x"].shape == (
            num_chains,
            num_samples,
            *state.observed_values["model/x"].shape,
        )
Exemplo n.º 4
0
def test_posterior_predictive(mixture):
    model, n, _ = mixture
    trace = pm.sample(model, num_samples=100, num_chains=2)
    ppc = pm.sample_posterior_predictive(model, trace).posterior_predictive
    if n == 1:
        assert ppc["mixture/mixture"].shape == (2, 100, 100)
    else:
        assert ppc["mixture/mixture"].shape == (2, 100, 100, n)
Exemplo n.º 5
0
def test_sample_deterministics(simple_model_with_deterministic, xla_fixture):
    model = simple_model_with_deterministic()
    trace = pm.sample(
        model=model, num_samples=10, num_chains=4, burn_in=100, step_size=0.1, xla=xla_fixture
    )
    norm = "simple_model_with_deterministic/simple_model/norm"
    determ = "simple_model_with_deterministic/determ"
    np.testing.assert_allclose(trace.posterior[determ], trace.posterior[norm] * 2)
Exemplo n.º 6
0
def posterior_predictive_fixture(model_with_observed_fixture):
    num_samples = 40
    num_chains = 3
    (model, observed, core_ppc_shapes,
     observed_in_RV) = model_with_observed_fixture
    trace = pm.sample(model(),
                      num_samples=num_samples,
                      num_chains=num_chains,
                      observed=observed)
    return model, observed, core_ppc_shapes, observed_in_RV, trace, num_samples, num_chains
Exemplo n.º 7
0
def test_beta_sample():
    @pm.model
    def model():
        dist = yield pm.Beta("beta", 0, 1)
        return dist

    trace = pm.sample(model(), num_samples=1, burn_in=1)

    assert trace.posterior["model/beta"] is not None
    assert trace.posterior["model/__sigmoid_beta"] is not None
Exemplo n.º 8
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.º 9
0
def test_compound_sampling(model_with_discrete_and_continuous, xla_fixture,
                           seed):
    model = model_with_discrete_and_continuous()
    trace = pm.sample(model=model,
                      sampler_type="compound",
                      xla_fixture=xla_fixture,
                      seed=seed)
    round_value = round(
        trace.posterior["model_with_discrete_and_continuous/disc"].mean().item(
        ), 1)
    np.testing.assert_allclose(round_value, 0.9, atol=0.1)
Exemplo n.º 10
0
def test_discrete_sampling_bernoulli(model_with_discrete_bernoulli,
                                     xla_fixture, seed):
    model = model_with_discrete_bernoulli()
    trace = pm.sample(model=model,
                      sampler_type="compound",
                      xla_fixture=xla_fixture,
                      seed=seed)
    round_value = round(
        trace.posterior["model_with_discrete_bernoulli/disc"].mean().item(), 1)
    # check to match the bernoulli prob parameter
    np.testing.assert_allclose(round_value, 0.9, atol=0.1)
Exemplo n.º 11
0
def test_gp_invalid_prior(tf_seed):
    """Test if an error is thrown for invalid model prior"""
    @pm.model
    def invalid_model(gp, X, X_new):
        f = gp.prior("f", X)
        cond = yield gp.conditional("fcond", X_new, given={"X": X, "f": f})

    with pytest.raises(ValueError, match=r"must be a numpy array or tensor"):
        gp = pm.gp.LatentGP(cov_fn=pm.gp.cov.ExpQuad(1.0, 1.0))
        X = tf.random.normal((2, 5, 1))
        X_new = tf.random.normal((2, 2, 1))
        trace = pm.sample(invalid_model(gp, X, X_new),
                          num_samples=1,
                          burn_in=1,
                          num_chains=1)
Exemplo n.º 12
0
def test_sampling_with_deterministics_in_nested_models(
    deterministics_in_nested_models, xla_fixture
):
    (
        model,
        expected_untransformed,
        expected_transformed,
        expected_deterministics,
        deterministic_mapping,
    ) = deterministics_in_nested_models
    trace = pm.sample(
        model=model(), num_samples=10, num_chains=4, burn_in=100, step_size=0.1, xla=xla_fixture
    )
    for deterministic, (inputs, op) in deterministic_mapping.items():
        np.testing.assert_allclose(
            trace.posterior[deterministic], op(*[trace.posterior[i] for i in inputs]), rtol=1e-6
        )
Exemplo n.º 13
0
def mc3_approach(T, width, height, N):
    shape = (height, width)
    x0 = np.random.randint(2, size=shape)
    with pm.model() as model:
        x = pm.Bernoulli('x', 0.5, shape=shape, testval=x0)
        magnetization = pm.Potential(
            'm',
            -get_H(to_spins(x)) / T
        )
        scaling = .0006
        mul = int(height * width * 1.75)
        step = pm.BinaryMetropolis([x], scaling=scaling)
        trace = pm.sample(N * mul * 5, step=step, chains=1, tune=False)
    dataset = [to_two_color(2 * t['x'] - 1) for t in trace[::mul * 5]]
    # Print out the final percent magnetization
    lattice = 2 * trace[-1]['x'] - 1
    print('Finished. Net magnetization: {:3.0%}'
              .format(abs(lattice.sum()) / lattice.size))
    return dataset
Exemplo n.º 14
0
def test_sampling_with_no_free_rvs(simple_model_no_free_rvs):
    model = simple_model_no_free_rvs()
    with pytest.raises(ValueError):
        trace = pm.sample(model=model, num_samples=1, num_chains=1, burn_in=1)
Exemplo n.º 15
0
# 4. Sampling
# ------------------------------------------------------------------------------ #

begin_time = time.time()
log.info("start")
num_chains = 4

trace_tuning, trace = pm.sample(
    this_model,
    num_samples=int(args.num_samples),
    num_samples_binning=10,
    burn_in_min=10,
    burn_in=int(args.num_burn_in),
    use_auto_batching=False,
    num_chains=num_chains,
    xla=False,
    initial_step_size=0.00001,
    ratio_tuning_epochs=1.3,
    max_tree_depth=4,
    decay_rate=0.75,
    target_accept_prob=0.75,
    # num_steps_between_results = 9,
    #    state=pm.evaluate_model_transformed(this_model)[1]
    # sampler_type="nuts",
)

end_time = time.time()
log.info("running time: {:.1f}s".format(end_time - begin_time))

# We also Sample the prior for the kde in the plots (optional)
trace_prior = pm.sample_prior_predictive(this_model,
                                         sample_shape=(10, ),
Exemplo n.º 16
0
@pymc4_linear_regression.define
def process(cfg):
    c = ed.Uniform(low=cfg.cmin, high=cfg.cmax, name="c")
    m = ed.Normal(loc=cfg.mmu, scale=cfg.msigma, name="m")

    y = ed.Normal(loc=(m*cfg.x+c), scale=np.float32(cfg.sigma), name="y")
    return y

# add observed data
pymc4_linear_regression.observe(y=data)

Nsamples = 2000  # final number of samples
Nburn = 2000     # number of tuning samples

pymc4_trace = pm4.sample(pymc4_linear_regression, num_results=Nsamples,
                         num_burnin_steps=Nburn, step_size=0.01,
                         num_leapfrog_steps=5)

postsamples = np.vstack((pymc4_trace['m'], pymc4_trace['c'])).T
print(postsamples)

# plot posterior samples (if corner.py is installed)
try:
    import corner # import corner.py
except ImportError:
    sys.exit(1)

print('Number of posterior samples is {}'.format(postsamples.shape[0]))

fig = corner.corner(postsamples, labels=[r"$m$", r"$c$"], truths=[m, c])
fig.savefig('pymc4.png')
Exemplo n.º 17
0
def test_sampling_unknown_sampler(simple_model):
    model = simple_model()
    with pytest.raises(KeyError):
        trace = pm.sample(model=model, sampler_type="unknown")
Exemplo n.º 18
0
    mvalue = yield pm4.Normal(name='m', loc=mmu, scale=msigma)  # prior on m

    # the straight line model
    mu = cvalue + mvalue * x

    # the likelihood
    yield pm4.Normal(name='t_obs', loc=mu, scale=sigma, observed=data)


Nsamples = 2000  # final number of samples
Nburn = 2000  # number of tuning samples

# perform the sampling
pymc4_trace = pm4.sample(
    model(xtensor, datatensor),
    num_chains=1,
    burn_in=Nburn,
    num_samples=Nsamples,
)

postsamples = np.vstack((pymc4_trace.posterior["model/m"].data.flatten(),
                         pymc4_trace.posterior["model/c"].data.flatten())).T

# plot posterior samples (if corner.py is installed)
try:
    import corner  # import corner.py
except ImportError:
    sys.exit(1)

print('Number of posterior samples is {}'.format(postsamples.shape[0]))

fig = corner.corner(postsamples, labels=[r"$m$", r"$c$"], truths=[m, c])
Exemplo n.º 19
0
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']

print(np.mean(pymc4_theta, axis=0))