STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 17
An Introduction to Bayesian Statistics
Bayesian statistics provides a coherent framework for reasoning about uncertainty using probability. Unlike the frequentist approach, which treats parameters as fixed but unknown, Bayesian inference treats parameters as random variables with prior distributions. In this chapter we introduce Bayes’ theorem, prior and posterior distributions, credible intervals and simple Bayesian models.
Bayes’ Theorem
For a parameter with prior density and likelihood given data , Bayes’ theorem yields the posterior distribution The prior reflects our beliefs about before observing data; the posterior combines the prior with the data. The denominator, known as the marginal likelihood or evidence, ensures the posterior integrates to one.
Conjugate Priors
For certain likelihood–prior combinations, the posterior has the same functional form as the prior; these are called conjugate priors. Conjugacy simplifies computation and yields closed–form posteriors.
Bernoulli/Binomial likelihood: For with Beta prior on , the posterior is .
Normal likelihood with known variance: For with prior , the posterior is normal with mean and variance .
Posterior Summaries
Bayesian inference often reports posterior means, medians or modes as point estimates, and credible intervals as interval estimates. A 95% credible interval for is an interval such that . Credible intervals have a direct probabilistic interpretation, unlike frequentist confidence intervals.
Example: Estimating a Proportion
Suppose we toss a coin times and observe heads. We want to estimate the probability of heads using a Beta prior with parameters , . The posterior is .
Worked example
Posterior distribution and credible interval.
In R:
alpha <- 2; beta <- 2
n <- 20; x <- 12
posterior_alpha <- alpha + x
posterior_beta <- beta + n - x
# Posterior mean and 95% credible interval
post_mean <- posterior_alpha/(posterior_alpha + posterior_beta)
ci <- qbeta(c(0.025, 0.975), posterior_alpha, posterior_beta)
post_mean
ci
In Python using scipy:
from scipy.stats import beta
alpha, beta_param = 14, 10
post_mean = alpha / (alpha + beta_param)
ci = beta.ppf([0.025, 0.975], alpha, beta_param)
print(post_mean, ci)
The posterior mean is approximately 0.58 and the 95% credible interval is approximately .
Bayesian Computation
When conjugate priors are unavailable or the posterior is
high–dimensional, numerical methods such as Markov chain Monte Carlo
(MCMC) approximate the posterior. R packages like rstan and
Python packages like PyMC or numpyro provide
tools for Bayesian inference. For example, using PyMC:
import pymc as pm
import numpy as np
x = 12
n = 20
with pm.Model() as model:
p = pm.Beta('p', alpha=2, beta=2)
y = pm.Binomial('y', n=n, p=p, observed=x)
trace = pm.sample(draws=1000, tune=1000)
pm.summary(trace)
Exercises
Practice
A quality–control engineer tests 50 products and finds 3 defective. Using a (uniform) prior on the defect rate, compute the posterior distribution and the posterior mean. Compute a 90% credible interval.
Consider a normal likelihood with known variance . Derive the posterior mean and variance for with prior .
Use PyMC or RStan to fit a simple Bayesian linear regression model predicting weight from height. Compare the posterior distributions of the slope and intercept to the frequentist estimates.
Discuss the difference between credible intervals and confidence intervals. Provide an example where the two intervals differ substantially.
References
Gelman A, Carlin JB, Stern HS, Dunson DB, Vehtari A, Rubin DB. Bayesian Data Analysis. 3rd ed. CRC Press; 2013.
McElreath R. Statistical Rethinking: A Bayesian Course with Examples in R and Stan. 2nd ed. CRC Press; 2020.
Kruschke JK. Doing Bayesian Data Analysis. 3rd ed. Academic Press; 2014.