Skip to content

Commit

Permalink
Merge e850e9f into 02ef44f
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Fonnesbeck committed Oct 3, 2016
2 parents 02ef44f + e850e9f commit 94e7dd9
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion pymc3/tests/test_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from .checks import close_to
from .models import mv_simple, mv_simple_discrete, simple_2model
from .helpers import SeededTest
from pymc3.sampling import assign_step_methods, sample
from pymc3.model import Model
from pymc3.step_methods import (NUTS, BinaryGibbsMetropolis, Metropolis, Constant, Slice,
CompoundStep, MultivariateNormalProposal, HamiltonianMC)
from pymc3.distributions import Binomial, Normal, Bernoulli, Categorical
from pymc3.distributions import Binomial, Normal, Bernoulli, Categorical, Uniform
from numpy.testing import assert_almost_equal
import numpy as np

Expand Down Expand Up @@ -113,3 +114,31 @@ def test_binomial(self):
Binomial('x', 10, 0.5)
steps = assign_step_methods(model, [])
self.assertIsInstance(steps, Metropolis)

class TestSampleEstimates(SeededTest):
def test_parameter_estimate(self):

alpha_true, sigma_true = 1, 0.5
beta_true = np.array([1, 2.5])

size = 100

X1 = np.random.randn(size)
X2 = np.random.randn(size) * 0.2
Y = alpha_true + beta_true[0] * X1 + beta_true[1] * X2 + np.random.randn(size) * sigma_true

for step_method in (NUTS(), Metropolis(), Slice()):

with Model() as model:
alpha = Normal('alpha', mu=0, sd=10)
beta = Normal('beta', mu=0, sd=10, shape=2)
sigma = Uniform('sigma', lower=0.0, upper=1.0)
mu = alpha + beta[0]*X1 + beta[1]*X2
Y_obs = Normal('Y_obs', mu=mu, sd=sigma, observed=Y)

trace = sample(1000, step=step_method)


assert np.isclose(np.median(trace.beta, 0), beta_true, rtol=0.1).all()
assert np.isclose(np.median(trace.alpha), alpha_true, rtol=0.1)
assert np.isclose(np.median(trace.sigma), sigma_true, rtol=0.1)

0 comments on commit 94e7dd9

Please sign in to comment.