Skip to main content
© 2026 ePowerAI. All rights reserved.
CoursesContact
eAI
CoursesContact
Week 2: Random Variables and Distributions
Prob & Stats
01Week 1: Probability Spaces and Events
02Week 2: Random Variables and Distributions
03Week 3: Expectation, Variance, and Covariance
04Week 4: Conditioning and Independence
05Week 5: Bayes' Rule and Generative Classification
06Week 6: Multivariate Gaussians
07Week 7: Laws of Large Numbers and the CLT
08Week 8: Point Estimation — Maximum Likelihood
09Week 9: Bayesian Estimation and MAP
10Week 10: Bias, Variance, and Uncertainty
11Week 11: Information Theory for ML
12Week 12: Exponential Families (lite)
13Week 13: Graphical Models (lite)
14Week 14: Capstone — Probabilistic Language of ML
Week 02· Prob & Stats· Foundations22 min read

Week 2: Random Variables and Distributions

✦Learning Outcomes
  • Define discrete and continuous random variables
  • Use PMF, PDF, and CDF correctly (including ∫p=1\int p = 1∫p=1 for PDFs)
  • Identify Bernoulli, Binomial, Uniform, Exponential models for real situations
  • Compute simple probabilities from a CDF or by integrating/summing
◆Prerequisites

Required: Week 1 (probability spaces and events). You should be comfortable with sample spaces, events as sets, and the Kolmogorov axioms.

Math: High-school calculus — sums for discrete work and basic definite integrals for continuous densities (reviewed in context).

Random Variables#

A random variable (RV) is a function X:Ω→RX: \Omega \to \mathbb{R}X:Ω→R that assigns a real number to each outcome in the sample space. It is a bridge: the experiment produces an abstract outcome ω∈Ω\omega \in \Omegaω∈Ω, and X(ω)X(\omega)X(ω) extracts the numerical quantity we care about.

Examples:

  • Coin toss with payout — if heads you win 1,tailsyoulose1, tails you lose 1,tailsyoulose1. Then X(H)=1X(\text{H}) = 1X(H)=1, X(T)=−1X(\text{T}) = -1X(T)=−1.
  • Sum of two dice — X((i,j))=i+jX((i, j)) = i + jX((i,j))=i+j.
  • Tomorrow's temperature in Celsius — XXX is the reading itself (identity map).

RVs let us talk about distributions rather than raw sample spaces. Instead of computing P({ω:X(ω)≤3})P(\{\omega: X(\omega) \le 3\})P({ω:X(ω)≤3}) from scratch every time, we characterise the behaviour of XXX through its distribution functions.

RVs are discrete if XXX takes values in a finite or countably infinite set (e.g., {0,1}\{0, 1\}{0,1}, N\mathbb{N}N). They are continuous if XXX can take any value in an interval or R\mathbb{R}R.

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 XXX, the PMFProbability Mass Function is

pX(x)=P(X=x)for each possible value x.p_X(x) = P(X = x) \quad \text{for each possible value } x.pX​(x)=P(X=x)for each possible value x.

Requirements: pX(x)≥0p_X(x) \ge 0pX​(x)≥0 for all xxx, and ∑xpX(x)=1\sum_x p_X(x) = 1∑x​pX​(x)=1.

Worked example — biased die. A loaded die has PMF: p(1)=p(2)=p(3)=0.1p(1) = p(2) = p(3) = 0.1p(1)=p(2)=p(3)=0.1, p(4)=p(5)=0.15p(4) = p(5) = 0.15p(4)=p(5)=0.15, p(6)=0.4p(6) = 0.4p(6)=0.4. Check: 3(0.1)+2(0.15)+0.4=0.3+0.3+0.4=1.03(0.1) + 2(0.15) + 0.4 = 0.3 + 0.3 + 0.4 = 1.03(0.1)+2(0.15)+0.4=0.3+0.3+0.4=1.0. Probability of rolling ≥5\ge 5≥5 is p(5)+p(6)=0.15+0.4=0.55p(5) + p(6) = 0.15 + 0.4 = 0.55p(5)+p(6)=0.15+0.4=0.55.

Probability Density Function (PDF) — continuous only#

For a continuous RV XXX, the PDFProbability Density Function fX(x)f_X(x)fX​(x) is a non-negative function that integrates to 1:

∫−∞∞fX(x) dx=1.\int_{-\infty}^{\infty} f_X(x)\, dx = 1.∫−∞∞​fX​(x)dx=1.

Probability of an interval is the area under the curve:

P(a≤X≤b)=∫abfX(x) dx.P(a \le X \le b) = \int_a^b f_X(x)\, dx.P(a≤X≤b)=∫ab​fX​(x)dx.

Critical: fX(x)f_X(x)fX​(x) is not a probability. It is probability density — probability per unit of xxx. For continuous RVs, P(X=c)=0P(X = c) = 0P(X=c)=0 for every single point ccc, even if fX(c)>0f_X(c) > 0fX​(c)>0. Probability is area, not height.

Cumulative Distribution Function (CDF) — always exists#

The CDFCumulative Distribution Function is defined for any RV, discrete or continuous:

FX(x)=P(X≤x).F_X(x) = P(X \le x).FX​(x)=P(X≤x).

Properties:

  • FXF_XFX​ is non-decreasing.
  • lim⁡x→−∞FX(x)=0\lim_{x \to -\infty} F_X(x) = 0limx→−∞​FX​(x)=0, lim⁡x→+∞FX(x)=1\lim_{x \to +\infty} F_X(x) = 1limx→+∞​FX​(x)=1.
  • For continuous RVs, FXF_XFX​ is smooth and FX′(x)=fX(x)F_X'(x) = f_X(x)FX′​(x)=fX​(x) (where FXF_XFX​ is differentiable).
  • For discrete RVs, FXF_XFX​ is a step function with jumps at each possible value; the jump size equals the PMF value.

CDF computes interval probabilities: P(a<X≤b)=FX(b)−FX(a)P(a < X \le b) = F_X(b) - F_X(a)P(a<X≤b)=FX​(b)−FX​(a).

Worked example — Uniform on [0,1][0,1][0,1]:

fX(x)={10≤x≤10otherwise,FX(x)={0x<0x0≤x≤11x>1f_X(x) = \begin{cases} 1 & 0 \le x \le 1 \\ 0 & \text{otherwise} \end{cases}, \quad F_X(x) = \begin{cases} 0 & x < 0 \\ x & 0 \le x \le 1 \\ 1 & x > 1 \end{cases}fX​(x)={10​0≤x≤1otherwise​,FX​(x)=⎩⎨⎧​0x1​x<00≤x≤1x>1​

Then P(0.3≤X≤0.7)=F(0.7)−F(0.3)=0.7−0.3=0.4P(0.3 \le X \le 0.7) = F(0.7) - F(0.3) = 0.7 - 0.3 = 0.4P(0.3≤X≤0.7)=F(0.7)−F(0.3)=0.7−0.3=0.4.

Bernoulli and Binomial#

Bernoulli distribution#

A single trial with two outcomes: success (1) with probability ppp, failure (0) with probability 1−p1-p1−p.

P(X=1)=p,P(X=0)=1−p.P(X = 1) = p, \quad P(X = 0) = 1 - p.P(X=1)=p,P(X=0)=1−p.

Notation: X∼Bernoulli(p)X \sim \text{Bernoulli}(p)X∼Bernoulli(p). The PMF is pX(x)=px(1−p)1−xp_X(x) = p^x (1-p)^{1-x}pX​(x)=px(1−p)1−x for x∈{0,1}x \in \{0, 1\}x∈{0,1}.

Example: A classifier makes the correct prediction with probability 0.9. Each prediction is a Bernoulli(0.9)\text{Bernoulli}(0.9)Bernoulli(0.9) trial.

Binomial distribution#

nnn independent Bernoulli(p)(p)(p) trials; XXX counts the number of successes:

P(X=k)=(nk)pk(1−p)n−k,k=0,1,…,n.P(X = k) = \binom{n}{k} p^k (1-p)^{n-k}, \quad k = 0, 1, \ldots, n.P(X=k)=(kn​)pk(1−p)n−k,k=0,1,…,n.

Notation: X∼Binomial(n,p)X \sim \text{Binomial}(n, p)X∼Binomial(n,p). The binomial coefficient (nk)=n!k!(n−k)!\binom{n}{k} = \frac{n!}{k!(n-k)!}(kn​)=k!(n−k)!n!​ counts the number of ways to choose which kkk of the nnn trials are successes.

Worked example. A factory produces chips; each chip is defective independently with probability p=0.05p = 0.05p=0.05. In a batch of n=20n = 20n=20 chips, what is the probability of exactly 2 defectives?

P(X=2)=(202)(0.05)2(0.95)18=190⋅0.0025⋅0.3972≈0.189.P(X = 2) = \binom{20}{2} (0.05)^2 (0.95)^{18} = 190 \cdot 0.0025 \cdot 0.3972 \approx 0.189.P(X=2)=(220​)(0.05)2(0.95)18=190⋅0.0025⋅0.3972≈0.189.

Binomial as sum of Bernoullis: If Y1,…,Yn∼i.i.d.Bernoulli(p)Y_1, \ldots, Y_n \stackrel{\text{i.i.d.}}{\sim} \text{Bernoulli}(p)Y1​,…,Yn​∼i.i.d.Bernoulli(p), then X=∑i=1nYi∼Binomial(n,p)X = \sum_{i=1}^n Y_i \sim \text{Binomial}(n, p)X=∑i=1n​Yi​∼Binomial(n,p). 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 X∼Uniform(a,b)X \sim \text{Uniform}(a, b)X∼Uniform(a,b):

fX(x)=1b−afor a≤x≤b,FX(x)=x−ab−afor a≤x≤b.f_X(x) = \frac{1}{b-a} \quad \text{for } a \le x \le b, \qquad F_X(x) = \frac{x-a}{b-a} \quad \text{for } a \le x \le b.fX​(x)=b−a1​for a≤x≤b,FX​(x)=b−ax−a​for a≤x≤b.

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 X∼Exponential(λ)X \sim \text{Exponential}(\lambda)X∼Exponential(λ) with rate λ>0\lambda > 0λ>0:

fX(x)=λe−λxfor x≥0,FX(x)=1−e−λxfor x≥0.f_X(x) = \lambda e^{-\lambda x} \quad \text{for } x \ge 0, \qquad F_X(x) = 1 - e^{-\lambda x} \quad \text{for } x \ge 0.fX​(x)=λe−λxfor x≥0,FX​(x)=1−e−λxfor x≥0.

Memoryless property (mention): P(X>s+t∣X>s)=P(X>t)P(X > s + t \mid X > s) = P(X > t)P(X>s+t∣X>s)=P(X>t). 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 λ=2\lambda = 2λ=2 per second. What is P(wait≤0.5 sec)P(\text{wait} \le 0.5\,\text{sec})P(wait≤0.5sec)?

F(0.5)=1−e−2⋅0.5=1−e−1≈1−0.368=0.632.F(0.5) = 1 - e^{-2 \cdot 0.5} = 1 - e^{-1} \approx 1 - 0.368 = 0.632.F(0.5)=1−e−2⋅0.5=1−e−1≈1−0.368=0.632.

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 ppp | Bernoulli(p)(p)(p) | | Count of successes in nnn independent trials | Binomial(n,p)(n, p)(n,p) | | Random value in a bounded interval, all values equally plausible | Uniform(a,b)(a, b)(a,b) | | Waiting time with constant hazard (no memory) | Exponential(λ)(\lambda)(λ) |

Key questions to ask:

  1. Is the quantity discrete (countable) or continuous (measured)?
  2. Is there a natural upper bound (Binomial has nnn; Uniform has a,ba, ba,b)?
  3. Are trials independent? (If not, Binomial is wrong.)
  4. 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.

Exercise · Fill in the blank

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#

Question 1 of 4

For a continuous RV, which statement is always true?

P(X = c) = 0 for any single value c
P(X = c) = f(c)
The PDF value f(c) is between 0 and 1
The CDF is equal to the PDF at every point

Browser lab#

Sample from named distributions and compare the histogram to the true PMF/PDF curve.

python · runs in browser
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}")
← Previous
Week 1: Probability Spaces and Events
Next →
Week 3: Expectation, Variance, and Covariance
On this page
  • Random Variables
  • PMF, PDF, CDF
  • Probability Mass Function (PMF) — discrete only
  • Probability Density Function (PDF) — continuous only
  • Cumulative Distribution Function (CDF) — always exists
  • Bernoulli and Binomial
  • Bernoulli distribution
  • Binomial distribution
  • Uniform and Exponential
  • Uniform distribution (continuous)
  • Exponential distribution
  • Choosing a Model
  • Knowledge check
  • Browser lab