Probability Intro
Probability theory is the mathematics of uncertainty. It tells you how likely a spam filter is to be wrong, how long a queue will get, how many servers a service needs, why a casino always wins in the long run, and how a language model picks its next word. Statistics runs the other way, from data back to the process that produced it; probability is the forward model that statistics stands on.
The reference book
The topic order of this course follows the open textbook Introduction to Probability by Charles M. Grinstead and J. Laurie Snell (second revised edition, American Mathematical Society). A free version of the book, prepared by Peter G. Doyle, is released under the GNU Free Documentation License, and its PDF is available from Dartmouth College. Thanks to the authors for making it open. The book is known for teaching through computer simulation, which suits a course built on runnable Python.
The book is used as a map of topics only. The explanations, examples, numbers and exercises here are written from scratch.
What is in the course
| Part | Topics |
|---|---|
| 1. Discrete distributions | simulation, sample spaces, distribution functions, rules of probability |
| 2. Continuous densities | simulating continuous experiments, densities, cumulative distribution functions |
| 3. Combinatorics | permutations, combinations, Bernoulli trials, card shuffling |
| 4. Conditional probability | conditioning, independence, Bayes' theorem, paradoxes |
| 5. Important distributions | geometric, Poisson, hypergeometric, exponential, normal, gamma and beta |
| 6. Expected value and variance | expectation, linearity, indicators, variance, covariance |
| 7. Sums and joint distributions | convolution, joint and marginal distributions, conditional expectation |
| 8. Law of large numbers | Markov's and Chebyshev's inequalities, the weak law |
| 9. Central limit theorem | the normal approximation and how to use it |
| 10. Generating functions | probability and moment generating functions, branching processes |
| 11. Markov chains | absorbing and ergodic chains, stationary distributions, first passage times |
| 12. Random walks | returns to the origin, gambler's ruin, the arc sine law |
| Probability in computing | Monte Carlo, randomized algorithms, naive Bayes, MDPs, LLM sampling |
Two ways to answer every question
Most questions in this course are answered twice: exactly, with a formula, and by simulation, by running the random experiment many times and counting. When the two agree you understand the problem; when they disagree, one of them has a bug. Here is a first example: the probability of at least one six in four rolls of a die.
import random
from fractions import Fraction
exact = 1 - Fraction(5, 6) ** 4
print("exact:", exact, "=", round(float(exact), 4))
rng = random.Random(1)
trials = 100_000
hits = sum(any(rng.randint(1, 6) == 6 for _ in range(4)) for _ in range(trials))
print("simulated:", hits / trials)exact: 671/1296 = 0.5177
simulated: 0.51511
The exact answer uses the complement: "at least one six" fails only when all four rolls avoid six, which happens with probability . The simulation gets within a few thousandths. How close a simulation gets, and why, is the subject of the law of large numbers and the central limit theorem.
How this course relates to others
- Discrete Mathematics has a short probability chapter (probability spaces to deviation). This course goes much further: continuous densities, limit theorems, generating functions, Markov chains.
- Statistics uses these results to estimate and test; links point there instead of repeating it.
- Calculus is needed for densities: probabilities become integrals.
- Linear Algebra powers Markov chains: a chain is a matrix, and its long-run behaviour is an eigenvector, as in PageRank.
- The Machine Learning track puts these ideas to work in classifiers and their evaluation, for example Gaussian mixtures and EM and calibrated probabilities.
How lessons work
Formulas are typeset with LaTeX. Code examples run in your browser with ▶ Run; they use Python's random module or NumPy with a fixed seed, so the "random" output is the same every time and matches the text. Change the seed to see another run. Every lesson ends with exercises (answer and worked solution) and a short quiz.
In software
Probability is everywhere in code: randomized load balancing, A/B tests, rate limiters with exponential backoff, Bloom filters, recommendation and ranking, simulation of queues and networks, reinforcement learning, and the sampling step of every generative model. Programmers who can estimate a probability on the back of an envelope, and then check it with a ten-line simulation, make better design decisions.
Exercises
Exercise 1: Complement rule
What is the probability of at least one head in 5 tosses of a fair coin?
Answer
.
Solution
All tails has probability , so at least one head has probability .
Exercise 2: Double sixes
A gambler bets on at least one double six in 24 rolls of two dice. Is the probability above or below 1/2?
Answer
Slightly below: about 0.491.
Solution
One roll of two dice gives a double six with probability . The complement over 24 rolls: . This problem, posed to Pascal in the 17th century, helped start probability theory.
Exercise 3: Simulate it (code)
Check Exercise 2 by simulating 50,000 sets of 24 rolls.
Answer
The simulated frequency is close to 0.491.
Solution
import random
rng = random.Random(7)
trials = 50_000
hits = sum(any(rng.randint(1, 6) == 6 and rng.randint(1, 6) == 6 for _ in range(24))
for _ in range(trials))
print(hits / trials, round(1 - (35 / 36) ** 24, 4))0.4888 0.4914
Note the and: the second die is only rolled when the first shows six, which does not change the probability of a double six.
Test yourself
Why do the code examples in this course use a fixed seed?
- To make the results exactly reproducible
- To make the simulation more accurate
- Because Python requires it
- To make the numbers truly random
The probability of at least one success in independent tries, each with probability , is…