TExcelTAD
Home › Probability Theory › Cumulative Distribution Functions

Cumulative Distribution Functions

Densities exist only for continuous variables and probability mass functions only for discrete ones. The cumulative distribution function (CDF) works for every random variable, and it is the easiest tool for finding the distribution of a transformed variable.

Definition and properties

F(x)=Pr⁡[X≤x]F(x) = \Pr[X \le x]

Every CDF is non-decreasing, tends to 0 at −∞-\infty and to 1 at +∞+\infty, and is right-continuous. Then Pr⁡[a<X≤b]=F(b)−F(a)\Pr[a < X \le b] = F(b) - F(a). For a discrete variable FF is a staircase that jumps by Pr⁡[X=x]\Pr[X = x] at each value; for a continuous variable with density ff,

F(x)=∫−∞xf(t) dt,f(x)=F′(x)F(x) = \int_{-\infty}^{x} f(t)\,dt, \qquad f(x) = F'(x)

by the fundamental theorem of calculus.

Distributions of functions of XX

To find the distribution of Y=g(X)Y = g(X), compute its CDF and differentiate. For Y=U2Y = U^2 with UU uniform on [0,1][0, 1]:

FY(y)=Pr⁡[U2≤y]=Pr⁡[U≤y]=y,fY(y)=12y(0<y≤1)F_Y(y) = \Pr[U^2 \le y] = \Pr[U \le \sqrt{y}] = \sqrt{y}, \qquad f_Y(y) = \frac{1}{2\sqrt{y}} \quad (0 < y \le 1)

For the maximum of nn independent uniforms, Pr⁡[max⁡≤y]=Pr⁡[all Ui≤y]=yn\Pr[\max \le y] = \Pr[\text{all } U_i \le y] = y^n, so f(y)=nyn−1f(y) = n y^{n-1}.

python · try it yourself
import random

rng = random.Random(4)
n_trials = 100_000
ys = [max(rng.random() for _ in range(3)) for _ in range(n_trials)]
for y in [0.25, 0.5, 0.75, 0.9]:
    emp = sum(v <= y for v in ys) / n_trials
    print(f"P(max of 3 <= {y}) empirical {emp:.4f}   exact y^3 = {y ** 3:.4f}")
P(max of 3 <= 0.25) empirical 0.0162   exact y^3 = 0.0156
P(max of 3 <= 0.5) empirical 0.1262   exact y^3 = 0.1250
P(max of 3 <= 0.75) empirical 0.4216   exact y^3 = 0.4219
P(max of 3 <= 0.9) empirical 0.7285   exact y^3 = 0.7290

The empirical CDF (the fraction of samples at most yy) is the standard way to compare a sample with a model; the largest gap between the two is the Kolmogorov–Smirnov statistic.

Inverse transform sampling

If FF is continuous and strictly increasing and UU is uniform on [0,1][0, 1], then X=F−1(U)X = F^{-1}(U) has CDF FF, since Pr⁡[F−1(U)≤x]=Pr⁡[U≤F(x)]=F(x)\Pr[F^{-1}(U) \le x] = \Pr[U \le F(x)] = F(x). This is how generators produce non-uniform numbers. For the exponential, F(x)=1−e−λxF(x) = 1 - e^{-\lambda x} inverts to x=−ln⁡(1−u)/λx = -\ln(1 - u)/\lambda:

python · try it yourself
import math
import random

rng = random.Random(6)
lam = 2.0
xs = [-math.log(1 - rng.random()) / lam for _ in range(100_000)]
print("sample mean:", round(sum(xs) / len(xs), 4), " theory 1/lambda =", 1 / lam)
for t in [0.1, 0.5, 1.0]:
    print(f"P(X <= {t}): {sum(x <= t for x in xs) / len(xs):.4f}  vs  {1 - math.exp(-lam * t):.4f}")
sample mean: 0.5017  theory 1/lambda = 0.5
P(X <= 0.1): 0.1804  vs  0.1813
P(X <= 0.5): 0.6309  vs  0.6321
P(X <= 1.0): 0.8640  vs  0.8647

The same trick works for discrete distributions: walk through the values, accumulating probability, until the total exceeds uu. That is what random.choices does with its weights.

Quantiles

The inverse CDF is the quantile function: F−1(0.5)F^{-1}(0.5) is the median, F−1(0.95)F^{-1}(0.95) the 95th percentile. Latency targets ("p99 under 200 ms") are statements about quantiles of a response-time distribution. The Statistics course computes them from data.

In software

NumPy's and SciPy's random generators use inverse transforms, rejection sampling and special-purpose algorithms (the ziggurat method for normals). Monitoring systems estimate quantiles of latency streams with sketches such as t-digest and HDR histograms, which are compact approximations of an empirical CDF.

Exercises

Exercise 1: A staircase

A fair die is rolled. What is F(3.5)F(3.5)? What is F(6)F(6)?

Answer

F(3.5)=1/2F(3.5) = 1/2; F(6)=1F(6) = 1.

Solution

Pr⁡[X≤3.5]=Pr⁡[X∈{1,2,3}]=3/6\Pr[X \le 3.5] = \Pr[X \in \{1, 2, 3\}] = 3/6, and every roll is at most 6.

Exercise 2: Minimum of uniforms

Find the CDF and density of the minimum of nn independent uniforms on [0,1][0, 1].

Answer

F(y)=1−(1−y)nF(y) = 1 - (1 - y)^n, f(y)=n(1−y)n−1f(y) = n(1 - y)^{n-1}.

Solution

Pr⁡[min⁡>y]=Pr⁡[all>y]=(1−y)n\Pr[\min > y] = \Pr[\text{all} > y] = (1 - y)^n; take the complement and differentiate.

Exercise 3: Inverse transform for a custom density (code)

Sample from f(x)=2xf(x) = 2x on [0,1][0, 1] by inverse transform and check the mean, which should be 2/32/3.

Answer

F(x)=x2F(x) = x^2, so X=UX = \sqrt{U}; the sample mean is close to 0.6670.667.

Solution

F(x)=∫0x2t dt=x2F(x) = \int_0^x 2t\,dt = x^2; solving u=x2u = x^2 gives x=ux = \sqrt{u}. The mean is ∫01x⋅2x dx=2/3\int_0^1 x \cdot 2x\,dx = 2/3.

python · try it yourself
import random

rng = random.Random(8)
xs = [rng.random() ** 0.5 for _ in range(100_000)]
print(round(sum(xs) / len(xs), 4), round(sum(x <= 0.5 for x in xs) / len(xs), 4))
0.665 0.2539

The second number estimates F(0.5)=0.25F(0.5) = 0.25.

Test yourself

If FF is the CDF of XX, what is P(a<X≤b)P(a < X \le b)?

  1. F(a)−F(b)F(a) - F(b)
  2. F(b)−F(a)F(b) - F(a)
  3. F(b)⋅F(a)F(b) \cdot F(a)
  4. F′(b)F'(b)

Inverse transform sampling for the exponential distribution with rate λ\lambda returns…

  1. λu\lambda u
  2. −ln⁡(1−u)/λ-\ln(1 - u)/\lambda
  3. e−λue^{-\lambda u}
  4. uλu^\lambda

The median of a continuous distribution is…

  1. F(0.5)F(0.5)
  2. F−1(0.5)F^{-1}(0.5)
  3. f(0.5)f(0.5)
  4. the mode