Skip to content

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

Closed
@AlexAndorra

Description

@AlexAndorra

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions