obs()#
- liesel.model.nodes.obs(value, distribution=None, name='')[source]#
Helper function that returns an observed
Var
.Deprecated since version v0.2.10: Use
Var.new_obs()
instead. This function will be removed in v0.4.0.Sets the
Var.observed
flag. If the observed variable is a random variable, i.e. if it has an associated probability distribution, its log-probability is automatically added to the model log-likelihood (seeModel.log_lik
).- Parameters:
- Return type:
- Returns:
An observed variable.
See also
Var.new_obs
Initializes a strong variable that holds observed data.
Notes
A variable will compute its log probability only when
Calc.update()
is called. This does not happen automatically upon initialization. Commonly, the first time this method is called is during the initialization of aModel
. To update the value immediately, you can callVar.update()
manually.Examples
>>> import tensorflow_probability.substrates.jax.distributions as tfd
We can declare an observed variable with a normal distribution as the observation model:
>>> dist = lsl.Dist(tfd.Normal, loc=0.0, scale=1.0) >>> y = lsl.obs(jnp.array([-0.5, 0.0, 0.5]), dist, name="y") >>> y Var(name="y")
Now we build the model graph:
>>> model = lsl.GraphBuilder().add(y).build_model()
The log-likelihood of the model is the sum of the log-probabilities of all observed variables. In this case this is only our
y
variable:>>> model.log_lik Array(-3.0068154, dtype=float32)
>>> jnp.sum(y.log_prob) Array(-3.0068154, dtype=float32)