Lab 4: Gibbs and Metropolis-Hastings Sampling#

This lab is adapted from AM207 (Harvard). Data files are in the book’s data/ folder. Worked solutions are collapsed: try each exercise before expanding them.

In this lab

  1. Gibbs on a Bivariate Gaussian

  2. Gibbs versus Metropolis Hastings on a Bivariate Gaussian

  3. Metropolis Hastings on a Mixture of Gaussians

Hide code cell content

### Import basic libraries
from functools import partial
import numpy as np
import scipy as sp
import pandas as pd
import sklearn as sk
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
%matplotlib inline

Gibbs on a Bivariate Gaussian#

Implement a Gibbs sampler to sample from the following bivariate Gaussian (you can look up the formulae for the conditionals of a bivariate Gaussian):

\[\begin{split} \mathcal{N}\left([120, 4], \left[\begin{array}{cc} 3& 1\\ 1 & 3 \end{array} \right] \right) \end{split}\]

Scatter plot the samples with the Gaussian pdf, connect consecutive samples with a line so that you can see how the sampler moves in the sample space.

Visually check for convergence of your chain. Apply the appropriate amount of burn-in and thinning to an appropriate choice of number of total samples.

Explore the effect of your choice of initialization on the performance of your sampler.

Implement a Gibbs sampler to sample from a different bivariate Gaussian:

\[\begin{split} \mathcal{N}\left([120, 4], \left[\begin{array}{cc} 10.2& 10\\ 10 & 10.2 \end{array} \right] \right) \end{split}\]

Again, scatter plot the samples with the Gaussian pdf, connect consecutive samples with a line so that you can see how the sampler moves in the sample space.

Discuss how the shape of the Gaussian affects the performance of the sampler. How do you explain what you observe?

Gibbs versus Metropolis Hastings on a Bivariate Gaussian#

Implement a Metropolis-Hastings sampler for the two Gaussians in the previous problem. Keep track of the acceptance rate.

Try a bivariate Gaussian proposal distribution \(\mathcal{N}(X_n, \sigma^2I_{2\times 2})\), where \(I_{2\times 2}\) is the identity matrix, \(X_n\) is the current sample and \(\sigma^2\) is your choice of variance. Try different values of \(\sigma\).

Discuss how does the shape of the Gaussian effect the performance of your sampler.

How does MH perform in comparison to Gibbs?

Try a bivariate Gaussian proposal distribution

\[\begin{split} \mathcal{N}\left(X_n, \left[\begin{array}{cc} 0.4& 0.2\\ 0.2 & 0.4 \end{array} \right] \right) \end{split}\]

where \(X_n\) is the current sample.

Does this proposal distribution perform better or worse than \(\mathcal{N}(X_n, \sigma^2I_{2\times 2})\)? Why do you think it does (or does not)?

Metropolis Hastings on a Mixture of Gaussians#

Recall the mixture of two univariate Gaussians example in Chapter 9, where our Metropolis-Hastings sampler was unable to capture both modes in the target distribution.

Is this because this algorithm is incapable of sampling from this specific distribution of this class of multimodal distributions? Or did we make inappropriate design choices when we specified our sampler?

Exercise: play with the variance of the normal proposal distribution (try some very small values and some very large values), what is the effect of the variance have on the performance of your sampler?

Play with the choice of initialization of the Metropolis-Hastings algorithm, what is the effect of the initialization on the performance of your sampler?