Metropolis-hastings sampling
Metropolis-Hastings sampling is a Markov chain Monte Carlo method that proposes state changes and accepts or rejects them probabilistically to generate samples from 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 →
Related concepts
- markov chain monte carlokind of · Markov chain Monte Carlo is a family of sampling algorithms that generate samples using a Markov chain whose stationary or limiting distribution is a target probability distribution.
- proposal distributionuses · A proposal distribution is a probability distribution from which a sampling or inference algorithm generates candidate states or samples.
- initial state distributionuses · An initial state distribution is a probability distribution over the states from which a stochastic process or episode begins.
- accept/reject algorithmuses · An accept-reject algorithm samples candidate outcomes from a proposal process and accepts or rejects them according to a specified rule or probability.
- Markov chainimplements · A Markov chain is a stochastic process of states whose transition probabilities depend on the current state rather than the full preceding history.
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.

