Skip to content

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

@paw-lu

Description

@paw-lu

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions