The hat problem

At a party, 10 guests toss their hats into a pile. Each guest then picks a hat at random.

What do you think?
On average, how many guests get their own hat back?

This seems impossibly hard — the assignments are all tangled up with dependencies. But with indicator variables, it becomes a one-line calculation.

What is an indicator variable?

Indicator Random Variable

For any event AA, the indicator variable IAI_A is: IA={1if A occurs0if A does not occurI_A = \begin{cases} 1 & \text{if } A \text{ occurs} \\ 0 & \text{if } A \text{ does not occur} \end{cases}

Its expected value is simply the probability: E[IA]=P(A)E[I_A] = P(A).

The core property of indicator variables: E[IA]=1P(A)+0P(Ac)=P(A)E[I_A] = 1 \cdot P(A) + 0 \cdot P(A^c) = P(A). Expectation equals probability!

The indicator trick

Here's the strategy for "how many of these things happen?" problems:

  1. Define indicators: For each item ii, let Ii=1I_i = 1 if the event of interest happens for item ii
  2. Write the total: X=I1+I2++InX = I_1 + I_2 + \cdots + I_n
  3. Apply linearity: E[X]=E[I1]+E[I2]++E[In]E[X] = E[I_1] + E[I_2] + \cdots + E[I_n] (see linearity of expectation)
  4. Compute each E[Ii]E[I_i]: This is just P(event happens for item i)P(\text{event happens for item } i)

You never need to worry about dependence between the indicators. Linearity handles that automatically.

Solving the hat problem

Let Ii=1I_i = 1 if person ii gets their own hat.

Step 1: X=I1+I2++InX = I_1 + I_2 + \cdots + I_n = total matches

Step 2: E[Ii]=P(person i gets own hat)=1nE[I_i] = P(\text{person } i \text{ gets own hat}) = \frac{1}{n}

(Out of nn hats, exactly 1 is theirs.)

Step 3: E[X]=i=1nE[Ii]=n1n=1E[X] = \sum_{i=1}^n E[I_i] = n \cdot \frac{1}{n} = 1

Hat Match Simulator
Number of people
Trials
0
Avg matches
0.000
E[matches]
1.000

The indicators I1,I2,,InI_1, I_2, \ldots, I_n are not independent! If Alice got her own hat, it changes the odds for everyone else. But linearity of expectation doesn't care about dependence.

More examples

Fixed points in a random shuffle

Shuffle a deck of 52 cards. How many cards end up in their original position?

What do you think?
Expected number of cards in their original position?
Enter a whole number

Coupon collector (how many needed?)

A cereal box contains 1 of 5 toy types, equally likely. You want all 5.

When you have kk types, the probability the next box gives a new type is (5k)/5(5-k)/5.

The expected boxes needed for the next new type = 55k\frac{5}{5-k}.

E[total boxes]=55+54+53+52+51=1+1.25+1.67+2.5+5=11.42E[\text{total boxes}] = \Large\frac{5}{5} + \frac{5}{4} + \frac{5}{3} + \frac{5}{2} + \frac{5}{1} \normalsize= 1 + 1.25 + 1.67 + 2.5 + 5 = 11.42

With 6 toy types, what's the expected number of boxes needed? (Give to 2 decimal places) (decimal to 2 places, e.g. 5.23)

Simulate the coupon collector problem and see the indicator decomposition in action:

Coupon Collector Simulator
Number of coupon types
Trials
0
Avg draws
0.0
E[T] = nHₙ
14.7
Min draws
Why E[T] = nHₙ — the indicator trick
PhaseNew types leftP(new)E[draws]
116/61.001.00
225/60.831.20
334/60.671.50
443/60.502.00
552/60.333.00
661/60.176.00
Total E[T] = sum14.70

Expected inversions in a random permutation

Shuffle numbers 1 through nn. An inversion is a pair (i,j)(i,j) where i<ji < j but the value at position ii is greater than the value at position jj.

Let Iij=1I_{ij} = 1 if positions ii and jj form an inversion. For any pair, P(Iij=1)=1/2P(I_{ij} = 1) = 1/2 (one of the two orderings is equally likely).

E[inversions]=(n2)12=n(n1)4E[\text{inversions}] = \Large\binom{n}{2} \cdot \frac{1}{2} = \frac{n(n-1)}{4}

For n = 10, expected inversions? (decimal, e.g. 0.42)

When to use indicator variables

The indicator trick is perfect when you're asked:

  • "How many…" things satisfy a condition?
  • "What fraction…" of items have a property?
  • "Expected number…" of successes?

The pattern:

  1. Identify the individual items
  2. Define IiI_i for each
  3. Use linearity: E[Ii]=P(eventi)E[\sum I_i] = \sum P(\text{event}_i)

If you find yourself thinking "but the events are dependent!" — that's exactly when indicators shine. Independence is not required.

Summary

ConceptFormula
Indicator variableIA=1I_A = 1 if AA occurs, 00 otherwise
Expectation of indicatorE[IA]=P(A)E[I_A] = P(A)
Expected countE[Ii]=P(Ai)E\left[\sum I_i\right] = \sum P(A_i)
Key insightDependence between indicators doesn't matter

The indicator method: Decompose a complex count into a sum of binary questions. Each binary question is easy. Linearity adds them up for free.

Test your understanding

100 people in a room. Expected number sharing YOUR birthday? (decimal to 3 places, e.g. 0.456)
Shuffle 10 cards. Expected fixed points? (whole number)
Roll a die 6 times. Expected number of distinct values? (to 2 decimal places) (decimal to 2 places, e.g. 0.53)

What's next

Next we'll meet the geometric distribution — the waiting-time distribution for "how long until the first success?"