Skip to content

ENH: Enable changing observations of observed variables with observe operator #6932

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

Closed
AlexAndorra opened this issue Sep 27, 2023 · 4 comments · Fixed by #7679
Closed

ENH: Enable changing observations of observed variables with observe operator #6932

AlexAndorra opened this issue Sep 27, 2023 · 4 comments · Fixed by #7679

Comments

@AlexAndorra
Copy link
Contributor

AlexAndorra commented Sep 27, 2023

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

@cluhmann
Copy link
Member

cluhmann commented Apr 2, 2024

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()).

@ricardoV94
Copy link
Member

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

@ricardoV94 ricardoV94 changed the title ENH: Enable changing observations with do operator ENH: Enable changing observations of observed variables with observe operator Apr 3, 2024
@cluhmann
Copy link
Member

cluhmann commented Apr 3, 2024

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.

@ricardoV94
Copy link
Member

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

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

Successfully merging a pull request may close this issue.

3 participants