TExcelTAD
Home › Discrete Mathematics › Random Variables

Random Variables

A random variable turns the outcome of an experiment into a number: the number of heads, the running time of a randomized algorithm, the number of keys in a hash bucket. Formally it is a function R:S→RR: S \to \mathbb{R} on the sample space. Once you have random variables you can talk about their distributions, their averages and how they combine.

Distributions

The probability density function (PDF) of a discrete random variable RR is PDFR(x)=Pr⁡[R=x]\text{PDF}_R(x) = \Pr[R = x], and its cumulative distribution function is CDFR(x)=Pr⁡[R≤x]\text{CDF}_R(x) = \Pr[R \le x]. Common distributions in computer science:

Distribution Models Pr⁡[R=k]\Pr[R = k]
Bernoulli(pp) one trial: success or not pp for k=1k = 1, 1−p1 - p for k=0k = 0
uniform on {1..n}\{1..n\} a fair die, a random index 1n\frac{1}{n}
binomial(n,pn, p) successes in nn independent trials (nk)pk(1−p)n−k\binom{n}{k} p^k (1 - p)^{n-k}
geometric(pp) trials until the first success (1−p)k−1p(1 - p)^{k-1} p, k≥1k \ge 1
python · try it yourself
from math import comb

n, p = 10, 0.3
pdf = [comb(n, k) * p ** k * (1 - p) ** (n - k) for k in range(n + 1)]
cdf, acc = [], 0.0
for v in pdf:
    acc += v
    cdf.append(acc)
for k in range(n + 1):
    print(f"k={k:2d}  P(R=k)={pdf[k]:.4f}  P(R<=k)={cdf[k]:.4f}  " + "#" * round(pdf[k] * 100))
k= 0  P(R=k)=0.0282  P(R<=k)=0.0282  ###
k= 1  P(R=k)=0.1211  P(R<=k)=0.1493  ############
k= 2  P(R=k)=0.2335  P(R<=k)=0.3828  #######################
k= 3  P(R=k)=0.2668  P(R<=k)=0.6496  ###########################
k= 4  P(R=k)=0.2001  P(R<=k)=0.8497  ####################
k= 5  P(R=k)=0.1029  P(R<=k)=0.9527  ##########
k= 6  P(R=k)=0.0368  P(R<=k)=0.9894  ####
k= 7  P(R=k)=0.0090  P(R<=k)=0.9984  #
k= 8  P(R=k)=0.0014  P(R<=k)=0.9999
k= 9  P(R=k)=0.0001  P(R<=k)=1.0000
k=10  P(R=k)=0.0000  P(R<=k)=1.0000

The bars show the familiar hump of the binomial distribution, peaking near np=3np = 3. See Binomial Probabilities for more.

Independence of random variables

R1R_1 and R2R_2 are independent if for all values x1,x2x_1, x_2 the events [R1=x1][R_1 = x_1] and [R2=x2][R_2 = x_2] are independent. The number on the first die is independent of the number on the second; but the first die and the sum of both dice are not.

Hash bucket loads

Throw nn keys uniformly at random into mm buckets. The number of keys in bucket 1 is a sum of nn independent Bernoulli(1m\frac1m) indicators, so it is binomial(n,1mn, \frac1m). With n=mn = m, the probability that bucket 1 is empty is (1−1n)n≈1e≈0.37(1 - \frac1n)^n \approx \frac1e \approx 0.37: about 37% of the buckets stay empty, even though there are as many keys as buckets.

python · try it yourself
import random
from collections import Counter

rng = random.Random(42)
m = n = 10000
loads = Counter(rng.randrange(m) for _ in range(n))
dist = Counter(loads[b] for b in range(m))
for k in range(6):
    print(f"buckets with {k} keys: {dist[k] / m:.4f}")
print("max load:", max(loads.values()))
buckets with 0 keys: 0.3676
buckets with 1 keys: 0.3671
buckets with 2 keys: 0.1856
buckets with 3 keys: 0.0611
buckets with 4 keys: 0.0150
buckets with 5 keys: 0.0032
max load: 6

The fractions match the Poisson(1) probabilities e−1k!\frac{e^{-1}}{k!}: 0.368, 0.368, 0.184, 0.061, 0.015. The maximum load grows like ln⁡nln⁡ln⁡n\frac{\ln n}{\ln \ln n}, which is why hash table chains stay short.

Functions of random variables

If RR is a random variable, so is any f(R)f(R), and sums and products of random variables are random variables. The running time of an algorithm is typically a sum of many simple random variables, which is what makes linearity of expectation so useful.

In software

Simulation code samples random variables (random.random() is a uniform variable on [0,1)[0, 1)), load balancers are analysed with balls-into-bins distributions, and network packet losses are modelled as Bernoulli trials. NumPy's numpy.random.Generator samples every distribution in the table above.

Exercises

Exercise 1: Distribution of a sum

Two fair dice; let RR be their sum. Find Pr⁡[R=7]\Pr[R = 7] and Pr⁡[R≤4]\Pr[R \le 4].

Answer

636=16\frac{6}{36} = \frac{1}{6}; 636=16\frac{6}{36} = \frac{1}{6}.

Solution

Sum 7: 6 of the 36 outcomes. Sum at most 4: sums 2, 3, 4 occur in 1+2+3=61 + 2 + 3 = 6 outcomes.

Exercise 2: Geometric distribution

A request succeeds with probability 0.8, independently each try. What is the probability that the first success is on the third try? That more than 3 tries are needed?

Answer

0.0320.032; 0.0080.008.

Solution

Pr⁡[R=3]=0.22⋅0.8=0.032\Pr[R = 3] = 0.2^2 \cdot 0.8 = 0.032. More than 3 tries means the first 3 all fail: 0.23=0.0080.2^3 = 0.008.

Exercise 3: Empty buckets (code)

Estimate the fraction of empty buckets when n=2mn = 2m keys are thrown into m=5000m = 5000 buckets, and compare with e−2e^{-2}.

Answer

About 0.135.

Solution

A bucket stays empty with probability (1−1m)2m≈e−2≈0.1353(1 - \frac1m)^{2m} \approx e^{-2} \approx 0.1353.

python · try it yourself
import random, math

rng = random.Random(9)
m = 5000
used = {rng.randrange(m) for _ in range(2 * m)}
print(round(1 - len(used) / m, 4), round(math.exp(-2), 4))
0.135 0.1353

Test yourself

Formally, a random variable is…

  1. a probability
  2. a function from the sample space to the real numbers
  3. an event
  4. a set of outcomes

The number of heads in 20 independent fair coin flips has which distribution?

  1. uniform
  2. geometric
  3. binomial(20, 1/2)
  4. Bernoulli(1/2)

With nn keys hashed into nn buckets, about what fraction of buckets stay empty?

  1. 0
  2. 1/n1/n
  3. 1/e≈37%1/e \approx 37\%
  4. 50%