Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pm.set_data does not affect posterior predictive shape when pm.Deterministic is used #5512

Closed
paw-lu opened this issue Feb 22, 2022 · 2 comments

Comments

@paw-lu
Copy link
Contributor

paw-lu commented Feb 22, 2022

Description of your problem

Please provide a minimal, self-contained, and reproducible example.

First making a basic example demonstrating the expected result. I have a model, I make a change to the data using pm.set_data, and I expect the posterior predictive to reflect that change. Here I simply change the shape of the data from 100 to 3, and check that the shape of the posterior predictive matches. Everything works as expected.

>>> import numpy as np
>>> import pymc as pm
>>> 
>>> raw_data = np.random.default_rng().normal(loc=30, size=100)
>>> fit_data = raw_data + 2
>>> with pm.Model(coords={"index": list(range(raw_data.shape[0]))}) as >>> model:
>>>     alpha = pm.Normal("alpha", mu=0, sd=1)
>>>     shared_data = pm.MutableData("shared_data", value=raw_data)
>>>     pm.Normal("normal", mu=alpha + shared_data, observed=fit_data)
>>> 
>>>     idata = pm.sample(return_inferencedata=True)
>>>     pm.set_data({"shared_data": [1.0, 2.0, 1.0]})
>>>     pp_idata = pm.sample_posterior_predictive(idata, predictions=True)
>>> 
>>> pp_idata["predictions"]["normal"].shape
(4, 1000, 3)

Now, I run almost the exact same snippet as above, except I wrap alpha + shared_data in pm.Deterministic. The rest is identical. Now, when I check the posterior predictive, the values no longer seem to be affected by pm.set_data, and the shape is still 100.

>>> import numpy as np
>>> import pymc as pm
>>> 
>>> raw_data = np.random.default_rng().normal(loc=30, size=100)
>>> fit_data = raw_data + 2
>>> with pm.Model(coords={"index": list(range(raw_data.shape[0]))}) as model:
>>>     alpha = pm.Normal("alpha", mu=0, sd=1)
>>>     shared_data = pm.MutableData("shared_data", value=raw_data)
>>>     mu = pm.Deterministic("mu", alpha + shared_data)  # New line!
>>>     pm.Normal("normal", mu=mu, observed=fit_data)
>>> 
>>>     idata = pm.sample(return_inferencedata=True)
>>>     pm.set_data({"shared_data": [1.0, 2.0, 1.0]})
>>>     pp_idata = pm.sample_posterior_predictive(idata, predictions=True)
>>> 
>>> pp_idata["predictions"]["normal"].shape
(4, 1000, 100)

Versions and main components

  • PyMC/PyMC3 Version: 4.0.0b2 installed directly from source (24f9bd4)
  • Aesara/Theano Version: 2.3.8
  • Python Version: 3.9.5
  • Operating system: macOS
  • How did you install PyMC/PyMC3: pip directly from main branch (24f9bd4)
@ricardoV94
Copy link
Member

duplicate of #5302

@paw-lu
Copy link
Contributor Author

paw-lu commented Feb 22, 2022

ah didn't see that one!
thanks for linking the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants