This is an old revision of the document!
To understand MCMC and Hamiltonian MCMC.
You need to install the autograd
package. This can be installed with pip install autograd
.
For this lab, you will implement two variants of MCMC inference: basic Metropolis Hastings and Hamiltonian MCMC. Your notebook should present visualizations of both the resulting samples, as well as plots of the state over time.
For example, here are my visualization of basic Metropolis-Hastings, with different proposal parameters:
And here are plots of the resulting state evolution over time:
Your notebook will be graded on the following elements:
For this lab, you will code two different MCMC algorithms. Each will attempt to draw samples from the same distribution, given by the following density function:
import numpy as np def p( x, t=1.0 ): return np.exp( -10*t*((x-2)**2) ) + 0.3*np.exp( -0.5*10*t*((x+1)**2) )
This distribution has two modes that are separated by a region of low probability.
Part 1: Metropolis Hastings
For this part, you should implement the basic MH MCMC algorithm. You should use a Gaussian proposal distribution with three different variances (0.1, 1.0 and 10.0)
. Your sampler should start with an initial state of 0
.
For each different proposal distribution, you should run your MCMC chain for 10,000 steps, and record the sequence of states. Then, you should produce a visualization of the distribution of states, and overlay a plot of the actual target distribution. They may or may not match (see, for example, the first example plot in the Description section).
Furthermore, for each proposal distribution, you should run three independent chains (you can do these sequentially or in parallel, as you like). You should display each of these three chains on a single plot with time on the x-axis and the state on the y-axis. Ideally, you will see each of the three chains mixing between two modes.
Part 2: Hamiltonian MCMC
For this part, you will code the Hamiltonian MCMC algorithm, as discussed in class. To do this, you will need to compute the gradient of the density function with respect to the state. An easy easy way to do this is to use the autograd package:
from autograd import grad import autograd.numpy as np grad_p = grad( p )
The function grad_p
accepts the same parameters as p
, but it returns the gradient, instead of the density.
Remember that you will need to introduce as many momentum variables as there
Part 3: Observations
You have now coded two different inference algorithms, and a few variants of each. For this section, you must provide a small write-up that compares and contrasts each. For example, why don't some inference algorithms explore both modes of the density? Why
You may find plt.hist
with the normed=True
option helpful.