# The Metropolis-Hastings Algorithm

The Metropolis-Hastings algorithm, developed by Metropolis, Rosenbluth, Rosenbluth, Teller, and Teller (1953) and generalized by Hastings (1970), is a Markov chain Monte Carlo method which allows for sampling from a distribution when traditional sampling methods such as transformation or inversion fail. It requires only being able to evaluate the density function. The normalizing constant need not be known. This algorithm is very general and gives rise to the Gibbs sampler as a special case.

## Markov Chain Monte Carlo Simulation

Consider a Markov transition kernel $P\left(x,A\right)$ for $x\in {ℝ}^{d}$ and $A\in ℬ$, where $ℬ$ is the Borel $\sigma$-algebra on ${ℝ}^{d}$. Two major concerns in Markov chain theory are whether there exists an invariant distribution andf whether iterating the transition kernel converges to the invariant distribution. The invariant distribution ${\pi }_{0}$ satisfies ${\pi }_{0}\left(\mathrm{dy}\right)={\int }_{{ℝ}^{d}}P\left(x,\mathrm{dy}\right)\pi \left(x\right)\phantom{\rule{thinmathspace}{0ex}}\mathrm{dx},$ where $\pi$ is the density of ${\pi }_{0}$ with respect to Lebesgue measure. Let ${P}^{\left(n\right)}\left(x,A\right)={\int }_{{ℝ}^{d}}{P}^{\left(n-1\right)}\left(x,\mathrm{dy}\right)P\left(y,A\right)$ denote the $n$-th iteration of the transition kernel. We also want ${P}^{\left(n\right)}\left(x,A\right)$ to converge to ${\pi }_{0}$ as $n\to \infty$. That is, the distribution of the draws generated by the iterations is approximately ${\pi }_{0}$.

Markov chain Monte Carlo methods look at the problem from the opposite perspective. The invariant distribution is known: it is the target distribution. The problem is how to generate an appropriate transition kernel with the aforementioned convergence property. Suppose that the transition kernel can be expressed as $P\left(x,\mathrm{dy}\right)=p\left(x,y\right)\phantom{\rule{thinmathspace}{0ex}}\mathrm{dy}+r\left(x\right){\delta }_{x}\left(\mathrm{dy}\right)$ where $p\left(x,x\right)=0$, ${\delta }_{x}\left(\mathrm{dy}\right)=1$ if $x\in \mathrm{dy}$ and $0$ otherwise, and define $r\left(x\right)=1-{\int }_{{ℝ}^{d}}p\left(x,y\right)\phantom{\rule{thinmathspace}{0ex}}\mathrm{dy}$. The $r\left(x\right)$ term represents the probability of staying at $x$ which may be nonzero. If this kernel satisfies the reversibility condition

(1)$\pi \left(x\right)p\left(x,y\right)=\pi \left(y\right)p\left(y,x\right),$

then $\pi$ is the invariant distribution of $P\left(x,\cdot \right)$. The Metropolis-Hastings algorithm generates a $p\left(x,y\right)$ with this property.

## The Metropolis-Hastings Algorithm

Let $q\left(x,y\right)$ denote the candidate-generating density, where $\int q\left(x,y\right)\phantom{\rule{thinmathspace}{0ex}}\mathrm{dy}=1$. This is essentially the conditional density of $y$ given $x$. This density could potentially be used as the $p\left(x,y\right)$ term in the transition kernel, but it may not satisfy (1). If, for example, we have

(2)$\pi \left(x\right)q\left(x,y\right)>\pi \left(y\right)q\left(y,x\right),$

then we can adjust $q$ by using a probability of move $\alpha \left(x,y\right)$. Transitions will be made using ${p}_{\mathrm{MH}}\left(x,y\right)\equiv q\left(x,y\right)\alpha \left(x,y\right)$ for $x\ne y$.

The choice of $\alpha$ follows the following logic. If (2) holds, then moves from $x$ to $y$ are happening too often under $q$. We should thus choose $\alpha \left(y,x\right)=1$. But then, in order to satisfy (1), we must have $\begin{array}{rl}\pi \left(x\right)q\left(x,y\right)\alpha \left(x,y\right)& =\pi \left(y\right)q\left(y,x\right)\alpha \left(y,x\right)\\ & =\pi \left(y\right)q\left(y,x\right).\end{array}$ This implies that $\alpha \left(x,y\right)=\frac{\pi \left(y\right)q\left(y,x\right)}{\pi \left(x\right)q\left(x,y\right)}.$ Conversely, we can consider the case when the inequality in (1) were reversed to derive $\alpha \left(y,x\right)$.
Thus, to summarize, in order to satisfy reversibility, we set

(3)$\alpha \left(x,y\right)=\left\{\begin{array}{ll}\mathrm{min}\left[\frac{\pi \left(y\right)q\left(y,x\right)}{\pi \left(x\right)q\left(x,y\right)},1\right],& \text{if}\phantom{\rule{thickmathspace}{0ex}}\pi \left(x\right)q\left(x,y\right)>0\\ 1,& \text{otherwise.}\end{array}$

Hence, the desired transition kernel is

(4)${P}_{\mathrm{MH}}\left(x,\mathrm{dy}\right)=q\left(x,y\right)\alpha \left(x,y\right)\phantom{\rule{thinmathspace}{0ex}}\mathrm{dy}+\left[1-{\int }_{{ℝ}^{d}}q\left(x,y\right)\alpha \left(x,y\right)\phantom{\rule{thinmathspace}{0ex}}\mathrm{dy}\right]{\delta }_{x}\left(\mathrm{dy}\right).$

Thus, the Metropolis-Hastings algorithm is defined by the candidate-generating density $q\left(x,y\right)$. Note that $\alpha \left(x,y\right)$ does not require knowledge of the normalizing constant because it drops out of the ratio $\pi \left(y\right)/\pi \left(x\right)$. A special case arises when the candidate-generating density is symmetric: $q\left(x,y\right)=q\left(y,x\right)$ since the probability of move reduces to $\pi \left(y\right)/\pi \left(x\right)$. This special case forms the basis for optimization algorithms such as Simulated annealing.

The algorithm proceeds as follows: Given an initial value ${x}^{\left(0\right)}$, for each $j=1,2,\dots ,N$

1. Draw $y$ from $q\left({x}^{\left(j\right)},\cdot \right)$ and $u$ from $U\left(0,1\right)$
2. If $u\le \alpha \left({x}^{\left(j\right)},y\right)$, set ${x}^{\left(j+1\right)}=y$.
3. Otherwise, set ${x}^{\left(j+1\right)}={x}^{\left(j\right)}$.

## References

• Chib, S. and E. Greenberg (1995): “Understanding the Metropolis Hastings Algorithm,” American Statistical Journal, 49, 327–335.

• Hastings, W.K. (1970): “Monte Carlo Sampling Methods Using Markov Chains and Their Applications,” Biometrika, 57, 97–109.

• Metropolis, N., A.W. Rosenbluth, M.N. Rosenbluth, A.H. Teller, and E. Teller (1953): “Equations of State Calculations by Fast Computing Machines,” Journal of Chemical Physics, 21, 1087–1092.

• Robert, C.P., and G. Casella (2004): Monte Carlo Statistical Methods, Second Edition. New York: Springer.