Metropolis-hastings sampling
Metropolis-Hastings sampling is a Markov chain Monte Carlo method that proposes candidate states and accepts or rejects them with probabilities chosen to preserve a target distribution.
Fabulous Adventures in Data Structures and Algorithms
The Metropolis algorithm is a Markov chain Monte Carlo method that generates samples from a target distribution using proposed moves and acceptance probabilities based on relative weights.
- How it is used
- Generates samples from continuous posterior distributions when their non-normalized weight functions are available but direct sampling is not.
- How deep it goes
- Works through the acceptance rule and a one-dimensional C# implementation, while leaving multidimensional implementation aside.
- Examples
- Uses a weighted coin-flip transition, a two-peaked target histogram, and a coin-fairness posterior after nine heads in ten flips.
- What you can do after
- Build it from scratch in C# to sample a supplied target weight function or a continuous posterior.
Machine Learning Algorithms in Depth
Metropolis-Hastings sampling is a Markov chain Monte Carlo technique that proposes state changes and accepts or rejects them using a probability designed to preserve the target distribution.
- How it is used
- Constructs samples from high-dimensional target distributions when direct sampling is difficult.
- How deep it goes
- Derives the acceptance correction from detailed balance and implements the complete proposal, evaluation, and decision loop.
- Examples
- Demonstrates a multivariate Gaussian-mixture target with a multivariate normal proposal and a Python sampler implementation.
- What you can do after
- Build a Metropolis-Hastings sampler from scratch and apply it to a multivariate Gaussian mixture.
Read how Machine Learning Algorithms in Depth explains metropolis-hastings sampling →
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.
- How it is used
- Approximates Bayesian posteriors when non-conjugate priors prevent closed-form solutions.
- 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.
Read how Grokking Bayes explains metropolis-hastings sampling →
What to know first
- normal distributionA normal distribution is a continuous probability distribution with a symmetric, bell-shaped density determined by its mean and variance.
- target distributionA target distribution is the probability distribution that a sampling procedure is intended to generate, explore, or approximate.
Related concepts
- markov chain monte carlokind of · Markov chain Monte Carlo is a family of sampling algorithms that uses a Markov chain to generate samples from a target probability distribution.
- samplingkind of · Sampling is a procedure for selecting items from a collection or generating outcomes from a probability distribution according to a specified rule.
- proposal distributionuses · A proposal distribution is a probability distribution from which a sampling or inference algorithm generates candidate states or samples.
- gibbs samplingincludes · Gibbs sampling is a Markov chain Monte Carlo method that iteratively samples each variable from its conditional distribution given the current values of the other variables.
Questions about metropolis-hastings sampling
What is metropolis-hastings sampling?
Metropolis-Hastings sampling is a Markov chain Monte Carlo method that proposes candidate states and accepts or rejects them with probabilities chosen to preserve a target distribution.
Which Manning books explain metropolis-hastings sampling?
Fabulous Adventures in Data Structures and Algorithms, Machine Learning Algorithms in Depth and Grokking Bayes.
What should I know before learning metropolis-hastings sampling?
Manning books that teach metropolis-hastings sampling usually introduce normal distribution and target distribution first.
What kinds of metropolis-hastings sampling are there?
Kinds of metropolis-hastings sampling include metropolis algorithm and gibbs sampling.
What is metropolis-hastings sampling a kind of?
Metropolis-hastings sampling is a kind of markov chain monte carlo. Markov chain Monte Carlo is a family of sampling algorithms that uses a Markov chain to generate samples from a target probability distribution.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


