You have annual earthquake counts and want to understand why some years have many more events than others. A single average may hide variation that a model with two rates could describe. MCMC for mixture models lets us explore that possibility while keeping uncertainty about the rates and their relative frequency. The recorded annual counts range from 6 to 41 major earthquakes.
The counts leave each regime's rate uncertain. Sampling puts the quiet rate at roughly 14 to 17 earthquakes a year. The active rate is 24 to 31. Our Gaussian mixtures and EM example gave point estimates; the Poisson-mixture sampler returns intervals too.
By the end of this post, you'll build a Metropolis-Hastings sampler from scratch that infers the hidden parameters of a two-component Poisson mixture, giving you posterior distributions for each parameter, complete with uncertainty quantification.
The Data: 107 Years of Earthquakes
The dataset records the number of major earthquakes (Richter scale > 7) worldwide for each year from 1900 to 2006. It comes from Zucchini, MacDonald and Langrock's Hidden Markov Models for Time Series (2016), page 10.

The mean is about 19 earthquakes per year, but the distribution stretches from 6 to 41. That's far more spread than a single Poisson would predict (a Poisson with has standard deviation , yet the data ranges over 35 units). A two-component mixture is a natural hypothesis: one "quiet" regime and one "active" regime.
Quick Win: MCMC in Action
Let's fit this mixture model with Metropolis-Hastings. Click the badge to run it yourself:
Watch the MCMC chain explore the posterior. It starts at our initial guess () and gradually finds the high-probability region:

Here's the complete implementation. We need three pieces: the data, a log-likelihood function, and the Metropolis-Hastings sampler.
import numpy as np
from scipy.stats import poisson
# Major earthquakes per year (Richter > 7), 1900–2006
eq = np.array([
13, 14, 8, 10, 16, 26, 32, 27, 18, 32, 36, 24, 22, 23, 22, 18, 25,
21, 21, 14, 8, 11, 14, 23, 18, 17, 19, 20, 22, 19, 13, 26, 13, 14,
22, 24, 21, 22, 26, 21, 23, 24, 27, 41, 31, 27, 35, 26, 28, 36, 39,
21, 17, 22, 17, 19, 15, 34, 10, 15, 22, 18, 15, 20, 15, 22, 19, 16,
30, 27, 29, 23, 20, 16, 21, 21, 25, 16, 18, 15, 18, 14, 10, 15, 8,
15, 6, 11, 8, 7, 18, 16, 13, 12, 13, 20, 15, 16, 12, 18, 15, 16,
13, 15, 16, 11, 11,
])
The log-likelihood for a two-component Poisson mixture:
def poisson_mix_loglik(data, lam1, lam2, d1):
"""Log-likelihood: sum_i log[ d1 * Pois(x_i|lam1) + d2 * Pois(x_i|lam2) ]"""
d2 = 1.0 - d1
mix = d1 * poisson.pmf(data, lam1) + d2 * poisson.pmf(data, lam2)
mix = np.maximum(mix, 1e-300) # protect against log(0)
return np.sum(np.log(mix))
Now the Metropolis-Hastings sampler. We propose new parameter values from a multivariate normal centred on the current position, then accept or reject based on the likelihood ratio:
def run_mcmc(data, n_iter=1000, burn_in=100):
# Initial values and proposal covariance (from the original R code)
sigma = np.diag([1.0, 1.0, 0.01]) # proposal covariance
params = np.full((n_iter + 1, 5), np.nan) # lam1, lam2, d1, d2, loglik
# Starting point
params[0] = [10.0, 20.0, 0.3, 0.7,
poisson_mix_loglik(data, 10.0, 20.0, 0.3)]
n_accept = 0
for i in range(n_iter):
cur_ll = params[i, 4]
# Propose: multivariate normal centred on current [lam1, lam2, d1]
current = params[i, :3]
prop = np.random.multivariate_normal(mean=current, cov=sigma)
p_lam1, p_lam2, p_d1 = prop
# Enforce constraints: lambdas > 0, 0 < delta1 < 1
if p_lam1 <= 0 or p_lam2 <= 0 or p_d1 <= 0 or p_d1 >= 1:
prop_ll = -np.inf
else:
prop_ll = poisson_mix_loglik(data, p_lam1, p_lam2, p_d1)
# Accept/reject
log_ratio = prop_ll - cur_ll
if np.log(np.random.uniform()) < min(0.0, log_ratio):
params[i+1] = [p_lam1, p_lam2, p_d1, 1-p_d1, prop_ll]
n_accept += 1
else:
params[i+1] = params[i]
posterior = params[burn_in+1:]
return params, posterior, n_accept / n_iter
np.random.seed(42)
all_params, posterior, accept_rate = run_mcmc(eq, n_iter=1000, burn_in=100)
print(f"Acceptance rate: {accept_rate:.1%}")
print(f"lambda1: {posterior[:, 0].mean():.1f} ± {posterior[:, 0].std():.1f}")
print(f"lambda2: {posterior[:, 1].mean():.1f} ± {posterior[:, 1].std():.1f}")
print(f"delta1: {posterior[:, 2].mean():.2f} ± {posterior[:, 2].std():.2f}")
Acceptance rate: 31.8%
lambda1: 15.8 ± 0.7
lambda2: 27.1 ± 1.6
delta1: 0.67 ± 0.08
The sampler found two regimes: a "quiet" regime with earthquakes per year (67% of years) and an "active" regime with per year (33% of years). Here's how the fitted mixture matches the data:

The mixture captures the right-skew and the heavy tail that a single Poisson misses entirely.
What Just Happened?
The Poisson Mixture Model
We're modelling each year's earthquake count as drawn from one of two Poisson distributions:
where . We don't know which regime generated each observation (that's the "hidden" part). We want to infer three parameters: (quiet-regime rate), (active-regime rate), and (the probability a year belongs to the quiet regime). Since , there are three free parameters.
The log-likelihood across all 107 years is:
mix = d1 * poisson.pmf(data, lam1) + d2 * poisson.pmf(data, lam2)
loglik = np.sum(np.log(mix))
For each data point, we compute the probability under each component, weight by the mixing proportions, and sum the logs. The np.maximum(mix, 1e-300) guard prevents log(0) when a proposed parameter makes a data point essentially impossible.
Metropolis-Hastings: Propose, Evaluate, Accept/Reject

The sampler follows the same Metropolis-Hastings logic from our island-hopping post, but now in a continuous 3D parameter space instead of a discrete one. If you want a refresher on why we need MCMC at all rather than the simpler inverse-CDF, rejection, and importance sampling toolkit, the basic-methods comparison post covers when each one runs out of steam.
At each iteration:
- Propose a new point from a multivariate normal centred on the current position, with covariance .
- Evaluate the log-likelihood at the proposed point. If any constraint is violated ( or ), assign .
- Accept or reject with probability where is the likelihood ratio. In log space:
log_ratio = prop_ll - cur_ll
if np.log(np.random.uniform()) < min(0.0, log_ratio):
# accept
This is identical to the acceptance rule in our Bayesian inference post, but applied to a mixture likelihood rather than a single-distribution posterior.
The Proposal Distribution
The proposal covariance controls the step size. The parameters get steps of standard deviation 1 (reasonable, since they live around 10-30), while gets steps of standard deviation 0.1 (since it's bounded between 0 and 1).
sigma = np.diag([1.0, 1.0, 0.01])
prop = np.random.multivariate_normal(mean=current, cov=sigma)
If the steps are too large, most proposals land in low-probability regions and get rejected (acceptance rate near 0%). Too small, and the chain accepts nearly every step but barely moves, taking ages to explore the posterior. The 32% acceptance rate we got is in the sweet spot: Metropolis-Hastings theory suggests 20-50% is ideal for multivariate targets.
Burn-In: Forgetting the Starting Point
We initialised at , which is deliberately far from the posterior mode. The first ~100 iterations are the chain migrating from this poor starting point to the high-probability region. We discard these as burn-in.
The trace plots make this visible:

In the grey-shaded burn-in region, you can see all three parameters climbing from their initial values toward the posterior. After burn-in, the chains oscillate around their posterior means: , , .
Reading the Posterior
Unlike EM for Gaussian mixtures, which gives you point estimates, MCMC gives you the full posterior distribution. The marginal posteriors show the uncertainty in each parameter:

The 95% credible intervals are:
- : the quiet regime has 14-17 major earthquakes per year
- : the active regime has 24-31
- : the quiet regime accounts for roughly 49-83% of years
These intervals are the Bayesian answer to "how sure are we?" EM would give and nothing more. MCMC gives and the full shape of the uncertainty.
The pairs plot reveals how the parameters are correlated in the posterior:

Notice the positive correlation between and : when the sampler explores higher rates for the quiet regime, it tends to push the active regime higher too, to maintain the overall fit. Similarly, correlates positively with , because increasing the quiet-regime rate means it needs to account for a larger share of the data to maintain the same overall mean.
Going Deeper
MCMC vs EM

We could fit this mixture with EM, which is computationally simpler and faster. So why use MCMC?
- Full posterior, not point estimates. EM gives you the maximum likelihood values . MCMC gives you the entire posterior distribution, including uncertainty quantification and credible intervals. For a dataset of only 107 points, that uncertainty matters.
- No closed-form M-step needed. EM requires deriving the M-step analytically for each model. For Poisson mixtures, this is straightforward, but for more exotic likelihoods (as in our one-inflated Beta regression post), EM can be intractable. MCMC only needs to evaluate the likelihood.
- Natural uncertainty propagation. If you want to predict "what's the probability that next year has more than 30 earthquakes?", you can average over the posterior samples. With EM, you'd need to bootstrap or use asymptotic approximations.
The trade-off is computational cost. EM converges in a few dozen iterations; our MCMC needs 1,000 iterations and still shows some roughness in the posteriors.
Constraint Handling by Rejection
Our parameters have constraints: and . Rather than using a constrained sampler or transforming variables (e.g., log-transforming the lambdas), we take the simplest approach: if a proposed point violates any constraint, we set its log-likelihood to , which guarantees rejection.
if p_lam1 <= 0 or p_lam2 <= 0 or p_d1 <= 0 or p_d1 >= 1:
prop_ll = -np.inf
This is equivalent to placing a uniform prior over the valid region: for valid , otherwise. It's simple and works well when the posterior is far from the boundaries (as it is here). For parameters near boundaries, a reparameterisation would be more efficient.
Hyperparameter Sensitivity
| Parameter | Value | Why |
|---|---|---|
N (iterations) | 1,000 | Enough for this simple 3-parameter model. More iterations would give smoother posteriors. |
burn_in | 100 | The chain reaches the posterior mode within ~50 iterations; 100 gives a safety margin. |
sigma | diag(1, 1, 0.01) | Step sizes matched to each parameter's scale. Gives ~32% acceptance rate. |
| init | 10 | Deliberately low to test that the chain can find the right region. |
| init | 20 | Close to the overall mean but below the true posterior value. |
| init | 0.3 | Deliberately low (true value ~0.67) to test burn-in. |
The proposal covariance is the most important tuning parameter. Making sigma too large leads to high rejection rates; too small leads to slow exploration. A diagonal covariance ignores correlations between parameters. More sophisticated samplers (adaptive MH, Hamiltonian MC) tune this automatically.
Label Switching: The Mixture Model Trap
There's a subtlety we glossed over: our model has a symmetry. If you swap and , the likelihood is identical. This is called label switching, and in long MCMC runs, the chain can jump between these two modes, producing a posterior that's a symmetric mixture of both.
We avoid this here because our initial values and proposal dynamics keep throughout the run. For more complex models or longer chains, you'd impose an ordering constraint (e.g., ) or use post-processing to relabel samples.
From Scratch to PyMC
For production work, you wouldn't write your own sampler. A probabilistic programming framework like PyMC handles proposals, tuning, diagnostics, and convergence checking automatically. The same model in PyMC looks like:
import pymc as pm
with pm.Model():
delta = pm.Dirichlet("delta", a=np.ones(2))
lambdas = pm.Gamma("lambdas", alpha=1, beta=0.05, shape=2)
obs = pm.Mixture("obs", w=delta,
comp_dists=pm.Poisson.dist(mu=lambdas), observed=eq)
trace = pm.sample(2000)
PyMC uses the No-U-Turn Sampler (NUTS), a variant of Hamiltonian Monte Carlo that's far more efficient than our random-walk Metropolis. But the core idea is the same: draw samples from the posterior to approximate the full distribution.
Where This Comes From
Metropolis et al. (1953): The Original MCMC Paper
The Metropolis-Hastings algorithm has its roots in the physics of the atomic bomb. Metropolis, Rosenbluth, Rosenbluth, Teller, and Teller (1953) developed the algorithm at Los Alamos to simulate the thermodynamic equilibrium of interacting molecules. Their key insight was that you don't need to compute the normalisation constant of a distribution to sample from it; you only need the ratio of probabilities at two points.
W.K. Hastings generalised the algorithm in 1970 to allow asymmetric proposal distributions (Metropolis' version required symmetric proposals). Our implementation uses a symmetric multivariate normal proposal, so we're technically using the original Metropolis algorithm, a special case of Metropolis-Hastings.
The acceptance rule we used:
is the Metropolis ratio. The beauty of it: the normalisation constant cancels out in the ratio, so we never need to compute , which is generally intractable.
"The purpose of this paper is to describe a general method, suitable for fast electronic computing machines, of calculating the properties of any substance which may be considered as composed of interacting individual molecules." -- Metropolis et al. (1953)
They were simulating liquid states of hard spheres. Seven decades later, we're using the same algorithm to infer earthquake regimes.
Mixture Models and Data Augmentation
The idea of fitting mixture models with MCMC goes back to Tanner and Wong (1987), "The Calculation of Posterior Distributions by Data Augmentation." They showed that by introducing latent variables (which component generated each observation), you can construct a Gibbs sampler (an MCMC algorithm that samples each variable in turn, conditioning on the rest) that alternates between sampling component assignments and sampling parameters.
Our approach is different: we marginalise out the component assignments (sum over all possible assignments so they no longer appear in the expression) and sample only the parameters directly. This is simpler to implement but means we can't recover which regime each year belongs to (without a separate step). The marginalised likelihood is what makes our log-likelihood function work:
The Earthquake Data
Our dataset comes from Zucchini, MacDonald and Langrock (2016), Hidden Markov Models for Time Series: An Introduction Using R, page 10. They use this same data to motivate Hidden Markov Models, noting that a simple mixture model ignores the temporal structure (whether the regime persists from year to year). An HMM would add transition probabilities between regimes. We explored HMMs in our Hidden Markov Models post.
Further Reading
- The original MCMC paper: Metropolis et al. (1953), "Equation of State Calculations by Fast Computing Machines"
- The Hastings generalisation: Hastings (1970), "Monte Carlo Sampling Methods Using Markov Chains and Their Applications"
- Data augmentation for mixtures: Tanner & Wong (1987), "The Calculation of Posterior Distributions by Data Augmentation"
- Comprehensive MCMC reference: Robert & Casella (2004), Monte Carlo Statistical Methods, Springer
- The earthquake dataset source: Zucchini, MacDonald & Langrock (2016), Hidden Markov Models for Time Series, Chapter 1
- Next step: Our Hierarchical Bayesian Regression with PyMC post shows how a modern PPL automates the sampling
Interactive Tools
- Markov Chain Simulator: Explore Markov chain dynamics and transition matrices interactively
- Distribution Explorer: Visualise the Poisson and other distributions used in this mixture model
Related Posts
- MCMC Island Hopping: Understanding Metropolis-Hastings (MH fundamentals on a discrete example)
- Gaussian Mixture Models: EM in Practice (the EM alternative to MCMC for mixture models)
- From MLE to Bayesian Inference (why go Bayesian in the first place)
- Hidden Markov Models: When Clusters Have Memory (adding temporal structure to mixture models)
- Five Markets, 22 Years: Every Crisis from Price Data (another regime-detection approach, applied to financial markets)
- Custom Likelihoods in PyMC: One-Inflated Beta Regression (custom probabilistic models in PyMC for awkward, non-standard data)
- Bayesian Online Changepoint Detection in Python (online Bayesian changepoint inference, the streaming cousin of this regime model)







