Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Part 3: Markov Chain Monte Carlo

The Statistical Universe | Module 5 | COMP 521

“It is a capital mistake to theorize before one has data. Insensibly one begins to twist facts to suit theories, instead of theories to suit facts.”
— Arthur Conan Doyle, A Scandal in Bohemia

“The purpose of computing is insight, not numbers.”
— Richard Hamming


Learning Outcomes

By the end of Part 3, you will be able to:

Prerequisites: Parts 1-2 of this module (measurement philosophy, Bayes’ theorem), Module 1 (sampling distributions, CLT, ergodicity), basic Python.


Roadmap: Where We’re Going

Part 2 established the epistemic object of Bayesian inference: not a best-fit parameter, but a posterior distribution over plausible parameter values given data and assumptions. Part 3 asks the next scientific question: how do we compute scientifically meaningful quantities from that posterior when direct integration is impossible?

In course language, the logic is now:

MCMC does not replace inference. It is the computational bridge that makes Bayesian inference scientifically usable.

This lecture should be read through one computational chain:

We want E[f(θ)∣D]\text{We want } \mathbb{E}[f(\theta)\mid D]
⇓\Downarrow
This requires integrating over p(θ∣D)\text{This requires integrating over } p(\theta \mid D)
⇓\Downarrow
This is intractable\text{This is intractable}
⇓\Downarrow
So we need samples\text{So we need samples}
⇓\Downarrow
So we must construct a sampler\text{So we must construct a sampler}

The roadmap below is the same logic in flow-chart form:

The narrative arc: expectation as the scientific target, integration as the computational obstacle, sampling as the workaround, sampler construction as the algorithmic task, and diagnostics as the argument for trust. The goal is not just to memorize Metropolis-Hastings. The goal is to understand why posterior sampling is the natural computational continuation of Bayesian inference.

This isn’t just a numerical method. It’s a fundamentally different way of thinking about inference that emerged from mid-20th century physics and revolutionized statistics, machine learning, and computational science.


Part 1: The Computational Crisis — Why We Need MCMC

The Posterior is a Distribution, Not a Number

In Part 2, you learned Bayes’ theorem:

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

where

The posterior p(θ∣D)p(\theta \mid D) is a probability distribution over the parameter space. It tells you, for every possible value of θ\theta, how probable that value is given the data you observed.

From this point onward, we will use

π(θ)≡p(θ∣D)\pi(\theta) \equiv p(\theta \mid D)

as shorthand for the target distribution. This is only a change in notation, not a new object.

In the course language:

Let’s be concrete. Suppose you’re measuring the distance to a Cepheid variable star. The point here is not just to name the pieces, but to line them up in the exact order you would need for implementation.

A Complete Inference Pipeline You Could Reconstruct

Take a Cepheid with measured period P=10.0 daysP = 10.0~\mathrm{days} and observed apparent magnitude mobs=18.50±0.15 magm_{\mathrm{obs}} = 18.50 \pm 0.15~\mathrm{mag}.

1. Observable

The data are the quantities we actually measured:

2. Forward model

The Leavitt law gives the absolute magnitude:

MV(P)=−2.43log⁡10(P/10 days)−4.05M_V(P) = -2.43 \log_{10}(P / 10~\mathrm{days}) - 4.05

and the distance modulus gives the predicted apparent magnitude at distance dd:

mmodel(d)=MV(P)+5log⁡10(d/10 pc)m_{\mathrm{model}}(d) = M_V(P) + 5 \log_{10}(d / 10~\mathrm{pc})

This is the physics step: parameters →\to predicted observables.

3. Noise model

For this lecture’s 1D toy problem, we assume:

That assumption becomes

mobs=mmodel(d)+ϵ,ϵ∼N(0,σm2).m_{\mathrm{obs}} = m_{\mathrm{model}}(d) + \epsilon, \qquad \epsilon \sim \mathcal{N}(0, \sigma_m^2).

4. Likelihood

Those assumptions imply

p(mobs∣d,P)=N ⁣(mobs;mmodel(d),σm2)∝exp⁡ ⁣[−(mobs−mmodel(d))22σm2].p(m_{\mathrm{obs}} \mid d, P) = \mathcal{N}\!\left(m_{\mathrm{obs}}; m_{\mathrm{model}}(d), \sigma_m^2\right) \propto \exp\!\left[ -\frac{\left(m_{\mathrm{obs}} - m_{\mathrm{model}}(d)\right)^2}{2\sigma_m^2} \right].

5. Posterior

Once we choose a prior p(d)p(d), Bayes gives

p(d∣mobs,P)∝p(mobs∣d,P) p(d).p(d \mid m_{\mathrm{obs}}, P) \propto p(m_{\mathrm{obs}} \mid d, P)\,p(d).

6. MCMC target

This posterior is exactly the quantity MCMC will target:

π(d)≡p(d∣mobs,P).\pi(d) \equiv p(d \mid m_{\mathrm{obs}}, P).

The posterior p(d∣mobs,P)p(d \mid m_{\mathrm{obs}}, P) tells you not just the “best” distance, but the full probability distribution over possible distances. This captures:

In one dimension, you could still evaluate p(d∣mobs,P)p(d \mid m_{\mathrm{obs}}, P) on a grid of distance values and plot it. That is useful pedagogically because it is the last case where brute force still feels easy.

But the moment the physics becomes slightly more realistic, the problem stops being one-dimensional.

If extinction AVA_V is also unknown, then the forward model becomes

mmodel(d,AV)=MV(P)+5log⁡10(d/10 pc)+AV.m_{\mathrm{model}}(d, A_V) = M_V(P) + 5 \log_{10}(d / 10~\mathrm{pc}) + A_V.

Now the same observed brightness can be explained by a closer, dustier Cepheid or a farther, cleaner one. That is a genuine physical degeneracy, not a numerical inconvenience.

Distance-extinction degeneracy for a Cepheid-like inference problem. The elongated ridge shows that many combinations of distance and extinction can reproduce the same brightness. This is the first place students should feel why "just evaluate the posterior on a grid" stops being a satisfying answer: the physics already creates a structured, correlated posterior in only two parameters.{width=“100%” #fig-mcmc-degeneracy-2d}

Even this toy extension becomes expensive quickly. A grid with 400 points in dd and 400 points in AVA_V already needs 1.6×1051.6 \times 10^5 likelihood evaluations. Add metallicity, intrinsic scatter, a calibration offset, and one nuisance parameter for each observed star, and brute force becomes the bottleneck.

The Curse of Dimensionality

Imagine you want to map out a posterior distribution by evaluating it on a grid. How many grid points do you need?

1D example: To resolve a 1D posterior to reasonable accuracy, you might need N=100N = 100 grid points.

2D example: Now you have two parameters. You need 100×100=104100 \times 100 = 10^4 evaluations.

3D example: Three parameters →\to 100×100×100=106100 \times 100 \times 100 = 10^6 evaluations.

Scaling: For dd dimensions, you need NdN^d evaluations. This is exponential scaling — the curse of dimensionality.

Let’s make this concrete with realistic numbers:

DimensionsGrid points per dimensionTotal evaluationsTime (assuming 1 ms per eval)
11001020.1 seconds
210010410 seconds
310010616 minutes
51001010115 days
1010010203 trillion years
201001040Age of universe ×1023\times 10^{23}

Even “small” problems with 5-10 parameters become computationally impossible. And many real scientific problems have hundreds or thousands of parameters:

Grid evaluation simply cannot work.

What About Optimization? Just Find the Peak

You might think: “Why do I need the full distribution? Just find the maximum of p(θ∣D)p(\theta \mid D) and report that.”

That reasoning is appealing because optimization is familiar and computationally simpler. But sampling and optimization answer different epistemic questions.

The second question is scientifically richer because uncertainty, asymmetry, and parameter tradeoffs are often part of the result, not peripheral details.

This is maximum a posteriori (MAP) estimation, and it’s sometimes useful. But you lose crucial information:

1. Uncertainty quantification: The peak tells you nothing about how uncertain you are. Is it a sharp peak (confident) or broad (uncertain)? If you report θ=5.0\theta = 5.0 as the MAP estimate, can you distinguish whether your uncertainty is ±0.1\pm 0.1 or ±10\pm 10? The peak doesn’t tell you.

2. Asymmetries: The peak might be at d=500d = 500 pc, but the distribution could be skewed — maybe 500±20500 \pm 20 pc toward smaller distances, but +100 pc toward larger distances (imagine a long tail from dust extinction uncertainty).

3. Correlations: With multiple parameters, knowing the peak doesn’t tell you about degeneracies. Maybe radius and temperature are correlated — high R and low T give the same luminosity as low R and high T.

4. Model comparison: The evidence p(D)p(D) (the denominator in Bayes’ theorem) requires integrating over all parameter values. The peak tells you nothing about this.

5. Propagating uncertainty: If you use your inferred parameters to predict something else, you need the full distribution to get realistic error bars.

The scientific standard: Modern quantitative science does not stop at “best fit” values. It reports credible intervals and posterior structure. Bayesian posteriors do this directly.

A posterior is a full inferential object, not just a location of maximum probability. Prior, likelihood, and posterior combine into a curve with width and shape. A MAP estimate would choose one location on this curve, but the scientific answer lives in the entire posterior because uncertainty, asymmetry, and later predictions all depend on that full structure.{width=“100%” #fig-mcmc-posterior-full-object}

What Inference Actually Requires

Now we can state the computational target explicitly.

Everything in the rest of the lecture is in service of the same chain:

E[f(θ)∣D]  →  ∫f(θ) p(θ∣D) dθ  →  intractable integral  →  samples  →  sampler construction.\mathbb{E}[f(\theta)\mid D] \;\to\; \int f(\theta)\,p(\theta \mid D)\,d\theta \;\to\; \text{intractable integral} \;\to\; \text{samples} \;\to\; \text{sampler construction}.

In science, we rarely want the posterior curve itself. We want derived quantities:

All of these can be written as posterior expectations or posterior integrals. For any function f(θ)f(\theta),

E[f(θ)∣D]=∫f(θ) π(θ) dθ\mathbb{E}[f(\theta) \mid D] = \int f(\theta)\,\pi(\theta)\,d\theta

is the general quantity we want to compute.

For the Cepheid problem, examples include:

This is the conceptual hinge of the lecture:

At this point, all of the standard strategies have failed:

So MCMC is not merely a convenient tool we happen to like. Once direct integration, dense grids, and direct iid (independent and identically distributed) sampling are off the table, constructing a sampler becomes the natural general-purpose strategy if we still want posterior expectations. The natural general-purpose option is to construct a process whose long-run behavior reproduces the posterior. That conceptual move is what defines MCMC.

The Monte Carlo Solution: Sample, Don’t Integrate

Here’s where the inference becomes computational. If we had iid samples θ1,θ2,…,θN\theta_1, \theta_2, \ldots, \theta_N drawn from the posterior, then the intractable integral above would become a sample average:

E[f(θ)∣D]=∫f(θ) π(θ) dθ≈1N∑i=1Nf(θi).\mathbb{E}[f(\theta) \mid D] = \int f(\theta)\,\pi(\theta)\,d\theta \approx \frac{1}{N}\sum_{i=1}^N f(\theta_i).

This is the Monte Carlo idea: replace integration with averaging over samples. It works because an expectation under π(θ)\pi(\theta) is, by definition, an average of f(θ)f(\theta) weighted by that distribution; Monte Carlo replaces the exact weighted integral with an empirical average over draws from the same distribution.

If you have NN iid samples {θ1,θ2,…,θN}\{\theta_1, \theta_2, \ldots, \theta_N\} drawn from p(θ∣D)p(\theta \mid D), then you can estimate many posterior quantities:

Mean:

⟨θ⟩≈1N∑i=1Nθi\langle \theta \rangle \approx \frac{1}{N} \sum_{i=1}^N \theta_i

Variance:

Var(θ)≈1N∑i=1N(θi−⟨θ⟩)2\text{Var}(\theta) \approx \frac{1}{N} \sum_{i=1}^N (\theta_i - \langle \theta \rangle)^2

Credible intervals: Sort the samples and find the 16th and 84th percentiles for a 68% credible interval.

Predictions: For any function f(θ)f(\theta), estimate its expectation:

⟨f(θ)⟩≈1N∑i=1Nf(θi)\langle f(\theta) \rangle \approx \frac{1}{N} \sum_{i=1}^N f(\theta_i)

The key quantitative fact: For the Monte Carlo estimator

μ^f=1N∑i=1Nf(θi)\hat{\mu}_f = \frac{1}{N}\sum_{i=1}^N f(\theta_i)

with θi∼iidπ(θ)\theta_i \stackrel{\text{iid}}{\sim} \pi(\theta) and σf2=Var[f(θ)]\sigma_f^2 = \mathrm{Var}[f(\theta)], the estimator variance is

Var(μ^f)=σf2N,\mathrm{Var}(\hat{\mu}_f) = \frac{\sigma_f^2}{N},

so the standard error — the standard deviation of the estimator itself — is

SE(μ^f)=σfN.\mathrm{SE}(\hat{\mu}_f) = \frac{\sigma_f}{\sqrt{N}}.

The distinction matters:

The Central Limit Theorem implies that the sampling distribution of the estimator has variance σf2/N\sigma_f^2/N, so its standard deviation (the standard error) scales as σf/N\sigma_f/\sqrt{N}.

To gain one more decimal place of accuracy, you need about 100×100 \times more samples. That is slow convergence. But it is still fundamentally better than dense-grid integration in high dimensions, where the cost typically grows exponentially with the number of parameters.

This is revolutionary. Grid evaluation scales exponentially with dimension (Nd)(N^d). By contrast, iid Monte Carlo has an O(N−1/2)O(N^{-1/2}) convergence rate that does not itself worsen exponentially with dimension.

But here’s the catch: We do not have a machine that hands us independent samples from π(θ)=p(θ∣D)\pi(\theta) = p(\theta \mid D). We can evaluate the posterior at points we choose, but evaluation is not the same as sampling.

So the problem changes form:

How do we generate samples from a distribution we can evaluate but cannot sample from directly?

This is where Markov Chain Monte Carlo enters. It’s a method to generate samples from a very broad class of target distributions by constructing a Markov chain that explores parameter space.


Part 2: Markov Chains — The Mathematical Foundation

The Core Idea: Build a Stochastic Process

Because direct independent sampling from π(θ)=p(θ∣D)\pi(\theta) = p(\theta \mid D) is unavailable, we replace it with a stochastic process that produces dependent samples whose long-run frequencies still match the posterior.

This is the key conceptual relaxation. If independent samples are impossible, then we must allow dependence between successive samples. The simplest structure that introduces dependence without becoming hopelessly complicated is a Markov process: the next state depends only on the current one.

That is why Markov chains appear here. They are not arbitrary decoration. They are the minimal relaxation of independence that still gives us a tractable way to construct a sampler.

Instead of trying to sample directly from p(θ∣D)p(\theta \mid D), we’ll build a stochastic process (a Markov chain) that:

  1. Starts from any initial θ0\theta_0

  2. Takes random steps through parameter space

  3. Gradually “forgets” where it started

  4. Eventually produces samples distributed according to p(θ∣D)p(\theta \mid D)

Think of it like a random walker wandering around parameter space. We’ll design the rules of walking so that after sufficient wandering, the walker spends time in each region proportional to p(θ∣D)p(\theta \mid D).

If p(θ∣D)p(\theta \mid D) is high in some region, the walker visits often. If p(θ∣D)p(\theta \mid D) is low, the walker rarely goes there. The stationary distribution of the random walk is the posterior.

Markov Chains: Formal Definition

A Markov chain is not yet an inference method. It is a type of stochastic process. We choose it because it is the simplest sequential mechanism rich enough to produce dependent samples with a controllable long-run distribution.

Its defining property is simple:

the next state depends only on the current state, not on the full past history.

In symbols,

p(θt+1∣θt,θt−1,θt−2,...,θ0)=p(θt+1∣θt)p(\theta_{t+1} | \theta_t, \theta_{t-1}, \theta_{t-2}, ..., \theta_0) = p(\theta_{t+1} | \theta_t)

This is the Markov property: the future depends on the present, not the past. Once that idea is clear, we can introduce the next crucial object: the transition kernel T(θ′∣θ)T(\theta' \mid \theta), which tells us how the chain moves through parameter space.

Transition Kernels

The dynamics of a Markov chain are defined by the transition kernel (or transition probability):

T(θ′∣θ)T(\theta' | \theta)

This is the probability density of moving to θ′\theta' given that you’re currently at θ\theta. It must satisfy:

∫T(θ′∣θ) dθ′=1\int T(\theta' | \theta) \, d\theta' = 1

for all θ\theta (probabilities must sum to 1).

Given a starting distribution p0(θ)p_0(\theta), the distribution after one step is:

p1(θ′)=∫p0(θ)T(θ′∣θ) dθp_1(\theta') = \int p_0(\theta) T(\theta' | \theta) \, d\theta

After tt steps:

pt(θ′)=∫pt−1(θ)T(θ′∣θ) dθp_t(\theta') = \int p_{t-1}(\theta) T(\theta' | \theta) \, d\theta

Question: Can we design TT such that pt(θ)p_t(\theta) converges to our target distribution π(θ)=p(θ∣D)\pi(\theta) = p(\theta \mid D) as t→∞t \to \infty?

Answer: Yes! But we need two conditions:

  1. Stationarity: The target distribution π\pi must be stationary with respect to TT.

  2. Ergodicity: The chain must be able to explore the entire parameter space.

Stationarity: The Distribution Must Be Preserved

A distribution π(θ)\pi(\theta) is stationary with respect to TT if applying the transition kernel doesn’t change it:

∫π(θ)T(θ′∣θ) dθ=π(θ′)\int \pi(\theta) T(\theta' | \theta) \, d\theta = \pi(\theta')

What it is: If the Markov chain is distributed according to π\pi, then after one more step, it’s still distributed according to π\pi. The distribution is an equilibrium of the dynamics.

Physical analogy: A gas in thermal equilibrium. Molecules are constantly moving and colliding, but the overall distribution (Boltzmann distribution) doesn’t change. Individual states change, but the probability distribution is stationary.

These two ideas play different roles:

A chain can have the correct stationary distribution on paper and still be scientifically useless if it cannot explore the relevant regions of parameter space.

Ergodicity: The Chain Must Explore Everywhere

Stationarity means π\pi is preserved if you start from it. But how do we get there from an arbitrary starting point θ0\theta_0?

We need ergodicity: The chain must be able to reach any state from any other state in finite time. Formally, ergodicity requires:

1. Irreducibility: Every state is accessible from every other state (there are no isolated regions).

2. Aperiodicity: The chain doesn’t get stuck in cycles. (E.g., don’t alternate deterministically: left, right, left, right forever.)

The Ergodic Theorem: If a Markov chain is ergodic and has stationary distribution π\pi, then for any function f(θ)f(\theta):

lim⁡N→∞1N∑t=1Nf(θt)=∫f(θ)π(θ) dθ\lim_{N \to \infty} \frac{1}{N} \sum_{t=1}^N f(\theta_t) = \int f(\theta) \pi(\theta) \, d\theta

Time averages along the chain converge to expectations under π\pi. This is how MCMC works: run the chain for a long time, then use the samples to estimate expectations.

Detailed Balance: A Sufficient Condition for Stationarity

At this point we need a design test. A chain that merely “moves around” parameter space is not enough. A random walk could easily settle into the wrong distribution. We need a condition we can verify mathematically that guarantees the chain targets the right long-run distribution.

Here is the failure mode to keep in mind. Suppose we design a random walk that proposes steps and always accepts them. It will certainly explore parameter space, but it will not generally spend time in each region proportional to π(θ)\pi(\theta). In fact, for a symmetric proposal on a bounded space, it tends to behave much more like uniform exploration than posterior sampling.

So “motion” is not enough. We need a correction mechanism that reshapes the dynamics so the chain has the right equilibrium distribution. Detailed balance is the first clean way to express that requirement mathematically.

Detailed balance is not the only possible route to stationarity, but it is a powerful sufficient condition because it is local, checkable, and easy to build into an algorithm.

We need to design a transition kernel TT that has π\pi as its stationary distribution. There’s a powerful sufficient condition called detailed balance (also called microscopic reversibility):

π(θ)T(θ′∣θ)=π(θ′)T(θ∣θ′)\pi(\theta) T(\theta' | \theta) = \pi(\theta') T(\theta | \theta')

In words: the probability flow from θ\theta to θ′\theta' equals the flow from θ′\theta' back to θ\theta when both are drawn from π\pi.

Why does detailed balance imply stationarity? Here’s the key insight:

Detailed balance is a sufficient condition for stationarity. To see why, assume detailed balance holds and integrate both sides over θ\theta:

∫π(θ)T(θ′∣θ) dθ=∫π(θ′)T(θ∣θ′) dθ\int \pi(\theta) T(\theta' | \theta) \, d\theta = \int \pi(\theta') T(\theta | \theta') \, d\theta

The right side factors since π(θ′)\pi(\theta') doesn’t depend on the integration variable:

RHS=π(θ′)∫T(θ∣θ′) dθ=π(θ′)×1=π(θ′)\text{RHS} = \pi(\theta') \int T(\theta | \theta') \, d\theta = \pi(\theta') \times 1 = \pi(\theta')

(The integral equals 1 because T is a probability density in its first argument.)

The left side is exactly the definition of how the distribution evolves forward one step:

LHS=∫π(θ)T(θ′∣θ) dθ\text{LHS} = \int \pi(\theta) T(\theta' | \theta) \, d\theta

This integral gives you the probability at θ′\theta' after one transition starting from π\pi. Since LHS=RHS=π(θ′)\text{LHS} = \text{RHS} = \pi(\theta'), we’ve shown that starting from distribution π\pi produces distribution π\pi after one step. The distribution is stationary.

Now we have our blueprint:

  1. Choose a target distribution π(θ)=p(θ∣D)\pi(\theta) = p(\theta \mid D).

  2. Design a transition kernel TT that satisfies detailed balance with respect to π\pi.

  3. The Markov chain will converge to π\pi (if ergodic).

  4. Run the chain, collect samples, estimate expectations

The question: How do we actually construct such a TT? That’s where Metropolis-Hastings comes in.


Part 3: The Metropolis-Hastings Algorithm

The Construction: Proposal + Accept/Reject

Here’s the brilliant idea due to Metropolis et al. (1953) and generalized by Hastings (1970):

Separate the transition kernel into two parts:

  1. Proposal distribution Q(θ′∣θ)Q(\theta' \mid \theta): generates a candidate new state θ′\theta' given current state θ\theta

  2. Acceptance probability α(θ′∣θ)\alpha(\theta' \mid \theta): probability of accepting the proposal

For θ′≠θ\theta' \neq \theta, the accepted-move component of the transition kernel is:

T(θ′∣θ)=Q(θ′∣θ) α(θ′∣θ)T(\theta' \mid \theta) = Q(\theta' \mid \theta)\,\alpha(\theta' \mid \theta)

The full kernel also includes a self-transition at θ′=θ\theta' = \theta that collects the rejected proposals. In other words, “reject and stay where you are” is part of the Markov dynamics itself, not an implementation afterthought.

The algorithm:

  1. Start at some θ0\theta_0 (anywhere in parameter space)

  2. For t=0,1,2,...,N−1t = 0, 1, 2, ..., N-1:

    • Propose: Draw θ′\theta' from Q(θ′∣θt)Q(\theta' \mid \theta_t)

    • Evaluate: Compute acceptance probability α(θ′∣θt)\alpha(\theta' \mid \theta_t)

    • Decide:

      • With probability α\alpha, accept: θt+1=θ′\theta_{t+1} = \theta'

      • With probability 1−α1-\alpha, reject: θt+1=θt\theta_{t+1} = \theta_t (stay put)

  3. Return samples {θ1,θ2,…,θN}\{\theta_1, \theta_2, \ldots, \theta_N\}

The magic is in step 2: How do we choose α\alpha to satisfy detailed balance?

Deriving the Acceptance Probability

We want:

π(θ)T(θ′∣θ)=π(θ′)T(θ∣θ′)\pi(\theta) T(\theta' | \theta) = \pi(\theta') T(\theta | \theta')

Substituting T=QαT = Q\alpha where QQ is the proposal and α\alpha is the acceptance probability:

π(θ)Q(θ′∣θ)α(θ′∣θ)=π(θ′)Q(θ∣θ′)α(θ∣θ′)\pi(\theta) Q(\theta' | \theta) \alpha(\theta' | \theta) = \pi(\theta') Q(\theta | \theta') \alpha(\theta | \theta')

Rearrange:

α(θ′∣θ)α(θ∣θ′)=π(θ′)Q(θ∣θ′)π(θ)Q(θ′∣θ)\frac{\alpha(\theta' | \theta)}{\alpha(\theta | \theta')} = \frac{\pi(\theta') Q(\theta | \theta')}{\pi(\theta) Q(\theta' | \theta)}

Define the acceptance ratio:

r=π(θ′)Q(θ∣θ′)π(θ)Q(θ′∣θ)r = \frac{\pi(\theta') Q(\theta | \theta')}{\pi(\theta) Q(\theta' | \theta)}

When π\pi is the posterior: write

π(θ)∝p(D∣θ)p(θ).\pi(\theta) \propto p(D\mid\theta)p(\theta).

Then

r  =  π(θ′) Q(θ∣θ′)π(θ) Q(θ′∣θ)  =  p(D∣θ′) p(θ′)p(D∣θ) p(θ)⋅Q(θ∣θ′)Q(θ′∣θ),r \;=\; \frac{\pi(\theta')\,Q(\theta\mid\theta')}{\pi(\theta)\,Q(\theta'\mid\theta)} \;=\; \frac{p(D\mid\theta')\,p(\theta')}{p(D\mid\theta)\,p(\theta)} \cdot \frac{Q(\theta\mid\theta')}{Q(\theta'\mid\theta)},

so the evidence p(D)p(D) cancels because it is constant in θ\theta.

We need α(θ′∣θ)\alpha(\theta' \mid \theta) and α(θ∣θ′)\alpha(\theta \mid \theta') to have ratio rr. A simple choice that works:

α(θ′∣θ)=min⁡(1,r)=min⁡(1,π(θ′)Q(θ∣θ′)π(θ)Q(θ′∣θ))\alpha(\theta' | \theta) = \min(1, r) = \min\left(1, \frac{\pi(\theta') Q(\theta | \theta')}{\pi(\theta) Q(\theta' | \theta)}\right)

Verification: Let

R=π(θ′)Q(θ∣θ′)π(θ)Q(θ′∣θ)R = \frac{\pi(\theta')Q(\theta \mid \theta')}{\pi(\theta)Q(\theta' \mid \theta)}

Either way, the ratio condition is satisfied, so detailed balance holds!

Interpretation:

The accept/reject rule does something subtle and important:

That is exactly why MCMC samples a distribution instead of performing optimization. Occasional moves into lower-probability regions are not mistakes; they are part of correctly representing posterior uncertainty.

Proposal Distributions: Symmetric vs. Asymmetric

The acceptance probability depends on the proposal distribution QQ. Two important cases:

Symmetric proposals: Q(θ′∣θ)=Q(θ∣θ′)Q(\theta' \mid \theta) = Q(\theta \mid \theta').

Example — Gaussian random walk (symmetric in θ\theta):

θ′∼N(θ; Σ)⟺θ′=θ+ε,  ε∼N(0,Σ).\theta' \sim \mathcal{N}(\theta;\,\Sigma)\quad\Longleftrightarrow\quad \theta'=\theta+\varepsilon,\;\varepsilon\sim\mathcal{N}(0,\Sigma).

For symmetric QQ, the QQ-terms cancel and

α=min⁡ ⁣(1,π(θ′)π(θ)).\alpha=\min\!\left(1,\frac{\pi(\theta')}{\pi(\theta)}\right).

Here you can evaluate π\pi using only log-likelihood + log-prior; any constant normalizer cancels. This is the Metropolis algorithm (the original 1953 version). You only need to evaluate the ratio of posterior probabilities.

Asymmetric proposals: Q(θ′∣θ)≠Q(θ∣θ′)Q(\theta' \mid \theta) \neq Q(\theta \mid \theta')

Example: Propose θ′\theta' by drawing from the prior p(θ)p(\theta).

Then:

α=min⁡(1,π(θ′)Q(θ∣θ′)π(θ)Q(θ′∣θ))\alpha = \min\left(1, \frac{\pi(\theta') Q(\theta | \theta')}{\pi(\theta) Q(\theta' | \theta)}\right)

You need the full ratio including the QQ terms. This is the Metropolis-Hastings algorithm (Hastings 1970 generalization).

Tuning the Proposal: The Art of Step Size

The proposal distribution Q determines how the chain explores. A crucial parameter is the step size (scale of proposals).

Too small: Proposals are always accepted (α≈1)(\alpha \approx 1), but you take tiny steps. The chain explores very slowly. High autocorrelation between successive samples.

Too large: Proposals often land in low-probability regions. High rejection rate (α≈0)(\alpha \approx 0). The chain stays stuck in one place most of the time. Again, slow exploration.

Just right: Moderate acceptance rate (typically 20-50%). The chain explores efficiently, taking substantial steps while still accepting frequently enough.

Adaptive tuning: In practice, you might run the chain for a burn-in period, monitor the acceptance rate, and adjust σ\sigma. Common strategy:

Once tuned, fix the proposal and run your production chain. (Don’t keep adapting during production — this violates the Markov property and detailed balance.)

From Likelihood to Log-Posterior Code

This is the bridge students usually need made explicit. For any Bayesian model, the implementation pipeline is:

  1. Write the forward model that predicts observables from parameters.

  2. Write the log-likelihood from your noise model.

  3. Write the log-prior from your scientific assumptions and parameter bounds.

  4. Add them to get the unnormalized log-posterior:

log⁡π(θ)=log⁡p(D∣θ)+log⁡p(θ)+C,\log \pi(\theta) = \log p(D \mid \theta) + \log p(\theta) + C,

where the constant C=−log⁡p(D)C = -\log p(D) can be ignored.

In practice, this is the computational object nearly everything reduces to:

def log_posterior(theta):
    return log_likelihood(theta) + log_prior(theta)

MCMC algorithms work by comparing this quantity between the current and proposed states. Ratios in probability space become subtraction in log space, which is why log-space arithmetic is not optional numerical polish but part of the method itself.

  1. Run the sampler on differences in log⁡π(θ)\log \pi(\theta) between the current and proposed states.

That is the whole computational bridge:

physics→likelihood→log⁡posterior→proposal loop→samples.\text{physics} \to \text{likelihood} \to \log \text{posterior} \to \text{proposal loop} \to \text{samples}.

Purpose: the code below is a minimal reusable Metropolis-Hastings skeleton.

Inputs: a function that evaluates the unnormalized log-posterior, a proposal rule, an initial parameter vector, and the number of samples.

Outputs: a Markov chain of sampled parameter states plus the acceptance rate.

Common pitfall: on rejection, you must still record the current state again. Rejected proposals do not disappear from the chain; “staying put” is part of the Markov process.

Minimal Sampler Skeleton

import numpy as np  # for np.log, np.random.uniform, array handling

def metropolis_hastings(log_posterior, proposal, theta_init, n_samples):
    """
    Generic Metropolis-Hastings MCMC sampler.
    
    Parameters:
    -----------
    log_posterior : function
        Computes log pi(theta) (log posterior probability). Return the unnormalized log posterior: log p(D|theta) + log p(theta). Do not include log p(D); it is a constant and cancels in MH.
    proposal : function  
        Generates theta_prime given theta. Returns theta_prime and log_Q_ratio = log Q(theta|theta_prime) - log Q(theta_prime|theta) (order matters)
    theta_init : array
        Initial parameter values
    n_samples : int
        Number of MCMC samples to generate
    
    Returns:
    --------
    chain : array (n_samples, n_params)
        MCMC samples
    acceptance_rate : float
        Fraction of accepted proposals
    """
    
    theta = theta_init.copy()
    chain = np.zeros((n_samples, len(theta)))
    n_accepted = 0
    
    log_pi_current = log_posterior(theta)
    
    for i in range(n_samples):
        # 1. Propose new state
        theta_proposed, log_Q_ratio = proposal(theta)
        
        # 2. Evaluate posterior at proposed state
        log_pi_proposed = log_posterior(theta_proposed)
        
        # 3. Compute acceptance probability (in log space!)
        log_alpha = log_pi_proposed - log_pi_current + log_Q_ratio
        
        # 4. Accept or reject
        if np.log(np.random.uniform()) < log_alpha:
            # Accept
            theta = theta_proposed
            log_pi_current = log_pi_proposed
            n_accepted += 1
        # If rejected, theta stays the same (automatic in this code structure)
        
        # 5. Store sample
        chain[i] = theta
    
    acceptance_rate = n_accepted / n_samples
    return chain, acceptance_rate

Key implementation details:

  1. Work in log space

    Purpose: show why a sampler evaluates log-probabilities instead of raw probabilities. Inputs: two extremely small probability values. Output: a finite log-ratio that can still be compared numerically. Pitfall: direct probabilities underflow to zero, so ratio calculations become undefined.

# Direct space: numerical disaster
p1 = 1e-1000  # Underflows to 0!
p2 = 1e-1001  # Also underflows to 0!
ratio = p2 / p1  # 0/0 = NaN. Game over.

# Log space: works perfectly  
log_p1 = -1000 * np.log(10)  # ~-2302.6
log_p2 = -1001 * np.log(10)  # ~-2305.0  
log_ratio = log_p2 - log_p1  # -2.3 (a perfectly fine number!)
# If log_p = -1000, then p = exp(-1000) = 0 (underflow!)
# But log_p = -1000 is a perfectly fine number to work with.
  1. Proposal functions

    Purpose: generate a candidate move for a symmetric random-walk sampler. Input: current parameter vector and a proposal scale. Output: proposed parameter vector plus the proposal-density correction term. Pitfall: if the proposal is not actually symmetric, you cannot set log_Q_ratio = 0.0.

def gaussian_proposal(theta, sigma=1.0):
    theta_new = theta + np.random.normal(0, sigma, size=len(theta))
    log_Q_ratio = 0.0  # Symmetric, so Q(theta|theta_prime)/Q(theta_prime|theta) = 1
    return theta_new, log_Q_ratio
  1. Acceptance decision: Compare log(uniform random) to log⁡α\log \alpha instead of comparing a uniform random draw to α\alpha. This avoids exponentiating large negative numbers. (Use unnormalized log posterior: loglike + logprior; do not add log p(D).)

What About the Normalization Constant?

Recall: we only need π(θ)\pi(\theta) up to proportionality; this section shows explicitly how p(D)p(D) cancels from the MH acceptance ratio.

Remember Bayes’ theorem:

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

The denominator p(D)p(D) is often intractable (that’s why we’re doing MCMC in the first place!). But look at the acceptance probability:

α=min⁡(1,p(θ′∣D)p(θ∣D))=min⁡(1,p(D∣θ′)p(θ′)/p(D)p(D∣θ)p(θ)/p(D))\alpha = \min\left(1, \frac{p(\theta' | D)}{p(\theta | D)}\right) = \min\left(1, \frac{p(D | \theta') p(\theta') / p(D)}{p(D | \theta) p(\theta) / p(D)}\right)

The p(D)p(D) terms cancel! You only need:

α=min⁡(1,p(D∣θ′)p(θ′)p(D∣θ)p(θ))\alpha = \min\left(1, \frac{p(D | \theta') p(\theta')}{p(D | \theta) p(\theta)}\right)

Profound implication: MCMC only requires evaluating the unnormalized posterior (likelihood ×\times prior). You never need the evidence p(D)p(D).

This is why MCMC works for arbitrarily complex models. As long as you can evaluate p(D∣θ)p(D \mid \theta) (your likelihood/model) and p(θ)p(\theta) (your prior), you can sample from the posterior.


Part 4: Practical Diagnostics — Has Your Chain Converged?

You’ve run your MCMC algorithm for N steps. You have N samples. Now the crucial question: Are these samples from the posterior?

The ergodic theorem says the chain eventually converges to π\pi. But how long is “eventually”? If you haven’t run long enough, your samples don’t represent the posterior — they’re still biased toward your starting point.

This is the burn-in problem: Early samples are not from π\pi. You need to discard them.

Convergence is therefore not something you “prove” from a single plot. It is a judgment built from several pieces of evidence that all point in the same direction: stationarity, mixing, agreement across chains, and enough effectively independent samples.

Visual Diagnostic: Trace Plots

The first and most important diagnostic: Plot your samples over time.

Representative trace-plot behaviors for MCMC. A good trace plot looks like stationary, overlapping noise after burn-in, while problematic chains show drift or long sticky plateaus. This is the visual version of the convergence question students should ask before trusting any posterior summary.{width=“100%” #fig-mcmc-trace-plot-comparison}

A trace plot shows θt\theta_t vs. tt. What to look for:

Good signs:

Bad signs:

Quantitative Diagnostic 1: Autocorrelation Function

The autocorrelation function (ACF) measures how correlated θt\theta_t is with θt+k\theta_{t+k}:

ρk=Cov(θt,θt+k)Var(θt)=E[(θt−μ)(θt+k−μ)]E[(θt−μ)2]\rho_k = \frac{\text{Cov}(\theta_t, \theta_{t+k})}{\text{Var}(\theta_t)} = \frac{\mathbb{E}[(\theta_t - \mu)(\theta_{t+k} - \mu)]}{\mathbb{E}[(\theta_t - \mu)^2]}

where μ=⟨θ⟩\mu = \langle \theta \rangle is the chain mean.

Interpretation:

Autocorrelation time τ\tau: the characteristic lag at which samples become independent:

τ=1+2∑k=1∞ρk\tau = 1 + 2 \sum_{k=1}^{\infty} \rho_k

In practice, truncate the infinite sum when ρk\rho_k becomes negligible (say, when ρk<0.05\rho_k < 0.05 or when ρk\rho_k is no longer statistically significant).

Effective sample size (ESS):

Neff=NτN_{\text{eff}} = \frac{N}{\tau}

If τ=10\tau = 10, then your N=10000N = 10000 samples are only as informative as Neff=1000N_\text{eff} = 1000 independent samples. Your error bars should be computed using NeffN_\text{eff}, not NN.

Quantitative Diagnostic 2: Gelman-Rubin Statistic

The Gelman-Rubin diagnostic (also called R-hat, R^\hat{R}) tests convergence by comparing multiple chains.

Idea: Run M chains from different starting points. If they’ve all converged to the same distribution, they should have:

The R-hat statistic compares variance between chains to variance within chains:

R^=VarpooledVarwithin\hat{R} = \sqrt{\frac{\text{Var}_{\text{pooled}}}{\text{Var}_{\text{within}}}}

More precisely:

  1. Run MM chains of length NN each (after burn-in)

  2. Compute within-chain variance W (average variance across chains)

  3. Compute between-chain variance B (variance of chain means)

  4. Estimate total variance: Var^=N−1NW+1NB\hat{\text{Var}} = \frac{N-1}{N} W + \frac{1}{N} B

  5. Compute: R^=Var^/W\hat{R} = \sqrt{\hat{\text{Var}} / W}

Interpretation:

Rule of thumb: R^<1.01\hat{R} < 1.01 for all parameters before trusting your results.

Burn-In: How Much to Discard?

The first part of each chain is burn-in: Samples before the chain has reached equilibrium. You must discard these before computing statistics.

How long is burn-in?

No universal answer. It depends on:

Conservative approach:

  1. Run the chain for NN total steps

  2. Plot trace plots for several parameters

  3. Identify where the chain stabilizes (stops drifting)

  4. Discard everything before that point (typically 10-50% of samples)

  5. Check that R-hat < 1.01 for the remaining samples

Practical tip: It’s safer to discard too much than too little. If you have 100,000 samples and discard the first 20,000, you still have plenty. But if you keep burn-in samples, your posterior estimates will be wrong.

Putting It All Together: A Convergence Checklist

Before trusting your MCMC results, verify:

Visual inspection:

Quantitative metrics:

Acceptance rate (for Metropolis-Hastings):

Sensitivity checks:

If all these pass, you can trust that your samples approximate the true posterior.

Failure Modes to Watch For

Not every chain is scientifically useful, even if it produces a long list of numbers.

These are not cosmetic issues. Each one breaks the argument that sample averages represent posterior expectations.


Part 5: Real Example — Cepheid Variable Distance

Let’s apply everything you’ve learned to a concrete astrophysical problem: Inferring the distance to a Cepheid variable star.

The Science: Cepheids as Standard Candles

Cepheid variables are pulsating stars discovered by Henrietta Leavitt in 1908. She found a remarkable relationship: The period of pulsation correlates with intrinsic luminosity.

This makes Cepheids standard candles: If you measure the period (P)(P), you know the absolute magnitude (M). Compare to the observed apparent magnitude (m)(m), and you can infer the distance.

Notice the full chain here:

MCMC enters only after this reasoning is complete. Its job is not to create the science question, but to make the answer computable.

The distance modulus relation:

m−M=5log⁡10(d/10 pc)m - M = 5 \log_{10}(d / 10 \text{ pc})

where d is the distance in parsecs.

The Period-Luminosity relation (Leavitt Law):

For Classical Cepheids in the V-band, the absolute magnitude depends on the logarithm of the period:

MV=−2.43log⁡10(P/10 days)−4.05M_V = -2.43 \log_{10}(P / 10 \text{ days}) - 4.05

This gives MVM_V for a reference metallicity. In practice, there are corrections for:

For our example, we’ll use the idealized case: A Cepheid with well-measured period, negligible extinction, and solar metallicity.

The Problem: Inferring Distance from Photometry

What you observe:

What you want to infer:

Step 1: Forward Model

The physics runs forward from period and distance to apparent magnitude.

From the Leavitt Law with P=10.0P = 10.0 days:

MV=−2.43log⁡10(10.0/10)−4.05=−2.43×0−4.05=−4.05M_V = -2.43 \log_{10}(10.0 / 10) - 4.05 = -2.43 \times 0 - 4.05 = -4.05

The distance modulus is:

μ=m−M=18.50−(−4.05)=22.55\mu = m - M = 18.50 - (-4.05) = 22.55

So:

μ=5log⁡10(d/10)=22.55\mu = 5 \log_{10}(d / 10) = 22.55

Solving for dd:

d=10(μ+5)/5=10(22.55+5)/5=105.51≈323,000 pc≈323 kpcd = 10^{(\mu + 5)/5} = 10^{(22.55 + 5)/5} = 10^{5.51} \approx 323,000 \text{ pc} \approx 323 \text{ kpc}

That gives the familiar point estimate, but it does not yet tell us how uncertain we are.

Step 2: Noise Model and Likelihood Assumptions

To turn this into inference, we need to say how noise enters the measurement. For the basic 1D example, we assume:

The Gaussian choice is not arbitrary. It is a first-pass model justified by the Central Limit Theorem: photometric uncertainty is often the sum of many small effects such as photon-counting noise, detector read noise, and calibration scatter. If those assumptions failed badly, we would need a different likelihood.

Holding PP fixed also means that, within this simplified example,

p(mobs∣d,P)=p(mobs∣d),p(m_{\mathrm{obs}} \mid d, P) = p(m_{\mathrm{obs}} \mid d),

because all period uncertainty has been neglected. In a more realistic model, uncertainty in PP would be part of the likelihood too.

That gives the observation model

mobs=mmodel(d)+ϵ,ϵ∼N(0,σm2).m_{\mathrm{obs}} = m_{\mathrm{model}}(d) + \epsilon, \qquad \epsilon \sim \mathcal{N}(0, \sigma_m^2).

Step 3: Likelihood

The corresponding likelihood is

p(mobs∣d)=N(mobs;mmodel(d),σm2)p(m_{\text{obs}} | d) = \mathcal{N}(m_{\text{obs}}; m_{\text{model}}(d), \sigma_m^2)

with

or, in the exponential form students often implement first,

p(mobs∣d)∝exp⁡ ⁣[−(mobs−mmodel(d))22σm2].p(m_{\mathrm{obs}} \mid d) \propto \exp\!\left[ -\frac{\left(m_{\mathrm{obs}} - m_{\mathrm{model}}(d)\right)^2}{2\sigma_m^2} \right].

In log form, which is what the code should evaluate,

log⁡p(mobs∣d)=−12[(mobs−mmodel(d))2σm2+log⁡(2πσm2)]\log p(m_{\text{obs}} | d) = -\frac{1}{2} \left[ \frac{(m_{\text{obs}} - m_{\text{model}}(d))^2}{\sigma_m^2} + \log(2\pi\sigma_m^2) \right]

One-dimensional likelihood for a Cepheid-style distance inference. This is the shape MCMC is trying to explore efficiently once brute-force evaluation becomes too expensive. The key pedagogical point is that the likelihood is already a function you could plot in 1D, inspect, and then later generalize to higher-dimensional posteriors where the same workflow is no longer cheap on a grid.{width=“100%” #fig-mcmc-likelihood-1d}

Step 4: Prior

We now encode a simple scale prior. We will use a prior uniform in log-distance, which corresponds to the density

p(d)∝1d.p(d) \propto \frac{1}{d}.

This is a common choice for positive scale parameters because it does not privilege one multiplicative scale over another.

We will actually sample in log⁡d\log d, where the prior is uniform:

p(log⁡d)=Uniform[log⁡(50),log⁡(10,000)]p(\log d) = \text{Uniform}[\log(50), \log(10,000)]

This corresponds to distances between 50 kpc and 10 Mpc, a reasonable range for extragalactic Cepheids (e.g., in M31, M33, or Local Group galaxies).

Step 5: Posterior

By Bayes’ theorem,

p(d∣mobs)∝p(mobs∣d)×p(d)p(d | m_{\text{obs}}) \propto p(m_{\text{obs}} | d) \times p(d)

In log form, the quantity we actually hand to the sampler is

log⁡π(d)=log⁡p(mobs∣d)+log⁡p(d)+C,\log \pi(d) = \log p(m_{\mathrm{obs}} \mid d) + \log p(d) + C,

where the additive constant CC does not depend on dd and can be dropped.

This is the answer to the practical question “What function do I write?” You write the function that returns log⁡p(mobs∣d)+log⁡p(d)\log p(m_{\mathrm{obs}} \mid d) + \log p(d).

Step 6: What Changes in More Than One Parameter?

Real Cepheid inference is rarely this clean. If extinction is also uncertain, then the model becomes

mmodel(d,AV)=MV+5log⁡10(d/10 pc)+AV.m_{\mathrm{model}}(d, A_V) = M_V + 5 \log_{10}(d / 10~\mathrm{pc}) + A_V.

Now distance and extinction trade off against each other: a larger AVA_V can mimic a larger distance. The posterior becomes a correlated ridge rather than a single narrow bump. That is exactly the kind of geometry MCMC is built for.

In that 2D version, the computational target becomes

log⁡π(d,AV)=log⁡p(mobs∣d,AV)+log⁡p(d)+log⁡p(AV)+C.\log \pi(d, A_V) = \log p(m_{\mathrm{obs}} \mid d, A_V) + \log p(d) + \log p(A_V) + C.

This is the real conceptual bridge from Part 2 to Part 3: the physics and uncertainty model define the posterior, and MCMC supplies the machinery for exploring it when that posterior is no longer something you can exhaustively map on a grid.

Pause and Predict

Before seeing any numerical result, make a scientific prediction:

  1. Roughly what distance should the posterior center on?

  2. Should the posterior be nearly symmetric in dd, or slightly skewed?

  3. If the proposal scale is tuned reasonably, do you expect the chain to mix quickly in this one-parameter problem?

This matters because MCMC should not feel like a black box. You should have physical expectations before the chain runs.

What the Sampler Should Recover

If you connect the generic Metropolis-Hastings sampler above to this log-likelihood and prior, the chain should recover a posterior centered near d≈323 kpcd \approx 323~\text{kpc} with a width of a few tens of kiloparsecs.

The important teaching point is not the exact number. It is the structure of the answer:

For a well-tuned one-dimensional sampler here, an acceptance rate in the rough range of 25% to 50% is plausible, and an autocorrelation time of order 10 would indicate healthy mixing.

Scientific Interpretation

What changes when we switch from a point estimate to a posterior?

That is the real payoff of MCMC. It turns an inverse problem into a defensible uncertainty statement.


Synthesis — The Conceptual Architecture

You’ve learned a lot. Let’s step back and see the structure of what you now know.

The Four Pillars of MCMC

Each pillar rests on the previous one. You can’t appreciate MCMC diagnostics without understanding Markov chains. You can’t design good samplers without knowing detailed balance. And you can’t trust your results without proper statistical foundations.

One way to summarize the entire lecture is:

Universal Patterns Across Modules

One of the profound lessons of this course: The same mathematical structures appear everywhere. Let’s make this explicit:

ConceptModule 1: StatisticsModules 3: Statistical MechanicsModule 5: Bayesian Inference
What we’re samplingRandom variablesMicrostates in phase spaceParameters in posterior
Target distributionPopulation distributionBoltzmann distributionPosterior p(θ∣D)p(\theta \mid D)
How we sampleIndependent and Identically Distributed (IID) random drawsMolecular dynamicsMarkov chain (MCMC)
Why it worksLaw of Large NumbersErgodic hypothesisErgodic theorem
Convergence conditionCLT (independent samples)Thermal equilibrationDetailed balance
What we computeSample statisticsThermodynamic quantitiesParameter estimates
Time average = ?Ensemble average (Module 1)Ensemble average (Module 3)Expectation under π\pi (Module 5)

The profound unity: Whether you’re:

You’re using the same mathematical structure: stochastic processes converging to equilibrium distributions, with time averages equaling ensemble averages.

This is why physics is so powerful and predictive. The same principles work everywhere, from atoms to stars to probability distributions.

From Theory to Practice: Your Toolbox

You now have:

1. Conceptual understanding:

2. Practical skills:

3. Scientific applications:

4. Connections:

This is a complete inference framework you can apply to any scientific problem.

Common Misconceptions About MCMC

Before moving to Project 4, let’s address some frequent misunderstandings that trip up even experienced practitioners:

Misconception 1: “High acceptance rate means good sampling”

Reality: High acceptance (>60%) usually means your proposals are too timid — you’re taking baby steps and exploring slowly. The chain mixes poorly and has high autocorrelation. Conversely, very low acceptance (<15%) means proposals are too aggressive. Optimal acceptance depends on dimension: aim for 20-40% in most cases.

Why it’s tempting: It feels good when proposals are accepted! But acceptance rate is about efficiency, not validity.

Misconception 2: “R-hat < 1.1 guarantees convergence”

Reality: R-hat < 1.1 is necessary but not sufficient for convergence. If your posterior has multiple well-separated modes and each chain gets stuck in one mode, R-hat will look fine but you haven’t converged to the full posterior. You’ve converged to individual modes.

How to catch it: Always visually inspect trace plots and 2D marginal distributions. Look for chains that never overlap. Run chains from diverse starting points spanning the prior range.

Misconception 3: “MCMC samples are independent”

Reality: MCMC samples are correlated. The effective sample size Neff=N/τN_\text{eff} = N/\tau accounts for this. If τ=100\tau = 100, your 10,000 samples carry as much information as 100 independent samples. Always report NeffN_\text{eff}, not just NN.

Practical impact: If you compute credible intervals assuming independence, they’ll be too narrow (overconfident). Use N_eff for uncertainty quantification.

Misconception 4: “Longer chains are always better”

Reality: Running a poorly-tuned chain longer doesn’t fix the problem — it just wastes time. If your proposals are bad, adding more samples doesn’t help. Fix the sampler first (tune step size, use better proposals, add constraints), then run long chains.

The exception: If your chain is well-tuned but exploring a complex posterior, then yes, longer is better. But “longer” means N_eff > 400 per parameter, not just more samples.

Misconception 5: “I can start MCMC anywhere”

Reality: While theory says chains converge from any starting point, starting very far from the typical set (the region of high probability) means long burn-in. Starting outside your prior bounds or at parameter values where the likelihood is undefined will cause immediate failure.

Best practice: Start from reasonable values. For well-specified priors, sample from the prior. For complex problems, run optimization first to find the posterior mode, then start MCMC nearby.

Misconception 6: “Burn-in is waste”

Reality: Burn-in is not waste — it’s how the chain finds the typical set! It’s like a spacecraft’s trajectory correction burns to reach the right orbit. The real waste is keeping burn-in samples and contaminating your posterior estimates.

How much to discard: Be conservative. If in doubt, discard more. Look at trace plots — when do they stabilize?

Misconception 7: “MCMC always works”

Reality: MCMC can fail in many ways:

Recognition: That’s why diagnostics exist! If R-hat is bad, acceptance is extreme, or chains look weird, don’t trust the results. Fix the sampler or use a different method.

Misconception 8: “The posterior mean is the best parameter estimate”

Reality: Depends on your loss function! The posterior mean minimizes squared-error loss. The posterior median minimizes absolute-error loss. The posterior mode (MAP) minimizes 0-1 loss. Which one you report depends on what you’re trying to do. For asymmetric posteriors, they can differ substantially.

Best practice: Report the full posterior (credible intervals) rather than a single point estimate. Let readers make their own decisions about point estimates.


Looking Ahead: Project 4 and Beyond

What’s Next

In Project 4, you’ll implement everything from this module to measure cosmological parameters from Type Ia supernovae:

Your tasks:

  1. Build the forward model (cosmological distance-redshift relation)

  2. Implement Metropolis-Hastings MCMC

  3. Apply diagnostics (trace plots, R-hat, ACF, corner plots)

  4. Run inference on real SNe data (JLA sample)

  5. Measure Ωm\Omega_m and hh (the contents of the universe!)

  6. Extend to Hamiltonian Monte Carlo (using your leapfrog integrator from Project 2)

This is the same data and methods that earned the 2011 Nobel Prize in Physics for the discovery of dark energy.

Advanced Topics (Module 5.4 Preview)

After mastering Metropolis-Hastings, you’ll learn:

Hamiltonian Monte Carlo (HMC):

Affine-invariant ensemble samplers (like emcee):

No-U-Turn Sampler (NUTS):

But these are all variations on the core principles you learned here. Metropolis-Hastings is the foundation.

Where is MCMC Heading? The Frontier of Computational Inference

MCMC is not a settled field — it’s actively evolving. Here are some exciting research directions that may transform how we do inference in the next decade:

Neural network-assisted MCMC: Recent work uses machine learning to learn optimal proposal distributions from data. Instead of hand-tuning proposals, train a neural network to predict good proposals based on the posterior landscape. Early results show dramatic speedups for complex problems. This combines classical statistics with modern deep learning.

Adaptive and self-tuning methods: Modern samplers can adapt their behavior during sampling without violating detailed balance (which traditional theory said was impossible!). Methods like adaptive Metropolis (Haario et al. 2001) and delayed rejection algorithms show that we can be smarter about exploration while maintaining theoretical guarantees.

Non-reversible MCMC: Traditional MCMC satisfies detailed balance (microscopic reversibility). But what if we intentionally break reversibility to explore faster? Non-reversible Langevin samplers and lifted particle filters can have much lower autocorrelation than reversible methods. This is cutting-edge research connecting to non-equilibrium statistical mechanics.

Variational inference and hybrid methods: Not everything requires sampling. Variational methods approximate posteriors with simpler distributions (like Gaussians) by optimization rather than sampling. Modern hybrid approaches combine MCMC’s accuracy with variational inference’s speed. The future may be algorithms that switch between sampling and optimization adaptively.

Quantum MCMC: As quantum computers mature, researchers are developing quantum algorithms for Bayesian inference. Quantum walks could explore posterior landscapes exponentially faster than classical MCMC — though practical implementations are years away.

Amortized inference: Instead of running MCMC separately for each dataset, train a model once that can instantly produce posteriors for new data. This “simulation-based inference” approach (also called likelihood-free inference or neural posterior estimation) is revolutionizing fields where likelihood evaluation is expensive.

The common thread: All these advances build on the foundations you learned — Markov chains, stationarity, convergence theory. Understanding Metropolis-Hastings deeply prepares you to understand and contribute to these cutting-edge methods. The principles don’t change; the implementations get ever more sophisticated.

MCMC has come a long way from simulating nuclear reactions on MANIAC I in 1953. Where will it go next? Perhaps you’ll help decide.

The Professional Path

After building MCMC from scratch, you’ll be able to use professional tools intelligently:

emcee (Python): Affine-invariant ensemble sampler
PyMC (Python): Full Bayesian modeling framework with HMC/NUTS
Stan (C++): Gold-standard Bayesian inference (autodiff, NUTS)
JAX/numpyro (Python): HMC with Just-In-Time (JIT) compilation and GPU acceleration

You won’t be a black-box user clicking buttons. You’ll understand:

That’s the glass-box philosophy in action.


Congratulations! You’ve learned one of the most important computational techniques in modern science. MCMC underpins:

More importantly, you’ve seen how measurement, probability, physics, and computation all connect into a unified framework for learning from data.

Now go build your inference engine. The universe is waiting to be measured.


Self-Assessment Rubric

Before moving forward, honestly assess your mastery of this module. This rubric helps you identify areas for review and confirms readiness for Project 4.

Level 1: Conceptual Understanding

Can you explain the ideas to someone else?

Level 2: Mathematical Foundations

Can you derive key results?

Level 3: Implementation Skills

Can you code MCMC from scratch?

Level 4: Diagnostic Expertise

Can you tell if your sampler is working?

Level 5: Scientific Application

Can you apply MCMC to real problems?

Level 6: Connections and Transfer

Can you see how MCMC fits into the bigger picture?

Remember: This is self-assessment for learning, not grading. Be honest with yourself. Identifying gaps now makes Project 4 more productive.


Next Module: Hamiltonian Monte Carlo (HMC) and advanced sampling methods