Probability fundamentals
Probability lets you reason about randomised algorithms and average-case cost. This lesson covers sample spaces, events, conditional probability, independence, expected value, linearity of expectation and indicator variables.
Learning goals
- Compute probabilities in a finite sample space
- Use conditional probability and independence
- Compute expected values with linearity and indicator variables
Prerequisites
Sample space
The sample space Ω is the set of all possible outcomes of a random experiment. With equally likely outcomes, a probability is a ratio of counts. Two fair dice have |Ω| = 36 outcomes; a random permutation of n items has n! outcomes.
Clearly defining Ω first prevents most counting mistakes.
Events
An event is a subset of the sample space, and its probability is |event| / |Ω| for equally likely outcomes. P(A ∪ B) = P(A) + P(B) - P(A ∩ B) and P(not A) = 1 - P(A).
“The two dice sum to 7” contains 6 outcomes ((1,6), (2,5), (3,4), (4,3), (5,2), (6,1)), so its probability is 6/36 = 1/6.
1int hits = 0;2for (int a = 1; a <= 6; a++) for (int b = 1; b <= 6; b++) if (a + b == 7) hits++;3double p = hits / 36.0; // 0.1666...
Conditional probability
P(A | B) = P(A ∩ B) / P(B) is the probability of A once you know B happened; it restricts the sample space to B. Example: given that the first die shows 6, the probability that the sum is at least 10 is 3/6 = 1/2 (second die 4, 5 or 6).
Bayes' rule P(A | B) = P(B | A) · P(A) / P(B) reverses the conditioning and underlies spam filters.
Independence
Events A and B are independent if P(A ∩ B) = P(A) · P(B): knowing one tells nothing about the other. Two separate coin flips are independent; drawing two cards without replacement is not.
Randomised algorithms rely on independence, for example independent hash functions in a Bloom filter. Repeating an independent trial that succeeds with probability p until it succeeds takes 1/p tries on average.
Expected value
The expected value E[X] = Σ x · P(X = x) is the average value of a random variable over many trials. A fair die has E = (1 + 2 + … + 6)/6 = 3.5. Average-case analysis computes the expected running time over a distribution of inputs.
If the target of a linear search is equally likely to be at any of n positions, the expected number of comparisons is (n + 1)/2. The animation shows the matching experiment: random permutations and the average count of fixed points.
Expected fixed points of a random shuffle
Interactive · edit the inputthis shuffle
count by fixed points
There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them.
1// X = number of cards that land in their own position after a random shuffle2// X = X0 + X1 + ... + X(n-1), where Xi is 1 if card i is fixed, else 0 (indicator variables)3double expected = 0;4for (int[] p : allPermutations(n)) { // each equally likely5int fixed = 0;6for (int i = 0; i < n; i++) if (p[i] == i) fixed++;7expected += fixed;8}9expected /= factorial(n); // = 1.0 for every n
Live state
n
4
shuffles
24
Execution trace (dry run table)Show 26 stepsHide
| Step | Line | State | What happens |
|---|---|---|---|
| 1 | L3 | n=4 shuffles=24 | There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them. |
| 2 | L6 | shuffle=1 fixed=4 total=4 average=4 | Shuffle 1: 4 fixed points. Running total 4. |
| 3 | L6 | shuffle=2 fixed=2 total=6 average=3 | Shuffle 2: 2 fixed points. Running total 6. |
| 4 | L6 | shuffle=3 fixed=2 total=8 average=2.667 | Shuffle 3: 2 fixed points. Running total 8. |
| 5 | L6 | shuffle=4 fixed=1 total=9 average=2.25 | Shuffle 4: 1 fixed point. Running total 9. |
| 6 | L6 | shuffle=5 fixed=1 total=10 average=2 | Shuffle 5: 1 fixed point. Running total 10. |
| 7 | L6 | shuffle=6 fixed=2 total=12 average=2 | Shuffle 6: 2 fixed points. Running total 12. |
| 8 | L6 | shuffle=7 fixed=2 total=14 average=2 | Shuffle 7: 2 fixed points. Running total 14. |
| 9 | L6 | shuffle=8 fixed=0 total=14 average=1.75 | Shuffle 8: 0 fixed points. Running total 14. |
| 10 | L6 | shuffle=9 fixed=1 total=15 average=1.667 | Shuffle 9: 1 fixed point. Running total 15. |
| 11 | L6 | shuffle=10 fixed=0 total=15 average=1.5 | Shuffle 10: 0 fixed points. Running total 15. |
| 12 | L6 | shuffle=11 fixed=0 total=15 average=1.364 | Shuffle 11: 0 fixed points. Running total 15. |
| 13 | L6 | shuffle=12 fixed=1 total=16 average=1.333 | Shuffle 12: 1 fixed point. Running total 16. |
| 14 | L6 | shuffle=13 fixed=1 total=17 average=1.308 | Shuffle 13: 1 fixed point. Running total 17. |
| 15 | L6 | shuffle=14 fixed=0 total=17 average=1.214 | Shuffle 14: 0 fixed points. Running total 17. |
| 16 | L6 | shuffle=15 fixed=2 total=19 average=1.267 | Shuffle 15: 2 fixed points. Running total 19. |
| 17 | L6 | shuffle=16 fixed=1 total=20 average=1.25 | Shuffle 16: 1 fixed point. Running total 20. |
| 18 | L6 | shuffle=17 fixed=0 total=20 average=1.176 | Shuffle 17: 0 fixed points. Running total 20. |
| 19 | L6 | shuffle=18 fixed=0 total=20 average=1.111 | Shuffle 18: 0 fixed points. Running total 20. |
| 20 | L6 | shuffle=19 fixed=0 total=20 average=1.053 | Shuffle 19: 0 fixed points. Running total 20. |
| 21 | L6 | shuffle=20 fixed=1 total=21 average=1.05 | Shuffle 20: 1 fixed point. Running total 21. |
| 22 | L6 | shuffle=21 fixed=1 total=22 average=1.048 | Shuffle 21: 1 fixed point. Running total 22. |
| 23 | L6 | shuffle=22 fixed=2 total=24 average=1.091 | Shuffle 22: 2 fixed points. Running total 24. |
| 24 | L6 | shuffle=23 fixed=0 total=24 average=1.043 | Shuffle 23: 0 fixed points. Running total 24. |
| 25 | L6 | shuffle=24 fixed=0 total=24 average=1 | Shuffle 24: 0 fixed points. Running total 24. |
| 26 | L9 | total=24 shuffles=24 return=1 | Average = 24 / 24 = 1.000. Linearity of expectation predicts this without enumerating: each of the 4 cards is fixed with probability 1/4, so E[X] = 4 × (1/4) = 1, even though the indicator variables are not independent. |
Linearity of expectation
E[X + Y] = E[X] + E[Y] always, even when X and Y are dependent. That is what makes expectations easy to compute: split a complicated variable into simple parts and add their expectations.
A random permutation of n items has, on average, exactly one fixed point (an item in its original position) for every n ≥ 1, although the individual position events are dependent.
Expected fixed points of a random shuffle
Interactive · edit the inputthis shuffle
count by fixed points
There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them.
1// X = number of cards that land in their own position after a random shuffle2// X = X0 + X1 + ... + X(n-1), where Xi is 1 if card i is fixed, else 0 (indicator variables)3double expected = 0;4for (int[] p : allPermutations(n)) { // each equally likely5int fixed = 0;6for (int i = 0; i < n; i++) if (p[i] == i) fixed++;7expected += fixed;8}9expected /= factorial(n); // = 1.0 for every n
Live state
n
4
shuffles
24
Execution trace (dry run table)Show 26 stepsHide
| Step | Line | State | What happens |
|---|---|---|---|
| 1 | L3 | n=4 shuffles=24 | There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them. |
| 2 | L6 | shuffle=1 fixed=4 total=4 average=4 | Shuffle 1: 4 fixed points. Running total 4. |
| 3 | L6 | shuffle=2 fixed=2 total=6 average=3 | Shuffle 2: 2 fixed points. Running total 6. |
| 4 | L6 | shuffle=3 fixed=2 total=8 average=2.667 | Shuffle 3: 2 fixed points. Running total 8. |
| 5 | L6 | shuffle=4 fixed=1 total=9 average=2.25 | Shuffle 4: 1 fixed point. Running total 9. |
| 6 | L6 | shuffle=5 fixed=1 total=10 average=2 | Shuffle 5: 1 fixed point. Running total 10. |
| 7 | L6 | shuffle=6 fixed=2 total=12 average=2 | Shuffle 6: 2 fixed points. Running total 12. |
| 8 | L6 | shuffle=7 fixed=2 total=14 average=2 | Shuffle 7: 2 fixed points. Running total 14. |
| 9 | L6 | shuffle=8 fixed=0 total=14 average=1.75 | Shuffle 8: 0 fixed points. Running total 14. |
| 10 | L6 | shuffle=9 fixed=1 total=15 average=1.667 | Shuffle 9: 1 fixed point. Running total 15. |
| 11 | L6 | shuffle=10 fixed=0 total=15 average=1.5 | Shuffle 10: 0 fixed points. Running total 15. |
| 12 | L6 | shuffle=11 fixed=0 total=15 average=1.364 | Shuffle 11: 0 fixed points. Running total 15. |
| 13 | L6 | shuffle=12 fixed=1 total=16 average=1.333 | Shuffle 12: 1 fixed point. Running total 16. |
| 14 | L6 | shuffle=13 fixed=1 total=17 average=1.308 | Shuffle 13: 1 fixed point. Running total 17. |
| 15 | L6 | shuffle=14 fixed=0 total=17 average=1.214 | Shuffle 14: 0 fixed points. Running total 17. |
| 16 | L6 | shuffle=15 fixed=2 total=19 average=1.267 | Shuffle 15: 2 fixed points. Running total 19. |
| 17 | L6 | shuffle=16 fixed=1 total=20 average=1.25 | Shuffle 16: 1 fixed point. Running total 20. |
| 18 | L6 | shuffle=17 fixed=0 total=20 average=1.176 | Shuffle 17: 0 fixed points. Running total 20. |
| 19 | L6 | shuffle=18 fixed=0 total=20 average=1.111 | Shuffle 18: 0 fixed points. Running total 20. |
| 20 | L6 | shuffle=19 fixed=0 total=20 average=1.053 | Shuffle 19: 0 fixed points. Running total 20. |
| 21 | L6 | shuffle=20 fixed=1 total=21 average=1.05 | Shuffle 20: 1 fixed point. Running total 21. |
| 22 | L6 | shuffle=21 fixed=1 total=22 average=1.048 | Shuffle 21: 1 fixed point. Running total 22. |
| 23 | L6 | shuffle=22 fixed=2 total=24 average=1.091 | Shuffle 22: 2 fixed points. Running total 24. |
| 24 | L6 | shuffle=23 fixed=0 total=24 average=1.043 | Shuffle 23: 0 fixed points. Running total 24. |
| 25 | L6 | shuffle=24 fixed=0 total=24 average=1 | Shuffle 24: 0 fixed points. Running total 24. |
| 26 | L9 | total=24 shuffles=24 return=1 | Average = 24 / 24 = 1.000. Linearity of expectation predicts this without enumerating: each of the 4 cards is fixed with probability 1/4, so E[X] = 4 × (1/4) = 1, even though the indicator variables are not independent. |
Indicator variables
An indicator variable Xᵢ is 1 if an event happens and 0 otherwise, so E[Xᵢ] = P(event). Write the quantity of interest as a sum of indicators and use linearity. For fixed points, Xᵢ = 1 when item i lands in position i, which has probability 1/n, so the expected total is n · (1/n) = 1.
The same method gives the expected number of comparisons in randomised quick sort, O(n log n), and the expected number of collisions in a hash table with n keys and m buckets, about n(n - 1)/(2m).
1Random rnd = new Random(1);2int n = 10, trials = 100_000; long total = 0;3for (int t = 0; t < trials; t++) {4List<Integer> p = new ArrayList<>();5for (int i = 0; i < n; i++) p.add(i);6Collections.shuffle(p, rnd);7for (int i = 0; i < n; i++) if (p.get(i) == i) total++;8}9double avg = (double) total / trials; // close to 1.0
Expected fixed points of a random shuffle
Interactive · edit the inputthis shuffle
count by fixed points
There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them.
1// X = number of cards that land in their own position after a random shuffle2// X = X0 + X1 + ... + X(n-1), where Xi is 1 if card i is fixed, else 0 (indicator variables)3double expected = 0;4for (int[] p : allPermutations(n)) { // each equally likely5int fixed = 0;6for (int i = 0; i < n; i++) if (p[i] == i) fixed++;7expected += fixed;8}9expected /= factorial(n); // = 1.0 for every n
Live state
n
4
shuffles
24
Execution trace (dry run table)Show 26 stepsHide
| Step | Line | State | What happens |
|---|---|---|---|
| 1 | L3 | n=4 shuffles=24 | There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them. |
| 2 | L6 | shuffle=1 fixed=4 total=4 average=4 | Shuffle 1: 4 fixed points. Running total 4. |
| 3 | L6 | shuffle=2 fixed=2 total=6 average=3 | Shuffle 2: 2 fixed points. Running total 6. |
| 4 | L6 | shuffle=3 fixed=2 total=8 average=2.667 | Shuffle 3: 2 fixed points. Running total 8. |
| 5 | L6 | shuffle=4 fixed=1 total=9 average=2.25 | Shuffle 4: 1 fixed point. Running total 9. |
| 6 | L6 | shuffle=5 fixed=1 total=10 average=2 | Shuffle 5: 1 fixed point. Running total 10. |
| 7 | L6 | shuffle=6 fixed=2 total=12 average=2 | Shuffle 6: 2 fixed points. Running total 12. |
| 8 | L6 | shuffle=7 fixed=2 total=14 average=2 | Shuffle 7: 2 fixed points. Running total 14. |
| 9 | L6 | shuffle=8 fixed=0 total=14 average=1.75 | Shuffle 8: 0 fixed points. Running total 14. |
| 10 | L6 | shuffle=9 fixed=1 total=15 average=1.667 | Shuffle 9: 1 fixed point. Running total 15. |
| 11 | L6 | shuffle=10 fixed=0 total=15 average=1.5 | Shuffle 10: 0 fixed points. Running total 15. |
| 12 | L6 | shuffle=11 fixed=0 total=15 average=1.364 | Shuffle 11: 0 fixed points. Running total 15. |
| 13 | L6 | shuffle=12 fixed=1 total=16 average=1.333 | Shuffle 12: 1 fixed point. Running total 16. |
| 14 | L6 | shuffle=13 fixed=1 total=17 average=1.308 | Shuffle 13: 1 fixed point. Running total 17. |
| 15 | L6 | shuffle=14 fixed=0 total=17 average=1.214 | Shuffle 14: 0 fixed points. Running total 17. |
| 16 | L6 | shuffle=15 fixed=2 total=19 average=1.267 | Shuffle 15: 2 fixed points. Running total 19. |
| 17 | L6 | shuffle=16 fixed=1 total=20 average=1.25 | Shuffle 16: 1 fixed point. Running total 20. |
| 18 | L6 | shuffle=17 fixed=0 total=20 average=1.176 | Shuffle 17: 0 fixed points. Running total 20. |
| 19 | L6 | shuffle=18 fixed=0 total=20 average=1.111 | Shuffle 18: 0 fixed points. Running total 20. |
| 20 | L6 | shuffle=19 fixed=0 total=20 average=1.053 | Shuffle 19: 0 fixed points. Running total 20. |
| 21 | L6 | shuffle=20 fixed=1 total=21 average=1.05 | Shuffle 20: 1 fixed point. Running total 21. |
| 22 | L6 | shuffle=21 fixed=1 total=22 average=1.048 | Shuffle 21: 1 fixed point. Running total 22. |
| 23 | L6 | shuffle=22 fixed=2 total=24 average=1.091 | Shuffle 22: 2 fixed points. Running total 24. |
| 24 | L6 | shuffle=23 fixed=0 total=24 average=1.043 | Shuffle 23: 0 fixed points. Running total 24. |
| 25 | L6 | shuffle=24 fixed=0 total=24 average=1 | Shuffle 24: 0 fixed points. Running total 24. |
| 26 | L9 | total=24 shuffles=24 return=1 | Average = 24 / 24 = 1.000. Linearity of expectation predicts this without enumerating: each of the 4 cards is fixed with probability 1/4, so E[X] = 4 × (1/4) = 1, even though the indicator variables are not independent. |
Practice ladder
Now try it yourself
Easy
- What is the probability that two dice show the same number?
- What is the expected value of a roll of a fair die?
Medium
- Compute the expected number of heads in 10 fair coin flips using indicators
- Given a sum of 8 on two dice, what is the chance that one die shows 2?
Advanced
- Show that the expected number of collisions for n keys in m buckets under a uniform hash is n(n - 1) / (2m)
Key takeaways
- Define the sample space, then count favourable outcomes.
- Independence multiplies probabilities; conditioning restricts the sample space.
- Linearity of expectation plus indicator variables solves many analysis problems without independence.