Model Building (liesel.model)

Model Building (liesel.model)#

This is an overview of the most important building blocks for setting up your model graph with liesel.model.

We usually import liesel.model as follows:

import liesel.model as lsl

Additionnaly, it often makes sense to import jax.numpy and tensorflow probability:

import jax.numpy as jnp
import tensorflow_probability.substrates.jax.distributions as tfd

The model building workflow in Liesel consists of two steps:

  1. Set up the nodes and variables that make up your model.

  2. Initialize a Model with your root variable(s).

Note

This document provides an overview of the most important classes for model building. You find more guidance on how to use them in the respective API documentation and in the tutorials.

Variables: The model building blocks#

The fundamental building blocks of your model graph are given by just two classes. Both are documented with examples, so make sure to check them out.

Var

A variable in a statistical model.

Dist

A Node subclass that wraps a probability distribution.

To set up Var objects, Liesel provides four constructors:

new_param

Initializes a strong variable that acts as a model parameter.

new_obs

Initializes a strong variable that holds observed data.

new_calc

Initializes a weak variable that is a function of other variables.

new_value

Initializes a strong variable without a distribution.

We recommend to always use one of these constructors when initializing a variable. This makes sure that the respective Var.observed and Var.parameter flags are correctly set. This in turn ensures that the Var.log_prob of an observed variable will be included in the Model.log_lik and the Var.log_prob of a parameter variable will be included in the Model.log_prior.

Build and plot your model#

The most important class here is the Model.

Model

A model with a static graph.

plot_vars

Plots the variables of a Liesel model.

For advanced users, further interesting functionality can be found here:

GraphBuilder

A graph builder, used to set up a Model.

plot_nodes

Plots the nodes of a Liesel model.