There is a new bug in fast_sample_posterior_predictive() in pymc version 3.11.2, a bug that was not present as recently as version 3.9.3. The bug is not present in sample_posterior_predictive().
Here’s a minimal example that exhibits the bug. Note that the example uses @lucianopaz 's model factory approach, with two somewhat different models, one sampled and one sampled posterior predictive:
import pymc3 as pm
b_observed = 1
with pm.Model() as m:
p = pm.Beta('p', 2, 2)
b = pm.Binomial('b', n=1, p=p, observed=b_observed)
trace = pm.sample(
draws=600, tune=1000, chains=2, return_inferencedata=True)
with pm.Model() as m_forward:
p = pm.Beta('p', 2, 2)
b2 = pm.Binomial('b2', n=1, p=p)
b3 = pm.Binomial('b3', n=1, p=p*b2)
with m_forward:
trace_forward = pm.fast_sample_posterior_predictive(
trace, var_names=['p', 'b2', 'b3'])
This call to fast_sample_posterior_predictive() results in an AssertionError:
The AssertionError is raised in this code, within pymc3/distributions/posterior_predictive.py:
def random_sample(
meth: Callable[..., np.ndarray],
param,
point: _TraceDict,
size: int,
shape: tuple[int, ...],
) -> np.ndarray:
val = meth(point=point, size=size)
try:
assert val.shape == (size,) + shape, (
"Sampling from random of %s yields wrong shape" % param
)
The debugger reveals the following values for variables:
I’m not sure whether size should be 1200 instead of (1200,), or whether
the assertion should be assert val.shape == size + shape instead of assert val.shape == (size,) + shape. But something is wrong.
Note that this error was not raised in v3.9.3, and is not raised in v3.11.2 when sample_posterior_predictive() is called instead.