TExcelTAD
Home › Probability Theory › Linearity and Indicator Variables

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 X,YX, Y and constants a,ba, b:

E[aX+bY]=a E[X]+b E[Y]E[aX + bY] = a\,E[X] + b\,E[Y]

The proof is one line: E[X+Y]=∑ω(X(ω)+Y(ω))m(ω)E[X + Y] = \sum_\omega (X(\omega) + Y(\omega)) m(\omega), 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 AA is IA=1I_A = 1 if AA happens and 00 otherwise. Its expectation is a probability: E[IA]=Pr⁡[A]E[I_A] = \Pr[A]. To find the expected number of events that happen, write the count as a sum of indicators:

N=IA1+⋯+IAn⟹E[N]=Pr⁡[A1]+⋯+Pr⁡[An]N = I_{A_1} + \cdots + I_{A_n} \quad\Longrightarrow\quad E[N] = \Pr[A_1] + \cdots + \Pr[A_n]

Examples

Fixed points. In a random permutation of nn items, position ii is fixed with probability 1/n1/n, so the expected number of fixed points is n⋅1n=1n \cdot \frac{1}{n} = 1, for every nn, even though the events are dependent.

Binomial mean. S=I1+⋯+InS = I_1 + \cdots + I_n with E[Ij]=pE[I_j] = p gives E[S]=npE[S] = np without summing k(nk)pkqn−kk\binom{n}{k}p^k q^{n-k}.

Empty bins. Throw mm balls into nn bins. Bin jj is empty with probability (1−1/n)m(1 - 1/n)^m, so the expected number of empty bins is n(1−1/n)m≈ne−m/nn(1 - 1/n)^m \approx n e^{-m/n}. With as many balls as bins, about 1/e≈37%1/e \approx 37\% of the bins stay empty.

python · try it yourself
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 i≥2i \ge 2 that differs from toss i−1i - 1, which has probability 1/21/2. So E[runs]=1+99⋅12=50.5E[\text{runs}] = 1 + 99 \cdot \frac{1}{2} = 50.5.

python · try it yourself
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 kk times with replacement from nn items. Item jj is seen with probability 1−(1−1/n)k1 - (1 - 1/n)^k, so the expected number of distinct items is n(1−(1−1/n)k)n\left(1 - (1 - 1/n)^k\right). A bootstrap sample (k=nk = n) contains on average about 1−1/e≈63.2%1 - 1/e \approx 63.2\% of the original data points, which is why bagging leaves about 36.8% "out of bag" for validation.

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

(302)/365=435/365≈1.19\binom{30}{2}/365 = 435/365 \approx 1.19.

Solution

One indicator per pair; each pair matches with probability 1/3651/365.

Exercise 2: Hats

nn 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 1/n1/n; summing over nn people gives 1.

Exercise 3: Local maxima (code)

In a random permutation of 1,…,n1, \ldots, n, an interior position ii (with 1<i<n1 < i < n) is a local maximum if it is larger than both neighbours. Find the expected number of local maxima and check by simulation for n=20n = 20.

Answer

(n−2)/3(n - 2)/3, so 6 for n=20n = 20.

Solution

Among three consecutive values, each is equally likely to be the largest, so an interior position is a local maximum with probability 1/31/3.

python · try it yourself
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 E[X+Y]=E[X]+E[Y]E[X + Y] = E[X] + E[Y] require XX and YY to be independent?

  1. Yes
  2. No
  3. Only for discrete variables
  4. Only if they have the same distribution

What is the expectation of the indicator of an event AA?

  1. 1
  2. 0
  3. P(A)P(A)
  4. 1−P(A)1 - P(A)

With nn balls thrown into nn bins, about what fraction of bins stay empty for large nn?

  1. 0
  2. 1/2
  3. 1/e≈0.371/e \approx 0.37
  4. 1−1/e1 - 1/e