Tutorials › Stochastic Processes › Deriving the Metropolis-Hastings Acceptance Probability

Markov Chain Monte Carlo · Tutorial 913 of 994

Deriving the Metropolis-Hastings Acceptance Probability

Learn to derive the Metropolis–Hastings acceptance probability from pairwise flow balance and use it with discrete and continuous proposals.

Advanced 11 min read

What You'll Learn

  • Write the accepted probability flow from one state to another.
  • Derive the Metropolis–Hastings ratio from detailed balance.
  • Explain why the usual rule maximizes acceptance subject to pairwise balance.
  • Use unnormalized target densities without computing their normalizing constant.
  • Handle proposals whose reverse transition has probability or density zero.
  • Verify that the resulting transition kernel leaves the target distribution invariant.

From Proposed Moves to Probability Flow

Markov chain Monte Carlo constructs a Markov chain whose long-run sampling distribution is a chosen target distribution. A proposal mechanism suggests a move; an acceptance rule decides whether the chain makes it or stays where it is. The key design question is how to choose that rule so that flow from one state to another is balanced by flow in the reverse direction.

Let \((X_n)_{n\ge0}\) be a discrete-time Markov chain on a state space \(S\), defined on a probability space \((\Omega,\mathcal{F},\mathbb{P})\). Let \(\pi\) be the desired target distribution. In a finite or countable state space, write \(\pi(x)\) for its probability mass at \(x\). In a continuous state space, write \(\pi(x)\) for its density with respect to a common reference measure \(\nu\). The same derivation applies in either case. We assume the chain is currently at a state \(x\) with \(\pi(x)>0\).

Let \(q(y\mid x)\) denote the proposal probability or density for proposing \(y\) from \(x\). For each \(x\), the proposal probabilities sum to one in a countable space, or the proposal density integrates to one in a continuous space. Given a proposed \(y\), the chain accepts with probability \(\alpha(x,y)\in[0,1]\). If it rejects, it remains at \(x\). Thus the probability or density of an accepted move from \(x\) to \(y\), before accounting for the possibility of staying put, is \(q(y\mid x)\alpha(x,y)\).

For two distinct states \(x\) and \(y\), the target-weighted flow of accepted moves from \(x\) to \(y\) is \(\pi(x)q(y\mid x)\alpha(x,y)\). The reverse flow is \(\pi(y)q(x\mid y)\alpha(y,x)\). Detailed balance asks these two quantities to agree. It is a sufficient condition for \(\pi\) to be invariant, and the derivation below shows exactly how it determines the familiar acceptance rule.

Definition: For a proposed move from \(x\) to \(y\), the Metropolis–Hastings acceptance probability is \[ \alpha(x,y)=\min\!\left\{1,\frac{\pi(y)q(x\mid y)}{\pi(x)q(y\mid x)}\right\} \] when \(\pi(x)q(y\mid x)>0\). If the forward proposal has positive probability or density but the reverse proposal has zero probability or density, set \(\alpha(x,y)=0\). If the forward proposal has zero probability or density, the acceptance value there does not affect the chain.

When the target is known only up to a positive normalizing constant, write \(\pi(x)=\widetilde{\pi}(x)/Z\), where \(Z\) does not depend on \(x\). The ratio in the definition is unchanged: the factor \(1/Z\) appears in both numerator and denominator and cancels. This is why the rule can be implemented using an unnormalized target.

Deriving the Rule from Pairwise Balance

Theorem (Pairwise derivation and maximality): Fix distinct states \(x,y\) such that \(a=\pi(x)q(y\mid x)>0\) and \(b=\pi(y)q(x\mid y)>0\). An acceptance rule satisfies detailed balance for this pair exactly when \(a\alpha(x,y)=b\alpha(y,x)\). Among such rules with both acceptance probabilities in \([0,1]\), the Metropolis–Hastings choice \(\alpha(x,y)=\min(1,b/a)\), \(\alpha(y,x)=\min(1,a/b)\) makes both probabilities as large as possible. If \(a>0\) and \(b=0\), detailed balance requires \(\alpha(x,y)=0\).

Proof. The accepted flow from \(x\) to \(y\) is \(a\alpha(x,y)\), while the reverse accepted flow is \(b\alpha(y,x)\). Their equality is therefore precisely the displayed pairwise balance equation. For \(a,b>0\), that equation implies \(\alpha(y,x)=(a/b)\alpha(x,y)\). The constraints that both probabilities are at most one give \(\alpha(x,y)\le1\) and \(\alpha(x,y)\le b/a\). Hence \(\alpha(x,y)\le\min(1,b/a)\). Applying the same argument in the reverse direction gives \(\alpha(y,x)\le\min(1,a/b)\). Choosing these two upper bounds gives equal flows: if \(b\le a\), the forward acceptance is \(b/a\) and the reverse acceptance is \(1\), so both flows equal \(b\); if \(a\le b\), the forward acceptance is \(1\) and the reverse acceptance is \(a/b\), so both flows equal \(a\). Thus the rule balances flow and is maximal in each direction. If \(a>0\) and \(b=0\), pairwise balance reads \(a\alpha(x,y)=0\), which forces \(\alpha(x,y)=0\). \(\square\)

Balance alone does not force this particular rule: multiplying both Metropolis–Hastings acceptance probabilities for a pair by the same constant in \([0,1]\) also balances flow, but accepts less often. The standard rule is the largest pairwise-balanced choice. If both \(a\) and \(b\) are zero, neither direction carries target-weighted proposal flow, so the pairwise equation imposes no restriction.

1
Write the forward and reverse flows.
They are \(\pi(x)q(y\mid x)\alpha(x,y)\) and \(\pi(y)q(x\mid y)\alpha(y,x)\).
2
Balance the flows.
Set them equal and require each acceptance probability to lie in \([0,1]\).
3
Use as much acceptance as possible.
The smaller of the two proposal-weighted target flows is accepted in both directions; dividing by the forward flow gives the ratio rule.

Why the Rule Preserves the Target

Detailed balance is useful because it implies invariance, including the probability of remaining at the current state after rejection. The next result gives the argument for both countable and continuous state spaces. In the continuous case, assume the proposal has density \(q(y\mid x)\) with respect to \(\nu\), and that the target has density \(\pi\) with respect to the same measure.

Theorem (Invariance): Suppose the Metropolis–Hastings transition is defined at every state in the support of \(\pi\). Then \(\pi\) is invariant for the resulting transition kernel \(K\): starting with distribution \(\pi\), one step of the chain again has distribution \(\pi\).

Proof. Write \(A(x,dy)=q(y\mid x)\alpha(x,y)\nu(dy)\) for the accepted-move part of the kernel, with sums replacing integrals on a countable space. For \(\pi(x)q(y\mid x)>0\), the acceptance definition gives \[ \pi(x)q(y\mid x)\alpha(x,y) =\min\{\pi(x)q(y\mid x),\pi(y)q(x\mid y)\}. \] The right-hand side is unchanged when \(x\) and \(y\) are exchanged. If either forward weighted proposal flow is zero, both sides of the corresponding accepted-flow balance are zero by the zero-reverse rule. Thus the accepted-flow measure is symmetric in \(x,y\).

Let \(r(x)=1-\int q(y\mid x)\alpha(x,y)\nu(dy)\) be the rejection probability; on a countable space the integral is a sum. It is nonnegative because \(0\le\alpha\le1\) and the proposal integrates or sums to one. The full kernel consists of accepted moves plus rejection at \(x\): \(K(x,dy)=A(x,dy)+r(x)\delta_x(dy)\). For any measurable set \(C\), symmetry of the accepted-flow measure implies that its incoming mass to \(C\) equals its outgoing mass from \(C\). The outgoing accepted mass plus rejection mass from \(C\) is \(\int_C\pi(x)(1-r(x))\nu(dx)+\int_C\pi(x)r(x)\nu(dx)=\int_C\pi(x)\nu(dx)\). Therefore \(\int\pi(dx)K(x,C)=\int_C\pi(x)\nu(dx)\), proving invariance. The same calculation with sums proves the countable case. \(\square\)

Worked Derivations

Worked Example: An Asymmetric Two-State Proposal

Let \(S=\{A,B\}\) and let the target probabilities be \(\pi(A)=\tfrac27\), \(\pi(B)=\tfrac57\). From \(A\), propose \(B\) with probability \(\tfrac34\) and stay at \(A\) with probability \(\tfrac14\). From \(B\), propose \(A\) with probability \(\tfrac14\) and stay at \(B\) with probability \(\tfrac34\). Each proposal row sums to one: \(\tfrac34+\tfrac14=1\) and \(\tfrac14+\tfrac34=1\).

For a proposed move \(A\to B\), the target-weighted forward proposal is \(\tfrac27\cdot\tfrac34=\tfrac6{28}\). The reverse weighted proposal is \(\tfrac57\cdot\tfrac14=\tfrac5{28}\). Thus \[ \alpha(A,B)=\min\!\left(1,\frac{5/28}{6/28}\right)=\frac56. \] For \(B\to A\), the ratio is \((6/28)/(5/28)=\tfrac65\), so \(\alpha(B,A)=1\).

Check: The accepted flow from \(A\) to \(B\) is \(\tfrac27\cdot\tfrac34\cdot\tfrac56=\tfrac5{28}\). The reverse flow is \(\tfrac57\cdot\tfrac14\cdot1=\tfrac5{28}\). The two flows agree, as required.

Worked Example: A Symmetric Continuous Proposal

Take the state space \(S=\mathbb{R}\) and target density \(\pi(x)=\tfrac12 e^{-|x|}\), which integrates to one. Propose uniformly from \([x-1,x+1]\), so \(q(y\mid x)=\tfrac12\) when \(|y-x|\le1\), and \(q(y\mid x)=0\) otherwise. This proposal density integrates to \(\tfrac12\cdot2=1\) for every \(x\). It is symmetric wherever it is positive: \(q(y\mid x)=q(x\mid y)=\tfrac12\).

At \(x=0\), consider the proposed value \(y=1\), which is within the proposal interval. The target density at \(0\) is \(\tfrac12 e^0=\tfrac12\); at \(1\) it is \(\tfrac12 e^{-1}\). The proposal terms cancel, so \[ \alpha(0,1)=\min\!\left(1,\frac{(\tfrac12e^{-1})(\tfrac12)}{(\tfrac12)(\tfrac12)}\right)=e^{-1}. \] For the reverse proposal, \(\alpha(1,0)=\min(1,e)=1\).

Check: The forward accepted-flow density at the pair \((0,1)\) is \((\tfrac12)(\tfrac12)e^{-1}=\tfrac14e^{-1}\). The reverse density is \((\tfrac12e^{-1})(\tfrac12)(1)=\tfrac14e^{-1}\). They match. Rejection at \(x\) accounts for the remaining probability after integrating accepted proposals over the interval.

Worked Example: A One-Way Proposal Must Be Rejected

Let \(S=\{A,B\}\), with target probabilities \(\pi(A)=\pi(B)=\tfrac12\). From \(A\), propose \(B\) with probability \(1\). From \(B\), propose \(B\) with probability \(1\), so the reverse proposal \(B\to A\) has probability \(0\). Both proposal rows sum to one. Although the target probabilities are equal, the reverse proposal probability is not.

The forward proposal \(A\to B\) has positive target-weighted flow \(\tfrac12\cdot1=\tfrac12\), whereas the reverse weighted proposal is \(\tfrac12\cdot0=0\). Detailed balance therefore requires \(\alpha(A,B)=0\). The chain rejects every proposal from \(A\) and stays there; from \(B\), it also stays at \(B\). This kernel leaves the target invariant because it never changes the state, although it does not move between the two states.

Check: The flow from \(A\) to \(B\) is \(\tfrac12\cdot1\cdot0=0\), and the reverse flow is \(\tfrac12\cdot0\cdot\alpha(B,A)=0\). Thus balance holds for any reverse acceptance value; in this example no reverse proposal is generated.

A Common Error and Its Correction

A frequent mistake is to compare only the target values and omit the proposal probabilities. In the asymmetric two-state example, that shortcut gives the proposed acceptance probability for \(A\to B\) as \(\min(1,\pi(B)/\pi(A))=\min(1,5/2)=1\). But it would produce forward flow \(\tfrac27\cdot\tfrac34\cdot1=\tfrac3{14}=\tfrac6{28}\), while the reverse flow, even with acceptance one, is only \(\tfrac57\cdot\tfrac14=\tfrac5{28}\). The flows do not balance. Including the reverse-to-forward proposal ratio gives \(\alpha(A,B)=\tfrac56\), making both flows \(\tfrac5{28}\). The target ratio alone is appropriate only when the proposal probabilities in the two directions are equal and positive.

The acceptance rule balances the proposed moves, not the probabilities of being at the two states by themselves. A proposal that favors one direction must be corrected by the reverse proposal probability. Also, invariance does not by itself guarantee that a chain can reach every part of the state space: as the one-way example shows, a kernel can preserve a target while failing to explore it. Conditions for convergence and irreducibility are separate questions.

Check Your Understanding

For each question, distinguish target weights from proposal probabilities and state any support condition you use.

  1. Suppose \(\pi(x)=\tfrac13\), \(\pi(y)=\tfrac23\), \(q(y\mid x)=\tfrac12\), and \(q(x\mid y)=\tfrac14\). Find both Metropolis–Hastings acceptance probabilities and verify the accepted flows.
  2. Why does a common normalizing constant in an unnormalized target cancel from the acceptance ratio?
  3. If \(q(y\mid x)>0\) but \(q(x\mid y)=0\), what acceptance probability is required by pairwise detailed balance, assuming \(\pi(x)>0\)?
  4. Give a reason that the Metropolis–Hastings rule is maximal among pairwise-balanced rules, rather than the only rule that balances flow.
  5. For a symmetric proposal with positive density in both directions, express the acceptance probability using only the target densities at \(x\) and \(y\).