Skip to content

Commit f167af3

Browse files
authored
add test case for idata.posterior in test_sample_posterior_predictive_w (#4282)
1 parent 96c430e commit f167af3

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

pymc3/sampling.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1871,8 +1871,8 @@ def sample_posterior_predictive_w(
18711871

18721872
except KeyboardInterrupt:
18731873
pass
1874-
1875-
return {k: np.asarray(v) for k, v in ppc.items()}
1874+
else:
1875+
return {k: np.asarray(v) for k, v in ppc.items()}
18761876

18771877

18781878
def sample_prior_predictive(

pymc3/tests/test_sampling.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -705,29 +705,53 @@ def test_variable_type(self):
705705

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

710711
with pm.Model() as model_0:
711712
mu = pm.Normal("mu", mu=0, sigma=1)
712713
y = pm.Normal("y", mu=mu, sigma=1, observed=data0)
713-
trace_0 = pm.sample()
714+
with pytest.warns(UserWarning, match=warning_msg):
715+
trace_0 = pm.sample(10, tune=0, chains=2, return_inferencedata=False)
714716
idata_0 = az.from_pymc3(trace_0)
715717

716718
with pm.Model() as model_1:
717719
mu = pm.Normal("mu", mu=0, sigma=1, shape=len(data0))
718720
y = pm.Normal("y", mu=mu, sigma=1, observed=data0)
719-
trace_1 = pm.sample()
721+
with pytest.warns(UserWarning, match=warning_msg):
722+
trace_1 = pm.sample(10, tune=0, chains=2, return_inferencedata=False)
720723
idata_1 = az.from_pymc3(trace_1)
721724

725+
with pm.Model() as model_2:
726+
# Model with no observed RVs.
727+
mu = pm.Normal("mu", mu=0, sigma=1)
728+
with pytest.warns(UserWarning, match=warning_msg):
729+
trace_2 = pm.sample(10, tune=0, return_inferencedata=False)
730+
722731
traces = [trace_0, trace_1]
723732
idatas = [idata_0, idata_1]
724733
models = [model_0, model_1]
725734

726735
ppc = pm.sample_posterior_predictive_w(traces, 100, models)
727-
assert ppc["y"].shape == (100, 500)
736+
assert ppc["y"].shape == (100, 50)
728737

729738
ppc = pm.sample_posterior_predictive_w(idatas, 100, models)
730-
assert ppc["y"].shape == (100, 500)
739+
assert ppc["y"].shape == (100, 50)
740+
741+
with model_0:
742+
ppc = pm.sample_posterior_predictive_w([idata_0.posterior], None)
743+
assert ppc["y"].shape == (20, 50)
744+
745+
with pytest.raises(ValueError, match="The number of traces and weights should be the same"):
746+
pm.sample_posterior_predictive_w([idata_0.posterior], 100, models, weights=[0.5, 0.5])
747+
748+
with pytest.raises(ValueError, match="The number of models and weights should be the same"):
749+
pm.sample_posterior_predictive_w([idata_0.posterior], 100, models)
750+
751+
with pytest.raises(
752+
ValueError, match="The number of observed RVs should be the same for all models"
753+
):
754+
pm.sample_posterior_predictive_w([trace_0, trace_2], 100, [model_0, model_2])
731755

732756

733757
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)