R에서 처음부터 MCMC 구축: Brms와 Stan의 50라인 알고리즘
hackernews
|
|
📰 뉴스
#머신러닝/연구
원문 출처: hackernews · Genesis Park에서 요약 및 분석
요약
R 사용자가 brms나 Stan을 통해 베이지안 모델을 실행할 때 내부적으로 작동하는 MCMC 알고리즘의 원리를 50줄의 코드로 직접 구축해 배우는 방법을 소개합니다. 이 가이드는 9명의 방문자 중 6명이 버튼을 클릭한 실제 예시를 통해, 알고리즘이 생성한 표본이 교과서의 정답인 Beta(7, 4) 분포와 일치함을 보여줍니다. 사용자는 코드를 직접 작성하며 베이지안 도구가 실제로 데이터를 처리하는 내부 메커니즘을 명확히 이해할 수 있습니다.
본문
Build MCMC From Scratch in R: The 50-Line Algorithm Behind brms and Stan You may have used brms or Stan to fit Bayesian models and accepted that an algorithm called MCMC works behind the scenes. This post opens the box. In about 50 lines of base R, you'll build the same kind of algorithm those packages run, watch it produce samples that match the textbook closed-form answer, and end with a working understanding of what every Bayesian tool you'll ever use is doing internally. Why do we need MCMC at all? Most R users who run brms::brm(...) have no real picture of what's happening between when they press Run and when the answer appears. There's an algorithm called MCMC, it draws samples from the posterior, and that's about as far as it usually goes. The shortest path to actually understanding it is to build it. Here is the entire 50-line implementation, working on a real example, before any explanation. The setup is the same one we've been using throughout the Bayesian section: 9 visitors saw a new button, 6 of them clicked, and we want a posterior over the true click-through rate. We already know the textbook answer (it's a Beta(7, 4) distribution; the most likely rate is about 0.636). MCMC will produce thousands of samples that approximate that same answer. If our 50 lines are right, the samples should match. That's the whole thing. Let's walk through what it actually did, then check the answer against what we know to be correct. The first chunk defines log_posterior , a function that takes a candidate click-through rate and returns a number that is bigger when the rate is more plausible (given our 9 visitors and 6 clicks). It uses log-scale to avoid numerical underflow, but conceptually it's just "how good is this candidate?" The if (theta samples_A ? That's the posterior probability that B's true rate exceeds A's. A's most likely rate: 7.1%. B's most likely rate: 9.0%. Probability B beats A: 97%. That's the same answer the closed-form Beta-Binomial gave us in the Conjugate Priors post, computed by a completely different mechanism. Once you have the sampler, you can adapt this template to any model: just write a new log_post function and call metropolis_hastings() . This is the workflow that makes Bayesian analysis general. Summary The Metropolis-Hastings algorithm in one breath: propose a small random step, compare the densities at the new and old positions, accept the move with probability equal to the density ratio (capped at 1), repeat. The sequence of accepted positions is your posterior sample. | Component | What it does | Common values | |---|---|---| log_target(theta) | Returns log(prior) + log(likelihood) at theta | Specific to your problem | start | Where the chain begins | Anywhere reasonable; use multiple starts | sigma | Standard deviation of the proposal step | Tuned to give 25-50% acceptance | n_iter | Number of iterations | 5,000-10,000 for one parameter | | Burn-in | Discard the first N samples | 10-20% of n_iter | | Multiple chains | Run 3-4 from different starts | Required for convergence diagnosis | When you have one or two parameters, this 50-line implementation is enough. For real Bayesian models with many parameters, hierarchical structure, or custom likelihoods, use brms or rstan . They run a smarter, faster version of the same idea: prior plus data give samples from the posterior, accept-reject is the universal mechanism. References - Hastings, W. K. "Monte Carlo Sampling Methods Using Markov Chains and Their Applications." Biometrika 57(1), 1970. The original paper. - Robert, C. & Casella, G. Monte Carlo Statistical Methods, 2nd ed. Springer, 2004. The standard graduate-level reference. - Gelman, A., Carlin, J. B., Stern, H. S. et al. Bayesian Data Analysis, 3rd ed. Chapman & Hall, 2013. Chapter 11 covers MCMC algorithms. - Johnson, A. A., Ott, M. Q., Dogucu, M. Bayes Rules! An Introduction to Applied Bayesian Modeling, Chapman & Hall, 2022. Chapter 7 builds Metropolis-Hastings step by step. bayesrulesbook.com/chapter-7. - Solomon, N. "Learn Metropolis-Hastings Sampling with R." nicksolomon.me. A short, accessible R-focused walkthrough. - Navarro, D. "The Metropolis-Hastings algorithm." blog.djnavarro.net. Clear pedagogy on tuning, burn-in, and lag. Continue Learning - Bayesian Statistics in R, the section opener that builds the prior-likelihood-posterior intuition with simulations, ideal context for what MCMC is sampling from. - Conjugate Priors in R, the closed-form shortcut MCMC generalizes. The Beta-Binomial answer in that post is exactly what we reproduced here with the sampler. - Grid Approximation in R, the brute-force alternative to MCMC. Fine for one or two parameters; MCMC takes over from there.
Genesis Park 편집팀이 AI를 활용하여 작성한 분석입니다. 원문은 출처 링크를 통해 확인할 수 있습니다.
공유