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
Every CDF is non-decreasing, tends to 0 at and to 1 at , and is right-continuous. Then . For a discrete variable is a staircase that jumps by at each value; for a continuous variable with density ,
by the fundamental theorem of calculus.
Distributions of functions of
To find the distribution of , compute its CDF and differentiate. For with uniform on :
For the maximum of independent uniforms, , so .
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 ) 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 is continuous and strictly increasing and is uniform on , then has CDF , since . This is how generators produce non-uniform numbers. For the exponential, inverts to :
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 . That is what random.choices does with its weights.
Quantiles
The inverse CDF is the quantile function: is the median, 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 ? What is ?
Answer
; .
Solution
, and every roll is at most 6.
Exercise 2: Minimum of uniforms
Find the CDF and density of the minimum of independent uniforms on .
Answer
, .
Solution
; take the complement and differentiate.
Exercise 3: Inverse transform for a custom density (code)
Sample from on by inverse transform and check the mean, which should be .
Answer
, so ; the sample mean is close to .
Solution
; solving gives . The mean is .
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 .
Test yourself
If is the CDF of , what is ?
Inverse transform sampling for the exponential distribution with rate returns…
The median of a continuous distribution is…
- the mode