d8a80f6272
이름만 다른(표기 변형) [[위키링크]]를 대상 문서의 canonical 제목으로 치환해 끊겼던 1,200개 링크를 연결. 제목/파일명 정규화 일치만 적용하고 별칭 매칭은 과병합 위험으로 제외(애매성 가드). 원본은 _link_reconcile_backup/ 에 백업. 도구: Datacollect/scripts/link_reconcile_apply.mjs Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
160 lines
5.1 KiB
Markdown
160 lines
5.1 KiB
Markdown
---
|
||
id: wiki-2026-0508-markov-chain-monte-carlo
|
||
title: Markov Chain Monte Carlo (MCMC)
|
||
category: 10_Wiki/Topics
|
||
status: verified
|
||
canonical_id: self
|
||
aliases: [MCMC, Metropolis-Hastings, Gibbs Sampling, HMC, NUTS]
|
||
duplicate_of: none
|
||
source_trust_level: A
|
||
confidence_score: 0.9
|
||
verification_status: applied
|
||
tags: [bayesian, sampling, mcmc, hmc, pymc, numpyro]
|
||
raw_sources: []
|
||
last_reinforced: 2026-05-10
|
||
github_commit: pending
|
||
tech_stack: { language: Python, framework: PyMC/NumPyro }
|
||
---
|
||
|
||
# Markov Chain Monte Carlo (MCMC)
|
||
|
||
## 매 한 줄
|
||
> **"매 MCMC = stationary distribution이 target인 chain 만들기"**. 정규화 상수 모르고도 posterior 샘플 가능.
|
||
|
||
## 매 핵심
|
||
### 매 알고리즘
|
||
- **Metropolis-Hastings**: propose q(x'|x), accept α=min(1, π(x')q(x|x')/(π(x)q(x'|x))).
|
||
- **Random walk MH**: q = Normal(x, σ²). σ가 acceptance 결정.
|
||
- **Gibbs**: 조건부 p(x_i | x_{-i}) 순차 샘플. conjugate에 강함.
|
||
- **Slice sampling**: 보조 변수, tuning 적음.
|
||
- **HMC (Hamiltonian)**: gradient + leapfrog. high-dim 효율.
|
||
- **NUTS**: HMC trajectory 자동 결정. Stan/PyMC/NumPyro 기본.
|
||
- **SMC, parallel tempering**: multi-modal에 유리.
|
||
|
||
### 매 진단
|
||
- **Trace plot**: chain 안정성 시각 검사
|
||
- **R̂ (Gelman-Rubin)**: 다중 chain 수렴, <1.01 권장
|
||
- **ESS (effective sample size)**: 자기상관 보정 샘플 수
|
||
- **Energy diagnostic** (HMC): divergent transitions 0 목표
|
||
- **Posterior predictive check**: model fit
|
||
|
||
### 매 응용
|
||
1. Bayesian posterior 추정 (intractable normalizer)
|
||
2. Hierarchical models (multilevel regression)
|
||
3. Latent variable models
|
||
4. Bayesian deep learning (BNN, variational alternative)
|
||
5. Phylogenetics, epidemiology
|
||
|
||
## 💻 패턴
|
||
### Metropolis-Hastings (numpy)
|
||
```python
|
||
import numpy as np
|
||
def mh(log_target, x0, n=10000, step=0.5, rng=np.random.default_rng()):
|
||
x = np.array(x0, dtype=float); samples = [x.copy()]
|
||
log_p = log_target(x); accepts = 0
|
||
for _ in range(n):
|
||
x_new = x + rng.normal(scale=step, size=x.shape)
|
||
log_p_new = log_target(x_new)
|
||
if np.log(rng.uniform()) < log_p_new - log_p:
|
||
x, log_p = x_new, log_p_new; accepts += 1
|
||
samples.append(x.copy())
|
||
return np.array(samples), accepts / n # target ~0.234 (high-d), 0.44 (1d)
|
||
```
|
||
|
||
### Gibbs sampling (bivariate normal)
|
||
```python
|
||
def gibbs_bvn(rho, n=10000, rng=np.random.default_rng()):
|
||
x = y = 0.0; out = np.empty((n, 2))
|
||
for i in range(n):
|
||
x = rng.normal(rho * y, np.sqrt(1 - rho**2))
|
||
y = rng.normal(rho * x, np.sqrt(1 - rho**2))
|
||
out[i] = (x, y)
|
||
return out
|
||
```
|
||
|
||
### PyMC (NUTS)
|
||
```python
|
||
import pymc as pm, numpy as np
|
||
y = np.random.normal(2, 1, 100)
|
||
with pm.Model() as m:
|
||
mu = pm.Normal("mu", 0, 10)
|
||
sigma = pm.HalfNormal("sigma", 1)
|
||
pm.Normal("y", mu, sigma, observed=y)
|
||
idata = pm.sample(2000, tune=1000, chains=4, target_accept=0.9)
|
||
print(pm.summary(idata, var_names=["mu", "sigma"])) # r_hat, ess
|
||
```
|
||
|
||
### NumPyro (JAX, fast)
|
||
```python
|
||
import jax, numpyro
|
||
import numpyro.distributions as dist
|
||
from numpyro.infer import MCMC, NUTS
|
||
|
||
def model(y):
|
||
mu = numpyro.sample("mu", dist.Normal(0, 10))
|
||
sigma = numpyro.sample("sigma", dist.HalfNormal(1))
|
||
numpyro.sample("obs", dist.Normal(mu, sigma), obs=y)
|
||
|
||
mcmc = MCMC(NUTS(model), num_warmup=1000, num_samples=2000, num_chains=4)
|
||
mcmc.run(jax.random.PRNGKey(0), y=y)
|
||
mcmc.print_summary()
|
||
```
|
||
|
||
### Diagnostics (ArviZ)
|
||
```python
|
||
import arviz as az
|
||
az.plot_trace(idata)
|
||
az.plot_rank(idata)
|
||
print(az.rhat(idata).max(), az.ess(idata).min())
|
||
az.plot_pair(idata, divergences=True)
|
||
```
|
||
|
||
### 효율 팁
|
||
```python
|
||
# 1) Reparameterize (non-centered): theta = mu + sigma * z, z~N(0,1)
|
||
# 2) target_accept 0.9~0.99 if divergences
|
||
# 3) 표준화/스케일링 → leapfrog 안정
|
||
# 4) Initial values: pm.find_MAP() or jitter+adapt_diag
|
||
```
|
||
|
||
## 매 결정 기준
|
||
| 상황 | Sampler |
|
||
|---|---|
|
||
| Low-dim, custom posterior | MH (간단) |
|
||
| Conjugate hierarchical | Gibbs |
|
||
| Continuous, gradient 가능 | NUTS/HMC |
|
||
| Discrete latent | MH within Gibbs, SMC |
|
||
| Multi-modal | Parallel tempering, SMC |
|
||
| 대용량 / GPU | NumPyro (JAX), BlackJAX |
|
||
| 빠른 prod 근사 | VI (대안), Laplace |
|
||
|
||
**기본값**: continuous → NumPyro/PyMC NUTS. Discrete → Gibbs / SMC.
|
||
|
||
## 🔗 Graph
|
||
- 부모: [[Bayesian Inference]]
|
||
- 변형: [[Metropolis-Hastings]], [[Gibbs-Sampling]], [[NUTS]]
|
||
- 응용: [[Bayesian-Regression]]
|
||
- Adjacent: [[Variational-Inference]], [[Stan]]
|
||
|
||
## 🤖 LLM 활용
|
||
**언제**: 모델 작성, sampler 선택, divergence 진단 가이드.
|
||
**언제 X**: 복잡 hierarchical model 검증은 도메인 전문가 + posterior predictive.
|
||
|
||
## ❌ 안티패턴
|
||
- Centered hierarchical에 NUTS 그대로 (divergences) → non-centered
|
||
- Single chain → R̂ 불가, 수렴 진단 X
|
||
- Burn-in/warmup 무시
|
||
- Acceptance rate 99% (step 너무 작음) or 1% (너무 큼)
|
||
- Trace plot 안 보고 mean만 신뢰
|
||
- VI로 충분한데 MCMC 돌리기 (시간 낭비)
|
||
|
||
## 🧪 검증 / 중복
|
||
- Verified (Gelman BDA3, Neal HMC review, Hoffman NUTS, PyMC/NumPyro docs). 신뢰도 A.
|
||
- 중복: 없음.
|
||
|
||
## 🕓 Changelog
|
||
| 날짜 | 변경 |
|
||
|---|---|
|
||
| 2026-05-08 | Phase 1 |
|
||
| 2026-05-10 | Manual cleanup — PyMC/NumPyro 패턴, ArviZ diagnostic |
|