Skip to main content
ExplainerBayesian InferenceAlgorithm Explainer· Updated · 5 min read· in Data & Analysis

Metropolis-Hastings Cancels Impossible Denominators to Sample Posterior Distributions

By proposing random steps and applying a mathematical acceptance filter, this algorithm allows statisticians to sample from complex posterior distributions without calculating their impossible denominators.

By Sofia Matos

Share this story

Listen

In short

  • The algorithm maps complex probability distributions by building a Markov chain that wanders toward high-density regions.
  • It bypasses the need to calculate the intractable marginal likelihood denominator in Bayesian inference.
  • Proposed steps that move to lower-probability states are occasionally accepted based on a random uniform draw.

Rejection sampling can map a complex probability distribution by drawing random points inside a bounding box and discarding those that fall outside the target curve. But as the number of dimensions grows, the bounding box becomes mostly empty space, and rejection sampling discards almost everything it draws.

The Metropolis-Hastings algorithm solves this dimensional curse by abandoning the bounding box entirely. Instead of drawing independent points, it builds a Markov chain—a sequence where each new sample depends only on the current one, allowing the algorithm to wander toward high-probability regions and stay there.[8]

The core problem this algorithm solves lies at the heart of Bayesian statistics. To update beliefs based on new data, statisticians must calculate a posterior distribution, which requires dividing by a marginal likelihood. For models with more than three or four parameters, calculating that denominator requires an integral over all possible parameter values—a mathematical operation that is analytically impossible.[7]

In 1953, physicist Nicholas Metropolis introduced a workaround for symmetric distributions, which statistician W.K. Hastings generalized in 1970. They proved that the impossible denominator is actually irrelevant if you only care about the relative probability of two points. Because the denominator is a constant across the entire distribution, it cancels out when you divide the probability of one state by the probability of another.[3][8]

As data scientist Brian Keng notes in his analysis of the method, Metropolis-Hastings is "a method that allows us to sample from any probability distribution, provided we can compute the value of a function that is proportional to the density of the target distribution."[8]

The algorithm begins by selecting an arbitrary starting point in the parameter space. From there, it uses a "proposal distribution"—often a standard Gaussian bell curve centered on the current location—to suggest a random step to a new parameter value.[1][4]

Once a new state is proposed, the algorithm calculates the acceptance ratio. This metric divides the unnormalized probability of the proposed state by the unnormalized probability of the current state, factoring in any asymmetry in the proposal mechanism.[5]

The algorithm's efficiency relies on tuning the proposal variance to hit specific mathematical acceptance thresholds.

If the acceptance ratio is greater than 1.0, the proposed state is strictly more probable than the current one. The algorithm accepts the move immediately, stepping uphill toward the denser regions of the target distribution.[6]

The mathematical brilliance of Metropolis-Hastings lies in how it handles proposed steps that go downhill. If the acceptance ratio is less than 1.0—say, 0.3—the algorithm does not automatically reject the move. Instead, it draws a random number between 0.0 and 1.0 from a uniform distribution.[2][6]

If the random draw is lower than the acceptance ratio (0.3), the algorithm accepts the inferior step. If the draw is higher, it rejects the step, stays in its current position, and records that current position as the next sample in the chain.[1][4]

This probabilistic filter prevents the Markov chain from getting permanently trapped on a single local peak. By occasionally accepting downhill moves, the algorithm explores the entire parameter space. Over 10,000 or more iterations, the frequency with which the chain visits any specific region becomes perfectly proportional to that region's true underlying probability.[5][7]

The efficiency of this exploration depends entirely on the variance of the proposal distribution. If the variance is set too small, the proposed steps are tiny. The algorithm will accept nearly 100 percent of them, but it will take an impractically long time to traverse a wide distribution, a failure mode known as high autocorrelation.[3][8]

Conversely, if the proposal variance is too large, the algorithm frequently suggests wild leaps into low-probability wastelands. The acceptance ratio for these leaps approaches zero, causing the algorithm to reject almost everything and freeze in place, recording the same state hundreds of times in a row.[1][7]

A well-tuned proposal distribution (top) explores the space efficiently, while a poorly tuned one (bottom) freezes in place.

Finding the mathematical sweet spot requires tuning the acceptance rate. For a single-dimension target distribution, theoretical results show the optimal proposal variance achieves an acceptance rate of roughly 0.44.[3]

As the number of dimensions increases, that ideal threshold drops. For high-dimensional models with 20 or more parameters, the optimal acceptance rate converges to exactly 0.234. Hitting this specific fraction ensures the chain takes steps large enough to explore, but conservative enough to avoid constant rejection.[3][7]

Even with perfect tuning, the initial samples in the chain are heavily biased by the arbitrary starting point. Practitioners typically discard the first 1,000 to 5,000 iterations—often up to 50 percent of the total run—as a "burn-in" period, using only the subsequent samples for their final analysis.[2][4]

The primary limitation of Metropolis-Hastings emerges in highly correlated or multimodal distributions. If a target distribution features two isolated probability peaks separated by a vast, near-zero probability valley, the algorithm struggles to cross the gap.[2][6]

Standard random-walk algorithms struggle to map multimodal distributions, often getting trapped on a single local peak.

Because the algorithm relies on a random walk, the probability of proposing a sequence of steps that successfully navigates a deep valley is vanishingly small. The chain may spend its entire run mapping only one peak, leaving the researcher entirely unaware that the other exists.[5][8]

When faced with these pathological geometries, modern statisticians often abandon the random walk entirely. Instead, they turn to advanced variants like Hamiltonian Monte Carlo, which calculates the gradient of the probability space to slide efficiently toward high-density regions without relying on blind proposals.[7]

Key terms

Markov Chain
A sequence of events where the probability of each event depends only on the state attained in the previous event.
Posterior Distribution
The updated probability of a hypothesis after new evidence is considered.
Proposal Distribution
The mathematical rule used to suggest the next potential step in the Markov chain.
Acceptance Ratio
The calculated fraction that determines whether a proposed step is kept or discarded.
Burn-in Period
The initial set of iterations in a Markov chain that are discarded because they are too heavily influenced by the arbitrary starting point.

Viewpoints in depth

Bayesian Statisticians

View the algorithm as the foundational breakthrough that unlocked modern statistics.

For decades, Bayesian inference was mathematically elegant but practically useless for complex problems because the posterior denominator could not be computed. Statisticians in this camp view Metropolis-Hastings as the computational engine that rescued the field. By proving that the denominator cancels out in a ratio, the algorithm allowed researchers to use complex, non-conjugate priors that reflect reality, rather than being forced to choose mathematically convenient but inaccurate models.

Computational Scientists

Focus on the computational cost and the strict necessity of tuning proposal distributions.

From a computational perspective, the algorithm is a brute-force mechanism that requires careful calibration. Practitioners emphasize that a poorly tuned proposal distribution wastes massive amounts of CPU time either rejecting steps or taking steps too small to matter. They focus heavily on diagnostic tools like trace plots and autocorrelation functions to ensure the Markov chain has actually converged on the true distribution rather than just appearing stable.

Machine Learning Engineers

Treat standard Metropolis-Hastings as a mathematically elegant but often too-slow method.

In the realm of deep learning and massive datasets, engineers often view standard random-walk Metropolis-Hastings as impractically slow. When a model has thousands or millions of parameters, the 23.4% acceptance rule means the algorithm spends most of its time rejecting proposals. This camp generally prefers gradient-based MCMC variants like Hamiltonian Monte Carlo, or abandons sampling entirely in favor of variational inference, which approximates the posterior much faster.

Bayesian Statisticians 40%Computational Scientists 35%Machine Learning Engineers 25%
Bayesian Statisticians
View the algorithm as the foundational breakthrough that unlocked the ability to use complex, non-conjugate priors in modern statistics.
Computational Scientists
Focus on the computational cost of the algorithm and the strict necessity of tuning proposal distributions to achieve optimal acceptance rates.
Machine Learning Engineers
Treat standard Metropolis-Hastings as a mathematically elegant but often too-slow method, preferring gradient-based variants for massive datasets.

Perspectives this story doesn't cover

  • Frequentist Statisticians
  • Hardware Optimization Engineers

Sources

Source coverage

9 outlets

3 viewpoints surfaced

Bayesian Statisticians 40%Computational Scientists 35%Machine Learning Engineers 25%
  1. [1]StatLect

    Metropolis Hastings algorithm

    Read on StatLect →
  2. [2]PMC - NIH

    A simple introduction to Markov Chain Monte–Carlo sampling

    Read on PMC - NIH →
  3. [3]arXivComputational Scientists

    The Metropolis–Hastings algorithm

    Read on arXiv →
  4. [4]Notes from a data witchBayesian Statisticians

    The Metropolis Hastings Algorithm

    Read on Notes from a data witch →
  5. [5]Gregory GundersenBayesian Statisticians

    Why Metropolis–Hastings Works

    Read on Gregory Gundersen →
  6. [6]Stats StackExchangeMachine Learning Engineers

    What is the intuition behind the Metropolis-Hastings Algorithm?

    Read on Stats StackExchange →
  7. [7]ToptalMachine Learning Engineers

    MCMC Methods: Metropolis-Hastings and Bayesian Inference

    Read on Toptal →
  8. [8]Bounded RationalityComputational Scientists

    Markov Chain Monte Carlo Methods, Rejection Sampling and the Metropolis-Hastings Algorithm

    Read on Bounded Rationality →
  9. [9]Factlen Editorial Team

    Synthesis by Factlen editorial team

    Read on Factlen Editorial Team →
Listen

Comments

Stay informed

Every angle. Every day.

Get Data & Analysis stories with full source coverage and perspective breakdowns, free every day.