AI Knowledge Tree

Random variables and distributions

Define random variables, their CDF, and the PMF/PDF, and tell discrete from continuous.

A probability space tells you which outcomes are possible and how likely each event is, but outcomes are often unwieldy objects (a card, a coin face, a point in space). A random variable attaches a number to each outcome, turning probability into something we can average, plot, and compute with.

Definition

A random variable is a function X:Ω→RX: \Omega \to \mathbb{R} that assigns a real number to each outcome[1]. For a die, X(ω)=ωX(\omega) = \omega is the face value; for two coins, XX could count the number of heads.

The probability of XX landing in a set S⊆RS \subseteq \mathbb{R} is the probability of the outcomes that map into SS:

P(X∈S)=P({ω:X(ω)∈S}).P(X \in S) = P(\{\omega : X(\omega) \in S\}).

The right-hand side is a probability of an event in the original space; the left-hand side is the notation we actually use. The distribution of XX is the collection of these probabilities over all SS.

The cumulative distribution function

The cumulative distribution function (CDF) of XX is[2]

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

It works for any random variable, discrete or continuous. Three properties follow directly from the probability axioms:

  1. Monotone nondecreasing: if x1≤x2x_1 \le x_2 then {X≤x1}⊆{X≤x2}\{X \le x_1\} \subseteq \{X \le x_2\}, so FX(x1)≤FX(x2)F_X(x_1) \le F_X(x_2) by monotonicity of PP.
  2. Limits: FX(x)→0F_X(x) \to 0 as x→−∞x \to -\infty and FX(x)→1F_X(x) \to 1 as x→+∞x \to +\infty.
  3. Right-continuous.

Any function with these properties is the CDF of some random variable.

Discrete random variables and the PMF

XX is discrete if it takes values in a finite or countable set. Its distribution is captured by the probability mass function (PMF)

pX(x)=P(X=x),∑xpX(x)=1.p_X(x) = P(X = x), \qquad \sum_x p_X(x) = 1.

The CDF is a sum of masses: FX(x)=∑xi≤xpX(xi)F_X(x) = \sum_{x_i \le x} p_X(x_i), a staircase that jumps by pX(xi)p_X(x_i) at each value xix_i.

Continuous random variables and the PDF

XX is continuous if its CDF can be written as an integral

FX(x)=∫−∞xfX(t) dt,F_X(x) = \int_{-\infty}^x f_X(t)\, dt,

for some nonnegative probability density function (PDF) fXf_X. By the fundamental theorem of calculus, fX(x)=FX′(x)f_X(x) = F_X'(x) where the derivative exists.

A density is not a probability: fX(x)f_X(x) can exceed 1. Probabilities come from integrating it over an interval,

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

and a single point has probability zero, P(X=a)=0P(X = a) = 0. The normalization is ∫−∞∞fX(x) dx=1\int_{-\infty}^\infty f_X(x)\, dx = 1.

The density as a limit

The interpretation of fXf_X is a probability per unit length: for a small width ε\varepsilon,

P ⁣(x≤X≤x+ε)=∫xx+εfX(t) dt≈fX(x) ε.P\!\left(x \le X \le x + \varepsilon\right) = \int_x^{x+\varepsilon} f_X(t)\,dt \approx f_X(x)\,\varepsilon.

So fX(x)f_X(x) measures how concentrated the probability is near xx, just as mass density measures how concentrated matter is near a point.

A random variable's PMF gives the probability of each value and its CDF the running total. Shown for Binomial(3, 0.5).
A random variable's PMF gives the probability of each value and its CDF the running total. Shown for Binomial(3, 0.5).

Check it on arrays

Sampling lets us reconstruct both the PMF (by counting) and the PDF (by a normalized histogram), and verify that the empirical CDF tracks the theoretical one.

python · numpy
import numpy as np

rng = np.random.default_rng(1)
N = 200_000

# Discrete: number of heads in 3 fair coin flips -> values 0..3
flips = rng.integers(0, 2, size=(N, 3))
X = flips.sum(axis=1)
values, counts = np.unique(X, return_counts=True)
pmf = counts / N
# Exact PMF: C(3,k)/8
from math import comb
exact = np.array([comb(3, k) for k in values]) / 8
for v, p, e in zip(values, pmf, exact):
    print(f"P(X={v}) = {p:.4f}   exact {e:.4f}")
print("PMF sums to:", pmf.sum())

# Continuous: standard normal, compare histogram to the PDF
Z = rng.normal(size=N)
hist, edges = np.histogram(Z, bins=50, density=True)   # density=True -> integrates to 1
centers = 0.5 * (edges[:-1] + edges[1:])
pdf = np.exp(-centers**2 / 2) / np.sqrt(2 * np.pi)
print("max |hist - pdf|:", np.max(np.abs(hist - pdf)))   # small

# Empirical CDF at 0 should be ~0.5
print("F_Z(0) empirical:", (Z <= 0).mean(), "  theoretical 0.5")

Counting recovers the binomial PMF, the normalized histogram matches the Gaussian density, and the empirical CDF lands on the theoretical value.

Exercises

Exercise 1. A discrete random variable XX has PMF pX(1)=0.2p_X(1) = 0.2, pX(2)=0.5p_X(2) = 0.5, pX(3)=0.3p_X(3) = 0.3. Write its CDF FX(x)F_X(x) as a staircase and evaluate FX(2)F_X(2) and P(1<X≤3)P(1 < X \le 3).

Solution

Step 1. The CDF accumulates mass from the left:

FX(x)={0x<10.21≤x<20.72≤x<31x≥3.F_X(x) = \begin{cases} 0 & x < 1 \\ 0.2 & 1 \le x < 2 \\ 0.7 & 2 \le x < 3 \\ 1 & x \ge 3. \end{cases}

Step 2. Read off FX(2)=0.2+0.5=0.7F_X(2) = 0.2 + 0.5 = 0.7.

Step 3. For the interval, use P(1<X≤3)=FX(3)−FX(1)=1−0.2=0.8P(1 < X \le 3) = F_X(3) - F_X(1) = 1 - 0.2 = 0.8. This counts the mass at 22 and 33 but excludes the mass at 11, matching 0.5+0.3=0.80.5 + 0.3 = 0.8.

Exercise 2. A continuous random variable has density fX(x)=2xf_X(x) = 2x for 0≤x≤10 \le x \le 1 and 00 elsewhere. Confirm it is a valid density, find the CDF, and compute P(X≤0.5)P(X \le 0.5).

Solution

Step 1. Validity needs nonnegativity (true since 2x≥02x \ge 0 on [0,1][0, 1]) and total integral 11:

∫012x dx=[x2]01=1.\int_0^1 2x\,dx = \left[x^2\right]_0^1 = 1.

Step 2. The CDF is the running integral, for 0≤x≤10 \le x \le 1:

FX(x)=∫0x2t dt=x2,F_X(x) = \int_0^x 2t\,dt = x^2,

with FX(x)=0F_X(x) = 0 below 00 and 11 above 11.

Step 3. Evaluate P(X≤0.5)=FX(0.5)=0.52=0.25P(X \le 0.5) = F_X(0.5) = 0.5^2 = 0.25. Less mass sits below the midpoint than above it, as expected from a density that grows with xx.

Exercise 3 (code). Sample from the density fX(x)=2xf_X(x) = 2x on [0,1][0, 1] by inverse transform: if UU is uniform on [0,1][0, 1], then X=UX = \sqrt{U} has CDF x2x^2. Verify the empirical CDF at 0.50.5 matches the theoretical 0.250.25.

Solution
python · numpy
import numpy as np

rng = np.random.default_rng(4)
N = 500_000
U = rng.random(N)
X = np.sqrt(U)                      # inverse of CDF F(x) = x^2 on [0, 1]

emp_cdf_half = (X <= 0.5).mean()    # should approach F(0.5) = 0.25
theoretical = 0.5**2

assert np.isclose(emp_cdf_half, theoretical, atol=2e-3)
print("F_X(0.5) empirical:", round(emp_cdf_half, 4),
      " theoretical:", theoretical)

Inverting the CDF turns uniform draws into samples from 2x2x, and the empirical CDF at 0.50.5 lands on the theoretical 0.250.25 to sampling precision.

The next lesson collapses a whole distribution into a single representative number: its expectation.


References

  1. [1]Wasserman, L. (2004). All of Statistics. Springer. Ch. 2, random variables and distribution functions. doi:10.1007/978-0-387-21736-9↩
  2. [2]Casella, G., & Berger, R. L. (2002). Statistical Inference (2nd ed.). Duxbury. §1.5-1.6, CDFs and density/mass functions.↩