Bayesian Inference Primer
Bayesian inference updates beliefs about parameters θ given data D using Bayes' theorem:
p(θ|D) ∝ p(D|θ) × p(θ)
- Posterior p(θ|D): Updated belief.
- Likelihood p(D|θ): How well θ explains D.
- Prior p(θ): Initial belief before seeing D.
We'll use coin flips: fair coin p(heads) = θ. Data: 7 heads in 10 flips (D = [H,H,H,H,H,H,H,T,T,T]).
1. Priors
Priors encode pre-data knowledge. Types:
- Informative: Strong beliefs (e.g., θ ~ Beta(20,5) for biased coin).
- Non-informative: Weak/vague (e.g., uniform θ ~ Beta(1,1) = Uniform(0,1)).
- Jeffreys: Invariant under reparameterization.
Example: Beta(α,β) prior for θ ∈ [0,1]. Mean = α/(α+β).
- Uniform prior: Beta(1,1), mean=0.5.
- For our coin: Beta(2,2) prior (slightly favors fair).
Prior predictive: p(D) = ∫ p(D|θ) p(θ) dθ.
2. Likelihood
p(D|θ) = probability of D given θ. For binomial: Bin(n=10,k=7|θ) = C(10,7) θ^7 (1-θ)^3.
Numeric: θ=0.6 → lik = 120 × (0.6)^7 × (0.4)^3 ≈ 0.215.
Likelihood peaks at θ̂ = k/n = 0.7 (MLE).
3. Posteriors
Posterior ∝ likelihood × prior. Often unnormalized; normalize via ∫ [...] dθ.
Numeric Example (conjugacy next): Uniform prior Beta(1,1).
Posterior = Beta(1+7,1+3) = Beta(8,4), mean=8/12≈0.667.
- 95% credible interval (next): [0.39, 0.89] (via Beta quantiles).
4. Conjugacy
Prior family closed under likelihood → analytic posterior.
- Beta-Binomial: Prior Beta(α,β) + Bin(n,k) → Posterior Beta(α+k, β+n-k).
Full Numeric:
- Prior: Beta(2,2), mean=0.5.
- Data: k=7,n=10.
- Posterior: Beta(2+7,2+3)=Beta(9,5), mean=9/14≈0.643.
- PDF at θ=0.6: ∝ [0.6^7 0.4^3] × Beta(2,2;0.6) (normalize to sum=1).
Non-conjugate? Use numerical methods (below).
5. Credible Intervals
Highest Density Interval (HDI): Shortest interval containing 95% posterior mass (vs. CI's long-run freq coverage).
Numeric: Posterior Beta(9,5).
- Mean=0.643, sd≈0.14.
- 95% HDI: [0.42, 0.84] (equal-tail: 2.5th=0.40, 97.5th=0.86).
- Prob(θ>0.5|D)=1 - BetaCDF(0.5;9,5)≈0.89.
Interpret: 89% prob θ>0.5 given data+prior.
6. MCMC/VI: Sampling Complex Posteriors
When non-conjugate/high-dim, approximate posterior via samples.
MCMC (Markov Chain Monte Carlo)
Samples from p(θ|D) via random walk.
- Metropolis-Hastings: Propose θ' ~ q(θ'|θ), accept w.p. min(1, [p(θ'|D)/p(θ|D)] × [q(θ|θ')/q(θ'|θ)] ).
- Chains converge to posterior.
Tiny Numeric Example (1D Gaussian posterior, mean μ, sd=1; true post N(0.5,0.5^2)):
- Start θ=0.
- 3 steps (normal prop sd=0.3):
- Prop 0.2, ratio=exp(-(0.2-0.5)^2/0.5 + (0-0.5)^2/0.5)=1.47>1 → accept.
- Prop 0.1, ratio=0.89 → accept (rand u<0.89).
- Prop 0.4, ratio=1.22 → accept.
- Samples: [0,0.2,0.1,0.4] → sample mean≈0.18 (more steps →0.5).
Tools: Stan/PyMC (NUTS sampler).
VI (Variational Inference)
Optimize q(θ;φ) ≈ p(θ|D) by KL-divergence min: ELBO = E_q[log p(D,θ)] - E_q[log q].
- Fast, approx (e.g., mean-field Gaussian).
Example: For Beta post, VI Gaussian q~N(μ,σ^2) fits mean/var exactly.
7. Model Checking
Does model predict data well? Posterior Predictive Checks (PPC).
- Draw θ* ~ p(θ|D), y* ~ p(y|θ*).
- Compare y* dist to observed y (e.g., T(y*)=mean(y*) vs T(y)=0.7).
Numeric: Beta(9,5) post → θ~Beta(9,5), y~Bin(10,θ*).
- 1000 y*: mean(y*)=E[E[y*|θ*]]=E[10θ*]=10*0.643=6.43 (close to 7).
- P(|mean(y*)-7|> |0.7-7| | D) small → good fit.
- If data=10 heads, post mean~0.9, pred mean=9 → fits.
Bayesian p-value: P(T(y*) ≥ T(y) | D).
8. Decision Theory
Choose action a to minimize expected posterior loss:
L(a,θ) = loss if truth=θ, act=a.
Risk = E_θ|D [L(a,θ)] = ∫ L(a,θ) p(θ|D) dθ.
Examples (bet on θ>0.5? a=0/1; 0-1 loss):
- Posterior prob(θ>0.5)=0.89 → choose a=1, risk=0.11 (vs. a=0 risk=0.89).
Quadratic loss L(a,θ)=(a-θ)^2 → Bayes act = E[θ|D] (post mean).
Numeric Decision:
- Actions: Bet "fair" (guess=0.5) or "heads-biased" (0.7).
- Loss: |guess - θ|.
- Post Beta(9,5): E[|0.5-θ|]=0.143, E[|0.7-θ|]=0.112 → choose 0.7.
| Prior | Post Mean | HDI(95%) | P(θ>0.5) |
|---|
| Beta(1,1) | 0.70 | [0.46,0.88] | 0.96 |
| Beta(2,2) | 0.64 | [0.42,0.84] | 0.89 |
Bayes: Iterative belief updating. Scale with hierarchies/MCMC for real models. Code in PyMC/Stan for practice! 🕵️