What Is a Random Variable?
A random variable $X$ is a measurable function from a sample space $\Omega$ to the real line $\mathbb{R}$. This formal definition is less important than the intuition: $X$ is a quantity whose value is determined by a random experiment. Rolling a fair die gives $X \in \{1, 2, 3, 4, 5, 6\}$. Recording a person's height gives $X \in (0, \infty)$. The distinction between these two examples — countable vs. uncountable range — defines the two major classes.
Discrete Random Variables and PMFs
A discrete random variable takes values in a finite or countably infinite set. Its behavior is completely described by the probability mass function (PMF):
The PMF must satisfy $p_X(x) \geq 0$ for all $x$ and $\sum_{x} p_X(x) = 1$.
Example — Poisson distribution. The number of events in a fixed interval when events occur independently at rate $\lambda$ follows a Poisson distribution:
This models website hits per second, customer arrivals per hour, mutations per genome segment, and countless other count phenomena.
Continuous Random Variables and PDFs
A continuous random variable $X$ has a probability density function (PDF) $f_X(x)$ such that:
The PDF itself is not a probability — it can exceed 1. The probability lives in the area under the curve. Requirements: $f_X(x) \geq 0$ and $\int_{-\infty}^{\infty} f_X(x)\, dx = 1$. Crucially, $P(X = x) = 0$ for any single point; only intervals carry positive probability for continuous variables.
Example — Exponential distribution. The time between successive Poisson events has PDF:
This is the unique continuous memoryless distribution: $P(X > s + t \mid X > s) = P(X > t)$.
The Cumulative Distribution Function
The CDF unifies discrete and continuous cases:
For discrete $X$: $F_X(x) = \sum_{k \leq x} p_X(k)$ (a right-continuous step function). For continuous $X$: $F_X(x) = \int_{-\infty}^x f_X(t)\, dt$, and differentiating recovers the PDF. The CDF always satisfies: non-decreasing, right-continuous, $\lim_{x \to -\infty} F_X(x) = 0$, $\lim_{x \to \infty} F_X(x) = 1$.
Why the CDF matters in practice. Computing $P(a < X \leq b) = F_X(b) - F_X(a)$ requires only two CDF evaluations regardless of distribution complexity. The empirical CDF (ECDF) from data converges uniformly to the true CDF by the Glivenko-Cantelli theorem — the theoretical basis for the Kolmogorov-Smirnov test.
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
# Compare PMF and CDF for Poisson(lambda=3)
lam = 3
k = np.arange(0, 15)
pmf = stats.poisson.pmf(k, mu=lam)
cdf = stats.poisson.cdf(k, mu=lam)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
ax1.stem(k, pmf, basefmt=' ')
ax1.set_title('Poisson PMF (λ=3)')
ax1.set_xlabel('k'); ax1.set_ylabel('P(X=k)')
ax2.step(k, cdf, where='post')
ax2.set_title('Poisson CDF (λ=3)')
ax2.set_xlabel('k'); ax2.set_ylabel('P(X≤k)')
plt.tight_layout()
plt.show()
# Compute P(2 <= X <= 5) two ways
print('Direct sum:', sum(stats.poisson.pmf(range(2,6), mu=lam)))
print('CDF method:', stats.poisson.cdf(5, mu=lam) - stats.poisson.cdf(1, mu=lam))Common Pitfalls
- Confusing PDF value with probability. A PDF value of 2.5 at a point is valid; it means probability density is high there, but no single point has nonzero probability.
- Using
pmfwhen you needcdfor vice versa. Always check: <!--MATHBLOCK28--> is a CDF call; <!--MATHBLOCK29--> is a PMF call. - Off-by-one in discrete CDFs. <!--MATHBLOCK30--> for integer-valued <!--MATHBLOCK31-->, not <!--MATHBLOCK32-->.