LogProb

Contents

LogProb#

class liesel.model.LogProb(model, component='log_prob', diff_mode='forward')[source]#

Bases: object

Interface for evaluating the unnormalized log probability of a Liesel model.

Also provides access to the first and second derivatives.

Parameters:
  • model (Model) – A Liesel model instance.

  • component (Literal['log_prob', 'log_lik', 'log_prior'], default: 'log_prob') – Which component of the model’s log probability to evaluate.

  • diff_mode (Literal['forward', 'reverse'], default: 'forward') – Which auto-diff mode to use for the Hessian.

See also

FlatLogProb

A similar class that returns gradients and hessians as arrays.

Examples

We initialize a very basic Liesel model:

>>> x = lsl.Var.new_param(
...     jnp.zeros(2),
...     dist=lsl.Dist(tfd.Normal, loc=0.0, scale=1.0),
...     name="x",
... )
>>> model = lsl.Model([x])

Now we initialize the log prob object:

>>> lp = lsl.LogProb(model)

And evaluate the log prob (the unnormalized log posterior) at a new position:

>>> lp({"x": jnp.array([1.0, 2.0])})
Array(-4.3378773, dtype=float32)

Now we evaluate the gradient of the unnormalized log posterior at the new position:

>>> lp.grad({"x": jnp.array([1.0, 2.0])})
{'x': Array([-1., -2.], dtype=float32)}

And, finally, the hessian:

>>> lp.hessian({"x": jnp.array([1.0, 2.0])})
{'x': {'x': Array([[-1., -0.],
       [-0., -1.]], dtype=float32)}}

Methods

grad(position)

Gradient of the log probability function with respect to the position.

hessian(position)

Hessian of the log probability function with respect to the position.

log_prob(position)

Log probability function evaluated at provided position.