0Pricing
R Academy · Lesson

Introduction to Bayesian Thinking

Understand prior, likelihood, and posterior in the Bayesian framework.

Frequentist vs Bayesian

In frequentist statistics, probability is the long-run frequency of an event. In Bayesian statistics, probability represents a degree of belief. The key difference: frequentists treat parameters as fixed (unknown constants); Bayesians treat parameters as random variables with probability distributions.

# Frequentist: parameter theta is fixed, data is random
# Bayesian: data is fixed (observed), theta has a distribution

# Example: estimating coin bias p
# Frequentist: MLE -> p_hat = heads / total
heads <- 7; total <- 10
p_mle <- heads / total
cat('MLE estimate:', p_mle, '\n')

# Bayesian: update prior belief with observed data
# Prior: Beta(2, 2) -> slightly informative, centered at 0.5
# Posterior: Beta(2 + heads, 2 + (total - heads)) = Beta(9, 5)
alpha_post <- 2 + heads
beta_post  <- 2 + (total - heads)
p_bayes <- alpha_post / (alpha_post + beta_post)  # posterior mean
cat('Bayesian posterior mean:', round(p_bayes, 3), '\n')

Bayes' Theorem

Bayes' theorem relates the prior belief about a parameter, the likelihood of the data given that parameter, and the posterior belief after observing the data:

P(θ|data) = P(data|θ) × P(θ) / P(data)

The denominator P(data) is a normalising constant — it makes the posterior a proper probability distribution.

# Bayes' theorem components
# P(theta | data)   = posterior  (what we want)
# P(data  | theta)  = likelihood (how well theta explains data)
# P(theta)          = prior      (what we believed before data)
# P(data)           = evidence   (normalising constant)

# Medical test example
P_disease   <- 0.01   # prior: 1% prevalence
P_pos_given_disease  <- 0.99  # sensitivity
P_pos_given_no_disease <- 0.05  # false positive rate

P_pos <- P_pos_given_disease * P_disease +
         P_pos_given_no_disease * (1 - P_disease)

P_disease_given_pos <- (P_pos_given_disease * P_disease) / P_pos

cat('P(positive test):', round(P_pos, 4), '\n')
cat('P(disease | positive test):', round(P_disease_given_pos, 4), '\n')
cat('Only', round(P_disease_given_pos * 100, 1), '% chance despite positive test!\n')

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