pymc icon indicating copy to clipboard operation
pymc copied to clipboard

`sample_posterior_predictive` does not consider changes in "ConstantData" and "Constant coords"

Open ricardoV94 opened this issue 2 years ago • 3 comments

Description

The existing checks assume the same model is used between sample and sample_posterior_predictive, in which case the only variable that can be changed are mutable ones.

However if one wants to sample in a new model (as described in https://www.pymc-labs.io/blog-posts/out-of-model-predictions-with-pymc/), they have to artificially use mutable variables even if they never intend to change them after definition:

import pymc as pm

with pm.Model() as m:
    x = pm.ConstantData("x", [0, 0, 0, 0, 0, 0])
    b = pm.Normal("b", x)
    y = pm.Normal("y", b.sum(), observed=0)
    idata = pm.sample()

with pm.Model() as pred_m:
    x = pm.ConstantData("x", [0, 0, 0])
    # x = pm.MutableData("x", [0, 0, 0])  # This works fine
    b = pm.Normal("b", x)
    y = pm.Normal("y", b.sum(), observed=0)
    idata = pm.sample_posterior_predictive(idata, predictions=True,)  # Fails because b isn't resampled

A similar thing happens for variables whose dims are changed. If they were defined in coords_mutable it will resample correctly, but not if they were defined as vanilla "constant" coords

ricardoV94 avatar Aug 24 '23 11:08 ricardoV94

Related to #6977

ricardoV94 avatar Feb 14 '24 08:02 ricardoV94