Metropolis–Hastings algorithm


In statistics and statistical physics, the Metropolis–Hastings algorithm is a Markov chain Monte Carlo method for obtaining a sequence of random samples from a probability distribution from which direct sampling is difficult. This sequence can be used to approximate the distribution or to compute an integral. Metropolis–Hastings and other MCMC algorithms are generally used for sampling from multi-dimensional distributions, especially when the number of dimensions is high. For single-dimensional distributions, there are usually other methods that can directly return independent samples from the distribution, and these are free from the problem of autocorrelated samples that is inherent in MCMC methods.

History

The algorithm was named after Nicholas Metropolis, who authored the 1953 article Equation of State Calculations by Fast Computing Machines together with Arianna W. Rosenbluth, Marshall Rosenbluth, Augusta H. Teller and Edward Teller. This article proposed the algorithm for the case of symmetrical proposal distributions, and W. K. Hastings extended it to the more general case in 1970.
Some controversy exists with regard to credit for development of the algorithm. Metropolis had coined the term "Monte Carlo" in an earlier article with Stanislav Ulam, was familiar with the computational aspects of the method, and led the group in the Theoretical Division that designed and built the MANIAC I computer used in the experiments in 1952. However, prior to 2003, there was no detailed account of the algorithm's development. Then, shortly before his death, Marshall Rosenbluth attended a 2003 conference at LANL marking the 50th anniversary of the 1953 publication. At this conference, Rosenbluth described the algorithm and its development in a presentation titled "Genesis of the Monte Carlo Algorithm for Statistical Mechanics". Further historical clarification is made by Gubernatis in a 2005 journal article recounting the 50th anniversary conference. Rosenbluth makes it clear that he and his wife Arianna did the work, and that Metropolis played no role in the development other than providing computer time.
This contradicts an account by Edward Teller, who states in his memoirs that the five authors of the 1953 article worked together for "days ". In contrast, the detailed account by Rosenbluth credits Teller with a crucial but early suggestion to "take advantage of statistical mechanics and take ensemble averages instead of following detailed kinematics". This, says Rosenbluth, started him thinking about the generalized Monte Carlo approach – a topic which he says he had discussed often with Von Neumann. Arianna recounted that Augusta Teller started the computer work, but that Arianna herself took it over and wrote the code from scratch. In an oral history recorded shortly before his death, Rosenbluth again credits Teller with posing the original problem, himself with solving it, and Arianna with programming the computer. In terms of reputation, there is little reason to question Rosenbluth's account. In a biographical memoir of Rosenbluth, Freeman Dyson writes:

Intuition

The Metropolis–Hastings algorithm can draw samples from any probability distribution, provided that we know a function proportional to the density of and the values of can be calculated. The requirement that must only be proportional to the density, rather than exactly equal to it, makes the Metropolis–Hastings algorithm particularly useful, because calculating the necessary normalization factor is often extremely difficult in practice.
The Metropolis–Hastings algorithm works by generating a sequence of sample values in such a way that, as more and more sample values are produced, the distribution of values more closely approximates the desired distribution. These sample values are produced iteratively, with the distribution of the next sample being dependent only on the current sample value. Specifically, at each iteration, the algorithm picks a candidate for the next sample value based on the current sample value. Then, with some probability, the candidate is either accepted or rejected —the probability of acceptance is determined by comparing the values of the function of the current and candidate sample values with respect to the desired distribution.
For the purpose of illustration, the Metropolis algorithm, a special case of the Metropolis–Hastings algorithm where the proposal function is symmetric, is described below.
Metropolis algorithm
Let be a function that is proportional to the desired probability distribution .
  1. Initialization: Choose an arbitrary point to be the first sample and choose an arbitrary probability density that suggests a candidate for the next sample value, given the previous sample value. In this section, is assumed to be symmetric; in other words, it must satisfy. A usual choice is to let be a Gaussian distribution centered at, so that points closer to are more likely to be visited next, making the sequence of samples into a random walk. The function is referred to as the proposal density or jumping distribution.
  2. For each iteration t:
  3. * Generate a candidate for the next sample by picking from the distribution.
  4. * Calculate the acceptance ratio, which will be used to decide whether to accept or reject the candidate. Because f is proportional to the density of P, we have that.
  5. * Accept or reject:
  6. ** Generate a uniform random number.
  7. ** If, then accept the candidate by setting,
  8. ** If, then reject the candidate and set instead.
This algorithm proceeds by randomly attempting to move about the sample space, sometimes accepting the moves and sometimes remaining in place. Note that the acceptance ratio indicates how probable the new proposed sample is with respect to the current sample, according to the distribution. If we attempt to move to a point that is more probable than the existing point, we will always accept the move. However, if we attempt to move to a less probable point, we will sometimes reject the move, and the more the relative drop in probability, the more likely we are to reject the new point. Thus, we will tend to stay in high-density regions of, while only occasionally visiting low-density regions. Intuitively, this is why this algorithm works and returns samples that follow the desired distribution.
Compared with an algorithm like adaptive rejection sampling that directly generates independent samples from a distribution, Metropolis–Hastings and other MCMC algorithms have a number of disadvantages:
On the other hand, most simple rejection sampling methods suffer from the "curse of dimensionality", where the probability of rejection increases exponentially as a function of the number of dimensions. Metropolis–Hastings, along with other MCMC methods, do not have this problem to such a degree, and thus are often the only solutions available when the number of dimensions of the distribution to be sampled is high. As a result, MCMC methods are often the methods of choice for producing samples from hierarchical Bayesian models and other high-dimensional statistical models used nowadays in many disciplines.
In multivariate distributions, the classic Metropolis–Hastings algorithm as described above involves choosing a new multi-dimensional sample point. When the number of dimensions is high, finding the suitable jumping distribution to use can be difficult, as the different individual dimensions behave in very different ways, and the jumping width must be "just right" for all dimensions at once to avoid excessively slow mixing. An alternative approach that often works better in such situations, known as Gibbs sampling, involves choosing a new sample for each dimension separately from the others, rather than choosing a sample for all dimensions at once. This is especially applicable when the multivariate distribution is composed of a set of individual random variables in which each variable is conditioned on only a small number of other variables, as is the case in most typical hierarchical models. The individual variables are then sampled one at a time, with each variable conditioned on the most recent values of all the others. Various algorithms can be used to choose these individual samples, depending on the exact form of the multivariate distribution: some possibilities are the adaptive rejection sampling methods, the adaptive rejection Metropolis sampling algorithm or its improvements, a simple one-dimensional Metropolis–Hastings step, or slice sampling.

Formal derivation

The purpose of the Metropolis–Hastings algorithm is to generate a collection of states according to a desired distribution. To accomplish this, the algorithm uses a Markov process, which asymptotically reaches a unique stationary distribution such that.
A Markov process is uniquely defined by its transition probabilities, the probability of transitioning from any given state to any other given state. It has a unique stationary distribution when the following two conditions are met:
  1. Existence of stationary distribution: there must exist a stationary distribution. A sufficient but not necessary condition is detailed balance, which requires that each transition is reversible: for every pair of states, the probability of being in state and transitioning to state must be equal to the probability of being in state and transitioning to state,.
  2. Uniqueness of stationary distribution: the stationary distribution must be unique. This is guaranteed by ergodicity of the Markov process, which requires that every state must be aperiodic—the system does not return to the same state at fixed intervals; and be positive recurrent—the expected number of steps for returning to the same state is finite.
The Metropolis–Hastings algorithm involves designing a Markov process that fulfills the two above conditions, such that its stationary distribution is chosen to be. The derivation of the algorithm starts with the condition of detailed balance:
which is re-written as
The approach is to separate the transition in two sub-steps; the proposal and the acceptance-rejection. The proposal distribution is the conditional probability of proposing a state given, and the acceptance distribution is the probability to accept the proposed state. The transition probability can be written as the product of them:
Inserting this relation in the previous equation, we have
The next step in the derivation is to choose an acceptance ratio that fulfills the condition above. One common choice is the Metropolis choice:
For this Metropolis acceptance ratio, either or and, either way, the condition is satisfied.
The Metropolis–Hastings algorithm thus consists in the following:
  1. Initialise
  2. # Pick an initial state.
  3. # Set.
  4. Iterate
  5. #Generate a random candidate state according to.
  6. # Calculate the acceptance probability.
  7. # Accept or reject:
  8. ## generate a uniform random number ;
  9. ## if, then accept the new state and set ;
  10. ## if, then reject the new state, and copy the old state forward.
  11. # Increment: set.
Provided that specified conditions are met, the empirical distribution of saved states will approach. The number of iterations required to effectively estimate depends on the number of factors, including the relationship between and the proposal distribution and the desired accuracy of estimation. For distribution on discrete state spaces, it has to be of the order of the autocorrelation time of the Markov process.
It is important to notice that it is not clear, in a general problem, which distribution one should use or the number of iterations necessary for proper estimation; both are free parameters of the method, which must be adjusted to the particular problem in hand.

Use in numerical integration

A common use of Metropolis–Hastings algorithm is to compute an integral. Specifically, consider a space and a probability distribution over,. Metropolis–Hastings can estimate an integral of the form of
where is a function of interest.
For example, consider a statistic and its probability distribution, which is a marginal distribution. Suppose that the goal is to estimate for on the tail of. Formally, can be written as
and, thus, estimating can be accomplished by estimating the expected value of the indicator function, which is 1 when and zero otherwise.
Because is on the tail of, the probability to draw a state with on the tail of is proportional to, which is small by definition. The Metropolis–Hastings algorithm can be used here to sample states more likely and thus increase the number of samples used to estimate on the tails. This can be done e.g. by using a sampling distribution to favor those states.

Step-by-step instructions

Suppose that the most recent value sampled is. To follow the Metropolis–Hastings algorithm, we next draw a new proposal state with probability density and calculate a value
where
is the probability ratio between the proposed sample and the previous sample, and
is the ratio of the proposal density in two directions.
This is equal to 1 if the proposal density is symmetric.
Then the new state is chosen according to the following rules.
The Markov chain is started from an arbitrary initial value, 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 represent a sample from the distribution.
The algorithm works best if the proposal density matches the shape of the target distribution, from which direct sampling is difficult, that is.
If a Gaussian proposal density is used, the variance parameter 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 samples.
The desired acceptance rate depends on the target distribution, however it has been shown theoretically that the ideal acceptance rate for a one-dimensional Gaussian distribution is about 50%, decreasing to about 23% for an -dimensional Gaussian target distribution.
If is too small, the chain will mix slowly. On the other hand,
if 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 will be very small, and again the chain will converge very slowly. One typically tunes the proposal distribution so that the algorithms accepts on the order of 30% of all samples – in line with the theoretical estimates mentioned in the previous paragraph.
s running on the 3D Rosenbrock function using the Metropolis–Hastings algorithm. The algorithm samples from regions where the posterior probability is high, and the chains begin to mix in these regions. The approximate position of the maximum has been illuminated. Note that the red points are the ones that remain after the burn-in process. The earlier ones have been discarded.