| name | bayesian-inference |
| description | Bayesian parameter estimation with MCMC (emcee) and probabilistic programming (PyMC). Posterior distributions, corner plots, model evidence, convergence diagnostics. Use when you need full posterior distributions, not just point estimates. |
| category | physics |
| version | 1.0.0 |
| author | Synthetic Sciences |
| license | MIT |
| tags | ["Bayesian","MCMC","Posterior","emcee","PyMC","Inference","Uncertainty","Physics"] |
| dependencies | ["emcee>=3.1.0","corner>=2.2.0","scipy>=1.11.0","numpy>=1.24.0","matplotlib>=3.7.0"] |
Bayesian Inference
Overview
Full Bayesian parameter estimation using MCMC sampling. Get posterior distributions (not just best-fit values), compute credible intervals, compare models via Bayes factors, and diagnose sampler convergence.
When to Use
- You need full posterior distributions (not just point estimates)
- Parameters are correlated and you want the joint posterior
- You want to compare models using Bayesian evidence
- Prior information is available and should be incorporated
- Error bars from least-squares seem unreliable
Do NOT Use When
- A simple
curve_fit with error bars is sufficient (use physics-fitting)
- You have > 20 parameters (consider variational inference instead)
- Likelihood is cheap and you want speed (least-squares is faster)
Core Workflows
1. MCMC with emcee (Ensemble Sampler)
import numpy as np
import emcee
import corner
import matplotlib.pyplot as plt
def model(t, A, gamma, omega):
return A * np.exp(-gamma * t) * np.cos(omega * t)
t_data = np.linspace(0, 10, 50)
y_true = model(t_data, 2.0, 0.3, 1.5)
y_err = 0.1 * np.ones_like(t_data)
y_data = y_true + y_err * np.random.randn(len(t_data))
def log_prior(theta):
A, gamma, omega = theta
if A < 0 or gamma < 0 or omega < 0:
return -np.inf
A > gamma > omega > :
-np.inf
():
A, gamma, omega = theta
y_model = model(t, A, gamma, omega)
chi2 = np.(((y - y_model) / yerr)**)
- * chi2 - np.(np.log(yerr)) - * (y) * np.log(*np.pi)
():
lp = log_prior(theta)
np.isfinite(lp):
-np.inf
lp + log_likelihood(theta, t, y, yerr)
ndim =
nwalkers =
p0 = np.array([, , ]) + * np.random.randn(nwalkers, ndim)
sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability,
args=(t_data, y_data, y_err))
sampler.run_mcmc(p0, , progress=)
flat_samples = sampler.get_chain(discard=, thin=, flat=)
()