pymc.model.transform.conditioning.observe#

pymc.model.transform.conditioning.observe(model, vars_to_observations)[source]#

Convert free RVs or Deterministics to observed RVs.

Parameters:
model: PyMC Model
vars_to_observations: Dict of variable or name to TensorLike

Dictionary that maps model variables (or names) to observed values. Observed values must have a shape and data type that is compatible with the original model variable.

Returns:
new_model: PyMC model

A distinct PyMC model with the relevant variables observed. All remaining variables are cloned and can be retrieved via new_model[“var_name”].

Examples

import pymc as pm

with pm.Model() as m:
    x = pm.Normal("x")
    y = pm.Normal("y", x)
    z = pm.Normal("z", y)

m_new = pm.observe(m, {y: 0.5})

Deterministic variables can also be observed. If the variable has already been observed, its old value is replaced with the one provided.

This relies on PyMC ability to infer the logp of the underlying expression

import pymc as pm

with pm.Model() as m:
    x = pm.Normal("x")
    y = pm.Normal.dist(x, shape=(5,))
    y_censored = pm.Deterministic("y_censored", pm.math.clip(y, -1, 1))

new_m = pm.observe(m, {y_censored: [0.9, 0.5, 0.3, 1, 1]})