Gibbs#

class cuqi.sampler.Gibbs(target, sampling_strategy)#

Gibbs sampler for sampling a joint distribution.

Gibbs sampling samples the variables of the distribution sequentially, one variable at a time. When a variable represents a random vector, the whole vector is sampled simultaneously.

The sampling of each variable is done by sampling from the conditional distribution of that variable given the values of the other variables. This is often a very efficient way of sampling from a joint distribution if the conditional distributions are easy to sample from.

Parameters:
  • target (cuqi.distribution.JointDistribution) – Target distribution to sample from.

  • sampling_strategy (dict) – Dictionary of sampling strategies for each parameter. Keys are parameter names. Values are sampler objects.

Example

import cuqi
import numpy as np

# Model and data
A, y_obs, probinfo = cuqi.testproblem.Deconvolution1D(phantom='square').get_components()
n = A.domain_dim

# Define distributions
d = cuqi.distribution.Gamma(1, 1e-4)
l = cuqi.distribution.Gamma(1, 1e-4)
x = cuqi.distribution.GMRF(np.zeros(n), lambda d: d)
y = cuqi.distribution.Gaussian(A, lambda l: 1/l)

# Combine into a joint distribution and create posterior
joint = cuqi.distribution.JointDistribution(d, l, x, y)
posterior = joint(y=y_obs)

# Define sampling strategy
sampling_strategy = {
    'x': cuqi.sampler.LinearRTO,
    ('d', 'l'): cuqi.sampler.Conjugate,
}

# Define Gibbs sampler
sampler = cuqi.sampler.Gibbs(posterior, sampling_strategy)

# Run sampler
samples = sampler.sample(Ns=1000, Nb=200)

# Plot results
samples['x'].plot_ci(exact=probinfo.exactSolution)
samples['d'].plot_trace(figsize=(8,2))
samples['l'].plot_trace(figsize=(8,2))
__init__(target, sampling_strategy)#

Methods

__init__(target, sampling_strategy)

sample(Ns[, Nb])

Sample from target distribution

step(current_samples)

Sequentially go through all parameters and sample them conditionally on each other

step_tune(current_samples)

Perform a single MCMC step for each parameter and tune the sampler