Theory¶
Bayesian model updating¶
Given a model with parameters \(\theta\) and observed data \(y\), Bayes' theorem gives the posterior:
where:
- \(p(\theta)\) is the prior — knowledge about \(\theta\) before seeing data.
- \(p(y \mid \theta)\) is the likelihood — how probable the data is given \(\theta\).
- \(p(y) = \int p(y \mid \theta)\, p(\theta)\, d\theta\) is the evidence (marginal likelihood).
All samplers in mcmckit work with the log-posterior:
Metropolis-Hastings¶
At each step, a proposal \(\theta^* \sim q(\cdot \mid \theta_t)\) is accepted with probability:
For a symmetric Gaussian proposal \(q(\theta^* \mid \theta_t) = \mathcal{N}(\theta_t, \Sigma)\) this simplifies to:
Optimal acceptance rate for a \(d\)-dimensional Gaussian target is approximately 23.4%.
MALA — Metropolis-adjusted Langevin Algorithm¶
MALA adds a gradient drift to the proposal:
The gradient bias makes proposals more likely to move towards high-density regions. Because the proposal is asymmetric, the MH correction uses the full ratio:
Optimal acceptance rate for MALA is approximately 57.4%.
Adaptive algorithms¶
RAM — Robust Adaptive Metropolis¶
RAM (Vihola 2012) adapts the Cholesky factor \(S_t\) of the proposal covariance via a rank-1 update targeting a desired acceptance rate \(\alpha^*\):
where \(r_t = \theta^* - \theta_t\) and \(\eta_t = t^{-\gamma}\) (default \(\gamma = 0.51\)). The target rate is \(\alpha^* = 0.234\).
DRAM — Delayed Rejection Adaptive Metropolis¶
DRAM (Haario et al. 2006) combines two ideas:
Adaptive Metropolis (AM): Update the proposal covariance using the empirical covariance of the chain so far:
Delayed Rejection (DR): If the first proposal \(\theta^*_1\) is rejected, a second proposal \(\theta^*_2\) is drawn from a smaller proposal and accepted with a corrected probability (Tierney-Mira correction) that maintains detailed balance.
AdaptiveMALA¶
Adapts the MALA step size \(\varepsilon\) in log-space:
where \(\eta_t = t^{-\gamma}\) (default \(\gamma = 0.6\)).
TMCMC — Transitional Markov Chain Monte Carlo¶
TMCMC (Ching & Chen 2007) bridges the prior to the posterior through a sequence of tempered intermediate distributions:
At each stage \(j\):
- Importance weights: \(w_j(\theta) \propto p(y\mid\theta)^{\Delta\beta_j}\), \(\Delta\beta_j = \beta_j - \beta_{j-1}\)
- Log-evidence contribution:
computed in a numerically stable way as \(\log(\text{mean}(\exp(\Delta\beta_j \cdot \ell_i - \Delta\beta_j \cdot \max \ell)))+ \Delta\beta_j \cdot \max \ell\).
- Systematic resampling of particles according to \(w_j\).
- MH rejuvenation using the weighted covariance as proposal.
Adaptive \(\beta\) schedule: at each stage, \(\Delta\beta\) is chosen by bisection so that the effective sample size ratio \(\text{ESS}/N \approx \tau\) (default \(\tau = 0.5\)).
Gibbs sampling¶
Metropolis-within-Gibbs updates one block of parameters at a time, holding the rest fixed. For block \(k\):
accepted with probability \(\min(1, \exp[\log p(\theta^* \mid y) - \log p(\theta_t \mid y)])\).
This preserves the correct joint distribution even though only one block moves per step.
Gaussian noise model¶
For a forward model \(f(\theta)\) and observed data \(y\), the Gaussian likelihood is:
Estimated \(\sigma\)¶
The last element of \(\theta\) is \(\log\sigma\) (log-transform ensures positivity) and is sampled jointly.
Marginalised \(\sigma\)¶
With an Inverse-Gamma\((\alpha, \beta)\) prior on \(\sigma^2\), integration over \(\sigma^2\) is analytic for any \(f(\theta)\):
The posterior mean of \(\sigma\) is \(\sqrt{(\beta + \text{RSS}/2)\,/\,(\alpha + n/2 - 1)}\).
Per-channel noise¶
When different measurement types have different noise levels (e.g. frequency 1 vs frequency 2), observations are partitioned into channels. Each channel \(i\) has its own \(\sigma_i\) and the total log-likelihood is:
In the marginalised case, each channel has an independent InvGamma prior and is integrated separately.
Convergence diagnostics¶
Effective sample size¶
MCMC samples are correlated, so \(N\) samples are worth fewer than \(N\) independent draws. The effective sample size (ESS) quantifies this:
where \(\rho_k\) is the autocorrelation at lag \(k\). The sum is truncated using Geyer's (1992) initial positive sequence criterion to avoid noise blow-up at large lags.
Autocorrelation¶
The normalised autocorrelation at lag \(k\) for a scalar chain \(\{\theta_t\}\) is:
Computed via FFT for efficiency. Slow decay (high autocorrelation at large lags) indicates poor mixing — increase the proposal covariance or run longer.
Gelman-Rubin \(\hat{R}\)¶
Given \(M\) independent chains of length \(N\), split each chain in half to obtain \(2M\) sub-chains. Define the between-chain variance \(B\) and within-chain variance \(W\):
The pooled variance estimate is:
and the statistic is:
\(\hat{R} \approx 1\) means all chains sample the same distribution. A common threshold is \(\hat{R} < 1.01\). The split-chain variant (Vehtari et al., 2021) also detects non-stationarity within a single chain.
Model class selection¶
The evidence as a model score¶
The evidence (marginal likelihood) of a model \(M\) is:
It is the probability of the data averaged over the prior. A model that fits the data well and does so with a compact prior gets a high evidence — this is the mathematical expression of Occam's razor: unnecessary parameters are penalized because they spread prior probability over regions that do not contribute to the fit.
Bayes factor¶
Given two competing models \(M_1\) and \(M_2\), the Bayes factor is:
It is the factor by which the data update the prior odds in favour of \(M_1\). In log form:
Jeffreys scale¶
Interpretation of \(\log_{10} B_{12}\) (evidence in favour of \(M_1\)):
| \(\log_{10} B_{12}\) | Interpretation |
|---|---|
| \(> 2\) | Decisive |
| \(1\) – \(2\) | Strong |
| \(0.5\) – \(1\) | Substantial |
| \(< 0.5\) | Barely worth mentioning |
Negative values indicate evidence in favour of \(M_2\).
Evidence from TMCMC¶
TMCMC accumulates the log-evidence as a byproduct of the tempering stages. At each stage the incremental contribution is:
which is estimated from the particles without any additional computation. No other sampler in mcmckit computes the evidence — use TMCMC for model comparison.
Workflow¶
import mcmckit as mc
comp = mc.ModelComparison(
models=[
("M1", problem_1, prior_samples_1),
("M2", problem_2, prior_samples_2),
],
tmcmc_kwargs={"n_particles": 1000},
)
comp.run()
comp.summary()
For a standalone pairwise comparison:
bf = mc.bayes_factor(result_1.log_evidence, result_2.log_evidence)
# {'log_bf': ..., 'log10_bf': ..., 'bf': ..., 'preferred': 'M1', 'evidence': 'Decisive'}
Bayesian Model Averaging¶
When no model is decisively preferred — or when you want predictions that are robust to model uncertainty — Bayesian Model Averaging (BMA) combines all models weighted by their posterior probability.
Model posterior probabilities¶
Assuming equal prior model probabilities \(p(M_k) = 1/K\):
With informative prior model probabilities:
BMA posterior predictive¶
For any quantity of interest \(Q = f(\theta)\):
The BMA predictive is a mixture of each model's posterior predictive, weighted by \(w_k\). In practice, this is approximated by drawing \(\lfloor w_k \cdot N \rfloor\) posterior samples from model \(k\) and evaluating \(f(\theta)\) at those samples.
Behaviour¶
- When evidence is decisive (\(w_{\text{best}} \approx 1\)), BMA collapses to the best model's prediction.
- When two models are comparably supported (\(w_1 \approx w_2 \approx 0.5\)), BMA genuinely averages the two predictive distributions — potentially widening the credible band to reflect model uncertainty.
- BMA predictions are always more conservative (wider bands) than conditioning on a single model.