MCMC Sampling (liesel.goose)#

This is an overview of the most important building blocks for sampling from the posterior of your model liesel.goose.

We usually import liesel.goose as follows:

import liesel.goose as gs

The workflow for MCMC sampling goose consists of the following steps:

  1. Set up your model

  2. Set up an Engine with MCMC kernels for your parameters and draw posterior samples

  3. Inspect your results

Note

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

Set up your model#

A natural option for setting up your model is the use of liesel.model. See Model Building with liesel.model for more. However, you are not locked in to using Model. Goose currently includes the following interfaces:

LieselInterface

A ModelInterface for a Liesel Model.

DataclassInterface

A model interface for a model state represented by a dataclass and a corresponding log-probability function.

DictInterface

A model interface for a model state represented by a dict[str, Array] and a corresponding log-probability function.

NamedTupleInterface

A model interface for a model state represented by a NamedTuple and a corresponding log-probability function.

PyMCInterface

An implementation of the Goose ModelInterface to be used with a PyMC model.

Find good starting values#

It can often be beneficial to find good starting values to get your MCMC sampling scheme going. Goose provides the function optim() for this purpose, which allows you to run stochastic gradient descent on a liesel model.

OptimResult

Holds the results of model optimization with optim_flat().

Set up an MCMC Engine and draw posterior samples#

To set up an MCMC engine, goose provides the EngineBuilder. Please refer to the linked EngineBuilder documentation to learn how to use it.

EngineBuilder

The EngineBuilder is used to construct an MCMC Engine.

Engine

MCMC engine capable of combining multiple transition kernels.

Available MCMC kernels

Goose makes it easy for you to combine different MCMC kernels for different blocks of model parameters. Currently, the available MCMC kernels are:

RWKernel

A random walk kernel.

IWLSKernel

An IWLS kernel with dual averaging and an (optional) user-defined function for computing the Cholesky decomposition of the Fisher information matrix, implementing the liesel.goose.types.Kernel protocol.

HMCKernel

A HMC kernel with dual averaging and an inverse mass matrix tuner, implementing the Kernel protocol.

NUTSKernel

A NUTS kernel with dual averaging and an inverse mass matrix tuner, implementing the Kernel protocol.

GibbsKernel

A Gibbs kernel implementing the Kernel protocol.

You can also define your own kernel by implementing the Kernel protocol.

To draw samples from your posterior, you will want to call sample_all_epochs(). Once sampling is done, you can obtain the results with get_results(), which will return a SamplingResults instance.

Inspect your results#

The two central classes for handling your sampling results are:

SamplingResults

Contains the results of the MCMC engine.

Summary

A summary object.

You can obtain your posterior samples as a dictionary via get_posterior_samples(). There is also experimental support for turning your samples into an arviz.InferenceData object via to_arviz_inference_data().

Plot posterior samples

Goose comes with a number of plotting functions that give you quick acccess to important diagnostics.

plot_param

Visualizes trace plot, density plot and autocorrelation plot of a single subparameter.

plot_trace

Visualizes posterior samples over time with a trace plot.

plot_density

Visualizes posterior distributions with a density plot.

plot_pairs

Produces a pairplot panel.

plot_cor

Visualizes autocorrelations of posterior samples.