pymc icon indicating copy to clipboard operation
pymc copied to clipboard

ENH: Enable changing observations of observed variables with `observe` operator

Open AlexAndorra opened this issue 2 years ago • 4 comments

As discussed with @ricardoV94, currently, we can change observations with the do operator only for a model that has no observations, i.e only free or deterministic variables:

Before

# define model
with pm.Model() as m:
    x = pm.MutableData("x", np.linspace(-5, 5, 50))
    
    b0 = pm.Normal("b0")
    b1 = pm.Normal("b1")
    
    noise = pm.HalfNormal("noise")
    y = pm.Normal("y", pm.math.sigmoid(b0 + b1 * x), sigma=noise, observed=np.random.randn(50))

# generate new observations, e.g for parameter-recovery study
with pm.do(m, {noise: 1.0}) as prior_m:
    prior_pred = pm.sample_prior_predictive(1).isel(chain=0, draw=0)

# run inference with these new observations
with pm.observe(m, {y: prior_pred.prior_predictive["y"]}) as observed_m:
    posterior = pm.sample()

# this raises
ValueError: At least one var is not a free variable or deterministic in the model

After

Being able to do the above

AlexAndorra avatar Sep 27 '23 09:09 AlexAndorra

Is the issue here with pm.do() or with pm.observe()? The usage of pm.do() in the example seems to run without a problem. The call to pm.observe() fails if observed=np.random.randn(50), but runs fine otherwise (with or without the prior call to pm.do()).

cluhmann avatar Apr 02 '24 23:04 cluhmann

The problem is with observe. Specifically observing a variable that's already observed

ricardoV94 avatar Apr 03 '24 06:04 ricardoV94

This limitation particularly odd in that it seems like it prevents users from observe()ing MutableData, correct? Definitely not intuitive why this is invalid (set_data() and observe() seem strongly related).

Furthermore, the error message that is generated here is cryptic. Clearly the code knows which parameter(s) is(are) neither deterministic nor free, but fails to provide this information to the user for unknown reasons. But this will all go away once we can observe() more stuff.

cluhmann avatar Apr 03 '24 19:04 cluhmann

The error message makes sense if you know the original idea was to go from free/deterministic -> observed.

The author (me) just didn't consider going from observed -> observed

ricardoV94 avatar Apr 03 '24 19:04 ricardoV94