Skip to content

eeshalriz/bayesian-sampling-methods

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Bayesian Sampling Methods

Three fundamental Bayesian computational sampling algorithms implemented from scratch in R — Inverse CDF, Rejection Sampling, and Metropolis-Hastings MCMC.

Overview

This project implements and compares three core simulation methods used in Bayesian inference and statistical computing, each targeting a different probability distribution to demonstrate how the techniques work and when to apply them.

Method Target Distribution Acceptance Rate
Inverse CDF Sampling f(x) ∝ exp(x), x ∈ [0,1] 100% (exact)
Rejection Sampling f(x) = 4x³, x ∈ [0,1] ~25% (M=4)
Metropolis-Hastings MCMC f(x) ∝ exp(−x), x ≥ 0 ~75% (σ=0.5)

Each method includes density overlay plots for visual verification against the true distribution.

Methods

1. Inverse CDF Sampling

Analytically derives the inverse CDF and applies the probability integral transform. Exact and efficient — works when the CDF is invertible in closed form.

2. Rejection Sampling (Accept-Reject)

Proposes candidates from a Uniform(0,1) envelope and accepts with probability proportional to the target density. Demonstrates the trade-off between envelope tightness and acceptance rate.

3. Metropolis-Hastings MCMC

Constructs a Markov chain whose stationary distribution matches the target. Uses a Normal proposal distribution truncated to the support x ≥ 0. Tracks acceptance rate and convergence diagnostics.

Key Results

All three methods successfully recover the target distributions:

  • Inverse CDF: mean ≈ 0.418 (matches theoretical E[X] = 1 − 1/(e−1))
  • Rejection sampling: mean ≈ 0.800 (matches theoretical E[X] = 4/5 for f(x) = 4x³)
  • MH MCMC: mean ≈ 1.000, variance ≈ 1.000 (matches Exp(1))

Tech Stack

  • Language: R (base)
  • No external packages required

How to Run

# No package installation needed — uses base R only
source("bayesian_sampling_methods.R")

Each method prints summary statistics and generates a histogram with the true density overlaid in red.

Files

bayesian_sampling_methods.R   # Full annotated R script
README.md

Author

Eeshal Rizwan
BSc Statistical Data Science — Heriot-Watt University Dubai
linkedin.com/in/eeshalrizwan

About

Inverse CDF, rejection sampling, and Metropolis-Hastings MCMC implemented from scratch in R

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages