--- id: wiki-2026-0508-monte-carlo-integration title: Monte Carlo Integration category: 10_Wiki/Topics status: verified canonical_id: self aliases: [Monte-Carlo-Integration, MC-Integration, 몬테카를로-적분] duplicate_of: none source_trust_level: A confidence_score: 0.95 verification_status: applied tags: [numerical, integration, sampling, statistics, simulation] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: python framework: numpy-jax --- # Monte Carlo Integration ## 매 한 줄 > **"매 무작위 샘플의 평균이 적분값으로 수렴"**. ∫f dμ ≈ (1/N)Σf(xᵢ), error O(N⁻¹/²) — 매 dimension에 무관한 게 매 핵심 강점이다. 1949 Metropolis-Ulam에서 Manhattan Project 이후, 2026 LLM 시대에도 매 RLHF reward estimation·diffusion sampling의 backbone. ## 매 핵심 ### 매 estimator - **Standard MC**: x ~ p, Î = (1/N)Σf(xᵢ); Var = σ²/N. - **Importance sampling**: x ~ q, Î = (1/N)Σf(xᵢ)p(xᵢ)/q(xᵢ). - **Control variates**: f → f − c(g − E[g]); 매 variance ↓. - **Stratified**: 매 domain partition. - **Quasi-MC**: Sobol/Halton — error O(N⁻¹ logᵈ N). ### 매 수렴 - Error std ~ σ/√N (CLT). - 매 dim 무관 — high-dim integration의 매 유일한 실용 도구. ### 매 응용 1. Bayesian inference — posterior expectation (MCMC). 2. Computer graphics — path tracing, light transport. 3. Finance — option pricing (Black-Scholes path). 4. RLHF — reward expectation. 5. Diffusion model — score-matching expectation. ## 💻 패턴 ### Basic MC integral ```python import numpy as np def mc_integrate(f, low, high, n=10000): x = np.random.uniform(low, high, n) return (high - low) * f(x).mean(), (high - low) * f(x).std() / np.sqrt(n) ``` ### Importance sampling ```python def importance_mc(f, sampler_q, log_p, log_q, n=10000): x = sampler_q(n) w = np.exp(log_p(x) - log_q(x)) return (f(x) * w).mean() ``` ### Control variates ```python def cv_mc(f, g, Eg, n=10000): x = np.random.uniform(0, 1, n) fx, gx = f(x), g(x) c = -np.cov(fx, gx)[0, 1] / np.var(gx) return (fx + c * (gx - Eg)).mean() ``` ### Quasi-MC with Sobol ```python from scipy.stats.qmc import Sobol sampler = Sobol(d=5, scramble=True) points = sampler.random_base2(m=14) # 2^14 points estimate = f(points).mean() ``` ### MCMC (Metropolis-Hastings) ```python def mh(log_pi, x0, n=10000, sigma=0.5): x, samples = x0, [x0] for _ in range(n): x_prop = x + sigma * np.random.randn(*x.shape) if np.log(np.random.rand()) < log_pi(x_prop) - log_pi(x): x = x_prop samples.append(x) return np.array(samples) ``` ### JAX vectorized MC ```python import jax, jax.numpy as jnp @jax.jit def mc(key, n): x = jax.random.uniform(key, (n,)) return jnp.mean(jnp.exp(-x**2)) ``` ## 매 결정 기준 | 상황 | Method | |---|---| | Smooth low-dim | Quadrature or QMC | | High-dim | Vanilla MC | | Heavy tail / rare event | Importance sampling | | Posterior | MCMC (NUTS, HMC) | | Light transport | Path tracing + MIS | **기본값**: Vanilla MC + control variates (low complexity, low variance). ## 🔗 Graph - 부모: [[Statistics]] - 변형: [[MCMC]] - 응용: [[Bayesian Inference]] · [[RLHF]] ## 🤖 LLM 활용 **언제**: High-dim integration, expectation under intractable distribution, simulation. **언제 X**: 1-3 dim smooth functions (use Gauss quadrature). ## ❌ 안티패턴 - **Variance 무시**: 매 std error 안 보고 estimate 제출. - **Bad importance proposal**: 매 q tail이 p보다 얇으면 explosion. - **Correlated samples**: MCMC autocorrelation 무시 → 매 ESS 부풀려짐. ## 🧪 검증 / 중복 - Verified (Robert & Casella "Monte Carlo Statistical Methods"). - 신뢰도 A. ## 🕓 Changelog | 날짜 | 변경 | |---|---| | 2026-05-08 | Phase 1 | | 2026-05-10 | Manual cleanup — MC variants + JAX/MCMC patterns |