পাঠ ৪০ · ৪৫-এর মধ্যে · মডিউল ৫

MCMC ও Gibbs sampling

Markov Chain Monte Carlo — Bayesian sampling
৮ মিনিট পড়া উচ্চ · Advanced NumPy / PyMC

এই পাঠে যা শিখবেন

  • Bayesian inference — posterior intractable, কেন sampling
  • Markov chain — stationary distribution as posterior
  • Metropolis-Hastings — propose, accept-reject ratio
  • Gibbs sampling — conditional sampling for high-D
  • PyMC দিয়ে practical Bayesian linear regression

১ · Bayesian inference — posterior intractable

Bayes-এর সূত্র — observed data $\mathbf{X}$ ও parameter $\boldsymbol{\theta}$:

$$P(\boldsymbol{\theta} \mid \mathbf{X}) = \frac{P(\mathbf{X} \mid \boldsymbol{\theta}) P(\boldsymbol{\theta})}{P(\mathbf{X})}$$

Numerator সহজ — likelihood × prior। কিন্তু denominator $P(\mathbf{X}) = \int P(\mathbf{X} \mid \boldsymbol{\theta}) P(\boldsymbol{\theta}) d\boldsymbol{\theta}$ — high-D integral, intractable।

Conjugate prior থাকলে closed-form (e.g., Beta-Binomial)। কিন্তু realistic model-এ — non-conjugate, complex। তখন MCMC — directly compute না করে posterior থেকে sample।

MCMC-এর core idea

একটি Markov chain design করো যার stationary distribution = target posterior। যথেষ্ট iterate করলে — chain-এর state-গুলো posterior থেকে sample-এর মত। যেকোনো expectation $\mathbb{E}_{\theta}[f(\theta)]$ — sample average থেকে।

২ · Metropolis-Hastings algorithm

Metropolis (১৯৫৩) ও Hastings (১৯৭০) — universal sampler। Setup:

  • Target distribution $\pi(\boldsymbol{\theta})$ — proportional to known function $f(\boldsymbol{\theta})$।
  • Proposal distribution $q(\boldsymbol{\theta}' \mid \boldsymbol{\theta})$ — easy to sample from।

Algorithm:

  1. Current state $\boldsymbol{\theta}^{(t)}$।
  2. Propose $\boldsymbol{\theta}' \sim q(\cdot \mid \boldsymbol{\theta}^{(t)})$।
  3. Compute acceptance ratio: $$\alpha = \min\left(1, \frac{f(\boldsymbol{\theta}') q(\boldsymbol{\theta}^{(t)} \mid \boldsymbol{\theta}')}{f(\boldsymbol{\theta}^{(t)}) q(\boldsymbol{\theta}' \mid \boldsymbol{\theta}^{(t)})}\right)$$
  4. Accept with probability $\alpha$: $\boldsymbol{\theta}^{(t+1)} = \boldsymbol{\theta}'$, else stay $\boldsymbol{\theta}^{(t+1)} = \boldsymbol{\theta}^{(t)}$।

Symmetric proposal ($q(\boldsymbol{\theta}'|\boldsymbol{\theta}) = q(\boldsymbol{\theta}|\boldsymbol{\theta}')$) — Gaussian random walk — ratio simplify হয় $f(\boldsymbol{\theta}')/f(\boldsymbol{\theta})$।

৩ · Detailed balance ও convergence

Metropolis-Hastings detailed balance condition satisfy:

$$\pi(\boldsymbol{\theta}) P(\boldsymbol{\theta} \to \boldsymbol{\theta}') = \pi(\boldsymbol{\theta}') P(\boldsymbol{\theta}' \to \boldsymbol{\theta})$$

এই condition + ergodicity (chain-এর সব state visit-able) — guarantee যে stationary distribution = $\pi$।

৪ · Gibbs sampling — high-D special case

Multi-variable $\boldsymbol{\theta} = (\theta_1, \ldots, \theta_d)$। Gibbs — প্রতিটি component conditional distribution থেকে sample (অন্যদের fix রেখে):

  1. Initialize $\boldsymbol{\theta}^{(0)}$।
  2. For $t = 1, 2, \ldots$:
    • Sample $\theta_1^{(t)} \sim P(\theta_1 \mid \theta_2^{(t-1)}, \ldots, \theta_d^{(t-1)}, \mathbf{X})$।
    • Sample $\theta_2^{(t)} \sim P(\theta_2 \mid \theta_1^{(t)}, \theta_3^{(t-1)}, \ldots, \mathbf{X})$।
    • Sample $\theta_3^{(t)} \sim P(\theta_3 \mid \theta_1^{(t)}, \theta_2^{(t)}, \theta_4^{(t-1)}, \ldots, \mathbf{X})$।
    • ...

প্রতিটি step একটি Metropolis step (acceptance ratio = 1 — always accept)। তাই Gibbs = MH special case।

Conditional distribution closed-form থাকলে — Gibbs efficient। নাহলে — Metropolis-within-Gibbs।

৫ · উদাহরণ — coin bias Bayesian estimation

Coin flip $n$ বার, $k$ head। Bias $\theta \in [0, 1]$ posterior?

Likelihood: $P(k \mid \theta, n) = \binom{n}{k} \theta^k (1-\theta)^{n-k}$।
Prior: $\theta \sim \text{Beta}(\alpha, \beta)$।
Posterior: $\text{Beta}(\alpha + k, \beta + n - k)$ — closed-form (conjugate)।

Conjugate তাই MCMC লাগে না, কিন্তু demonstration হিসেবে — Metropolis দিয়ে sample।

৬ · Convergence diagnostics

MCMC sample independent না — autocorrelated। Care needed:

  • Burn-in: initial sample-গুলো (transient phase) discard। প্রথম ১০০০-৫০০০।
  • Thinning: autocorrelation কমাতে প্রতি ১০ম sample রাখুন।
  • Trace plot: chain-এর time series — visually mixing check।
  • R-hat (Gelman-Rubin): multiple chain-এর variance ratio। ১.০ মানে converged। < ১.১ acceptable।
  • Effective sample size (ESS): autocorrelation-adjusted sample count।
  • Acceptance rate: Metropolis-এ — ২০-৪০% optimal (Gelman et al.)।
MCMC — Markov chain converges to target distribution Burn-in → mixing → posterior samples Trace plot: θ over MCMC iteration iteration → θ true θ burn-in ↓ discard initial ↓ posterior samples (use) α = min(1, f(θ')q(θ|θ') / f(θ)q(θ'|θ)) accept proposal if α > uniform(0,1) target ratio 20-40% (Gaussian RW)
MCMC trace plot — initial burn-in transient, তারপর chain settle near posterior mean। R-hat, ESS, acceptance rate — convergence diagnostic।

৭ · NumPy scratch — Metropolis

Python · NumPy
import numpy as np

# Target: posterior of coin bias
# Likelihood × prior — proportional to bias^k * (1-bias)^(n-k) * Beta(2,2)
def log_target(theta, k, n, alpha=2, beta=2):
    if not (0 < theta < 1):
        return -np.inf
    return (k + alpha - 1) * np.log(theta) + (n - k + beta - 1) * np.log(1 - theta)

def metropolis(k, n, n_iter=5000, step=0.05):
    rng = np.random.default_rng(0)
    theta = 0.5
    samples, accepts = [], 0
    for _ in range(n_iter):
        proposal = theta + rng.normal(0, step)
        log_alpha = log_target(proposal, k, n) - log_target(theta, k, n)
        if np.log(rng.uniform()) < log_alpha:
            theta = proposal
            accepts += 1
        samples.append(theta)
    return np.array(samples), accepts / n_iter

# Coin: 7 head out of 10 tosses
samples, ar = metropolis(k=7, n=10, n_iter=5000)
burn = 1000
post = samples[burn:]

print(f"Acceptance rate: {ar:.3f}")
print(f"Posterior mean: {post.mean():.3f}")
print(f"95% credible interval: ({np.percentile(post, 2.5):.3f}, "
      f"{np.percentile(post, 97.5):.3f})")
# Conjugate analytical posterior: Beta(9, 5), mean = 9/14 ≈ 0.643
print(f"Analytical mean (Beta(9,5)): {9/14:.3f}")

    
Sampler-এর posterior mean ~০.৬৪৩ — analytical Beta posterior-এর সাথে match। Acceptance rate ~৪০% — optimal range।

৮ · PyMC — production Bayesian inference

Python · PyMC
# pip install pymc
import pymc as pm
import numpy as np

# Bangladesh-like rainfall data: simple Bayesian linear regression
# rain (mm/day) = a + b * temp + noise
np.random.seed(0)
n = 100
temp = np.random.uniform(20, 35, n)
true_a, true_b = 2.0, 0.3
rain = true_a + true_b * temp + np.random.normal(0, 1.5, n)

with pm.Model() as model:
    a = pm.Normal("a", mu=0, sigma=10)
    b = pm.Normal("b", mu=0, sigma=10)
    sigma = pm.HalfNormal("sigma", sigma=5)

    rain_pred = a + b * temp
    pm.Normal("obs", mu=rain_pred, sigma=sigma, observed=rain)

    # NUTS sampler (modern, gradient-based MCMC)
    trace = pm.sample(1000, tune=500, chains=2,
                      progressbar=False, random_seed=0)

# Posterior summary
print(pm.summary(trace, var_names=["a", "b", "sigma"], round_to=3))

    
PyMC NUTS — modern Hamiltonian Monte Carlo। R-hat ~১.০ — converged। True a=2, b=0.3 — recovered with credible interval। Bayesian uncertainty quantification — frequentist linear regression-এর চেয়ে rich।

৯ · Modern alternatives

  • HMC (Hamiltonian Monte Carlo): physics-inspired — gradient ব্যবহার করে fast mixing। NUTS — auto-tuned।
  • Variational Inference: posterior-এর approximation — fast, less accurate।
  • SMC (Sequential Monte Carlo): particle-based, parallel।
  • Stochastic gradient MCMC: SGLD, large data-এ minibatch।
  • Normalizing flows: flexible posterior approximation।

Production-এ — Stan, PyMC, NumPyro, TensorFlow Probability — সবগুলো NUTS-based default।

MCMC slow — ১ লাখ data + complex model = hours/days। Variational inference faster but biased। Big data Bayesian-এ stochastic methods বা amortized inference (VAE)। Pure MCMC research/regulated industry-এ — production-এ trade-off।

ভাবনার প্রশ্ন

প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।

প্র ০১ R-hat ও effective sample size — কী measure? Convergence trustable কখন?

MCMC-এর critical diagnostic — না হলে result misleading।

R-hat (Gelman-Rubin):

  • Multiple parallel chain run।
  • Within-chain variance ($W$) ও between-chain variance ($B$)।
  • $\hat{R} = \sqrt{\frac{(n-1)/n \cdot W + B/n}{W}}$।
  • $\hat{R} \to 1$ as chains converge।
  • $\hat{R} < 1.01$ = good, $> 1.1$ = problematic।

Effective sample size (ESS):

  • Autocorrelation-aware sample count।
  • $\text{ESS} = N / (1 + 2 \sum_k \rho_k)$।
  • Highly autocorrelated → ESS << N।
  • Reliable estimate need ESS > 400 (rule of thumb)।
  • Tail estimates — ESS > 1000।

What they catch:

  • R-hat — chain disagreement (multiple modes, slow mixing)।
  • ESS — autocorrelation, slow mixing।
  • Both essential — pass each।

What they miss:

  • Multimodal posterior — chain stuck in one mode, R-hat OK।
  • "All chains agree" doesn't mean correct।
  • Bias from finite samples।

Additional checks:

  • Trace plot visual inspection।
  • Autocorrelation plot।
  • Rank plots (Vehtari et al., ২০২১)।
  • Pair plots — joint posterior।
  • Posterior predictive check — model fit data?

Common failure:

  • Chain stuck — single mode visited।
  • Slow mixing — high autocorrelation।
  • Divergent transitions (HMC) — geometry issue।
  • Funnel posterior — Hamiltonian inefficient।

Practical workflow:

  1. 4 chains, ১০০০ tune + ১০০০ sample।
  2. R-hat < ১.০১ all parameters।
  3. ESS > ৪০০ all parameters।
  4. No divergent transition (HMC)।
  5. Trace plot looks "fuzzy caterpillar"।
  6. Posterior predictive check pass।

Diagnostic tools:

  • pm.summary(trace) — comprehensive table।
  • arviz — visualization library।
  • pm.plot_trace, pm.plot_pair।
  • Stan: cmdstanpy diagnostics।

Bangladesh case:

  • Hierarchical regression on district-level health data।
  • Group effect prior — slow mixing।
  • Reparameterize — non-centered parameterization।
  • R-hat ভাল না হলে — model investigate।

Convergence ≠ correctness:

  • Wrong model can converge cleanly।
  • Always combine with model checking।
  • Cross-validation — predictive performance।
  • Sensitivity analysis — prior choice impact।

মূল উপলব্ধি: R-hat + ESS necessary but not sufficient। Visual + posterior predictive check — full picture। Bayesian rigorous workflow — diagnostics-driven।

প্র ০২ HMC/NUTS Metropolis-এর চেয়ে এত efficient কেন? Trade-off?

MCMC-এর সবচেয়ে impactful innovation — geometric understanding।

Metropolis problem:

  • Random walk — local exploration।
  • High-D-এ — proposal step small required → slow mixing।
  • Step large → low acceptance।
  • Curse of dimensionality।

HMC (Hamiltonian Monte Carlo):

  • Physics analogy — particle in potential energy field।
  • Potential = $-\log \pi(\boldsymbol{\theta})$।
  • Add momentum variable $\mathbf{p}$।
  • Hamiltonian dynamics simulation — leapfrog integrator।
  • Long, distance-far moves।
  • High acceptance preservation।

NUTS (No-U-Turn Sampler):

  • Hoffman-Gelman (২০১৪) — HMC-এর auto-tuning।
  • Step size, trajectory length auto-determined।
  • "U-turn" detection — turning back stop।
  • User-friendly — black-box।

Why faster:

  • Gradient-informed — efficient direction।
  • Long jumps — independent sample-এর মত।
  • High-D scale — well।
  • Geometric structure exploit।

Trade-offs:

(১) Differentiability:

  • HMC need gradient — discrete parameter problematic।
  • Categorical, mixture indicator — Metropolis fallback।
  • Mixed model — handle с care।

(২) Compute cost:

  • Per iteration HMC slow — gradient compute।
  • But effective sample better — net faster।

(৩) Geometry:

  • Funnel posterior — HMC struggle।
  • Reparameterization — non-centered।
  • Riemannian manifold HMC — advanced fix।

(৪) Tuning:

  • HMC step size, mass matrix tuning।
  • NUTS automate।
  • Warmup phase critical।

Comparison Metropolis vs NUTS:

  • Metropolis ESS/sec ~10-100।
  • NUTS ESS/sec ~1000-10000।
  • 10-100× speedup typical।

Modern landscape:

  • Stan — pioneer, rock-solid NUTS।
  • PyMC — Aesara/JAX backend।
  • NumPyro — JAX, GPU।
  • TensorFlow Probability।

When Metropolis still:

  • Discrete parameter।
  • Black-box likelihood (no gradient)।
  • Educational/simple problem।
  • Custom proposal exploit problem structure।

Beyond NUTS:

  • Variational inference — speed, accuracy trade।
  • Normalizing flows — flexible posterior।
  • Score-based sampling।
  • Diffusion-based MCMC।

মূল উপলব্ধি: Metropolis universal but inefficient। NUTS modern default — gradient-aware। Bayesian production-এ NUTS standard, Metropolis legacy।

প্র ০৩ MCMC-এর সাথে variational inference comparison — কখন কোনটা?

Bayesian inference-এর দু'টি পথ — accuracy vs speed trade-off।

MCMC:

  • Sample from exact posterior।
  • Asymptotically unbiased।
  • Slow — many iterations।
  • Memory: store many samples।

VI (variational inference):

  • Posterior approximation (Gaussian family)।
  • Optimization, not sampling।
  • Fast — gradient descent on ELBO।
  • Biased — approximation family limits।

কখন MCMC:

  • Accuracy critical — clinical, financial।
  • Posterior multimodal — VI struggles।
  • Heavy tail — variational Gaussian misses।
  • Posterior predictive checks reliable।
  • Small data — compute affordable।

কখন VI:

  • Big data — millions of points।
  • High-D — neural latent variable।
  • Real-time inference — production।
  • Quick exploration — initial modeling।
  • Approximation acceptable।

VI variants:

  • Mean-field: independent factor — fastest, biased।
  • Full-rank Gaussian: capture correlation।
  • Normalizing flows: flexible — closer to MCMC accuracy।
  • Amortized: neural network output posterior — VAE।

Common pitfalls:

VI underestimates uncertainty:

  • Mean-field decouple correlation।
  • Posterior tighter than truth।
  • Calibration miscalibrated।

VI mode-seeking:

  • KL(q || p) — reverse KL।
  • Mode-seeking, not mean-seeking।
  • Multimodal — only one mode।

Comparison metrics:

  • Posterior mean accuracy।
  • Credible interval coverage।
  • Predictive log-likelihood।
  • Wall-clock time।

Hybrid:

  • VI initialization, MCMC refine।
  • Variational MCMC — flow-based proposal।
  • Best of both worlds।

Software:

  • PyMC — both NUTS ও ADVI।
  • NumPyro — VI integrated।
  • Stan — primarily NUTS, ADVI experimental।
  • Pyro (Uber) — VI-first।

Bangladesh case:

  • Hospital outcome model — small data, MCMC।
  • Mobile app personalization — millions of users, VI।
  • Stock regime detection — moderate data, NUTS feasible।
  • Topic modeling million-document — VI essential।

মূল উপলব্ধি: Accuracy → MCMC, Scale → VI। Modern probabilistic ML — both tools, problem-specific। Pure MCMC research-grade; VI production-grade often।

প্র ০৪ Bangladesh-এ rainfall prediction-এ Bayesian model + MCMC — pipeline ও motivation?

Climate change-এর contextে Bangladesh rainfall — agriculture, flood preparedness critical।

Why Bayesian for rainfall:

  • Uncertainty critical — point forecast incomplete।
  • Hierarchical structure — district-level + national।
  • Limited historical data — prior helps।
  • Decision support — risk-aware।

Data:

  • BMD (Bangladesh Meteorological Department) station data।
  • Satellite — TRMM, GPM।
  • Reanalysis — ERA5।
  • Hourly to monthly aggregation।

Model structure:

  • Hierarchical — district nested in division nested in country।
  • Random effect — geographic, seasonal।
  • Covariates — temperature, humidity, ENSO index, IOD।
  • Spatial correlation — Gaussian process।

Likelihood:

  • Rainfall positive, skewed — log-normal বা gamma।
  • Zero-inflated — dry day frequency।
  • Hurdle model — rain/no-rain বinary + amount।

Priors:

  • Weakly informative — Cauchy on coefficient।
  • Domain prior — historical mean ~2000mm/year।
  • Hierarchical priors — district variation pool।

MCMC implementation:

  • PyMC বা Stan।
  • NUTS sampler।
  • 4 chains, 2000 sample post-warmup।
  • Reparameterize hierarchical (non-centered)।

Validation:

  • Posterior predictive check।
  • Cross-validation — block (year-out)।
  • Calibration — credible interval coverage।
  • Compare with frequentist baseline।

Challenges:

  • Data quality: station gap, missing।
  • Computational: spatial GP-এ scale।
  • Climate change: non-stationary।
  • Local effects: Sundarbans vs Sylhet differ।
  • Stakeholder communication: probability distribution explain।

Use cases:

  • Flood prediction: probabilistic flood map।
  • Agriculture: sowing date recommendation।
  • Water management: reservoir operation।
  • Insurance: crop insurance pricing।
  • Disaster preparedness: early warning।

Output:

  • Per-district probabilistic forecast।
  • Credible interval, percentile।
  • Decision-relevant probability — "P(rainfall > 200mm)"।

Communication:

  • Map-based visualization।
  • Bangla decision aids।
  • Farmer-friendly simplification।
  • Govt agency dashboard।

Stakeholders:

  • BMD, Ministry of Agriculture।
  • Department of Disaster Management।
  • BARI, BARC research।
  • WorldClimate, NASA collaboration।

Beyond MCMC:

  • Deep learning (CNN-LSTM) for satellite data।
  • Ensemble — Bayesian + ML।
  • Causal — climate change impact।

মূল উপলব্ধি: Bangladesh rainfall — perfect Bayesian use case। Hierarchical structure, data scarcity, uncertainty critical, decision-driven। MCMC computationally feasible at district level; bigger scale → variational/ML hybrid।

অনুশীলন

  1. হিসাব করুন: Metropolis acceptance ratio for symmetric proposal — current $\theta=0.4$, proposal $\theta'=0.6$, target $f(\theta) \propto \theta^2$। $\alpha$ কত?
    • Symmetric proposal → $q$ cancel।
    • $\alpha = \min(1, f(\theta')/f(\theta)) = \min(1, 0.36/0.16) = \min(1, 2.25) = 1$।
    • Always accept moving up (towards higher target)।
  2. NumPy-তে চেষ্টা: Metropolis sample from N(3, 1) using random walk।
    import numpy as np
    
    def metropolis_normal(target_mu, target_sigma, n_iter=5000, step=1.0):
        rng = np.random.default_rng(0)
        x = 0.0
        samples = []
        for _ in range(n_iter):
            x_prop = x + rng.normal(0, step)
            log_alpha = (-0.5 * (x_prop - target_mu)**2 / target_sigma**2
                         + 0.5 * (x - target_mu)**2 / target_sigma**2)
            if np.log(rng.uniform()) < log_alpha:
                x = x_prop
            samples.append(x)
        return np.array(samples)
    
    s = metropolis_normal(3, 1, n_iter=5000)
    print(f"Sample mean: {s[1000:].mean():.3f}, std: {s[1000:].std():.3f}")
    # Target: mean=3, std=1
  3. ভাবুন: Bangladesh-এ COVID infection rate Bayesian estimation — কী প্রয়োজন? Modeling consideration?
    • Data: daily case, test count, death।
    • Model: SEIR with reproduction number $R_t$।
    • Prior: $R_t \sim$ informative (1.5-3.0 SARS-CoV-2)।
    • Hierarchy: district-level।
    • Test bias: reported case < actual। Adjust।
    • MCMC: Stan, NumPyro। Computational হিসেব।
    • Output: $R_t$ posterior, hospitalization forecast।
    • Stakeholder: DGHS, district health office।
    • Caveats: data quality, behavioral change unmodeled।

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

কোড রানার কাজ না করলে? Google Colab ব্যবহার করুন।
পূর্ববর্তী পাঠ
পাঠ ৩৯ · EM Algorithm