# Metropolis-Hastings algorithm

File:Metropolis hastings algorithm.png
The Proposal distribution Q proposes the next point that the random walk might move to.

In mathematics and physics, the Metropolis-Hastings algorithm is a rejection sampling algorithm used to generate a sequence of samples from a probability distribution that is difficult to sample from directly. This sequence can be used in Markov chain Monte Carlo simulation to approximate the distribution (i.e., to generate a histogram), or to compute an integral (such as an expected value). The algorithm was named in reference to Nicholas Metropolis, who published it in 1953 for the specific case of the Boltzmann distribution, and W.K. Hastings,[1] who generalized it in 1970. The Gibbs sampling algorithm is a special case of the Metropolis-Hastings algorithm which is usually faster and easier to use but is less generally applicable.

The Metropolis-Hastings algorithm can draw samples from any probability distribution ${\displaystyle P(x)}$, requiring only that a function proportional to the density can be calculated at ${\displaystyle x}$. In Bayesian applications, the normalization factor is often extremely difficult to compute, so the ability to generate a sample without knowing this constant of proportionality is a major virtue of the algorithm. The algorithm generates a Markov chain in which each state ${\displaystyle x^{t+1}}$ depends only on the previous state ${\displaystyle x^{t}}$. The algorithm uses a proposal density ${\displaystyle Q(x';x^{t})}$, which depends on the current state ${\displaystyle x^{t}}$, to generate a new proposed sample ${\displaystyle x'}$. This proposal is 'accepted' as the next value (${\displaystyle x^{t+1}}$=${\displaystyle x'}$) if ${\displaystyle \alpha }$ drawn from ${\displaystyle U(0,1)}$ is

${\displaystyle \alpha <{\frac {P(x')Q(x^{t}|x')}{P(x^{t})Q(x'|x^{t})}}\,\!.}$

If the proposal is not accepted, then the current value of ${\displaystyle x}$ is retained: ${\displaystyle x^{t+1}=x^{t}}$.

For example, the proposal density could be a Gaussian function centred on the current state ${\displaystyle x^{t}}$:

${\displaystyle Q(x';x^{t})\sim N(x^{t},\sigma ^{2}I)\,\!}$

reading ${\displaystyle Q(x';x^{t})}$ as the probability density function for ${\displaystyle x'}$ given the previous value ${\displaystyle x^{t}}$. This proposal density would generate samples centred around the current state with variance ${\displaystyle \sigma ^{2}I}$. The original Metropolis algorithm calls for the proposal density to be symmetric ( ${\displaystyle Q(x;y)=Q(y;x)}$ ); the generalization by Hastings lifts this restriction. It is also permissible for ${\displaystyle Q(x',x^{t})}$ not to depend on ${\displaystyle x'}$ at all, in which case the algorithm is called "Independence Chain Metropolis-Hastings" ( as opposed to "Random Walk Metropolis-Hastings" ). The Independence Chain M-H algorithm with a suitable proposal density function can offer higher accuracy than the random walk version, but it requires some a priori knowledge of the distribution.

## Step-by-step instructions

Suppose the most recent value sampled is ${\displaystyle x^{t}}$. To follow the Metropolis-Hastings algorithm, we next draw a new proposal state ${\displaystyle x'}$ with probability ${\displaystyle Q(x';x^{t})}$, and calculate a value

${\displaystyle a=a_{1}a_{2}\,}$

where

${\displaystyle a_{1}={\frac {P(x')}{P(x^{t})}}\,\!}$

is the likelihood ratio between the proposed sample ${\displaystyle x'}$ and the previous sample ${\displaystyle x^{t}}$, and

${\displaystyle a_{2}={\frac {Q(x^{t};x')}{Q(x';x^{t})}}}$

is the ratio of the proposal density in two directions (from ${\displaystyle x^{t}}$ to ${\displaystyle x'}$ and vice versa). This is equal to 1 if the proposal density is symmetric. Then the new state ${\displaystyle x^{t+1}}$ is chosen according to the following rules.

${\displaystyle {\begin{matrix}{\mbox{If }}a\geq 1:&\\&x^{t+1}=x',\end{matrix}}}$
${\displaystyle {\begin{matrix}{\mbox{and if }}a<1:&\\&x^{t+1}=\left\{{\begin{matrix}x'{\mbox{ with probability }}a\\x^{t}{\mbox{ with probability }}1-a.\end{matrix}}\right.\end{matrix}}}$

The Markov chain is started from a random initial value ${\displaystyle x^{0}}$ and the algorithm is run for many iterations until this initial state is "forgotten". These samples, which are discarded, are known as burn-in. The remaining set of accepted values of ${\displaystyle x}$ represent a sample from the distribution ${\displaystyle P(x)}$.

File:3dRosenbrock.png
The result of three Markov chains running on the 3d Rosenbrock function using the Metropolis-Hastings algorithm. It is obvious how the algorithm samples from regions where the posterior probability is high and how the chains begin to mix in these regions. The approximate position of the maximum has been illuminated.

The algorithm works best if the proposal density matches the shape of the target distribution ${\displaystyle P(x)}$, that is ${\displaystyle Q(x';x^{t})\approx P(x')\,\!}$, but in most cases this is unknown. If a Gaussian proposal density ${\displaystyle Q}$ is used the variance parameter ${\displaystyle \sigma ^{2}}$ has to be tuned during the burn-in period. This is usually done by calculating the acceptance rate, which is the fraction of proposed samples that is accepted in a window of the last ${\displaystyle N}$ samples. It is usually desirable to obtain an acceptance rate around 60%. If ${\displaystyle \sigma ^{2}}$ is too small the chain will mix slowly (i.e., the acceptance rate will be too high, so the sampling will move around the space slowly and converge slowly to ${\displaystyle P(x)}$). If ${\displaystyle \sigma ^{2}}$ is too large the acceptance rate will be very low because the proposals are likely to land in regions of much lower probability density so ${\displaystyle a_{1}}$ will be very small.