How Metropolis-Hastings samples a target density

Short answer: Metropolis-Hastings draws samples from a target density π that is known up to a constant. From the current state it proposes a neighbor from a density q, and it accepts the proposal with probability min(1, π(y)q(x|y) / π(x)q(y|x)). The π constants cancel, so an unnormalized density is enough. If the chain is irreducible and aperiodic, its stationary distribution is π. A finite run produces dependent samples, not independent draws, and the early samples follow the start rather than π.


What the problem actually asks

Rejection sampling and inverse transform need a handle on the normalized density or a tight envelope. A Bayesian posterior often supplies an unnormalized product of likelihood and prior. The Metropolis rule uses only ratios of π, so the unknown normalizer cancels. Detailed balance for this acceptance probability makes π the stationary distribution of the chain.

The proposal q is a design choice. A symmetric random walk has q(y|x) = q(x|y), and the acceptance ratio simplifies to π(y)/π(x). A proposal that rarely moves mixes slowly. A proposal that jumps too far is rarely accepted. The algorithm does not choose q for you.


What the algorithm guarantees

Under standard regularity, the chain has stationary distribution π, and sample averages converge to expectations under π as the number of steps goes to infinity. The samples are correlated. Effective sample size is smaller than the number of iterations. A burn-in prefix is discarded because the start may be atypical. There is no finite-time certificate that burn-in is over. Diagnostics are heuristics.

The acceptance probability must use the proposal ratio when q is not symmetric. Dropping it samples the wrong distribution. Hamiltonian Monte Carlo and Gibbs sampling are other MCMC methods. Metropolis-Hastings is the accept/reject correction on a general proposal.


The working steps

  1. Start at a state x where π(x) > 0. Choose a proposal density q.
  2. Draw y from q(·|x). Compute a = π(y) q(x|y) / (π(x) q(y|x)). If π is unnormalized, use the unnormalized values. The constant cancels.
  3. Accept y with probability min(1, a). Otherwise stay at x. Either way, emit the resulting state as the next sample.
  4. Discard a burn-in prefix. Use the remaining samples, remembering they are dependent, to estimate expectations.
  5. If q is symmetric, a reduces to π(y)/π(x). Do not use that reduction for an asymmetric proposal.

Stationarity is an asymptotic statement. A short chain started far from the mass of π does not yield draws from π. The acceptance formula is the algorithm. Always accepting is a different chain. Simulated annealing uses the Metropolis rule with a changing target. Here the target stays fixed because the goal is sampling, not minimization.


Approaches that look simpler and fail

Approach Why it fails when the proposal ratio is dropped
Accept with probability min(1, π(y)/π(x)) for an asymmetric proposal That ratio is correct only when q is symmetric. An asymmetric q samples a different stationary distribution unless the proposal ratio is included.
Treat successive samples as independent The next state depends on the current one by construction. Variance estimates that assume independence are too small.
Skip burn-in from an arbitrary start and average everything The early samples reflect the start. There is no universal burn-in length, and ignoring the issue biases the average.
Always accept to mix faster The chain then follows q, not π. The rejections are the correction.

Optimization by cooling this rule is simulated annealing. A special proposal that uses gradients is Hamiltonian Monte Carlo. Metropolis-Hastings is the general accept/reject MCMC step.


A working example

This Python fragment proposes a neighbor on {0, 1, 2} and accepts it with probability min(1, π(y)/π(x)). The target weights are 1, 2, 3, so the chain spends the most time at 2. It prints 2.

import random
weights = [1, 2, 3]
rng = random.Random(0)
state = 0
counts = [0, 0, 0]
for step in range(4000):
    proposal = state + rng.choice((-1, 1))
    if proposal < 0 or proposal > 2:
        proposal = state
    if rng.random() < min(1.0, weights[proposal] / weights[state]):
        state = proposal
    if step >= 500:
        counts[state] += 1
assert counts[2] > counts[1] > counts[0]
print(max(range(3), key=lambda i: counts[i]))

Where it is used

Metropolis-Hastings draws samples from a density a Bayesian model or a physics simulation can evaluate but cannot integrate. Stan and similar tools often use Hamiltonian Monte Carlo, which is a later sampler on the same Markov-chain idea. A plain random walk is the proposal this algorithm accepts or rejects.


When you can drop this algorithm

Drop a short chain as a substitute for independent draws. Run it long enough that diagnostics are boring, and keep the proposal ratio in the acceptance probability.


Why this stays published

We publish this so a search for Metropolis-Hastings returns the acceptance ratio that includes the proposal density and the fact that the samples are dependent. Metropolis-Hastings samples an unnormalized density by accepting a proposal with a probability that restores detailed balance, and a finite run yields dependent samples.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *