Random Variables#
A random variable (RV) is a function that assigns a real number to each outcome in the sample space. It is a bridge: the experiment produces an abstract outcome , and extracts the numerical quantity we care about.
Examples:
- Coin toss with payout — if heads you win 1. Then , .
- Sum of two dice — .
- Tomorrow's temperature in Celsius — is the reading itself (identity map).
RVs let us talk about distributions rather than raw sample spaces. Instead of computing from scratch every time, we characterise the behaviour of through its distribution functions.
RVs are discrete if takes values in a finite or countably infinite set (e.g., , ). They are continuous if can take any value in an interval or .
PMF, PDF, CDF#
Three functions completely describe any random variable. Each answers a slightly different question.
Probability Mass Function (PMF) — discrete only#
For a discrete RV , the PMFProbability Mass Function is
Requirements: for all , and .
Worked example — biased die. A loaded die has PMF: , , . Check: . Probability of rolling is .
Probability Density Function (PDF) — continuous only#
For a continuous RV , the PDFProbability Density Function is a non-negative function that integrates to 1:
Probability of an interval is the area under the curve:
Critical: is not a probability. It is probability density — probability per unit of . For continuous RVs, for every single point , even if . Probability is area, not height.
Cumulative Distribution Function (CDF) — always exists#
The CDFCumulative Distribution Function is defined for any RV, discrete or continuous:
Properties:
- is non-decreasing.
- , .
- For continuous RVs, is smooth and (where is differentiable).
- For discrete RVs, is a step function with jumps at each possible value; the jump size equals the PMF value.
CDF computes interval probabilities: .
Worked example — Uniform on :
Then .
Bernoulli and Binomial#
Bernoulli distribution#
A single trial with two outcomes: success (1) with probability , failure (0) with probability .
Notation: . The PMF is for .
Example: A classifier makes the correct prediction with probability 0.9. Each prediction is a trial.
Binomial distribution#
independent Bernoulli trials; counts the number of successes:
Notation: . The binomial coefficient counts the number of ways to choose which of the trials are successes.
Worked example. A factory produces chips; each chip is defective independently with probability . In a batch of chips, what is the probability of exactly 2 defectives?
Binomial as sum of Bernoullis: If , then . This is a key insight: complex distributions are often built from simpler ones by summation.
Uniform and Exponential#
Uniform distribution (continuous)#
All values in an interval are equally likely. If :
The Uniform is the continuous analogue of equally likely discrete outcomes. It models "complete ignorance" over a bounded range — e.g., a bus arriving uniformly at random between 10:00 and 10:15.
Exponential distribution#
The Exponential models waiting times between independent events (e.g., time until the next radioactive decay, next customer arrival). If with rate :
Memoryless property (mention): . The distribution of remaining wait time does not depend on how long you have already waited — unique to the Exponential among continuous distributions. This makes it a natural model for systems where events occur "at random" with no aging.
Worked example. Server requests arrive at rate per second. What is ?
Choosing a Model#
Every distribution tells a story. Choosing the right model means matching the data-generating process to the distribution's story:
| Story | Distribution | |---|---| | Single yes/no trial with probability | Bernoulli | | Count of successes in independent trials | Binomial | | Random value in a bounded interval, all values equally plausible | Uniform | | Waiting time with constant hazard (no memory) | Exponential |
Key questions to ask:
- Is the quantity discrete (countable) or continuous (measured)?
- Is there a natural upper bound (Binomial has ; Uniform has )?
- Are trials independent? (If not, Binomial is wrong.)
- Does the process reset (memoryless)? (If yes, Exponential may fit.)
A model is never "true" — it is a useful approximation. The art is choosing one that captures the essential structure while remaining tractable.
A discrete RV has PMF values: P(X=1)=0.2, P(X=2)=0.3, P(X=3)=0.5. What is P(X ≤ 2)?
Knowledge check#
For a continuous RV, which statement is always true?
Browser lab#
Sample from named distributions and compare the histogram to the true PMF/PDF curve.
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(42)
n = 10000
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
# Binomial
binom = rng.binomial(n=20, p=0.3, size=n)
ks = np.arange(0, 21)
pmf_true = np.array([np.math.comb(20, k) * 0.3**k * 0.7**(20 - k) for k in ks])
axes[0, 0].bar(ks, pmf_true, alpha=0.5, label="True PMF")
axes[0, 0].hist(binom, bins=np.arange(-0.5, 21.5, 1), density=True, alpha=0.7, label="Empirical")
axes[0, 0].set_title("Binomial(20, 0.3)")
axes[0, 0].legend()
# Uniform
unif = rng.uniform(2, 6, size=n)
x_unif = np.linspace(1, 7, 200)
pdf_unif = np.where((x_unif >= 2) & (x_unif <= 6), 0.25, 0)
axes[0, 1].plot(x_unif, pdf_unif, "tab:blue", label="True PDF")
axes[0, 1].hist(unif, bins=40, density=True, alpha=0.7, label="Empirical")
axes[0, 1].set_title("Uniform(2, 6)")
axes[0, 1].legend()
# Exponential
exp_samples = rng.exponential(scale=1/1.5, size=n)
x_exp = np.linspace(0, 5, 200)
pdf_exp = 1.5 * np.exp(-1.5 * x_exp)
axes[1, 0].plot(x_exp, pdf_exp, "tab:blue", label="True PDF")
axes[1, 0].hist(exp_samples, bins=60, density=True, alpha=0.7, label="Empirical")
axes[1, 0].set_title("Exponential(λ=1.5)")
axes[1, 0].legend()
# Empirical CDF vs true CDF for Exponential
sorted_exp = np.sort(exp_samples)
empirical_cdf = np.arange(1, n + 1) / n
true_cdf = 1 - np.exp(-1.5 * sorted_exp)
axes[1, 1].step(sorted_exp, empirical_cdf, label="Empirical CDF")
axes[1, 1].plot(sorted_exp, true_cdf, label="True CDF")
axes[1, 1].set_title("CDF: Exponential(λ=1.5)")
axes[1, 1].legend()
plt.tight_layout()
plt.show()
print(f"Samples: {n}")
print(f"Binomial mean (true=6.0): {binom.mean():.3f}")
print(f"Uniform mean (true=4.0): {unif.mean():.3f}")
print(f"Exponential mean (true=0.667): {exp_samples.mean():.3f}")