R and Python for Computer Simulation

Author

Sam Wiwatanapataphee

Published

May 22, 2026

Preface

This book introduces computer simulation as a core methodological tool in modern statistics for analysing complex systems and modelling uncertainty when analytical solutions are unavailable or impractical. Students will develop an understanding of the capabilities and limitations of simulation, and how simulation complements theoretical and data-driven statistical approaches.

The book adopts a computational and algorithmic approach to simulation. Students will learn to design, implement, and verify simulation procedures in an appropriate computing environment, with emphasis on Monte Carlo methods, simulation of univariate and multivariate random variables, dependence structures, and simulation-based statistical inference. Simulation is treated as a structured experimental process, from problem formulation and model design to implementation and evaluation.

Students will develop skills in the analysis, visualisation, and interpretation of simulation output, including assessing variability, uncertainty, and convergence.

The book is hands-on, with computational activities using R and/or Python. Students will complete a substantial project in which they apply simulation techniques to a discipline-specific problem, demonstrating the ability to develop, analyse, and report a simulation study in a clear, rigorous, and professional manner.

Unit Materials

  • Now available in dark mode. Click the icon next to the book title in the navigation bar to toggle between light and dark themes.

  • Links to weekly materials are available by clicking on the emojis in the table below:

# Lecture Slide CN WS WS Solutions
1 Introduction to Computer Simulation πŸ–₯️ πŸ“– πŸ§‘πŸ»β€πŸ’» ,
2 Statistical Distribution πŸ–₯️ πŸ“– πŸ§‘πŸ»β€πŸ’» ,
3 Joint Distributions and Statistical Inference πŸ–₯️ πŸ“– πŸ§‘πŸ»β€πŸ’» ,
4 Simulation of Discrete Random Variables πŸ–₯️ πŸ“– πŸ§‘πŸ»β€πŸ’» ,
5 Simulation of Continuous Random Variables πŸ–₯️ πŸ“– πŸ§‘πŸ»β€πŸ’» ,
6 Multivariate Simulation πŸ–₯️ πŸ“– πŸ§‘πŸ»β€πŸ’» ,
7 Stochastic Modelling πŸ–₯️ πŸ“– πŸ§‘πŸ»β€πŸ’» ,
8 Time Series Modelling πŸ–₯️ πŸ“– πŸ§‘πŸ»β€πŸ’» ,
9 Monte Carlo Integration and MCMC πŸ–₯️ πŸ“– πŸ§‘πŸ»β€πŸ’» ,
10 Simulation in Practice πŸ–₯️ πŸ“– N/A N/A

Program Calendar

Week Lecture Content Workshop
1 Introduction to Simulation
β€’ Monte Carlo history & motivation
β€’ Simulation workflow
β€’ Simulation vs analytical solutions
β€’ PRNGs (LCG, Mersenne Twister)
β€’ Probability revision & LLN intuition
Implement LCG; compare with built-in RNG; empirical LLN
2 Statistical Distributions (Univariate & Joint)
β€’ Univariate distributions
β€’ Conditional probabilities
β€’ Bayesian inference (Intro)
Univariate distributions simulation and visualisations
3 Statistical Inference (Frequentist & Bayesian)
β€’ Joint distributions & dependence
β€’ Frequentist inference
β€’ Bayesian inference
Joint distributions simulation; Frequentist inference; MLE;
4 Simulation of Discrete RVs
β€’ Discrete inverse CDF method
β€’ The precision method
β€’ Case study: Epidemic SIR Modelling
Bayesian inference; simulation using precision method
5 Test 1 Test 1
6 Simulation of Continuous RVs
β€’ Inverse Transform Sampling
β€’ Acceptance–rejection
β€’ Precision of Simulation Results
Project Clinic 1
Implement inverse transform sampling and A–R sampling
7 Multivariate Simulation
β€’ Transformation of multiple rvs
β€’ Linear transformations
β€’ Multivariate Normal
β€’ Cholesky decomposition
β€’ Copulas
β€’ Simulation dependent variable
β€’ Simulate correlated rvs using linear transformations
β€’ Multivariate normal samples
β€’ Cholesky decomposition
β€’ Copula-based simulation
β€’ Case study: Air Pollution Monitoring
β€’ Case study: Portfolio Risk Simulation
8-9 Tuition Free Week
10 Stochastic Modelling
β€’ Stochastic process
β€’ Poisson process
β€’ Thinning algorithm
β€’ Markov process
β€’ Queueing theory
β€’ Simulate HPP and NHPP
β€’ M/M/\(c\) queues
β€’ Case study: EV Charging Bays Simulation
11 Time Series
β€’ White noise
β€’ Autoregressive (AR) models
β€’ Behaviour via \(\phi\)
β€’ Mean Reversion
β€’ Autocorrelation Function (ACF)
β€’ Shock propagation
β€’ Simulate AR models
β€’ Shock propagation analysis
β€’ Case Study: Traffic Flow Simulation
12 Test 2 Test 2
13 Monte Carlo Integration & MCMC
β€’ Monte Carlo integration & CLT
β€’ Gibbs sampling & intuition
β€’ Case Study: Estimating Household Energy Savings
Guest Lecture
β€’ Monte Carlo integration
14 Simulation in Practice
β€’ Metropolis-Hastings algorithm
β€’ Variance reduction methods (importance sampling, stratified, Latin Hypercube Sampling)
β€’ Simulation optimisation (Intuitive Overview)
Revision
Project Clinic 2
15 Study Week
16-17 Centrally Scheduled Examination