TExcelTAD
Home › Probability Theory › Uniform and Exponential Densities

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

XX is uniform on [a,b][a, b] if its density is 1b−a\frac{1}{b - a} there and 0 elsewhere. Probabilities are proportional to length: Pr⁡[c≤X≤d]=d−cb−a\Pr[c \le X \le d] = \frac{d - c}{b - a} for a≤c≤d≤ba \le c \le d \le b. The mean is the midpoint a+b2\frac{a + b}{2} and the variance is (b−a)212\frac{(b - a)^2}{12}.

Rounding errors are a classic example: rounding to the nearest integer makes an error roughly uniform on [−0.5,0.5][-0.5, 0.5], with standard deviation 1/12≈0.2891/\sqrt{12} \approx 0.289. Adding 100 rounded numbers gives a total error with standard deviation about 100⋅0.289≈2.9\sqrt{100} \cdot 0.289 \approx 2.9, not 50 (the worst case).

python · try it yourself
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

TT is exponential with rate λ\lambda if f(t)=λe−λtf(t) = \lambda e^{-\lambda t} for t≥0t \ge 0. Then

Pr⁡[T>t]=e−λt,E[T]=1λ,Var⁡(T)=1λ2\Pr[T > t] = e^{-\lambda t}, \qquad E[T] = \frac{1}{\lambda}, \qquad \operatorname{Var}(T) = \frac{1}{\lambda^2}

It is the continuous analogue of the geometric distribution: divide time into tiny steps of length Δt\Delta t, each with success probability λΔt\lambda \Delta t, and the waiting time becomes exponential as Δt→0\Delta t \to 0. It is memoryless, and it is the gap distribution of a Poisson process.

The minimum of exponentials

If T1,…,TnT_1, \ldots, T_n are independent exponentials with rates λ1,…,λn\lambda_1, \ldots, \lambda_n, then min⁡(T1,…,Tn)\min(T_1, \ldots, T_n) is exponential with rate λ1+⋯+λn\lambda_1 + \cdots + \lambda_n, because

Pr⁡[min⁡>t]=∏ie−λit=e−(λ1+⋯+λn)t\Pr[\min > t] = \prod_i e^{-\lambda_i t} = e^{-(\lambda_1 + \cdots + \lambda_n) t}

and the probability that TiT_i is the smallest is λi/∑jλj\lambda_i / \sum_j \lambda_j. A cluster of 20 machines, each failing at rate 1 per 10 years, sees its first failure after 10/20=0.510/20 = 0.5 years on average.

python · try it yourself
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, T=−ln⁡(1−U)/λT = -\ln(1 - U)/\lambda (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.

python · try it yourself
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 [2,8][2, 8].

Answer

Mean 5, standard deviation 3≈1.73\sqrt{3} \approx 1.73.

Solution

Mean (2+8)/2=5(2 + 8)/2 = 5; variance 62/12=36^2/12 = 3.

Exercise 2: Median lifetime

A light bulb's lifetime is exponential with mean 1000 hours. What is its median lifetime?

Answer

1000ln⁡2≈6931000 \ln 2 \approx 693 hours.

Solution

Solve e−t/1000=1/2e^{-t/1000} = 1/2: t=1000ln⁡2t = 1000 \ln 2. 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 1/(0.01+0.02)≈33.31/(0.01 + 0.02) \approx 33.3 ms; the slower server wins with probability 1/31/3.

Solution

This "hedged request" trick is used to cut tail latency in large services.

python · try it yourself
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 [0,1][0, 1]?

  1. 1/2
  2. 1/4
  3. 1/12
  4. 1

The minimum of independent exponentials with rates 1, 2 and 3 is exponential with rate…

  1. 1
  2. 2
  3. 6
  4. 1/6