Uniform and Exponential Densities
Two continuous distributions do most of the everyday work: the uniform, for "any value in a range is equally likely", and the exponential, for "how long until the next random event". They are also the raw material from which other distributions are simulated.
The uniform density
is uniform on if its density is there and 0 elsewhere. Probabilities are proportional to length: for . The mean is the midpoint and the variance is .
Rounding errors are a classic example: rounding to the nearest integer makes an error roughly uniform on , with standard deviation . Adding 100 rounded numbers gives a total error with standard deviation about , not 50 (the worst case).
import random
import math
rng = random.Random(1)
errors = []
for _ in range(20_000):
xs = [rng.uniform(0, 1000) for _ in range(100)]
errors.append(sum(round(x) for x in xs) - sum(xs))
m = sum(errors) / len(errors)
sd = math.sqrt(sum((e - m) ** 2 for e in errors) / len(errors))
print("mean total error:", round(m, 3))
print("sd of total error:", round(sd, 3), " predicted:", round(10 / math.sqrt(12), 3))
print("largest seen:", round(max(abs(e) for e in errors), 2), " worst case: 50")mean total error: -0.032
sd of total error: 2.918 predicted: 2.887
largest seen: 11.74 worst case: 50
The exponential density
is exponential with rate if for . Then
It is the continuous analogue of the geometric distribution: divide time into tiny steps of length , each with success probability , and the waiting time becomes exponential as . It is memoryless, and it is the gap distribution of a Poisson process.
The minimum of exponentials
If are independent exponentials with rates , then is exponential with rate , because
and the probability that is the smallest is . A cluster of 20 machines, each failing at rate 1 per 10 years, sees its first failure after years on average.
import random
rng = random.Random(2)
rates = [0.1] * 20
firsts = [min(rng.expovariate(r) for r in rates) for _ in range(50_000)]
print("mean time to first failure:", round(sum(firsts) / len(firsts), 3), " predicted:", 1 / sum(rates))
# competing exponentials: who finishes first?
lam = {"A": 1.0, "B": 2.0, "C": 3.0}
wins = {k: 0 for k in lam}
for _ in range(60_000):
times = {k: rng.expovariate(r) for k, r in lam.items()}
wins[min(times, key=times.get)] += 1
print({k: round(v / 60_000, 3) for k, v in wins.items()}, " predicted: 1/6, 2/6, 3/6")mean time to first failure: 0.501 predicted: 0.5
{'A': 0.168, 'B': 0.332, 'C': 0.499} predicted: 1/6, 2/6, 3/6
Simulating from uniforms
Every distribution in this course can be simulated from uniform numbers. The exponential uses the inverse CDF, (CDFs). A geometric variable is the ceiling of a suitable exponential. Rejection sampling handles densities whose CDF has no simple inverse: propose uniform points under a box enclosing the density and keep those that fall under the curve.
import random
import math
rng = random.Random(3)
f = lambda x: 6 * x * (1 - x) # a density on [0, 1] with maximum 1.5
samples, proposals = [], 0
while len(samples) < 50_000:
x, y = rng.random(), rng.uniform(0, 1.5)
proposals += 1
if y <= f(x):
samples.append(x)
print("acceptance rate:", round(len(samples) / proposals, 3), " expected 1/1.5 =", round(1 / 1.5, 3))
print("sample mean:", round(sum(samples) / len(samples), 4), " exact 0.5")
print("P(X < 0.25):", round(sum(s < 0.25 for s in samples) / len(samples), 4), " exact", 0.25 ** 2 * (3 - 2 * 0.25))acceptance rate: 0.668 expected 1/1.5 = 0.667
sample mean: 0.4989 exact 0.5
P(X < 0.25): 0.1573 exact 0.15625
In software
Exponential backoff in network retries uses random waits so that clients do not retry in sync. Load generators produce exponential gaps to mimic Poisson traffic. Time-to-live caches with random expiry and simulated annealing's acceptance rule also draw on exponentials. Uniform jitter is added to scheduled jobs ("cron at a random minute") to avoid thundering herds.
Exercises
Exercise 1: Uniform variance
Find the mean and standard deviation of a uniform variable on .
Answer
Mean 5, standard deviation .
Solution
Mean ; variance .
Exercise 2: Median lifetime
A light bulb's lifetime is exponential with mean 1000 hours. What is its median lifetime?
Answer
hours.
Solution
Solve : . The median is below the mean because the distribution has a long right tail.
Exercise 3: Race to finish (code)
Two servers answer a request in exponential times with means 100 ms and 50 ms. You send the request to both and take the first answer. Simulate the mean response time and the probability that the slower server wins.
Answer
Mean ms; the slower server wins with probability .
Solution
This "hedged request" trick is used to cut tail latency in large services.
import random
rng = random.Random(4)
n = 100_000
total = slow_wins = 0
for _ in range(n):
a, b = rng.expovariate(1 / 100), rng.expovariate(1 / 50)
total += min(a, b)
slow_wins += a < b
print(round(total / n, 2), round(slow_wins / n, 3))33.28 0.328
Test yourself
What is the variance of a uniform distribution on ?
- 1/2
- 1/4
- 1/12
- 1
The minimum of independent exponentials with rates 1, 2 and 3 is exponential with rate…
- 1
- 2
- 6
- 1/6