This is a great question about MCMC methods, particularly the Metropolis-Hastings algorithm. Let me break this down carefully.
The Metropolis-Hastings Acceptance Ratio
The Metropolis-Hastings algorithm is a method for sampling from a probability distribution π(x) by constructing a Markov chain that has π as its stationary distribution.
The Basic Setup
- We want to sample from a target distribution: π(x)
- We use a proposal distribution q(x*|x) to suggest new states
- We accept the proposal with some probability (the acceptance ratio)
The Acceptance Ratio
The acceptance ratio is:
α(x*, x) = min(1, π(x*)/π(x) × q(x|x*)/q(x*|x))
Or more commonly written as:
α(x*, x) = min(1, (π(x*) q(x|x*))/(π(x) q(x*|x)))
Let me think about why this works...
Why This Works: The Detailed Balance Condition
For a Markov chain to have π as its stationary distribution, we need the detailed balance condition:
π(x) P(x → x*) = π(x*) P(x* → x)
Where P(x → x*) is the transition probability from x to x*.
The transition probability consists of two parts:
- The proposal probability q(x*|x)
- The acceptance probability α(x*, x)
So: P(x → x*) = q(x*|x) α(x*, x)
Detailed balance requires:
π(x) q(x*|x) α(x*, x) = π(x*) q(x|x*) α(x*, x)
Wait, that's not quite right. Let me reconsider.
Actually, the detailed balance condition is:
π(x) P(x → x*) = π(x*) P(x* → x)
Where P includes both moving and accepting. If we're at x and propose x*, we move with probability q(x*|x) and accept with probability α(x*, x).
So the transition probability is: q(x*|x) α(x*, x)
The reverse transition from x* to x would be: q(x|x*) α(x, x*)
Detailed balance:
π(x) q(x*|x) α(x*, x) = π(x*) q(x|x*) α(x, x*)
We want to choose α to satisfy this. A common choice (Metropolis-Hastings) is:
α(x*, x) = min(1, (π(x*) q(x|x*))/(π(x) q(x*|x)))
This ensures detailed balance holds. When the ratio is greater than 1, we always accept (α = 1). When it's less than 1, we accept with that probability. This guarantees the chain converges to the target distribution π(x).
For symmetric proposal distributions where q(x*|x) = q(x|x*), the proposal terms cancel out entirely, leaving:
α(x*, x) = min(1, π(x*)/π(x))
This is the Metropolis algorithm. Random walk proposals like N(x, σ²) are symmetric since they depend only on the distance |x* - x|.
The elegance here is that we can sample from π(x) = (1/Z) f(x) without ever computing Z. The normalizing constant cancels in the acceptance ratio:
α(x*, x) = min(1, f(x*)/f(x))
We only need to evaluate the unnormalized density f at proposed and current states. The normalizing constant cancels because both the target and proposal distributions appear in a ratio. This means I only need the unnormalized density f(x), which is crucial for Bayesian inference where computing the marginal likelihood p(data) is often intractable. The acceptance ratio simplifies to comparing the ratio of unnormalized posteriors with the proposal ratio, making MCMC practical for complex models where we know p(data|x)p(x) but not the normalizing constant.
Metropolis-Hastings Acceptance Ratio
The Core Formula
The acceptance probability for moving from state x to proposed state x* is:
α(x∗∣x)=min(1,
Where:
- π(x) = target distribution
- q(x*|x) = proposal distribution
- The ratio compares how likely x* is relative to x under both the target and proposal
Why This Works: Detailed Balance
This ratio ensures the detailed balance condition:
π(x)q(x∗∣x)α(x∗
This guarantees that π is the stationary distribution of the resulting Markov chain. Intuitively:
- If π(x*) is high relative to π(x), accept moves to x* with high probability
- If the proposal suggests unlikely moves, weight toward rejection
Cancellation for Symmetric Proposals
If your proposal is symmetric: q(x∗∣x)=q(x∣x∗)
α(x∗∣x)=min(1,π(x)
The proposal terms vanish. This simplifies computation and reduces variance. Common symmetric proposals:
- Random walk: q(x∗∣x)=N(x,σ2) (only depends on distance)
- Uniform perturbations within a ball
Sampling Without the Normalizing Constant
This is the key practical advantage. In Bayesian inference:
π(x∣data)=p(data)
The evidence p(data) is an intractable normalizing constant. But in the acceptance ratio:
α=min(1
The Z cancels:
π(x)π(x
You only need to evaluate the unnormalized posterior: f(x)=p(data∣x)p(x)
Why This Matters
For most real problems:
- Computing p(data) requires integrating over all possible parameters
- This integral is analytically intractable
- But evaluating the likelihood × prior at any point is straightforward
MCMC solves this by only needing likelihood ratios, enabling Bayesian inference without calculating intractable normalizing constants.
Practical Example
# Target posterior (unnormalized)
def log_unnormalized_posterior(x, data):
return log_likelihood(x, data) + log_prior(x)
# Metropolis-Hastings step (symmetric random walk)
def mh_step(x_current, data, proposal_sd):
x_proposed = x_current + np.random.normal(0, proposal_sd)
# Log acceptance ratio (symmetric proposal cancels)
log_alpha = log_unnormalized_posterior(x_proposed, data) \
- log_unnormalized_posterior(x_current, data)
if np.log(np.random.uniform()) < log_alpha:
return x_proposed
return x_current
The algorithm never computes p(data) explicitly.