Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Bayesian Regression With Latent Gaussian Sampler

In this example, we want to illustrate how to use the marginal sampler implementation mgrad_gaussian of the article Auxiliary gradient-based sampling algorithms Titsias & Papaspiliopoulos, 2018. We do so by using the simulated data from the example Gaussian Regression with the Elliptical Slice Sampler. Please also refer to the complementary example Bayesian Logistic Regression With Latent Gaussian Sampler.

Sampler Overview

In section we give a brief overview of the idea behind this particular sampler. For more details please refer to the original paper Auxiliary gradient-based sampling algorithms (here you can access the arXiv preprint).

Motivation: Auxiliary Metropolis-Hastings samplers

Let us recall how to sample from a target density π(x)\pi(\mathbf{x}) using a Metropolis-Hasting sampler trough a marginal scheme process. The main idea is to have a mechanism that generate proposals yy which we then accept or reject according to a specific criterion. Concretely, suppose that we have an auxiliary scheme given by

  1. Sample u∣x∼π(u∣x)=q(u∣x)\mathbf{u}|\mathbf{x} \sim \pi(\mathbf{u}|\mathbf{x}) = q(\mathbf{u}|\mathbf{x}).

  2. Generate proposal y∣u,x∼q(y∣x,u)\mathbf{y}|\mathbf{u}, \mathbf{x} \sim q(\mathbf{y}|\mathbf{x}, \mathbf{u})

  3. Compute the Metropolis-Hasting ratio

ϱ~=π(y∣u)q(x∣y,u)π(x∣u)q(y∣x,u)\tilde{\varrho} = \frac{\pi(\mathbf{y}|\mathbf{u})q(\mathbf{x}|\mathbf{y}, \mathbf{u})}{\pi(\mathbf{x}|\mathbf{u})q(\mathbf{y}|\mathbf{x}, \mathbf{u})}
  1. Accept proposal yy with probability min⁡(1,ϱ~)\min(1, \tilde{\varrho}) and reject it otherwise.

This scheme targets the auxiliary distribution π(x,u)=π(x)q(u∣x)\pi(\mathbf{x}, \mathbf{u}) = \pi(\mathbf{x}) q(\mathbf{u}|\mathbf{x}) in two steps.

Now, suppose we can instead compute the marginal proposal distribution q(y∣x)=∫q(y∣x,u)q(u∣x)duq(\mathbf{y}|\mathbf{x}) = \int q(\mathbf{y}|\mathbf{x}, \mathbf{u}) q(\mathbf{u}|\mathbf{x}) \mathrm{d}u in closed form, then an alternative scheme is given by:

  1. We draw a proposal y∼q(y∣x)y \sim q(\mathbf{y}\mid\mathbf{x}).

  2. Then we compute the Metropolis-Hasting ratio

ϱ=π(y)q(x∣y)π(x)q(y∣x)\varrho = \frac{\pi(\mathbf{y})q(\mathbf{x}|\mathbf{y})}{\pi(\mathbf{x})q(\mathbf{y}|\mathbf{x})}
  1. Accept proposal yy with probability min⁡(1,ϱ)\min(1, \varrho) and reject it otherwise.

Example: Auxiliary Metropolis-Adjusted Langevin Algorithm (MALA)

Let’s consider the case of an auxiliary random walk proposal q(u∣x)=N(u∣x,(δ/2)I)q(\mathbf{u}|\mathbf{x}) = N(\mathbf{u}|\mathbf{x}, (\delta /2) \mathbf{I}) for δ>0\delta > 0 as in [Section 2.2] Auxiliary gradient-based sampling algorithms, it is shown that one can use a first order approximation to sample from the (intractable) π(x∣u)\pi(\mathbf{x}|\mathbf{u}) density by choosing

q(y∣u,x)∝N(y∣u+(δ/2)∇log⁡π(x),(δ/2)I).q(\mathbf{y}|\mathbf{u}, \mathbf{x}) \propto N(\mathbf{y}|\mathbf{u} + (\delta/2)\nabla \log \pi(\mathbf{x}), (\delta/2) I).

The resulting marginal sampler can be shown to correspond to the Metropolis-adjusted Langevin algorithm (MALA) with

q(y∣x)=N(y∣x+(δ/2)∇log⁡π(x),δI).q(\mathbf{y}| \mathbf{x}) = N(\mathbf{y}|\mathbf{x} + (\delta/2)\nabla \log \pi(\mathbf{x}), \delta I).

Latent Gaussian Models

A particular case of interest is the latent Gaussian model where the target density has the form

π(x)∝exp⁡{f(x)}⏞likelihoodN(x∣0,C)⏟Gaussian Prior\pi(\mathbf{x}) \propto \overbrace{\exp\{f(\mathbf{x})\}}^{\text{likelihood}} \underbrace{N(\mathbf{x}|\mathbf{0}, \mathbf{C})}_{\text{Gaussian Prior}}

In this case, instead of linearising the full log density log⁡π(x)\log \pi(\mathbf{x}), we can linearise ff only, which, when combined with a random walk proposal N(u∣x,(δ/2)I)N(\mathbf{u}|\mathbf{x}, (\delta /2) \mathbf{I}), recovers to the following auxiliary proposal

q(y∣x,u)∝N(y∣2δA(u+δ2∇f(x)),A),q(\mathbf{y}|\mathbf{x}, \mathbf{u}) \propto N\left(\mathbf{y}|\frac{2}{\delta} \mathbf{A}\left(\mathbf{u} + \frac{\delta}{2}\nabla f(\mathbf{x})\right), \mathbf{A}\right),

where A=δ/2(C+(δ/2)I)−1C\mathbf{A} = \delta / 2(\mathbf{C} + (\delta / 2)\mathbf{I})^{-1}\mathbf{C}. The corresponding marginal density is

q(y∣x)∝N(y∣2δA(x+δ2∇f(x)),2δA2+A).q(\mathbf{y}|\mathbf{x}) \propto N\left(\mathbf{y}|\frac{2}{\delta} \mathbf{A}\left(\mathbf{x} + \frac{\delta}{2}\nabla f(\mathbf{x})\right), \frac{2}{\delta}\mathbf{A}^2 + \mathbf{A}\right).

Sampling from π(x,u)\pi(\mathbf{x}, \mathbf{u}), and therefore from π(x)\pi(\mathbf{x}), is done via Hastings-within-Gibbs as above.

A crucial point of this algorithm is the fact that A\mathbf{A} can be precomputed and afterward modified cheaply when δ\delta varies. This makes it easy to calibrate the step-size δ\delta at low cost.


Now that we have a high-level understanding of the algorithm, let’s see how to use it in blackjax.

Notebook Cell

We generate data through a squared exponential kernel as in the example Gaussian Regression with the Elliptical Slice Sampler.

Let’s visualize the distribution of the vector y.

Source
<Figure size 800x500 with 1 Axes>

Sampling

Now we proceed to run the sampler. First, we set the sampler parameters:

Next, we define the the log-probability function. For this we need to set the log-likelihood function.

Now we are ready to initialize the sampler. The output is type is a NamedTuple with the following fields:

init:
    A pure function which when called with the initial position and the
    target density probability function will return the kernel's initial
    state.

step:
    A pure function that takes a rng key, a state and possibly some
    parameters and returns a new state and some information about the
    transition.

We continue by setting the inference loop.

We are now ready to run the sampler! The only extra parameters in the step function is delta, which (as seen in the sampler description) corresponds (in a loose sense) to the step-size of MALA algorithm.

CPU times: user 28.2 s, sys: 81.4 ms, total: 28.3 s
Wall time: 12.4 s

Diagnostics

Finally we evaluate the results.

Mean squared error for the mean vector 0.004364923574030399 and covariance matrix 1.3226800774646108e-06
Source
<Figure size 800x500 with 1 Axes>
References
  1. Titsias, M. K., & Papaspiliopoulos, O. (2018). Auxiliary gradient-based sampling algorithms. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 80(4), 749–767. https://doi.org/10.1111/rssb.12269