Linearity and Indicator Variables
The most powerful fact about expectation is that it is linear: the expected value of a sum is the sum of the expected values, whether or not the variables are independent. Combined with indicator variables, this turns hard counting questions into easy sums.
Linearity of expectation
For any random variables and constants :
The proof is one line: , and the sum splits. No independence is needed. That is what makes linearity so useful: the parts can be heavily dependent.
Indicator variables
The indicator of an event is if happens and otherwise. Its expectation is a probability: . To find the expected number of events that happen, write the count as a sum of indicators:
Examples
Fixed points. In a random permutation of items, position is fixed with probability , so the expected number of fixed points is , for every , even though the events are dependent.
Binomial mean. with gives without summing .
Empty bins. Throw balls into bins. Bin is empty with probability , so the expected number of empty bins is . With as many balls as bins, about of the bins stay empty.
import random
rng = random.Random(1)
n = m = 1000
runs = 500
empty_total = 0
for _ in range(runs):
bins = [0] * n
for _ in range(m):
bins[rng.randrange(n)] += 1
empty_total += bins.count(0)
print("average empty bins:", empty_total / runs)
print("n (1 - 1/n)^m =", round(n * (1 - 1 / n) ** m, 2))average empty bins: 367.5
n (1 - 1/n)^m = 367.7
Runs. In 100 fair coin tosses, how many runs (maximal blocks of equal results) are expected? A new run starts at toss 1 and at each toss that differs from toss , which has probability . So .
import random
rng = random.Random(2)
def runs(seq):
return 1 + sum(seq[i] != seq[i - 1] for i in range(1, len(seq)))
total = sum(runs([rng.random() < 0.5 for _ in range(100)]) for _ in range(20_000))
print("average runs in 100 tosses:", round(total / 20_000, 3), " predicted 50.5")average runs in 100 tosses: 50.499 predicted 50.5
Expected number of distinct values
Draw times with replacement from items. Item is seen with probability , so the expected number of distinct items is . A bootstrap sample () contains on average about of the original data points, which is why bagging leaves about 36.8% "out of bag" for validation.
import numpy as np
rng = np.random.default_rng(3)
n = 1000
fractions = [len(np.unique(rng.integers(0, n, n))) / n for _ in range(500)]
print("average fraction in a bootstrap sample:", round(float(np.mean(fractions)), 4))
print("1 - (1 - 1/n)^n =", round(1 - (1 - 1 / n) ** n, 4))average fraction in a bootstrap sample: 0.6323
1 - (1 - 1/n)^n = 0.6323
In software
Indicator sums analyse hash tables (expected number of collisions), random sampling (expected distinct items), randomized quicksort (expected comparisons, summed over pairs), and A/B testing metrics (expected conversions). Whenever a question asks "how many on average", look for indicators. The Discrete Mathematics course has more examples.
Exercises
Exercise 1: Birthdays shared
In a group of 30, what is the expected number of pairs that share a birthday?
Answer
.
Solution
One indicator per pair; each pair matches with probability .
Exercise 2: Hats
people throw their hats in a box and each draws one at random. What is the expected number of people who get a hat that belongs to their left neighbour (in a circle)?
Answer
Solution
Each person gets the specific hat with probability ; summing over people gives 1.
Exercise 3: Local maxima (code)
In a random permutation of , an interior position (with ) is a local maximum if it is larger than both neighbours. Find the expected number of local maxima and check by simulation for .
Answer
, so 6 for .
Solution
Among three consecutive values, each is equally likely to be the largest, so an interior position is a local maximum with probability .
import random
rng = random.Random(4)
n, runs = 20, 20_000
total = 0
for _ in range(runs):
p = list(range(n))
rng.shuffle(p)
total += sum(p[i] > p[i - 1] and p[i] > p[i + 1] for i in range(1, n - 1))
print(round(total / runs, 3), (n - 2) / 3)6.0 6.0
Test yourself
Does require and to be independent?
- Yes
- No
- Only for discrete variables
- Only if they have the same distribution
What is the expectation of the indicator of an event ?
- 1
- 0
With balls thrown into bins, about what fraction of bins stay empty for large ?
- 0
- 1/2