liveBook · Manning

Metropolis-hastings sampling

As explained in Grokking Bayes

Metropolis–Hastings is an MCMC algorithm that proposes candidate states and accepts or rejects them using a rule based on the target density and proposal probabilities.

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

How Grokking Bayes covers it

How deep it goes
Explains the walk, proposal, ratio, acceptance rule, and Python implementation, then defers detailed convergence practice to diagnostics.
Examples
Uses a Beta(4, 3) target, an unnormalized posterior function, and a NumPy sampling loop.
What you can do after
Builds a basic sampler and evaluates its samples with burn-in, multiple chains, R̂, ESS, trace plots, and predictive checks.
How it connects
Connects posterior distributions, unnormalized densities, proposal distributions, Markov chains, burn-in, R̂, ESS, and posterior predictive checks.

Also explained in

Compare how 3 books explain metropolis-hastings sampling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Bayes. The section links point to the parts of the book it draws on.