advanced38 min studyLesson 268 in Mathematics

Metropolis–Hastings and detailed balance

Build a target-invariant transition kernel, retain rejected states, and separate stationarity from finite-run convergence.

On this page

What you’ll understand

  • Calculate acceptance probabilities including asymmetric proposal terms.
  • Verify detailed balance in a finite example.
  • Explain why invariance alone does not establish useful mixing or convergence.

When independent posterior sampling is difficult, a Markov chain can produce a dependent sequence whose invariant distribution is the desired posterior. Metropolis–Hastings constructs such a chain by proposing a move and accepting or rejecting it with a carefully chosen probability. The normalizing constant cancels in the acceptance ratio, but this computational convenience does not eliminate requirements on support, reachability, or long-run behavior. We will verify the invariant distribution explicitly before asking whether a finite run actually explores it well.

Correct the proposal's directional imbalance

At current state xx with positive target density, draw a candidate zz from proposal density q(z∣x)q(z\mid x). The acceptance probability compares target-times-proposal flow in the two directions. A symmetric proposal makes the proposal factors cancel; an asymmetric proposal generally does not. If a proposed move has positive forward flow but zero reverse target-proposal flow, its acceptance is zero. Work with log ratios for numerical stability, comparing a log-uniform random draw with the smaller of zero and the log acceptance ratio.

Rejection means recording the current state again. Those repeated states are part of the transition kernel and of averages taken over iteration time. Discarding all rejected iterations usually changes the distribution of the retained jump sequence toward states with higher departure rates, unless a separate correction is applied. Repeated states are therefore neither missing values nor harmless duplicates to delete. Their frequency can reveal poor proposals, but a high acceptance rate alone is also insufficient: tiny steps can be accepted almost always while exploring the target extremely slowly.

α(x,z)=min⁡ ⁣(1,π~(z)q(x∣z)π~(x)q(z∣x))\alpha(x,z)=\min\!\left(1,\frac{\widetilde\pi(z)q(x\mid z)}{\widetilde\pi(x)q(z\mid x)}\right)
The target's common unknown normalizing constant cancels; both proposal directions still matter.↔ Scroll wide equations horizontally.

Detailed balance gives invariance

For distinct states, the accepted probability flow is the minimum of the two directional target-proposal flows. This expression is symmetric when the directions are swapped, establishing detailed balance. Summing or integrating that identity, together with the self-transition created by rejection, establishes target invariance. Detailed balance is sufficient for invariance, not necessary: nonreversible samplers can also preserve a target. The derivation assumes a legitimate proposal kernel and a proper normalized target exists behind the unnormalized density.

In a finite state space, irreducibility makes the invariant probability unique. Adding aperiodicity ensures convergence of the time-n marginal distribution from any start. Invariance alone supplies neither condition: a chain that never moves preserves every distribution, while deterministic alternation preserves a balanced distribution yet oscillates forever from a fixed start. For general continuous state spaces, appropriate recurrence and irreducibility conditions replace the simple finite argument. Do not transfer a finite-state convergence proof to an arbitrary posterior without checking its hypotheses.

π(x)q(z∣x)α(x,z)=min⁡{π(x)q(z∣x),π(z)q(x∣z)}=π(z)q(x∣z)α(z,x)\pi(x)q(z\mid x)\alpha(x,z)=\min\{\pi(x)q(z\mid x),\pi(z)q(x\mid z)\}=\pi(z)q(x\mid z)\alpha(z,x)
Equality of pairwise probability flows verifies detailed balance wherever these densities or masses are defined.↔ Scroll wide equations horizontally.

Finite computation requires diagnostic evidence

Even a valid ergodic kernel can remain near one mode for a very long time. Run multiple chains from dispersed valid starting points, inspect traces and between-chain agreement, and estimate Monte Carlo error for the quantities actually needed. Warmup can tune proposal parameters, but a proof for one fixed kernel does not automatically justify arbitrary ongoing adaptation. Standard workflows adapt during warmup and then fix tuning for retained draws; more general adaptive methods need their own conditions.

Separate uncertainty in the posterior from error in estimating its summaries. A narrow posterior interval can be computed poorly, and many correlated draws can contain less information about a mean than a much smaller independent sample. Autocorrelation-based effective sample size refers to a particular function or summary and is different from importance-weight ESS. Diagnostics can reveal trouble but cannot prove that an unseen mode does not exist. Analytical special cases, alternative parameterizations, and independent implementations provide useful additional checks of both the model and the sampler.

An exact two-state Metropolis kernel

The target probabilities of states 0 and 1 are 0.8 and 0.2. Always propose flipping to the other state. Compute acceptance probabilities, the transition matrix, and its stationary distribution.

  1. The flip proposal is symmetric. From 0 to 1 the target ratio is 0.2/0.8=0.25, so accept with probability 0.25. From 1 to 0 the ratio is four, so accept with probability one.

  2. The transition rows are therefore (0.75,0.25) from state 0 and (1,0) from state 1. Rejections at state 0 supply its self-transition. Detailed-balance flow from 0 to 1 is 0.8 times 0.25=0.2, equal to reverse flow 0.2 times one.

  3. Multiplying the target row vector (0.8,0.2) by this matrix gives (0.8,0.2). The chain is irreducible and has a self-loop at state 0, so it is aperiodic. It converges from either start, although consecutive states are not independent.

The result

The target is invariant and the finite chain converges. Keeping only successful flips would alternate states and incorrectly assign them equal iteration frequencies.

Explore the model

Interactive lab

Interactive lab · markov

Same stationary law, different mixing

Pause and Reset, then set both controls to p=0.20 and q=0.80. Starting from state 0, predict distribution (0.80,0.20) after one Step. Reset and set p=0.05 and q=0.20; its stationary law is also (0.80,0.20), but one Step gives (0.95,0.05). Compare subsequent Steps. The scene computes exact finite-step distributions, not random Metropolis trajectories.

Same stationary law, different mixingExact finite-step probabilities for a two-state chain starting in state 0, not sampled trajectories. Playback advances two transitions per second. When p = q = 0 every distribution is stationary; when p = q = 1 the chain alternates forever. A single zero transition creates an absorbing state. Current readings: P(state 1) at step 0: 0%; Stationary P(state 1): 60%; Chain behavior: Monotone convergence. All parameters are adjustable with the labeled controls below.Two-state Markov distribution after 0 steps: state zero probability 1, state one probability 0.Initial distribution (1, 0) · step 0p = 0.3q = 0.201100%0%1−p1−qP(0)P(1)Monotone convergence
P(state 0)P(state 1)Stationary distribution
P(state 1) at step 0
0%
Stationary P(state 1)
60%
Chain behavior
Monotone convergence
t = 0 s

Exact finite-step probabilities for a two-state chain starting in state 0, not sampled trajectories. Playback advances two transitions per second. When p = q = 0 every distribution is stationary; when p = q = 1 the chain alternates forever. A single zero transition creates an absorbing state. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forMatching stationary probabilities does not determine how fast a chain approaches them.

Check your understanding

For an asymmetric Metropolis–Hastings proposal, which acceptance ratio is generally correct?

Try it yourself

A two-state target is (0.6,0.4). The proposal moves 0 to 1 with probability 0.5 and 1 to 0 with probability 0.25, otherwise proposing to stay. Calculate the acceptance of each cross-state move and verify the accepted probability flows.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Acceptance uses the reverse proposal probability as well as the target ratio. Multiply acceptance by proposal probability to obtain an actual transition.

Compare with the solution

For 0 to 1 the ratio is (0.4 times 0.25)/(0.6 times 0.5)=1/3, so acceptance is 1/3 and the actual transition probability is 1/6. For 1 to 0 the reciprocal ratio is three, so acceptance is one and the transition probability is 1/4. Target-weighted flows are 0.6 times 1/6=0.1 and 0.4 times 1/4=0.1. The remaining transition probabilities are self-transitions. Omitting the proposal ratio would give the wrong target balance.

Take it with you

  • Acceptance corrects both target density and proposal asymmetry.
  • Rejected iterations belong in ordinary chain averages.
  • Detailed balance proves invariance; useful finite-run exploration requires more evidence.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.