Book contents

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 θ\theta with prior density p(θ)p(\theta) and likelihood L(θ;𝐱)L(\theta;\mathbf{x}) given data 𝐱\mathbf{x}, Bayes’ theorem yields the posterior distribution p(θ|𝐱)=L(θ;𝐱)p(θ)∫L(θ;𝐱)p(θ)dθ.p(\theta | \mathbf{x}) = \frac{L(\theta;\mathbf{x}) p(\theta)}{\int L(\theta;\mathbf{x}) p(\theta)\,d\theta}. The prior reflects our beliefs about θ\theta 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.

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 θ\theta is an interval [a,b][a,b] such that P(θ∈[a,b]|𝐱)=0.95P(\theta \in [a,b] | \mathbf{x}) = 0.95. Credible intervals have a direct probabilistic interpretation, unlike frequentist confidence intervals.

Example: Estimating a Proportion

Suppose we toss a coin n=20n=20 times and observe x=12x=12 heads. We want to estimate the probability of heads pp using a Beta prior with parameters α=2\alpha=2, β=2\beta=2. The posterior is Beta(α+x,β+n−x)=Beta(14,10)\mathrm{Beta}(\alpha+x, \beta+n-x) = \mathrm{Beta}(14,10).

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 [0.37,0.78][0.37,0.78].

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

  1. A quality–control engineer tests 50 products and finds 3 defective. Using a Beta(1,1)\mathrm{Beta}(1,1) (uniform) prior on the defect rate, compute the posterior distribution and the posterior mean. Compute a 90% credible interval.

  2. Consider a normal likelihood X1,…,Xn∼Normal(μ,σ2)X_1,\dots,X_n \sim \mathrm{Normal}(\mu, \sigma^2) with known variance σ2=1\sigma^2=1. Derive the posterior mean and variance for μ\mu with prior μ∼Normal(0,τ2)\mu \sim \mathrm{Normal}(0,\tau^2).

  3. 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.

  4. Discuss the difference between credible intervals and confidence intervals. Provide an example where the two intervals differ substantially.

References