Skip to content

Commit

Permalink
add test case for idata.posterior in test_sample_posterior_predictive…
Browse files Browse the repository at this point in the history
…_w (#4282)
  • Loading branch information
MarcoGorelli authored Dec 3, 2020
1 parent 96c430e commit f167af3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pymc3/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1871,8 +1871,8 @@ def sample_posterior_predictive_w(

except KeyboardInterrupt:
pass

return {k: np.asarray(v) for k, v in ppc.items()}
else:
return {k: np.asarray(v) for k, v in ppc.items()}


def sample_prior_predictive(
Expand Down
34 changes: 29 additions & 5 deletions pymc3/tests/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,29 +705,53 @@ def test_variable_type(self):

class TestSamplePPCW(SeededTest):
def test_sample_posterior_predictive_w(self):
data0 = np.random.normal(0, 1, size=500)
data0 = np.random.normal(0, 1, size=50)
warning_msg = "The number of samples is too small to check convergence reliably"

with pm.Model() as model_0:
mu = pm.Normal("mu", mu=0, sigma=1)
y = pm.Normal("y", mu=mu, sigma=1, observed=data0)
trace_0 = pm.sample()
with pytest.warns(UserWarning, match=warning_msg):
trace_0 = pm.sample(10, tune=0, chains=2, return_inferencedata=False)
idata_0 = az.from_pymc3(trace_0)

with pm.Model() as model_1:
mu = pm.Normal("mu", mu=0, sigma=1, shape=len(data0))
y = pm.Normal("y", mu=mu, sigma=1, observed=data0)
trace_1 = pm.sample()
with pytest.warns(UserWarning, match=warning_msg):
trace_1 = pm.sample(10, tune=0, chains=2, return_inferencedata=False)
idata_1 = az.from_pymc3(trace_1)

with pm.Model() as model_2:
# Model with no observed RVs.
mu = pm.Normal("mu", mu=0, sigma=1)
with pytest.warns(UserWarning, match=warning_msg):
trace_2 = pm.sample(10, tune=0, return_inferencedata=False)

traces = [trace_0, trace_1]
idatas = [idata_0, idata_1]
models = [model_0, model_1]

ppc = pm.sample_posterior_predictive_w(traces, 100, models)
assert ppc["y"].shape == (100, 500)
assert ppc["y"].shape == (100, 50)

ppc = pm.sample_posterior_predictive_w(idatas, 100, models)
assert ppc["y"].shape == (100, 500)
assert ppc["y"].shape == (100, 50)

with model_0:
ppc = pm.sample_posterior_predictive_w([idata_0.posterior], None)
assert ppc["y"].shape == (20, 50)

with pytest.raises(ValueError, match="The number of traces and weights should be the same"):
pm.sample_posterior_predictive_w([idata_0.posterior], 100, models, weights=[0.5, 0.5])

with pytest.raises(ValueError, match="The number of models and weights should be the same"):
pm.sample_posterior_predictive_w([idata_0.posterior], 100, models)

with pytest.raises(
ValueError, match="The number of observed RVs should be the same for all models"
):
pm.sample_posterior_predictive_w([trace_0, trace_2], 100, [model_0, model_2])


@pytest.mark.parametrize(
Expand Down

0 comments on commit f167af3

Please sign in to comment.