EXECUTED WITH R 4.3.3
This script is run with R 4.3.3, by tools/data-science/run_r_equivalents.py, and the lab page shows what it printed and the plots it drew. Every number in its comments was checked against that output.
Straight from labs/course-6-r/02_distributions.R, unchanged.
# =====================================================================
# Run with R 4.3.3 (Rscript --vanilla). What it prints, and the plots it
# draws, are on the lab page, and tools/data-science/run_r_equivalents.py
# runs it again. (Until October 2026 R could not be installed where these
# labs are checked, so this file was desk-checked only; every number in its
# comments has since been checked against R's own output.)
# =====================================================================
# Experiment 2: Visualise Binomial, Normal and Poisson distributions
# Python equivalent: python/02_distributions.py
# --- BINOMIAL(n = 10, p = 0.3) ---
# Step 1: Plot Binomial(10, 0.3), and find P(X = 3) and P(X <= 3)
k <- 0:10
pmf <- dbinom(k, size = 10, prob = 0.3)
barplot(pmf, names.arg = k, col = "#1e7fbf",
main = "Binomial(10, 0.3)", xlab = "k", ylab = "P(X = k)")
dbinom(3, 10, 0.3) # 0.266828 -- P(X = 3)
pbinom(3, 10, 0.3) # 0.649611 -- P(X <= 3)
# mean = np = 3.0 ; variance = np(1-p) = 2.1
# --- POISSON(lambda = 3) ---
# Step 2: Plot Poisson(3), and find P(X = 3) and P(X <= 3)
k <- 0:10
barplot(dpois(k, lambda = 3), names.arg = k, col = "#059669",
main = "Poisson(3)", xlab = "k", ylab = "P(X = k)")
dpois(3, 3) # 0.224042
ppois(3, 3) # 0.647232
# mean = variance = lambda = 3 <- the Poisson signature
# --- NORMAL(mu = 100, sigma = 15) ---
# Step 3: Plot Normal(100, 15), and find the areas within 1, 2 and 3 SD
x <- seq(50, 150, by = 0.5)
plot(x, dnorm(x, mean = 100, sd = 15), type = "l", lwd = 2, col = "#0f4c81",
main = "Normal(100, 15)", ylab = "density")
pnorm(115, 100, 15) # 0.841345
pnorm(115, 100, 15) - pnorm(85, 100, 15) # 0.682689 <- the 68% rule
pnorm(130, 100, 15) - pnorm(70, 100, 15) # 0.954500 <- 95%
pnorm(145, 100, 15) - pnorm(55, 100, 15) # 0.997300 <- 99.7%
qnorm(0.95, 100, 15) # 124.67 -- the 95th percentile
# NAMING CONVENTION -- worth memorising, it applies to every distribution:
# d<name> density / PMF dbinom, dnorm, dpois
# p<name> cumulative (CDF) pbinom, pnorm, ppois
# q<name> quantile (inverse) qbinom, qnorm, qpois
# r<name> random generation rbinom, rnorm, rpois
The same thing in Python: Binomial, Normal and Poisson Distributions in Python.
The theory behind it is in this course’s units; the whole lab, with all 18 experiments, is on the lab page.