Metropolis–Hastings sampling approximates an unknown posterior by taking a guided random walk. At each step, it proposes a new state, compares its unnormalized posterior density with the current state, and accepts the move with probability min(1, r), where r is the density ratio. This works even when the posterior has no closed-form solution. section 5.14 section 5.6
The basic procedure is:
- Start at an initial point in the hypothesis space. section 5.6
- Draw a proposed move from a Normal distribution centered on the current state. section 5.6
- Compute the ratio between proposed and current unnormalized posterior densities. section 5.6
- Accept the proposal with probability min(1, r); otherwise, repeat the current state. section 5.6
A small Python sampler
For a Beta(4, 3) example, define an unnormalized posterior that returns zero outside 0 < p < 1. Initialize the chain, propose Normal moves, evaluate each proposal, and store either the proposal or the current value. The resulting dependent samples approximate the target distribution. section 5.6
samples = np.empty(n_samples) #A
samples[0] = initial #A
current_post = unnormalized_posterior( #B
initial
)
for t in range(1, n_samples):
proposal = np.random.normal( #C
samples[t-1], proposal_stddev
)
proposal_post = unnormalized_posterior( #D
proposal
)
r = proposal_post / current_post #E
if np.random.rand() < r: #F
samples[t] = proposal
current_post = proposal_post
else: #G
samples[t] = samples[t-1]
Inspect the sampler loop showing initialization, proposal generation, density evaluation, acceptance, and rejection. section 5.6


