pymc icon indicating copy to clipboard operation
pymc copied to clipboard

BUG: `pm.sample_posterior_predictive` discards new results when `extend_inferencedata=True` and `posterior_predictive` group already exists

Open jessegrabowski opened this issue 11 months ago • 2 comments

Describe the issue:

pm.sample_posterior_predictive silently fails when extend_inferencedata = True and posterior_predictive group exists

Reproduceable code example:

import pymc as pm
import numpy as np

y = np.random.normal(loc=1, size=(100,))
with pm.Model() as m:
    mu = pm.Normal('mu')
    y_hat = pm.Normal('y_hat', mu=mu, sigma=1, observed=y)
    idata = pm.sample()
    idata = pm.sample_posterior_predictive(idata, extend_inferencedata=True)

with pm.Model() as m2:
    new_mu = pm.Normal('mu2', sigma=10)
    y_hat = pm.Normal('y_hat', mu=new_mu, sigma=1)
    idata = pm.sample_posterior_predictive(idata, extend_inferencedata=True)

Error message:

No response

PyMC version information:

5.11

Context for the issue:

In the above snippet, the 2nd sample_posterior_predictive will not error, but the samples will not be associated with the new_mu model.

jessegrabowski avatar Mar 22 '24 17:03 jessegrabowski

Fix needs to either raise an error if the group already exists or override the original as we do in https://github.com/pymc-devs/pymc-experimental/pull/249/commits/28a6cb19dbf9d2f22483055cdf6d666b429f4996

ricardoV94 avatar Mar 25 '24 11:03 ricardoV94

https://python.arviz.org/en/stable/api/generated/arviz.InferenceData.extend.html

ricardoV94 avatar Mar 25 '24 11:03 ricardoV94