Lesson 2: Markov Chain Monte Carlo (MCMC)

6–10 minutes

Understanding Metropolis-Hastings and Gibbs Sampling from First Principles

Learning Objectives

By the end of this lesson, you will:

  • Understand why MCMC was invented.
  • Understand what a Markov Chain is.
  • Understand why sampling solves Bayesian inference.
  • Understand the logic behind the Metropolis-Hastings acceptance ratio.
  • Learn Gibbs Sampling from first principles.
  • Implement both algorithms in Python.
  • Apply them to healthcare, epidemiology, and supply chain problems.

Why Do We Need MCMC?

Recall Bayes’ theorem:

p(θ|D)p(D|θ)p(θ)p(D|θ)p(θ)dθ p(\theta|D) \frac {p(D|\theta)p(\theta)} {\int p(D|\theta)p(\theta)d\theta}

The challenge is usually the denominator:

p(D|θ)p(θ)dθ\int p(D|\theta)p(\theta)d\theta

For most realistic Bayesian models, this integral cannot be evaluated analytically.

Examples include:

Healthcare:

θ=treatment effect \theta= \text{treatment effect}

Epidemiology:

θ=disease transmission rate \theta= \text{disease transmission rate}

Supply Chain:

θ=future demand parameter \theta=\text{future demand parameter}

Instead of solving the integral directly, MCMC asks a different question:

Can we generate samples from the posterior distribution?

If we can generate:

[θ(1),θ(2),,θ(N)][ \theta^{(1)}, \theta^{(2)}, \dots, \theta^{(N)} ]

from

p(θ|D)p(\theta|D)

then almost every Bayesian quantity can be estimated using simple averages.


The Big Idea Behind MCMC

Suppose we want the posterior mean.

Mathematically:

E(θ|D)=θp(θ|D)dθ E(\theta|D)=\int \theta p(\theta|D)d\theta

If we have posterior samples:

θ(1),θ(2),,θ(N)\theta^{(1)}, \theta^{(2)}, \dots, \theta^{(N)}

then:

E(θ|D)1Ni=1Nθ(i)E(\theta|D) \approx \frac1N \sum_{i=1}^{N} \theta^{(i)}

Similarly:

Posterior variance:

Var(θ|D)1Ni=1N(θ(i)θ)2 Var(\theta|D) \approx \frac1N \sum_{i=1}^{N} (\theta^{(i)}-\bar{\theta})^2

Posterior probability:

P(θ>0|D)#(θ(i)>0)NP(\theta>0\mid D) \approx \frac{\#(\theta^{(i)}>0)}{N}

The entire Bayesian problem becomes:

Generate samples from the posterior distribution.


What Is a Markov Chain?

A Markov Chain is a sequence:

[X1,X2,X3,][ X_1,X_2,X_3,\dots ]

satisfying:

P(Xt+1|Xt,Xt1,)=P(Xt+1|Xt) P(X_{t+1}|X_t,X_{t-1},\dots)= P(X_{t+1}|X_t)

The future depends only on the present.

It does not depend on the entire history.

Healthcare example:

Suppose a patient can be:

  • Healthy
  • Sick

The patient’s health tomorrow depends mostly on today’s condition.

This is a Markov process.


The Goal of MCMC

Instead of sampling directly from

p(θ|D)p(\theta|D)

which is difficult,

we construct a Markov chain whose long-run distribution is

p(θ|D) p(\theta|D)

Then after running the chain long enough:

θ(t)p(θ|D)\theta^{(t)} \sim p(\theta|D)

This is the central idea behind MCMC.


Metropolis-Hastings Algorithm

Metropolis-Hastings is the most widely used MCMC algorithm.

The algorithm repeatedly:

  1. Proposes a move.
  2. Decides whether to accept it.
  3. Repeats.

Over time the chain spends the correct proportion of time in each region of the posterior.


Step 1: Start Somewhere

Suppose our current value is:

θt \theta_t

For example:

θt=2\theta_t=2

The starting point is usually arbitrary.


Step 2: Generate a Proposal

We generate a candidate value:

θq(θ|θt)\theta^* \sim q(\theta^*|\theta_t)

A common choice is:

θN(θt,σ2)\theta^* \sim N(\theta_t,\sigma^2)

This means we randomly explore nearby values.

Suppose:

θ=2.4 \theta^*=2.4

Step 3: Decide Whether To Move

This is where the brilliance of Metropolis-Hastings appears.

We compute:

r=p(θ|D)p(θt|D)r=\frac {p(\theta^*|D)} {p(\theta_t|D)}


for symmetric proposal distributions.

Accept with probability:

min(1,r)\min(1,r)

Why Do We Use This Ratio?

This is one of the most important questions in Bayesian computation.

Suppose:

Current location:

p(θt|D)=0.10 p(\theta_t|D)=0.10

Proposed location:

p(θ|D)=0.50 p(\theta^*|D)=0.50

Then:

[r=0.500.10=5][ r= \frac{0.50}{0.10}= 5 ]

Since:

r>1 r>1

we automatically accept.

Why?

Because the proposal lies in a region that is five times more probable under the posterior.

The chain should spend more time there.


Why Not Always Reject Worse Points?

Suppose:

Current location: 0.50
Proposed location: 0.25

Then:

r=0.250.50=0.50 r= \frac{0.25}{0.50} =0.50

Accept with probability: 50%

At first this seems strange.

Why move to a less likely location?

Because if we only accepted better moves, the chain could become trapped.

Imagine a posterior with two peaks.

If the chain reaches one peak, it may never leave.

Allowing occasional downhill moves enables exploration of the entire posterior distribution.


The Real Metropolis-Hastings Ratio

For asymmetric proposal distributions:

r=p(θ|D)q(θt|θ)p(θt|D)q(θ|θt) r=\frac {p(\theta^*|D) q(\theta_t|\theta^*)} {p(\theta_t|D) q(\theta^*|\theta_t)}

The extra terms compensate for proposal bias.

For symmetric proposals:

q(θt|θ)=q(θ|θt)q(\theta_t|\theta^*)=q(\theta^*|\theta_t)

and they cancel.


Why Is MCMC So Powerful?

Notice:

p(θ|D)=p(D|θ)p(θ)p(D) p(\theta|D)=\frac {p(D|\theta)p(\theta)} {p(D)}

Substituting into the acceptance ratio:

r=p(D|θ)p(θ)p(D)p(D|θt)p(θt)p(D) r=\frac {\frac{p(D|\theta^*)p(\theta^*)}{p(D)}} {\frac{p(D|\theta_t)p(\theta_t)}{p(D)}}

The difficult term: p(D)p(D)cancels completely.

This is the key reason MCMC became so successful.

The hardest integral in Bayesian statistics never needs to be computed.


Example 1: Healthcare — Drug Effectiveness

Suppose:

θ=average reduction in blood pressure\theta=\text{average reduction in blood pressure}

Posterior:

p(θ|D)p(\theta|D)

Goal:

P(θ>0|D)P(\theta>0|D)

Generate 50,000 Metropolis-Hastings samples.

Estimate:

P(θ>0|D)#(θ(i)>0)50000P(\theta>0\mid D) \approx \frac{\#(\theta^{(i)}>0)}{50000}

This gives the probability that the treatment truly improves patient outcomes.


Example 2: Epidemiology

Suppose:

[β=infection transmission rate][ \beta=\text{infection transmission rate} ]

Posterior:

p(β|D)p(\beta|D)

Goal:

P(β>1)P(\beta>1)

Values above one indicate an expanding epidemic.

Metropolis-Hastings samples provide the answer immediately.


Example 3: Supply Chain Forecasting

Suppose:

λ=monthly demand rate\lambda=\text{monthly demand rate}

Posterior:

p(λ|Sales)p(\lambda|Sales)

Generate posterior samples:

λ(1),,λ(50000)\lambda^{(1)}, \dots, \lambda^{(50000)}

Then estimate:

P(Demand>Inventory)P(Demand>Inventory)

using simulation.


Python Example: Metropolis-Hastings

import numpy as np

def target(x):
    return np.exp(-x**2/2)

n = 10000

theta = 0

samples = []

for i in range(n):

    proposal = np.random.normal(
        theta,
        1
    )

    r = target(proposal) / target(theta)

    if np.random.rand() < min(1, r):
        theta = proposal

    samples.append(theta)

samples = np.array(samples)

Understanding Gibbs Sampling

Metropolis-Hastings is very general.

Gibbs Sampling is a special case that becomes extremely efficient when conditional distributions are available.


The Core Idea

Suppose we have two parameters θ1\theta_1 and θ2\theta_2

The joint posterior:

p(θ1,θ2|D)p(\theta_1,\theta_2|D)

may be difficult.

However, suppose we know:

p(θ1|θ2,D)p(\theta_1|\theta_2,D)

and

p(θ2|θ1,D)p(\theta_2|\theta_1,D)

These conditional distributions may be easy to sample from.


Gibbs Sampling Algorithm

Start with:

(θ1(0),θ2(0))(\theta_1^{(0)},\theta_2^{(0)})

Update:

θ1(1)p(θ1|θ2(0),D) \theta_1^{(1)} \sim p(\theta_1|\theta_2^{(0)},D)

Then:

θ2(1)p(θ2|θ1(1),D) \theta_2^{(1)} \sim p(\theta_2|\theta_1^{(1)},D)

Repeat indefinitely.


Why Does Gibbs Sampling Work?

Imagine moving around a mountain.

Metropolis-Hastings jumps randomly.

Gibbs moves one coordinate at a time.

Update

θ1\theta_1

Then update:

θ2 \theta_2

Then update:

θ1\theta_1

again.

Eventually the chain explores the entire posterior.


Why Is There No Acceptance Ratio?

Because every draw already comes from the correct conditional distribution.

Every proposed value is automatically valid.

Mathematically:

r=1 r=1

for every iteration.

Nothing is rejected.


Example 1: Blood Pressure Model

Parameters:

μ=population mean blood pressure,σ2=population variance\mu=\text{population mean blood pressure} , \\ \sigma^2=\text{population variance}

Posterior:

p(μ,σ2|D)p(\mu,\sigma^2|D)

Update:

p(μ|σ2,D)p(\mu|\sigma^2,D)

then

p(σ2|μ,D)p(\sigma^2|\mu,D)

Example 2: Hospital Readmission Model

Parameters:

β0\beta_0


and

β1 \beta_1

Posterior:

p(β0,β1|D) p(\beta_0,\beta_1|D)

Update:

β0|β1,D \beta_0|\beta_1,D

then

β1|β0,D \beta_1|\beta_0,D

Example 3: Supply Chain Forecasting

Parameters:

λ=demand rate \lambda=\text{demand rate}
α=seasonality effect \alpha=\text{seasonality effect}

Posterior:

p(λ,α|Sales)p(\lambda,\alpha|Sales)

Update each parameter conditionally.

Generate future demand forecasts using posterior samples.


Python Example: Gibbs Sampling

import numpy as np

n = 10000

x = 0
y = 0

samples = []

for i in range(n):

    x = np.random.normal(
        y,
        1
    )

    y = np.random.normal(
        x,
        1
    )

    samples.append([x, y])

samples = np.array(samples)

Metropolis-Hastings vs Gibbs Sampling

FeatureMetropolis-HastingsGibbs Sampling
Requires conditional distributionsNoYes
Acceptance ratioYesNo
Every proposal acceptedNoYes
General-purposeYesNo
Easier to applyYesSometimes
More efficient when conditionals knownNoYes

Key Takeaways

MCMC solves Bayesian inference by generating samples from the posterior distribution.

Metropolis-Hastings uses the acceptance ratio:

r=p(θ|D)p(θt|D) r=\frac {p(\theta^*|D)} {p(\theta_t|D)}

to ensure the chain visits regions according to their posterior probability.

The normalizing constant:

p(D) p(D)

cancels from the acceptance ratio, making Bayesian inference feasible.

Gibbs Sampling is a special case of MCMC where we sample directly from conditional distributions.

Because each sample already comes from the correct conditional distribution:

r=1 r=1

and every proposal is accepted.

Together, Metropolis-Hastings and Gibbs Sampling form the foundation of modern Bayesian computation and lead directly to advanced methods such as Hamiltonian Monte Carlo, NUTS, Sequential Monte Carlo, Bayesian Nonparametrics, and Bayesian Machine Learning.


References

  • Gelman, Carlin, Stern, Dunson, Vehtari, and Rubin. Bayesian Data Analysis.
  • Robert and Casella. Monte Carlo Statistical Methods.
  • Brooks, Gelman, Jones, and Meng. Handbook of Markov Chain Monte Carlo.
  • Murphy. Machine Learning: A Probabilistic Perspective.

Leave a Reply

Discover more from nerd-ish

Subscribe now to keep reading and get access to the full archive.

Continue reading