2.4 Markov chain Monte Carlo
So far we have been using that the posterior distribution is proportional to the likelihood times the prior distribution. We were also careful to choose the prior as the conjugate distribution so that we could recognize the posterior distribution. In general we have to calculate the normalizing constant: \[ \int p(x\mid \theta)p(\theta)d\theta. \] Many times, and in particular if \(\theta\) is high dimensional, it is not possible to calculate this integral.
In this section we will see how we can, instead of calculating it, generate samples from the posterior distribution. This is the method that is used in modern Bayesian statistics. Here we are only able to scratch the surface. Both in terms of theory, since explaining why the method works would require first learning about Markov chains, and in terms of complexity of the methods, since the methods that are used in practice are usually more advanced version of what we present here.
We assume that we are able to write down the likelihood of our data generating model and the prior on our parameters. Therefore we are also able to write down the posterior distribution up to a multiplying constant.
The method we will discuss is called the Metropolis-Hastings algorithm and it is an example of a Markov chain Monte Carlo (MCMC) method. Markov chain here means that we will obtain a sequence of samples from the posterior distributions and that the distribution of each sample only depends on the previous sample, and not on samples before that. Monte Carlo means that is is an algorithm that uses on random sampling.
The Metropolis-Hastings algorithm is as follows: Suppose we want to sample from the posterior \(p(\theta\mid x).\) Suppose also that this is not possible directly, but that we are able to generate samples from some other distribution \(q(\theta^\star \mid \theta).\) Then do the following:
Choose \(\theta_0\) arbitrarily. For \(i\geq 1\) do:
- Generate a proposal \(\theta^\star\sim q(\theta^\star\mid \theta_{i-1})\)
- Calculate \[ r = \min\left\{ \frac{p(\theta^\star\mid x)}{p(\theta_{i-1}\mid x)}\frac{q(\theta_{i-1}\mid \theta^\star)}{q(\theta^\star\mid\theta_{i-1})}, 1 \right\}. \]
- Set \[ \theta_i = \begin{cases} \theta^\star\quad \text{with probability } r\\ \theta_{i-1}\quad \text{with probability } 1-r. \end{cases} \]
Theory then tells us that as \(i\) becomes large, the distribution of \(\theta_i\) will be approximately that of \(p(\theta\mid x).\)
Note that here the normalizing constant in \(p(\theta\mid x)\) is cancelled since it appears both in the numerator and denominator.
It remains to choose \(q(\theta^\star\mid x)\). One common choice is called random walk Metropolis Hastings and consists of letting \[ \theta^\star = \theta_{i-1} + \varepsilon_{i-1}, \] where \(\varepsilon_{i-1}\) is and independent random variable. For example \(\varepsilon_{i-1}\overset{iid}{\sim} \mathsf N(0,b^2)\) for some variance \(b^2\). In this case \(q(\theta_{i-1}\mid \theta^\star)=q(\theta^\star\mid \theta_{i-1})\) and so the acceptance probability becomes \[ r=\min\left\{ \frac{p(\theta^\star\mid x)}{p(\theta_{i-1}\mid x)} ,1\right\} \] Here it is important to choose \(b\) such that the correlation between \(\theta_i\) and \(\theta_{i-1}\) is small. If \(b\) is small, almost all proposals will be accepted, but since \(\varepsilon_i\) also tends to be small, \(\theta_{i+1}\) will be highly correlated with \(\theta_{i}\). On the other hand, if \(b\) is large, the proposal will rarely be accepted, and \(\theta_i=\theta_{i-1}\). As a rule of thumb, \(b\) should be set such that about \(50\%\) of the proposals are accepted.
Another class of proposal distributions is \(q(\theta^\star\mid \theta_{i-1})\equiv q(\theta^\star)\), that is \(\theta^\star\) is independent of \(\theta_{i-1}\). The acceptance probability is then \[ r = \min\left\{ \frac{p(\theta^\star\mid x)}{p(\theta_{i-1}\mid x)}\frac{q(\theta_{i-1})}{q(\theta^\star)}, 1 \right\}. \] This is known as independence Metropolis Hastings. Here, since each time the proposal is accepted \(\theta_i\) will change, we aim for as high an acceptance probability as possible.
As an example let us see how one can generate samples from the exponential distribution using Metropolis Hastings. To be clear, this is not the best way to generate exponentially distributed random numbers, it is just an example.
The density is \[ p(x) = \lambda e^{-\lambda x}\propto e^{-\lambda x}. \] Here we know that the normalizing constant is \(\lambda\), but the idea of MCMC is that we do not need to know it, so let us proceed as if we did not know it. Also note that here we see \(x\) as random, in the typical Bayesian setting we would think of \(\lambda\) as random, but is is just a matter of notation.
First we implement the density. We do it on a log-scale. This is because we need to divide two densities in the algorithm. On a log-scale this becomes subtraction, which is numerically more stable.
Next implement the random-walk Metropolis-Hastings algorithm with normally distributed steps.
mcmc.iter <- function(x, logDensity, sigma, n.iter){
#Random walk Metropolis Hastings MCMC
res <- matrix(NA, n.iter+1, length(x)) #Create empty matrix
res[1,] <- x
logD <- logDensity(x)
accProb <- 0 #keep track of the proportion of proposals that are accepted
for (i in seq_len(n.iter)){
#New proposal
xProp <- x + rnorm(length(x), 0, sigma)
#Log density of proposal
logDProp <- logDensity(xProp)
#Acceptance probability
r <- min( c(1, exp(logDProp - logD) ) )
if(r>runif(1)){ #Accept with probability r, else keep old x
x <- xProp
logD <- logDProp
accProb <- accProb + 1
}
res[i+1,] <- x
}
list(sample = res, accProb = accProb/n.iter)
}Now we can run the algorithm. First for 1000 step that we throw away. This is since the distribution of the samples are correct only when the number of steps are large. Then we run it for as many steps as we need samples.
x.init <- 1 #Initial value
nIter <- 100000 #Number of MC steps
set.seed(42)
exp.mcmc <- mcmc.iter(x.init,
logDensity,
0.2,
1000)
exp.mcmc <- mcmc.iter(exp.mcmc$sample[nrow(exp.mcmc$sample),],
logDensity,
0.2,
nIter)We can check for example that the mean and standard deviation of the samples are as expected, i.e. in our case 1.
## [1] 1.072045
## [1] 1.114397
We can plot the histogram of the samples against the density.
