liesel.model.goose.finite_discrete_gibbs_kernel(name, model, outcomes=None)[source]#

Creates a Gibbs kernel for a parameter with a finite discrete (categorical) prior.

The prior distribution of the variable to sample must be a categorical distribution, usually implemented via tfd.FiniteDiscrete.

This kernel evaluates the full conditional log probability of the model for each possible value of the variable to sample. It then draws a new value for the variable from the categorical distribution defined by the full conditional log probabilities.

  • name (str) – The name of the variable to sample.

  • model (Model) – The model to sample from.

  • outcomes (Optional[Sequence]) – The possible outcomes of the variable to sample. If outcomes=None, the possible outcomes are extracted from the prior distribution of the variable to sample. Note however, that this only works for some prior distributions. If the possible outcomes cannot be extracted from the prior distribution, you must specify them manually via this argument. (default: None)

Return type:



In the following example, we create a categorical Gibbs kernel for a variable with three possible values. The prior distribution of the variable is a finite discrete (categorical) distribution with the probabilities [0.1, 0.2, 0.7].

You can then use the kernel to sample from the model:

>>> import tensorflow_probability.substrates.jax.distributions as tfd
>>> values = [0.0, 1.0, 2.0]
>>> prior_probs = [0.1, 0.2, 0.7]
>>> value_grid = lsl.Var(values, name="value_grid")
>>> prior = lsl.Dist(tfd.FiniteDiscrete, outcomes=value_grid, probs=prior_probs)
>>> categorical_var = lsl.Var(
...     value=values[0],
...     distribution=prior,
...     name="categorical_var",
... )
>>> model = lsl.GraphBuilder().add(categorical_var).build_model()
>>> kernel = finite_discrete_gibbs_kernel("categorical_var", model)
>>> type(kernel)
<class 'liesel.goose.gibbs.GibbsKernel'>

Example for a variable with a Bernoulli prior distribution:

>>> prior = lsl.Dist(tfd.Bernoulli, probs=lsl.Data(0.7))
>>> dummy_var = lsl.Var(
...     value=1,
...     distribution=prior,
...     name="dummy_var",
... )
>>> model = lsl.GraphBuilder().add(dummy_var).build_model()
>>> kernel = finite_discrete_gibbs_kernel("dummy_var", model, outcomes=[0, 1])
>>> type(kernel)
<class 'liesel.goose.gibbs.GibbsKernel'>