0Pricing
R Academy · Lesson

MCMC Sampling and Diagnostics

Run sampling, inspect chains, and interpret Rhat and ESS diagnostics.

What Is MCMC?

Markov Chain Monte Carlo (MCMC) is a family of algorithms for drawing samples from a probability distribution when direct sampling is impossible. In Bayesian statistics, MCMC samples from the posterior distribution P(parameters | data).

RStan implements the No-U-Turn Sampler (NUTS), a state-of-the-art MCMC algorithm.

A Simple Stan Model

A Stan model is a text block defining data types, parameters, and the log-posterior. The simplest model estimates the mean of a normal distribution with known variance.

# library(rstan)
#
# stan_code <- '
# data {
#   int<lower=0> N;
#   vector[N] y;
# }
# parameters {
#   real mu;
#   real<lower=0> sigma;
# }
# model {
#   mu    ~ normal(0, 10);   // prior
#   sigma ~ exponential(1);   // prior
#   y     ~ normal(mu, sigma); // likelihood
# }
# '

All lessons in this course

  1. Introduction to Bayesian Thinking
  2. Writing Stan Models in R
  3. MCMC Sampling and Diagnostics
  4. Posterior Predictive Checks
← Back to R Academy