Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsProbability and Statistics

Probability basics

P1.D140 min●○○ BeginnerReviewed Dec 1, 2026

Why this matters for machine learning

Probability is the language of uncertainty, and nearly every model in machine learning speaks it: a model does not say what will happen, it says how likely each thing is, and training data are samples from a world we cannot list. Everything later in probability and statistics stands on the rules of this lesson.

  • A classifier’s output must be a valid probability assignment: nonnegative numbers that add to 1. Softmax is the function that turns raw scores into exactly that. (P3.A8: Softmax regression, coming soon)
  • Mini-batch training draws a random sample of examples at every step, so the gradient it uses is the outcome of a random experiment. (P1.F4: Stochastic and mini-batch gradient descent, coming soon)
  • Dropout switches random neurons off at each training step, so each step is a random experiment. (P4.17: Regularization for neural nets, coming soon)
  • Weight initialization draws random starting numbers, so two training runs are two outcomes of the same random experiment. (P4.12: Weight initialization, coming soon)
  • A language model assigns a probability to each possible next token, and the simplest one estimates those probabilities as relative frequencies of counted word pairs. (P6.2: N-gram language models, coming soon)
  • Monte Carlo methods estimate hard quantities by repeating a random experiment many times and looking at relative frequencies, the idea of the simulator below. (P1.D9: Sampling and Monte Carlo)

Before this: Sets, notation and reading math

After this lesson you can

  • Write the sample space Ω\Omega of an experiment, write an event as a subset of it, and combine events with “not”, “or” and “and” (AcA^c, A∪BA\cup B, A∩BA\cap B)
  • State the three rules (axioms) of probability and use them to derive P(Ac)=1−P(A)\mathbb{P}(A^c)=1-\mathbb{P}(A) and P(A∪B)=P(A)+P(B)−P(A∩B)\mathbb{P}(A\cup B)=\mathbb{P}(A)+\mathbb{P}(B)-\mathbb{P}(A\cap B), and to say why a list of numbers cannot be a probability assignment
  • Compute P(A)=∣A∣/∣Ω∣\mathbb{P}(A)=|A|/|\Omega| for equally likely outcomes, say when it does not apply (the sum of two dice), and count outcomes with the multiplication rule, permutations and combinations
  • Find “at least one” probabilities with the complement, including the birthday problem (0.5070.507 for 23 people), and explain what a relative frequency does and does not tell you about a probability

A trained classifier looks at a photo and reports cat 0.700.70, dog 0.200.20, bird 0.100.10. Could it report cat 0.700.70, dog 0.400.40, bird 0.100.10? Something feels wrong with that second answer, but what, exactly? And what does “0.700.70” mean at all: that 70 out of 100 similar photos are cats, or that the model is 70 percent sure?

Probability answers both questions with a small set of rules. This lesson builds them from coins and dice, because there you can list every case and check every number.

Experiments, outcomes and the sample space

An experiment is anything whose result is not known in advance: flipping a coin, rolling a die, drawing a card from a shuffled deck, picking a random mini-batch of training examples. Each time you run it, exactly one outcome happens. The set of all possible outcomes is the sample space, written Ω\Omega (capital omega). It is a set in the sense of P1.A4, and ∣Ω∣|\Omega| is its size.

  • One coin flip: Ω={H,T}\Omega=\{H,T\}, so ∣Ω∣=2|\Omega|=2.
  • One die: Ω={1,2,3,4,5,6}\Omega=\{1,2,3,4,5,6\}, so ∣Ω∣=6|\Omega|=6.
  • Two coin flips, in order: Ω={HH,HT,TH,TT}\Omega=\{HH,HT,TH,TT\}, so ∣Ω∣=4|\Omega|=4. The pair HTHT (heads first) and THTH (tails first) are different outcomes.
  • Two dice, one red and one blue: Ω\Omega has the 3636 pairs (r,b)(r,b) with rr and bb from 11 to 66. The pair (2,5)(2,5) is a different outcome from (5,2)(5,2), even if you cannot tell the dice apart by eye.

The outcomes of a sample space must not overlap, and together they must cover everything that can happen. Which sample space you use is a modelling choice. For two dice you could instead use the sums, Ω={2,3,…,12}\Omega=\{2,3,\dots,12\}. That is a valid sample space, but it throws away information, and, as you will see, its outcomes are not equally likely.

Events are sets of outcomes

An event is a subset of Ω\Omega: a collection of outcomes you care about. The event happens when the outcome that occurred is one of its members.

On a die, “an even number” is the event A={2,4,6}A=\{2,4,6\}. If the die shows 44, then AA happened, because 4∈A4\in A. If it shows 55, then AA did not happen. For two dice, “the sum is 7” is the event made of the 66 pairs (1,6)(1,6), (2,5)(2,5), (3,4)(3,4), (4,3)(4,3), (5,2)(5,2) and (6,1)(6,1).

Two extremes: the empty event ∅\varnothing never happens, and the sure event Ω\Omega happens whatever the outcome. An event with a single outcome, such as {3}\{3\}, is called an elementary event. If A⊆BA\subseteq B, then BB happens whenever AA does: “the die shows 6” implies “the die shows an even number”.

Not, or, and: the algebra of events

Because events are sets, the set operations of P1.A4 give the everyday words. Take a die, A={2,4,6}A=\{2,4,6\} (even) and B={4,5,6}B=\{4,5,6\} (at least 4).

  • not AA is the complement Ac=Ω∖A={1,3,5}A^c=\Omega\setminus A=\{1,3,5\}. Some books write Aˉ\bar A or A′A'.
  • AA or BB is the union A∪B={2,4,5,6}A\cup B=\{2,4,5,6\}. In maths, “or” is inclusive: it happens when at least one of the two happens, and also when both do.
  • AA and BB is the intersection A∩B={4,6}A\cap B=\{4,6\}.
  • AA but not BB is A∖B={2}A\setminus B=\{2\}.

Two events are mutually exclusive (or disjoint) when A∩B=∅A\cap B=\varnothing: they cannot both happen in one run. “Even” {2,4,6}\{2,4,6\} and “at most 2” {1,2}\{1,2\} overlap in {2}\{2\}, but “even” and “odd” do not. Events that are mutually exclusive and together cover Ω\Omega form a partition. The six single faces form one, and so do AA and AcA^c. The class labels of a classifier form one too: the photo is exactly one of cat, dog or bird.

Events on the 36 outcomes of two dice

Tapping a cell changes:

Rows: first die. Columns: second die. Each cell shows “first,second” and the letters of the events it belongs to. Highlighted cells are in “A or B (or both)”.

|A| = 11, so P(A) = 11/36 = 0.306 |B| = 6, so P(B) = 6/36 = 0.167 |A ∩ B| = 5, so P(A and B) = 5/36 = 0.139 |A ∪ B| = 12, so P(A or B) = 12/36 = 0.333 Check: P(A) + P(B) − P(A and B) = 11 / 36 + 6 / 36 − 5 / 36 = 12 / 36, and counting A ∪ B directly gives 12 / 36. Highlighted, “A or B (or both)” (A ∪ B): 12 outcomes, P = 12/36 = 0.333.
5 outcomes are in both A and B, so adding P(A) and P(B) counts them twice. Subtract the overlap once.
The 36 outcomes of two dice as a grid (rows: the first die, columns: the second die). Choose two events A and B, or tap cells to change A or B yourself, and see not A, A or B, A and B and A but not B highlighted. The readout counts the cells and gives each probability as a count out of 36 (all 36 outcomes are equally likely for fair dice). It also checks that P(A) + P(B) − P(A and B) equals P(A or B), and tells you when A and B are mutually exclusive.

Things to try with the event grid:

  1. Start with A = “at least one six” and B = “sum is at least 10”. A has 1111 cells and B has 66, but A or B has only 1212, not 1717: the five cells with a six and a large sum were counted in both.
  2. Switch to not A. The highlighted cells number 2525, which is 36−1136-11.
  3. Choose A = “sum is 7” and B = “doubles”. No cell is in both, so the events are mutually exclusive and the overlap is 00.
  4. Choose A = “sum is even” and B = “first die is even”. Each has 1818 cells, they share 99, and A or B has 2727. Then tap cells to change A and watch the counts follow.

The rules of probability

A probability gives every event AA a number P(A)\mathbb{P}(A), read “the probability of AA”, which measures how likely AA is. ML papers often write p(x)p(x) for the probability of an outcome xx, as in P1.A4. For the number to be a sensible measure, it must follow three rules. They are called the axioms of probability, and were set out in this form by the Russian mathematician Andrey Kolmogorov in 1933.

  1. Not negative. P(A)≥0\mathbb{P}(A)\ge0 for every event AA.
  2. Something happens. P(Ω)=1\mathbb{P}(\Omega)=1.
  3. Additivity. If AA and BB are mutually exclusive, then P(A∪B)=P(A)+P(B)\mathbb{P}(A\cup B)=\mathbb{P}(A)+\mathbb{P}(B).

Rule 3 holds for any number of mutually exclusive events, and the full version of the axioms asks for it even for infinitely many in a row (see the second box below). Everything else in probability is built on these three rules. Here are the first consequences, each a short step from the rules.

  • Complement rule. P(Ac)=1−P(A)\mathbb{P}(A^c)=1-\mathbb{P}(A). Reason: AA and AcA^c are mutually exclusive and their union is Ω\Omega, so P(A)+P(Ac)=P(Ω)=1\mathbb{P}(A)+\mathbb{P}(A^c)=\mathbb{P}(\Omega)=1.
  • The impossible event. P(∅)=0\mathbb{P}(\varnothing)=0 (it is the complement of Ω\Omega).
  • Probabilities lie between 0 and 1. 0≤P(A)≤10\le\mathbb{P}(A)\le1: rule 1 gives the lower bound, and the complement rule with P(Ac)≥0\mathbb{P}(A^c)\ge0 gives the upper bound.
  • Bigger events are at least as likely. If A⊆BA\subseteq B, then P(A)≤P(B)\mathbb{P}(A)\le\mathbb{P}(B).
  • Overlapping events. For any two events, P(A∪B)=P(A)+P(B)−P(A∩B)\mathbb{P}(A\cup B)=\mathbb{P}(A)+\mathbb{P}(B)-\mathbb{P}(A\cap B). Adding P(A)\mathbb{P}(A) and P(B)\mathbb{P}(B) counts the overlap twice, so subtract it once. This is the probability version of ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A\cup B|=|A|+|B|-|A\cap B| from P1.A4. When AA and BB are mutually exclusive the overlap is empty and rule 3 comes back.
  • Union bound. P(A∪B)≤P(A)+P(B)\mathbb{P}(A\cup B)\le\mathbb{P}(A)+\mathbb{P}(B), because the subtracted term is not negative.
  • Outcomes add up. For a finite Ω\Omega, the probability of an event is the sum of the probabilities of its outcomes, and those add to 11 over all of Ω\Omega.

Check the overlap formula on a die with A={2,4,6}A=\{2,4,6\} and B={4,5,6}B=\{4,5,6\}. Then P(A)=12\mathbb{P}(A)=\frac12, P(B)=12\mathbb{P}(B)=\frac12 and P(A∩B)=P({4,6})=26\mathbb{P}(A\cap B)=\mathbb{P}(\{4,6\})=\frac26, so P(A∪B)=12+12−13=23\mathbb{P}(A\cup B)=\frac12+\frac12-\frac13=\frac23. Counting directly, A∪B={2,4,5,6}A\cup B=\{2,4,5,6\} has 44 of the 66 faces, which is 46=23\frac46=\frac23 too.

Breaking the rules. Suppose a model says cat 0.50.5, dog 0.40.4, bird 0.30.3. The three classes form a partition of Ω\Omega, so rule 3 says their probabilities must add to P(Ω)=1\mathbb{P}(\Omega)=1. They add to 1.21.2, so these numbers cannot be probabilities. One fix is to divide each by the sum: (0.5,0.4,0.3)/1.2≈(0.417,0.333,0.25)(0.5,0.4,0.3)/1.2\approx(0.417,0.333,0.25), which adds to 11. Softmax (P3.A8) takes the exponential of each raw score and then divides by the sum: scores (2,1,0.1)(2,1,0.1) become (0.659,0.242,0.099)(0.659,0.242,0.099), which add to 11. The earlier answer, 0.70+0.40+0.10=1.20.70+0.40+0.10=1.2, fails in the same way.

Go deeper Proving the consequences from the three rules

Empty event. The sets Ω\Omega and ∅\varnothing are mutually exclusive and Ω∪∅=Ω\Omega\cup\varnothing=\Omega, so P(Ω)=P(Ω)+P(∅)\mathbb{P}(\Omega)=\mathbb{P}(\Omega)+\mathbb{P}(\varnothing). Subtract P(Ω)\mathbb{P}(\Omega) from both sides: P(∅)=0\mathbb{P}(\varnothing)=0.

Monotonicity. If A⊆BA\subseteq B, write B=A∪(B∖A)B=A\cup(B\setminus A). These two pieces are mutually exclusive, so P(B)=P(A)+P(B∖A)≥P(A)\mathbb{P}(B)=\mathbb{P}(A)+\mathbb{P}(B\setminus A)\ge\mathbb{P}(A), using rule 1 for the second piece.

Overlap formula. Split A∪BA\cup B into the mutually exclusive pieces AA and B∖AB\setminus A, so P(A∪B)=P(A)+P(B∖A)\mathbb{P}(A\cup B)=\mathbb{P}(A)+\mathbb{P}(B\setminus A). Split BB into the mutually exclusive pieces B∩AB\cap A and B∖AB\setminus A, so P(B)=P(A∩B)+P(B∖A)\mathbb{P}(B)=\mathbb{P}(A\cap B)+\mathbb{P}(B\setminus A). Subtract the second line from the first: P(A∪B)−P(B)=P(A)−P(A∩B)\mathbb{P}(A\cup B)-\mathbb{P}(B)=\mathbb{P}(A)-\mathbb{P}(A\cap B), which is the formula.

Three events. The same idea gives P(A∪B∪C)=P(A)+P(B)+P(C)−P(A∩B)−P(A∩C)−P(B∩C)+P(A∩B∩C)\mathbb{P}(A\cup B\cup C)=\mathbb{P}(A)+\mathbb{P}(B)+\mathbb{P}(C)-\mathbb{P}(A\cap B)-\mathbb{P}(A\cap C)-\mathbb{P}(B\cap C)+\mathbb{P}(A\cap B\cap C). On the two-dice grid, let AA be “at least one six”, BB “sum at least 10” and CC “doubles”. Their sizes are 1111, 66, 66, the pairwise overlaps have 55, 11 and 22 cells, the triple overlap has 11 cell, and 11+6+6−5−1−2+1=1611+6+6-5-1-2+1=16 cells, which matches a direct count of A∪B∪CA\cup B\cup C. Adding, then removing the double counts, then adding back what was removed too often, is called inclusion–exclusion.

De Morgan’s laws. “Not (A or B)” is “not A and not B”: (A∪B)c=Ac∩Bc(A\cup B)^c=A^c\cap B^c. Likewise (A∩B)c=Ac∪Bc(A\cap B)^c=A^c\cup B^c. On the same grid, (A∪B)c(A\cup B)^c has 36−12=2436-12=24 cells, and so does Ac∩BcA^c\cap B^c. These laws turn “none of them” into “not this and not that”, which is how the complement trick below works.

Go deeper Infinite sample spaces and probability zero

Suppose you pick a real number “at random” from the interval [0,1][0,1]. Here Ω\Omega is infinite and cannot be listed. A natural model gives an interval a probability equal to its length, so P([0.2,0.5])=0.3\mathbb{P}([0.2,0.5])=0.3. But then the single outcome {0.5}\{0.5\} has length 00, so P({0.5})=0\mathbb{P}(\{0.5\})=0, even though 0.50.5 is a possible outcome. So in an infinite sample space, probability 00 does not mean impossible, and probability 11 does not mean certain. In a finite sample space with a sensible model, an outcome you give probability 00 is one you have decided to rule out.

For infinite sample spaces, rule 3 is strengthened to countable additivity: it must hold for an infinite list of mutually exclusive events A1,A2,…A_1,A_2,\dots, with P(A1∪A2∪⋯ )=P(A1)+P(A2)+⋯\mathbb{P}(A_1\cup A_2\cup\cdots)=\mathbb{P}(A_1)+\mathbb{P}(A_2)+\cdots. Mathematicians also show that on a continuum such as the interval [0,1][0,1] you cannot consistently give every subset a probability, so the full definition lets P\mathbb{P} act on a large, well-behaved collection of subsets called the events. You do not need these details for finite problems, and P1.D2 shows how continuous quantities are handled in practice with densities.

Equally likely outcomes: count and divide

Suppose Ω\Omega is finite and every outcome is equally likely: a fair coin, a fair die, a well-shuffled deck. There are ∣Ω∣|\Omega| outcomes whose probabilities are equal and add to 11, so each has probability 1/∣Ω∣1/|\Omega|. An event is a sum of its outcomes, so

P(A)=∣A∣∣Ω∣.\mathbb{P}(A)=\frac{|A|}{|\Omega|}.

In words: the number of outcomes in the event, divided by the number of all outcomes.

For the even die, ∣A∣=3|A|=3 and ∣Ω∣=6|\Omega|=6, so P(A)=36=12\mathbb{P}(A)=\frac36=\frac12. For two dice, “the sum is 7” has 66 outcomes out of 3636: P=636=16\mathbb{P}=\frac6{36}=\frac16. Two more: “doubles” {(1,1),…,(6,6)}\{(1,1),\dots,(6,6)\} has 66 outcomes, so 16\frac16, and “the two dice differ” is the complement of doubles, so 1−16=561-\frac16=\frac56.

The hard part is the word “equally likely”. It is an assumption about the experiment (a fair die, a fair shuffle), not something maths provides. Here is where it fails.

  • The sum of two dice. The 1111 sums 2,…,122,\dots,12 are not equally likely. Out of the 3636 equally likely pairs, the sums come up 1,2,3,4,5,6,5,4,3,2,11,2,3,4,5,6,5,4,3,2,1 times. So P(sum=7)=636\mathbb{P}(\text{sum}=7)=\frac6{36} and P(sum=2)=136\mathbb{P}(\text{sum}=2)=\frac1{36}, not 111\frac1{11} each. The trick is to count in the sample space of pairs, where outcomes are equally likely.
  • The number of heads in two flips. The values 0,1,20,1,2 are not equally likely. Of the four ordered outcomes, one has no heads, two have one head and one has two heads, so P(one head)=24=12\mathbb{P}(\text{one head})=\frac24=\frac12, not 13\frac13.
  • Unfair devices. A coin that lands heads with probability 0.30.3 has two outcomes with probabilities 0.30.3 and 0.70.7. The rule for a general finite Ω\Omega is: give each outcome a probability that is at least 00, make them add to 11, and add up the outcomes of an event. The three rules guarantee that this works.
  • “It either happens or it does not.” A lottery ticket either wins or loses, but the two outcomes are far from equally likely. Having two outcomes says nothing about their probabilities.

So the classical formula applies when you can argue from symmetry that the outcomes are equally likely. In other cases the probabilities have to come from a model or from data.

Intuition

Picture one unit of “sand” spread over the sample space. Every outcome gets a heap, the heaps are never negative, and the whole pile weighs 11. The probability of an event is the weight of the sand on its outcomes. For a fair die each face holds 16\frac16 of a unit.

The rules then become pictures. The union of two events that do not touch weighs the sum of the two. When two events overlap, adding their weights weighs the shared sand twice, so you remove it once. The complement is the sand outside the event, so its weight is 11 minus the weight inside. The picture breaks down for infinite sample spaces, where the sand has no outcome-by-outcome heaps (see the box above).

Math

For a finite sample space with outcome probabilities p(ω)≥0p(\omega)\ge0 adding to 11:

P(A)=∑ω∈Ap(ω),P(Ω)=1,P(Ac)=1−P(A).\begin{aligned} \mathbb{P}(A)&=\sum_{\omega\in A}p(\omega), \\ \mathbb{P}(\Omega)&=1, \\ \mathbb{P}(A^c)&=1-\mathbb{P}(A). \end{aligned}P(A∪B)=P(A)+P(B)−P(A∩B)≤P(A)+P(B).\begin{gathered} \mathbb{P}(A\cup B) \\ \quad=\mathbb{P}(A)+\mathbb{P}(B)-\mathbb{P}(A\cap B) \\ \quad\le\mathbb{P}(A)+\mathbb{P}(B). \end{gathered}

With equally likely outcomes p(ω)=1/∣Ω∣p(\omega)=1/|\Omega| and P(A)=∣A∣/∣Ω∣\mathbb{P}(A)=|A|/|\Omega|.

Code

Python can list a sample space and count, which is exactly the classical formula. A Fraction keeps the answers exact.

from itertools import product
from fractions import Fraction

omega = list(product(range(1, 7), repeat=2))      # 36 ordered pairs
event = [w for w in omega if sum(w) == 7]         # "the sum is 7"
print(len(omega), len(event), Fraction(len(event), len(omega)))   # 36 6 1/6

Do frequencies match probabilities? A simulator

The rules say a fair coin lands heads with probability 12\frac12. A real run of flips will not give exactly half heads. What do the numbers have to do with each other? Count how often an event happens in nn runs, say kk times. The relative frequency is k/nk/n. For 5454 heads in 100100 flips it is 0.540.54.

The simulator below uses your browser’s pseudo-random number generator (a new random run each time you press Reset). The bar chart shows the relative frequency of each outcome beside its true probability. The line shows the running relative frequency of one event you choose, as nn grows. The horizontal axis is logarithmic, so the first few flips are not squeezed into a sliver.

Coin and dice simulator

No flips yet. Press one of the flip buttons.

Relative frequency of every outcome

00.20.40.6HeadsTailsoutcome

observed relative frequency, darker for the outcomes inside the event true probability

Running relative frequency of “heads”

00.51110number of flips n (log scale)

running relative frequency true probability 0.5

number of flips n = 0 count of “heads” = 0 relative frequency = — true probability = 0.5 count minus n × P = —
Counts for every outcome
outcomecountfreq.true P
Heads0—0.5
Tails0—0.5
Flip a coin or roll dice many times. Left to right: choose the experiment (for the biased coin, set the probability p of heads), pick the event whose running frequency you want to follow, then use the roll buttons. The bars show the observed relative frequency of every outcome (blue) next to its true probability (pink mark). The line shows the running relative frequency of the chosen event, and the dashed line is its true probability. Changing the experiment or p starts a new run. The run stops at 100,000 rolls. The same numbers are listed under “Counts for every outcome”.

Things to try with the simulator:

  1. Keep the fair coin and press Flip 10, then Reset, a few times. The frequency of heads swings widely: 0.30.3 and 0.70.7 are not unusual. Exactly 55 heads in 1010 flips has probability only 252/1024≈0.246252/1024\approx0.246, about one run in four, and 33 or fewer, or 77 or more, heads happens in 352/1024≈0.34352/1024\approx0.34 of runs.
  2. Now press Flip 1000 on a fresh run, several times. The frequency of heads stays much closer to 0.50.5. The chance that it lands outside the range 0.40.4 to 0.60.6 is under one in a billion at 10001000 flips, compared with 0.3440.344 at 1010 flips.
  3. Choose Two dice, sum and press Roll 1000. The bars form a triangle with its peak at 77, like the counts 1,2,3,4,5,6,5,4,3,2,11,2,3,4,5,6,5,4,3,2,1 above. Follow the event “sum is 2” and press “Zoom in on the true value”: with n=100n=100 you expect only about 2.82.8 cases, so the line is jumpy.
  4. Choose Biased coin and set p=0.3p=0.3. The frequency of heads settles near 0.30.3, not 0.50.5.
  5. Watch “count minus n×Pn\times P” in the readout. For a fair coin its typical size grows with nn (about 5050 at 10,00010{,}000 flips), while the gap in frequency shrinks.

What you saw has a name: the law of large numbers (P1.D8). Informally, as nn grows, the relative frequency of an event gets close to its probability, with the chance of a big gap shrinking. It says “close with high probability”, not “equal”, and it says nothing sure about a short run. For a fair coin, the chance that the frequency of heads is within 0.10.1 of 0.50.5 is 0.6560.656 for 1010 flips and 0.9650.965 for 100100 flips (each is a count of sequences, 672/1024672/1024 and a sum of (100k)\binom{100}{k} terms over 21002^{100}). Within 0.050.05: 0.7290.729 for 100100 flips and 0.99860.9986 for 10001000 flips. P1.D8 proves the law and explains these shrinking gaps, and P1.D9 uses them on purpose to estimate quantities by simulation.

Counts and frequencies behave differently. The typical gap between the number of heads and n/2n/2 is about 55 for 100100 flips and about 5050 for 10,00010{,}000 flips (it grows like n\sqrt n; P1.D4 explains why). Divided by nn, those gaps are 0.050.05 and 0.0050.005. So the frequency gap shrinks while the count gap grows.

The gambler’s fallacy. After six heads in a row, is tails “due”? For a fair coin whose flips do not affect each other (that is independence, P1.D5), the next flip is still 12\frac12 heads. The law of large numbers does not pay the surplus back, it dilutes it. Say that after 100100 flips you have 5555 heads, a surplus of 55. The next 9,9009{,}900 flips have an expected 4,9504{,}950 heads, so the expected frequency at 10,00010{,}000 flips is 5005/10000=0.50055005/10000=0.5005. The surplus of 55 is still there, but it no longer matters. Random sequences are also streakier than most people expect: a run of 66 or more equal flips appears in about 81%81\% of sequences of 100100 fair flips.

What does a probability mean?

There are two common answers, and both obey the same three rules.

  • Long-run frequency. P(A)\mathbb{P}(A) is the value that the relative frequency of AA settles near when the experiment is repeated many times. This fits coins, dice and cards, and it is how you estimate probabilities from data: count and divide.
  • Degree of belief. P(A)\mathbb{P}(A) measures how strongly you believe AA, given what you know. This also fits one-off events that cannot be repeated, such as “this experiment’s new model beats the baseline”, and beliefs are updated when evidence arrives (P1.D6).

Probabilities from a model, such as a softmax output of 0.70.7 for “cat”, can be read either way. Whether a model’s numbers match how often it is right is a separate question, which has to be checked on data.

Counting outcomes

The classical formula needs ∣A∣|A| and ∣Ω∣|\Omega|, and listing outcomes stops being practical quickly: a five-card poker hand has 2,598,9602{,}598{,}960 possibilities. Counting rules do the work without a list.

Multiplication rule. If a process has steps with n1n_1, n2n_2, …\dots, nkn_k choices, and the number of choices at each step does not depend on the earlier choices, there are n1n2⋯nkn_1n_2\cdots n_k ways in total. A four-digit PIN has 10⋅10⋅10⋅10=10,00010\cdot10\cdot10\cdot10=10{,}000 possibilities. A password of 88 lowercase letters has 268=208,827,064,57626^8=208{,}827{,}064{,}576, about 2.1×10112.1\times10^{11}. A string of nn coin flips has 2n2^n outcomes.

Two questions then pick the formula for choosing kk items from nn:

  • Order matters, repeats allowed (with replacement). There are nkn^k choices, since each of the kk steps has nn options.
  • Order matters, no repeats (without replacement). The first step has nn options, the next n−1n-1, and so on:
n(n−1)⋯(n−k+1)=n!(n−k)!.n(n-1)\cdots(n-k+1)=\frac{n!}{(n-k)!}.

Here n!=n(n−1)⋯2⋅1n!=n(n-1)\cdots 2\cdot 1 is “nn factorial”, with 0!=10!=1. With k=nk=n this counts the permutations, the orderings of nn things: 5!=1205!=120. For the top three of 55 runners in a race, 5!2!=60\frac{5!}{2!}=60.

  • Order does not matter, no repeats. Each unordered set of kk items appears k!k! times among the ordered lists, so divide by k!k!. The result is the number of combinations, “nn choose kk”:
(nk)=n!k! (n−k)!.\binom{n}{k}=\frac{n!}{k!\,(n-k)!}.

Choosing 22 of 55 people gives (52)=5⋅42=10\binom52=\frac{5\cdot4}{2}=10 pairs, half of the 2020 ordered pairs.

  • Order does not matter, repeats allowed. This case exists too (see the box below). Its outcomes are usually not equally likely when they come from drawing one item at a time, so it is used less in probability.

Example: a PIN with all digits different. The first digit has 1010 options, the next 99, then 88, then 77: 10⋅9⋅8⋅7=5040{10\cdot9\cdot8\cdot7}=5040 of the 10,00010{,}000 PINs, so a random PIN has four different digits with probability 0.5040.504.

Example: cards. A fair shuffle makes every five-card hand equally likely, so ∣Ω∣=(525)=2,598,960|\Omega|=\binom{52}{5}=2{,}598{,}960. The hands with no ace are chosen from the 4848 other cards: (485)=1,712,304\binom{48}{5}=1{,}712{,}304. So P(no ace)=1,712,3042,598,960≈0.659\mathbb{P}(\text{no ace})=\frac{1{,}712{,}304}{2{,}598{,}960}\approx0.659. The probability of at least one ace is 1−0.659≈0.3411-0.659\approx0.341.

The golden rule is to count the favourable outcomes and all outcomes in the same way. If you count hands as unordered sets in the numerator, do the same in the denominator.

Go deeper Choosing with repeats allowed, order ignored

How many ways are there to choose kk items from nn types, with repeats allowed and order ignored? The answer is (n+k−1k)\binom{n+k-1}{k}. For two dice, n=6n=6 and k=2k=2: (72)=21\binom{7}{2}=21 unordered results (“a 3 and a 5”, “two 4s”). But these 2121 results are not equally likely. A double such as {4,4}\{4,4\} comes from one of the 3636 ordered pairs, so its probability is 136\frac1{36}, while {3,5}\{3,5\} comes from two ordered pairs, (3,5)(3,5) and (5,3)(5,3), so its probability is 236\frac2{36}. This is another reason to compute with the ordered sample space, where the outcomes are equally likely.

“At least one”: use the complement

“At least one” is the opposite of “none”, so its probability is 1−P(none)1-\mathbb{P}(\text{none}), by the complement rule. “None” is usually much easier to count.

Four rolls of a die, at least one six. There are 64=12966^4=1296 equally likely sequences of four rolls (this assumes the rolls do not affect each other, which is independence, P1.D5; the same assumption is used in the three-roll quiz question and in exercises 9 and 10). The sequences without a six use only the faces 11 to 55, so there are 54=6255^4=625 of them. So

P(at least one six)=1−6251296=6711296≈0.518.\begin{aligned} \mathbb{P}(\text{at least one six})&=1-\frac{625}{1296}\\ &=\frac{671}{1296}\approx0.518. \end{aligned}

Counting the cases “exactly one six”, “exactly two sixes” and so on would be much longer. The trap is to add: 4×16=46≈0.6674\times\frac16=\frac46\approx0.667 is too big, because the events “a six on roll 11”, “a six on roll 22” and the rest overlap. The union bound says only that the true value is at most 0.6670.667.

Two dice, at least one six. The complement is “no six on either die”, 5⋅5=255\cdot5=25 of the 3636 pairs, so P=1−2536=1136\mathbb{P}=1-\frac{25}{36}=\frac{11}{36}. This is the event A of the grid above.

The birthday problem. In a group of nn people, what is the chance that at least two share a birthday? Assume 365365 equally likely birthdays and that the people are unrelated (this ignores 29 February and twins). There are 365n365^n equally likely lists of nn birthdays. A list with no repeats is an ordered choice without replacement, so there are 365⋅364⋯(365−n+1)365\cdot364\cdots(365-n+1) of them. So the chance of no shared birthday is

qn=365⋅364⋯(365−n+1)365n,q_n=\frac{365\cdot364\cdots(365-n+1)}{365^n},

and the chance of some shared birthday is 1−qn1-q_n. For n=23n=23 this is 1−0.4927=0.50731-0.4927=0.5073, a little better than a coin flip. More values:

  • n=10n=10: 0.1170.117
  • n=20n=20: 0.4110.411
  • n=23n=23: 0.5070.507
  • n=30n=30: 0.7060.706
  • n=40n=40: 0.8910.891
  • n=57n=57: 0.9900.990
  • n=70n=70: 0.9990.999 (more exactly 0.999160.99916)

This feels too high because we picture one person matching me, which has only about 22/365≈6%22/365\approx6\% chance. But the question allows any pair, and 2323 people have (232)=253\binom{23}{2}=253 pairs. The same counting says how many random IDs can be handed out before two of them probably collide.

Go deeper A quick estimate: why 23 is enough

The chance that person i+1i+1 avoids the first ii birthdays is 1−i3651-\frac{i}{365}, and 1−x≤e−x1-x\le \mathrm{e}^{-x} with near equality for small xx. Multiplying for i=0,1,…,n−1i=0,1,\dots,n-1 gives

qn≈exp⁡ ⁣(−0+1+⋯+(n−1)365)=exp⁡ ⁣(−n(n−1)730),\begin{gathered} q_n \\ \approx\exp\!\left(-\frac{0+1+\cdots+(n-1)}{365}\right) \\ =\exp\!\left(-\frac{n(n-1)}{730}\right), \end{gathered}

using 0+1+⋯+(n−1)=n(n−1)/20+1+\cdots+(n-1)=n(n-1)/2. For n=23n=23 this is exp⁡(−253/365)≈0.500\exp(-253/365)\approx0.500, close to the exact value 0.4930.493. Setting the estimate equal to 12\frac12 gives n≈1.18365≈22.5n\approx1.18\sqrt{365}\approx22.5. With NN possible values instead of 365365, a collision becomes likely after roughly 1.18N1.18\sqrt N draws, which is why hash collisions arrive much sooner than NN draws.

Try it: enumerate and simulate

The first cell lists the two-dice sample space, builds events as Python sets, and checks the complement and overlap formulas with exact fractions. The second simulates with a fixed seed, so every run prints the same numbers, and compares relative frequencies with probabilities. The birthday problem is exact in one line and simulated in another. Predict before you run.

Try it: events as sets, counted exactly

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

Challenge: find the probability that two dice show a sum that is a prime number (2,3,5,7,112,3,5,7,11), by building the event as a set.

Try it: relative frequencies with a fixed seed

Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).

Challenge: change the seed and rerun. Which numbers change a lot at n=10n=10, and which barely move at n=10000n=10000?

With seed 7 the first cell printed 77, 5454, 537537 and 50305030 heads for n=10,100,1000,10000n=10,100,1000,10000: frequencies 0.70.7, 0.540.54, 0.5370.537 and 0.5030.503, which is the pattern of the simulator. Other seeds give other numbers.

Common mistakes

Check your understanding

  1. For two events, P(A)=0.3\mathbb{P}(A)=0.3, P(B)=0.5\mathbb{P}(B)=0.5 and P(A∩B)=0.1\mathbb{P}(A\cap B)=0.1. What is P(A∪B)\mathbb{P}(A\cup B)?

  2. Which of these lists cannot be the probabilities of the three outcomes of an experiment?

  3. Two fair dice are rolled. What is the probability that the sum is 44?

  4. A fair die is rolled three times. What is the probability of at least one six?

  5. A club has 1010 members and picks a committee of 33, where the order of the members does not matter. How many different committees are possible?

Exercises

  1. A coin is flipped and then a die is rolled. Write the sample space and say how many outcomes it has.
    Show answerThe outcomes are pairs such as (H,1)(H,1) and (T,6)(T,6): 2⋅6=122\cdot6=12 outcomes, by the multiplication rule.
  2. On a die, A={1,2,3}A=\{1,2,3\} and B={3,4}B=\{3,4\}. Find A∪BA\cup B, A∩BA\cap B, AcA^c and the probability of each.
    Show answerA∪B={1,2,3,4}A\cup B=\{1,2,3,4\} with probability 46=23\frac46=\frac23. A∩B={3}A\cap B=\{3\} with probability 16\frac16. Ac={4,5,6}A^c=\{4,5,6\} with probability 36=12\frac36=\frac12. Check: 36+26−16=46\frac36+\frac26-\frac16=\frac46.
  3. Two events have P(A)=0.6\mathbb{P}(A)=0.6 and P(B)=0.7\mathbb{P}(B)=0.7. Can they be mutually exclusive? What is the smallest possible value of P(A∩B)\mathbb{P}(A\cap B)?
    Show answerNo: mutually exclusive events would give P(A∪B)=1.3\mathbb{P}(A\cup B)=1.3, which is more than 11. Since P(A∪B)≤1\mathbb{P}(A\cup B)\le1, the overlap formula gives P(A∩B)=1.3−P(A∪B)≥0.3\mathbb{P}(A\cap B)=1.3-\mathbb{P}(A\cup B)\ge0.3. The value 0.30.3 is reached when A∪B=ΩA\cup B=\Omega.
  4. Two fair dice are rolled. Find the probability that the sum is 99, and that the sum is at most 33.
    Show answerSum 99: (3,6),(4,5),(5,4),(6,3)(3,6),(4,5),(5,4),(6,3), so 436=19\frac4{36}=\frac19. Sum at most 33: (1,1),(1,2),(2,1)(1,1),(1,2),(2,1), so 336=112\frac3{36}=\frac1{12}.
  5. Two fair dice are rolled. Find the probability that the dice show different numbers, and the probability that the larger of the two numbers is 44.
    Show answerDifferent: 1−636=3036=561-\frac6{36}=\frac{30}{36}=\frac56. Larger is 44: one die shows 44 and the other shows 11, 22 or 33 (66 pairs, counting both orders), or both show 44 (11 pair). That is 77 pairs, so 736\frac7{36}.
  6. A lock has 33 wheels with the digits 00 to 99. How many codes are possible, how many have three different digits, and what is the probability that a random code has three different digits?
    Show answer103=100010^3=1000 codes. Three different digits: 10⋅9⋅8=72010\cdot9\cdot8=720. The probability is 7201000=0.72\frac{720}{1000}=0.72.
  7. Ten people are in a club. In how many ways can they choose a president and a secretary (two different people), and in how many ways a pair of co-chairs?
    Show answerPresident and secretary: order matters, so 10⋅9=9010\cdot9=90. Co-chairs: order does not matter, so (102)=45\binom{10}{2}=45, half as many.
  8. A five-card hand is dealt from a well-shuffled 5252-card deck. What is the probability that all five cards are hearts?
    Show answer(135)/(525)=1287/2,598,960≈0.000495\binom{13}{5}/\binom{52}{5}=1287/2{,}598{,}960\approx0.000495, about one hand in 20002000.
  9. A fair coin is flipped 1010 times. What is the probability of at least one head?
    Show answerThe only sequence with no head is all tails, one of the 210=10242^{10}=1024. So the probability is 1−11024=10231024≈0.9991-\frac1{1024}=\frac{1023}{1024}\approx0.999.
  10. Three people are in a room. What is the probability that at least two share a birthday (same assumptions as the lesson)?
    Show answer1−365⋅364⋅3633653≈0.00821-\frac{365\cdot364\cdot363}{365^3}\approx0.0082.
  11. A fair coin gives 6060 heads in the first 100100 flips. If you flip 900900 more times, what is the expected frequency of heads over all 10001000 flips? Did the extra 1010 heads get “paid back”?
    Show answerExpected heads in the 900900 new flips: 450450. Expected total: 510510 out of 10001000, a frequency of 0.510.51. The surplus of 1010 heads over 5050 stays, but it is now a small part of the total, so the frequency is closer to 0.50.5.
  12. A model outputs the numbers (0.6,0.3,0.3)(0.6,0.3,0.3) for three classes. Why are they not probabilities, and what is one way to fix them?
    Show answerThey add to 1.21.2, but probabilities of a partition must add to 11. Divide each by 1.21.2 to get (0.5,0.25,0.25)(0.5,0.25,0.25).

Next: P1.D2 turns outcomes into numbers: random variables, and the probability mass function, density and cumulative distribution that describe them. P1.D4 shows what the average of those numbers settles to. P1.D5 studies how knowing one event changes the probability of another (conditional probability) and what it means for events to be independent, and P1.D6 turns that into Bayes’ theorem. P1.D8 states the law of large numbers precisely, and P1.D9 uses random samples to estimate hard quantities.

Key takeaways

  • An experiment has outcomes, the sample space Ω\Omega is the set of all of them, and an event is a subset of Ω\Omega. The event happens if the outcome that occurred is in it. Events are sets, so “not”, “or” and “and” are Ac=Ω∖AA^c=\Omega\setminus A, A∪BA\cup B and A∩BA\cap B. Events with A∩B=∅A\cap B=\varnothing are mutually exclusive.
  • A probability P\mathbb{P} follows three rules (Kolmogorov, 1933): P(A)≥0\mathbb{P}(A)\ge0, P(Ω)=1\mathbb{P}(\Omega)=1, and P(A∪B)=P(A)+P(B)\mathbb{P}(A\cup B)=\mathbb{P}(A)+\mathbb{P}(B) when AA and BB are mutually exclusive. From them follow P(∅)=0\mathbb{P}(\varnothing)=0, P(Ac)=1−P(A)\mathbb{P}(A^c)=1-\mathbb{P}(A), 0≤P(A)≤10\le\mathbb{P}(A)\le1, A⊆B⇒P(A)≤P(B)A\subseteq B\Rightarrow\mathbb{P}(A)\le\mathbb{P}(B) and P(A∪B)=P(A)+P(B)−P(A∩B)\mathbb{P}(A\cup B)=\mathbb{P}(A)+\mathbb{P}(B)-\mathbb{P}(A\cap B).
  • If the outcomes are equally likely, P(A)=∣A∣/∣Ω∣\mathbb{P}(A)=|A|/|\Omega|. This needs an argument for “equally likely”: the 11 sums of two dice are not, because 7 can be made 6 ways and 2 only one way (6/366/36 against 1/361/36). In general, give each outcome a probability that is at least 0, make them add to 1, and add the outcomes of an event.
  • Counting: n1n2⋯nkn_1n_2\cdots n_k for a process in steps, nkn^k ordered with repeats, n!/(n−k)!n!/(n-k)! ordered without repeats, and (nk)=n!k!(n−k)!\binom{n}{k}=\frac{n!}{k!(n-k)!} unordered without repeats. Count the favourable outcomes and all outcomes in the same way.
  • “At least one” is “not none”: P(at least one)=1−P(none)\mathbb{P}(\text{at least one})=1-\mathbb{P}(\text{none}). Four rolls give at least one six with probability 1−(5/6)4≈0.5181-(5/6)^4\approx0.518, and 23 people share a birthday with probability about 0.5070.507 (under the usual assumptions).
  • A relative frequency k/nk/n is close to the probability for large nn (the law of large numbers, P1.D8), but short runs can be far off, and the gap in counts typically grows while the gap in frequency shrinks. A fair coin has no memory: after a streak the next flip is still 1/21/2 (the gambler’s fallacy is the belief that it is not).
Found a mistake? Tell us