A random hand-back of graded problem sets motivates inclusion–exclusion. We correct over- and under-counting on the n=2 and n=3 Venn diagrams, state the general formula and prove it by induction, apply it to get the derangement probability tending to 1/e, then use linearity of expectation to show the expected number of correct returns is 1, solve the coupon collector problem, and introduce independence of random variables, variance and its additivity.
A class of n students submits one problem set each. After grading, the sets are handed back at random — equivalently, a uniformly random permutation of the n sets is chosen.
What is the probability that nobody receives their own problem set?
This is the classical derangement problem. Intuitively, when n is large the probability should settle near some constant — and that constant turns out to be 1/e≈0.368.
The tool that produces the answer is inclusion–exclusion. Its idea is as plain as can be: count in the crudest possible way, notice that you over-counted, subtract the excess, notice that you subtracted too much, add something back, and keep correcting until the count is exact.
Timeline
Time
Topic
0:00
Motivation: random hand-back, probability that nobody is correct
1:41
Splitting into events; the idea of inclusion–exclusion
3:40
Venn diagram: the correction for n=2
5:21
n=3: why the triple intersection must be added back
8:20
Concrete numbers in the small cases
12:34
The general formula
15:49
Back to the problem: P(Ai)=1/n, P(Ai∩Aj)=1/(n(n−1))
18:27
Binomial coefficients and the number of terms
22:57
Summing to 1−1/2!+1/3!−⋯ and 1/e
26:26
Inductive proof of the formula
38:31
Linearity of expectation: expected number of correct returns is 1
41:28
Turning to variance; the birthday problem
43:27
The coupon collector and nHn
52:13
Independence of random variables
1:08:20
Variance: two formulas and additivity under independence
Writing the problem in terms of events
Let Ai be the event that student i receives their own problem set. We want
P(A1∩A2∩⋯∩An).
This intersection is hard to compute directly, but its complement is easy to describe: at least one student receives their own set. By De Morgan,
A1∩⋯∩An=A1∪A2∪⋯∪An.
Hence
P(nobody correct)=1−P(A1∪⋯∪An).
The problem reduces to computing the probability of a union of n events.
Inclusion–exclusion: over-count, then correct
Figure:The n=3 derangement example: Venn counts, the correction ladder, the general formula。
The case n=2
Draw two overlapping disks A1 and A2. The crudest estimate is
P(A1)+P(A2).
Under this count the intersection A1∩A2 is counted twice, so subtract one copy:
P(A1∪A2)=P(A1)+P(A2)−P(A1∩A2).
That is inclusion–exclusion in embryo.
For our problem with n=2: the only permutations of two students are "both correct" and "they swap". Therefore
P(A1)=21,P(A2)=21,P(A1∩A2)=21,
since the only outcome satisfying both is the identity permutation. Thus
Note that P(A1∩A2)=1/2=1/4: the events are not independent. Once the first student receives their own set, the remaining set is forced, so the second student is automatically correct. This is exactly why the problem cannot be handled by naive multiplication.
The case n=3
Adding the three single events over-counts the pairwise overlaps; subtracting the three pairwise intersections over-corrects the common centreA1∩A2∩A3, which was added three times and subtracted three times — net count zero. So add it back once:
Plug in: P(Ai)=1/3; if two students are correct, the third has no choice, so P(Ai∩Aj)=1/6; and the triple intersection is again only the identity, P(A1∩A2∩A3)=1/6. Hence
P(at least one correct)=3⋅31−3⋅61+61=1−21+61=32,P(nobody correct)=1−32=31.
This matches D3/3!=2/6=1/3, where D3 is the number of derangements.
In words: singles add, pairs subtract, triples add, quadruples subtract, signs alternating up to the n-fold intersection.
Why must the signs alternate? Consider an outcome lying in exactly r of the events. It is counted
(1r)−(2r)+(3r)−⋯+(−1)r+1(rr)=1−(1−1)r=1
times, since the binomial expansion gives (1−1)r=0 for r≥1. Every outcome is therefore counted exactly once.
Back to the original problem
The individual probabilities
Handing back the sets is a uniformly random choice among the n! permutations. For any i,
P(Ai)=n!(n−1)!=n1.
For r specified students all to be correct, the other n−r sets can be permuted freely, so
P(Ai1∩⋯∩Air)=n!(n−r)!=n(n−1)⋯(n−r+1)1.
This is the conditional-probability reading: the first student is correct with probability 1/n; given that, the second is correct with probability 1/(n−1); and so on.
Counting the terms: binomial coefficients
In the inclusion–exclusion formula there are (rn) terms at level r, where
(rn)=r!n(n−1)⋯(n−r+1)=r!(n−r)!n!.
It counts the ways to choose r objects from n. The numerator chooses them in order — n choices, then n−1, and so on — but that counts each set r! times, hence the division. In particular
(2n)=2n(n−1).
Summing
Multiply the number of terms by the value of each term:
Enormous cancellation occurs. For the second term,
(2n)⋅n(n−1)1=2n(n−1)⋅n(n−1)1=2!1,
and in general the r-th term equals 1/r!. Therefore
P(at least one correct)=1−2!1+3!1−4!1+⋯+(−1)n+1n!1.
So
P(nobody correct)=1−P(at least one correct)=2!1−3!1+⋯+(−1)nn!1.
Padding with the cancelling first two terms 1−1=0, this is
P(nobody correct)=r=0∑nr!(−1)r.
As n→∞,
r=0∑∞r!(−1)r=e1≈0.3679.
The number e emerges from a pure permutation-counting problem. Numerically: 1/2 for n=2, 1/3 for n=3, 9/24=0.375 for n=4, after which the values oscillate around 1/e and converge quickly.
A useful heuristic
Strictly, the Ai are not independent. But for large n they are "almost independent": whether the first student is correct changes the second student's chance by only about 1/n. So the loose calculation
P(nobody correct)≈i=1∏n(1−n1)=(1−n1)nn→∞e1
gives the right answer. This is not a proof — independence fails — but inclusion–exclusion is exactly what turns the "almost independent" intuition into a theorem.
Inductive proof of inclusion–exclusion
Theorem. The formula above holds for all events A1,…,An.
Proof. Induct on n. The case n=2 is the Venn-diagram identity.
Assume the formula holds for n, and set C=A1∪⋯∪An. Consider the formula for n+1 events and split its terms into those not containingAn+1 and those containingAn+1.
The terms not containing An+1 are precisely the inclusion–exclusion formula for the first n events, so by the induction hypothesis they sum to P(C).
The terms containing An+1 have the opposite sign to their counterparts in the n-event formula. Define helper events
Bi=Ai∩An+1(i=1,…,n).
Then that group of terms is −1 times the inclusion–exclusion formula applied to B1,…,Bn, because
Bi∩Bj=(Ai∩An+1)∩(Aj∩An+1)=Ai∩Aj∩An+1
(intersecting twice with An+1 is the same as once). By the induction hypothesis the group equals
−P(B1∪⋯∪Bn)=−P((A1∪⋯∪An)∩An+1)=−P(C∩An+1).
Adding the two groups gives
P(C)+P(An+1)−P(C∩An+1),
which is the n=2 identity applied to C and An+1, hence equals P(C∪An+1)=P(A1∪⋯∪An+1). This completes the induction. □
What matters here is not the algebra but the structure: split the n+1 expression into "the old formula" and "minus the old formula", then reinterpret the latter in terms of new helper events. Designing the reader's path through a proof matters more than checking it line by line.
One line with linearity of expectation
Inclusion–exclusion took a whole lecture to get "nobody correct". Now ask a neighbouring question: what is the expected number of students who receive their own set?
Define
X=i=1∑n1Ai,
the number of correct returns. By linearity of expectation, with no independence assumption whatsoever,
E[X]=i=1∑nE[1Ai]=i=1∑nP(Ai)=i=1∑nn1=1.
The expectation is exactly one, independent of n.
This is where linearity earns its keep: it sidesteps dependence entirely. The Ai are highly correlated — one correct return drastically changes everyone else's chances — yet additivity of expectation does not care. The lecture stresses the contrast: this works for expectations, but not for products of expectations, and not for variances.
The coupon collector problem
The question
Each newspaper comes with one of n coupons, uniformly and independently. How many newspapers must be bought on average to collect all n? With n=365, this is "how many students must be asked to see every birthday".
Split into stages
The key trick is again to decompose a complicated random variable into simple ones. Let
T=T0+T1+⋯+Tn−1,
where Ti is the number of newspapers needed to go from i distinct coupons to i+1.
Having collected i kinds, the probability that the next paper brings a new one is
pi=nn−i,
since n−i of the n kinds are still unseen. Attempts are independent, so Ti is geometric with success probability pi, and by the previous lecture
E[Ti]=pi1=n−in.
Summing
E[T]=i=0∑n−1n−in=n(n1+n−11+⋯+11)=nHn,
where Hn=∑k=1n1/k is the n-th harmonic number. For n=365,
H365≈6.47,E[T]≈6.47×365≈2361.
So about 2361 students must be asked to cover all 365 birthdays — roughly 6.5 times the number of days.
Asymptotics
Since Hn≈lnn+γ with γ≈0.577,
E[T]=nHn≈nlnn.
The rule of thumb "covering n kinds takes nlogn draws" follows. Intuition: with k kinds still missing, each draw hits a new one with probability k/n, so waiting takes n/k draws; summing k=1,…,n gives nHn.
Independence of random variables
The previous lecture defined independence for events; now extend it to random variables.
Definition. Random variables f,g are independent if for all real α,β the events
{x:f(x)=α},{x:g(x)=β}
are independent:
P(f=α,g=β)=P(f=α)P(g=β).
Note the quantifier "for all α,β", which makes verification much harder than for events.
Consistency check: take f=1A and g=1B; then α,β need only range over 0,1 and the definition collapses to independence of the events A and B. So independence of random variables genuinely extends independence of events.
Lemma: the product rule for independent variables
Lemma. If f,g are independent, then
E[fg]=E[f]E[g].
Proof. Expand the definition and partition the sample space according to the values taken:
The last step factors the double sum and recognises each factor as the expectation written by grouping equal values. □
Independence cannot be dropped
Take f=1A and g=1Ac with 0<P(A)<1. They are clearly dependent. Then
fg=1A1Ac=0⟹E[fg]=0,
yet
E[f]E[g]=P(A)(1−P(A))>0.
So without independence the lemma fails. The counterexample also pinpoints the step where independence is used: splitting the joint probability into a product.
Variance
Definition
The variance of a random variable f is
Var(f)=E[(f(x)−E[f])2].
It shifts the distribution so its centre is 0, then averages the squared deviations. Variance is a coarse measure of how spread out the random variable is.
The middle line uses one key fact: E[f] is a constant, not a random variable, so it factors out of the expectation. Both forms are useful in different situations.
Two basic properties
Non-negativity: Var(f)≥0, since it is the expectation of a non-negative random variable.
Zero means constant: Var(f)=0 if and only if f is constant. If the variance is zero, the expectation of (f−E[f])2 is zero; being non-negative, it must vanish identically, so f=E[f] everywhere.
Variances add for independent variables
Theorem. If f,g are independent, then
Var(f+g)=Var(f)+Var(g).
Proof. Write μf=E[f], μg=E[g]. By definition and linearity,
Only the cross term remains. By independence, f−μf and g−μg are independent, so the expectation of the product is the product of expectations:
E[(f−μf)(g−μg)]=E[f−μf]E[g−μg]=0⋅0=0.
The cross term vanishes, proving the claim. □
Linearity alone is not enough here: without independence the cross term is generally nonzero and variances do not add. This is the contrast the lecture keeps emphasising — linearity of expectation is unconditional; additivity of variance requires independence.
Where these tools lead
Additivity of variance is the seed of the concentration inequalities later in the course. Consider polling: sample n people, each supporting a candidate with probability p, and let p^ be the sample proportion,
p^=n1i=1∑n1Ai.
Each indicator has variance p(1−p); by independence,
Var(p^)=n21⋅np(1−p)=np(1−p).
The variance decays like 1/n, so the standard deviation decays like 1/n — the origin of "quadruple the sample, halve the error". Later lectures turn this into rigorous confidence intervals via tail bounds.
What this lecture establishes
Inclusion–exclusion: the probability of a union equals "singles − pairs + triples − …", with alternating signs.
Why signs alternate: an outcome in exactly r events is counted ∑k=1r(−1)k+1(kr)=1 time.
Binomial coefficients (rn)=r!n(n−1)⋯(n−r+1) count the terms at level r.
Linearity of expectation needs no independence: the expected number of correct returns is always 1.
Coupon collector: split into stages to get E[T]=nHn≈nlnn; about 2361 for n=365.
Independent random variables satisfy E[fg]=E[f]E[g]; without independence it can fail (1A and 1Ac).
Variance Var(f)=E[(f−Ef)2]=E[f2]−(Ef)2≥0, zero exactly for constants.
Variances of independent random variables add; without independence the cross term is generally nonzero.
Comprehension check
1. For n=2, why is P(A1∩A2)=1/2 rather than 1/4?
Reference answer: The sample space is the set of all permutations of two students, of which there are only two: both correct, or they swap. The event A1∩A2 contains only the first, so its probability is 1/2. Using independence to write P(A1)P(A2)=1/2⋅1/2=1/4 is wrong: once the first student receives their own set, only one set remains, so the second must also be correct. In general P(Ai∩Aj)=1/(n(n−1)), not 1/n2.
2. State inclusion–exclusion and explain why the signs must alternate.
4. Why does "the expected number of correct returns is 1" need no independence? Where does the coupon collector use the same trick?
Reference answer: Linearity of expectation holds for arbitrary random variables:
E[i∑1Ai]=i∑E[1Ai]=i∑P(Ai),
using only "expectation of a sum is the sum of expectations", with no independence. Since P(Ai)=1/n for every i, the sum is n⋅(1/n)=1. The coupon collector splits T=∑i=0n−1Ti into stages; each Ti is geometric with success probability (n−i)/n and expectation n/(n−i); summing gives nHn. The splitting is justified by linearity alone, regardless of dependence between stages.
5. Give a counterexample showing E[fg]=E[f]E[g] fails without independence.
Reference answer: Take f=1A, g=1Ac with 0<P(A)<1. No outcome lies in both A and Ac, so fg≡0 and E[fg]=0. But
E[f]E[g]=P(A)(1−P(A))>0.
They differ. These variables are clearly dependent — knowing f determines g — showing that the independence hypothesis is essential.
6. Why does additivity of variance require independence? Which step fails otherwise?
Reference answer: Expanding gives
Var(f+g)=Var(f)+Var(g)+2E[(f−μf)(g−μg)].
Under independence the cross term is zero because the product rule applies:
E[(f−μf)(g−μg)]=E[f−μf]E[g−μg]=0.
Without independence that step is invalid and the cross term is generally nonzero, so variances do not add. Counterexample: g=f gives Var(f+g)=Var(2f)=4Var(f), while Var(f)+Var(g)=2Var(f); the cross term contributes the missing 2Var(f).
Relation to neighbouring lectures
This lecture links three tools: inclusion–exclusion for exact counts of "at least one event", linearity of expectation for bypassing dependence, and additivity of variance as a preview of concentration. The next lecture begins the counting unit — permutations, combinations and binomial coefficients — and generating functions later place 1/e, nHn and (rn) inside one algebraic framework.
One sentence to keep: once you can over-count and then correct, a union becomes a sum; once you can use indicators, counting becomes expectation.