Skip to content

sheperd007/Mathematical-Statistics-with-R

Repository files navigation

Mathematical Statistics with R

Code companion to the book Mathematical Statistics with R (Hamid Jahani, Vahid Rezaei Tabar) — nine annotated R scripts covering the core of mathematical statistics, plus a Python/TensorFlow notebook on Bayesian neural networks for image recognition and out-of-distribution detection.

Tech stack: R (base + MASS, boot, nimble, mvtnorm, car, …) · Python 3 · TensorFlow 2 / Keras · TensorFlow Probability

Overview

This repository collects the runnable code that accompanies the textbook. Each Chapter*.R file maps to a chapter of the book and reproduces its examples — from a first tour of the R language through estimation theory, confidence intervals, hypothesis testing, and resampling. A standalone Jupyter notebook extends the material into modern probabilistic deep learning, comparing a standard CNN against two Bayesian approaches on CIFAR-10 image classification.

Note: the code is provided as a study aid for the book. Any use of these scripts without reference to the book is not authorized by the authors.

What's inside

File Topic
Chapter 1.R Introduction to the R programming environment
Chapter 2.R Basic concepts and definitions (probability, simulation)
Chapter 3.R Sufficiency, minimal sufficiency, and completeness
Chapter 4.R Point estimators
Chapter 5.R Confidence intervals
Chapter 6.R Statistical hypothesis testing
Chapter 7.R Uniformly most powerful (UMP) tests
Chapter 8.R Likelihood ratio tests
Chapter 9.R Advanced topics (e.g. bootstrap resampling)
image recognition.ipynb Bayesian neural networks for image classification + OOD detection
Appendix.rar Supplementary appendix material
LICENSE GNU GPL v3

Methods / Approach

R scripts. Each chapter is heavily commented and self-contained: examples are numbered to match the book, and expected outputs are noted inline. Topics span analytic derivations checked by simulation (e.g. expectations of order statistics via both integrate and Monte-Carlo replication), maximum-likelihood estimation (stats4), confidence regions (ellipse, mvtnorm), classical testing, and the bootstrap (boot).

Image-recognition notebook. The notebook studies uncertainty quantification on CIFAR-10. The "horse" class is held out of training so the network is trained as a 9-class classifier; at test time the held-out horses act as out-of-distribution (OOD) inputs. Three models are compared:

  • Non-Bayesian baseline — a standard convolutional network (point estimate of the weights).
  • Variational Inference (VI) — Bayesian convolution layers via tfp.layers.Convolution2DFlipout, with a KL-divergence term scaled by the dataset size; predictions averaged over many stochastic forward passes.
  • MC Dropout — dropout kept active at inference time, sampling many forward passes as an approximate Bayesian posterior.

Predictive uncertainty is summarized with the predictive entropy and the negative log-likelihood of the maximum-probability class, and the known-vs-unknown (OOD) separation is examined to see whether the Bayesian models express higher uncertainty on the held-out class.

How to run

R scripts

Open any script in R / RStudio and run it. Some examples require additional packages; install them first:

install.packages(c(
  "MASS", "boot", "car", "ellipse", "mvtnorm", "Hmisc",
  "nimble", "propagate", "rgl", "rmutil", "rootSolve",
  "scatterplot3d", "statmod", "threejs", "foreign"
))

# then, for example:
source("Chapter 9.R")

Image-recognition notebook

The notebook is designed to run in Google Colab (it auto-detects Colab and installs dependencies), but it also runs locally:

pip install tensorflow tensorflow-probability matplotlib numpy pandas scikit-learn tqdm
jupyter notebook "image recognition.ipynb"

Pre-trained weights and training histories for the baseline, VI, and MC-Dropout models are downloaded automatically from the tensorchiefs/dl_book repository, so the full comparison can be reproduced without retraining (training cells are included but commented out).

Citation / links

If you use this code, please cite the accompanying book:

Jahani, H. & Rezaei Tabar, V. Mathematical Statistics with R.

The probabilistic deep-learning notebook builds on material and pre-trained artifacts from Probabilistic Deep Learning (tensorchiefs / dl_book): https://github.com/tensorchiefs/dl_book.

License

Released under the GNU General Public License v3.0 — see LICENSE.

About

Code companion to my book Mathematical Statistics with R - statistical inference via Monte-Carlo simulation

Topics

Resources

License

Stars

4 stars

Watchers

2 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors