A trained classifier looks at a photo and reports cat , dog , bird . Could it report cat , dog , bird ? Something feels wrong with that second answer, but what, exactly? And what does “” 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 (capital omega). It is a set in the sense of P1.A4, and is its size.
- One coin flip: , so .
- One die: , so .
- Two coin flips, in order: , so . The pair (heads first) and (tails first) are different outcomes.
- Two dice, one red and one blue: has the pairs with and from to . The pair is a different outcome from , 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, . 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 : 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 . If the die shows , then happened, because . If it shows , then did not happen. For two dice, “the sum is 7” is the event made of the pairs , , , , and .
Two extremes: the empty event never happens, and the sure event happens whatever the outcome. An event with a single outcome, such as , is called an elementary event. If , then happens whenever 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, (even) and (at least 4).
- not is the complement . Some books write or .
- or is the union . In maths, “or” is inclusive: it happens when at least one of the two happens, and also when both do.
- and is the intersection .
- but not is .
Two events are mutually exclusive (or disjoint) when : they cannot both happen in one run. “Even” and “at most 2” overlap in , but “even” and “odd” do not. Events that are mutually exclusive and together cover form a partition. The six single faces form one, and so do and . The class labels of a classifier form one too: the photo is exactly one of cat, dog or bird.
Things to try with the event grid:
- Start with A = “at least one six” and B = “sum is at least 10”. A has cells and B has , but A or B has only , not : the five cells with a six and a large sum were counted in both.
- Switch to not A. The highlighted cells number , which is .
- Choose A = “sum is 7” and B = “doubles”. No cell is in both, so the events are mutually exclusive and the overlap is .
- Choose A = “sum is even” and B = “first die is even”. Each has cells, they share , and A or B has . Then tap cells to change A and watch the counts follow.
The rules of probability
A probability gives every event a number , read “the probability of ”, which measures how likely is. ML papers often write for the probability of an outcome , 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.
- Not negative. for every event .
- Something happens. .
- Additivity. If and are mutually exclusive, then .
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. . Reason: and are mutually exclusive and their union is , so .
- The impossible event. (it is the complement of ).
- Probabilities lie between 0 and 1. : rule 1 gives the lower bound, and the complement rule with gives the upper bound.
- Bigger events are at least as likely. If , then .
- Overlapping events. For any two events, . Adding and counts the overlap twice, so subtract it once. This is the probability version of from P1.A4. When and are mutually exclusive the overlap is empty and rule 3 comes back.
- Union bound. , because the subtracted term is not negative.
- Outcomes add up. For a finite , the probability of an event is the sum of the probabilities of its outcomes, and those add to over all of .
Check the overlap formula on a die with and . Then , and , so . Counting directly, has of the faces, which is too.
Breaking the rules. Suppose a model says cat , dog , bird . The three classes form a partition of , so rule 3 says their probabilities must add to . They add to , so these numbers cannot be probabilities. One fix is to divide each by the sum: , which adds to . Softmax (P3.A8) takes the exponential of each raw score and then divides by the sum: scores become , which add to . The earlier answer, , fails in the same way.
Go deeper Proving the consequences from the three rules
Empty event. The sets and are mutually exclusive and , so . Subtract from both sides: .
Monotonicity. If , write . These two pieces are mutually exclusive, so , using rule 1 for the second piece.
Overlap formula. Split into the mutually exclusive pieces and , so . Split into the mutually exclusive pieces and , so . Subtract the second line from the first: , which is the formula.
Three events. The same idea gives . On the two-dice grid, let be “at least one six”, “sum at least 10” and “doubles”. Their sizes are , , , the pairwise overlaps have , and cells, the triple overlap has cell, and cells, which matches a direct count of . 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”: . Likewise . On the same grid, has cells, and so does . 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 . Here is infinite and cannot be listed. A natural model gives an interval a probability equal to its length, so . But then the single outcome has length , so , even though is a possible outcome. So in an infinite sample space, probability does not mean impossible, and probability does not mean certain. In a finite sample space with a sensible model, an outcome you give probability 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 , with . Mathematicians also show that on a continuum such as the interval you cannot consistently give every subset a probability, so the full definition lets 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 is finite and every outcome is equally likely: a fair coin, a fair die, a well-shuffled deck. There are outcomes whose probabilities are equal and add to , so each has probability . An event is a sum of its outcomes, so
In words: the number of outcomes in the event, divided by the number of all outcomes.
For the even die, and , so . For two dice, “the sum is 7” has outcomes out of : . Two more: “doubles” has outcomes, so , and “the two dice differ” is the complement of doubles, so .
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 sums are not equally likely. Out of the equally likely pairs, the sums come up times. So and , not 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 are not equally likely. Of the four ordered outcomes, one has no heads, two have one head and one has two heads, so , not .
- Unfair devices. A coin that lands heads with probability has two outcomes with probabilities and . The rule for a general finite is: give each outcome a probability that is at least , make them add to , 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 . The probability of an event is the weight of the sand on its outcomes. For a fair die each face holds 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 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 adding to :
With equally likely outcomes and .
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/6Do frequencies match probabilities? A simulator
The rules say a fair coin lands heads with probability . 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 runs, say times. The relative frequency is . For heads in flips it is .
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 grows. The horizontal axis is logarithmic, so the first few flips are not squeezed into a sliver.
Things to try with the simulator:
- Keep the fair coin and press Flip 10, then Reset, a few times. The frequency of heads swings widely: and are not unusual. Exactly heads in flips has probability only , about one run in four, and or fewer, or or more, heads happens in of runs.
- Now press Flip 1000 on a fresh run, several times. The frequency of heads stays much closer to . The chance that it lands outside the range to is under one in a billion at flips, compared with at flips.
- Choose Two dice, sum and press Roll 1000. The bars form a triangle with its peak at , like the counts above. Follow the event “sum is 2” and press “Zoom in on the true value”: with you expect only about cases, so the line is jumpy.
- Choose Biased coin and set . The frequency of heads settles near , not .
- Watch “count minus ” in the readout. For a fair coin its typical size grows with (about at flips), while the gap in frequency shrinks.
What you saw has a name: the law of large numbers (P1.D8). Informally, as 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 of is for flips and for flips (each is a count of sequences, and a sum of terms over ). Within : for flips and for 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 is about for flips and about for flips (it grows like ; P1.D4 explains why). Divided by , those gaps are and . 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 heads. The law of large numbers does not pay the surplus back, it dilutes it. Say that after flips you have heads, a surplus of . The next flips have an expected heads, so the expected frequency at flips is . The surplus of is still there, but it no longer matters. Random sequences are also streakier than most people expect: a run of or more equal flips appears in about of sequences of fair flips.
What does a probability mean?
There are two common answers, and both obey the same three rules.
- Long-run frequency. is the value that the relative frequency of 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. measures how strongly you believe , 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 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 and , and listing outcomes stops being practical quickly: a five-card poker hand has possibilities. Counting rules do the work without a list.
Multiplication rule. If a process has steps with , , , choices, and the number of choices at each step does not depend on the earlier choices, there are ways in total. A four-digit PIN has possibilities. A password of lowercase letters has , about . A string of coin flips has outcomes.
Two questions then pick the formula for choosing items from :
- Order matters, repeats allowed (with replacement). There are choices, since each of the steps has options.
- Order matters, no repeats (without replacement). The first step has options, the next , and so on:
Here is “ factorial”, with . With this counts the permutations, the orderings of things: . For the top three of runners in a race, .
- Order does not matter, no repeats. Each unordered set of items appears times among the ordered lists, so divide by . The result is the number of combinations, “ choose ”:
Choosing of people gives pairs, half of the 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 options, the next , then , then : of the PINs, so a random PIN has four different digits with probability .
Example: cards. A fair shuffle makes every five-card hand equally likely, so . The hands with no ace are chosen from the other cards: . So . The probability of at least one ace is .
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 items from types, with repeats allowed and order ignored? The answer is . For two dice, and : unordered results (“a 3 and a 5”, “two 4s”). But these results are not equally likely. A double such as comes from one of the ordered pairs, so its probability is , while comes from two ordered pairs, and , so its probability is . 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 , by the complement rule. “None” is usually much easier to count.
Four rolls of a die, at least one six. There are 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 to , so there are of them. So
Counting the cases “exactly one six”, “exactly two sixes” and so on would be much longer. The trap is to add: is too big, because the events “a six on roll ”, “a six on roll ” and the rest overlap. The union bound says only that the true value is at most .
Two dice, at least one six. The complement is “no six on either die”, of the pairs, so . This is the event A of the grid above.
The birthday problem. In a group of people, what is the chance that at least two share a birthday? Assume equally likely birthdays and that the people are unrelated (this ignores 29 February and twins). There are equally likely lists of birthdays. A list with no repeats is an ordered choice without replacement, so there are of them. So the chance of no shared birthday is
and the chance of some shared birthday is . For this is , a little better than a coin flip. More values:
- :
- :
- :
- :
- :
- :
- : (more exactly )
This feels too high because we picture one person matching me, which has only about chance. But the question allows any pair, and people have 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 avoids the first birthdays is , and with near equality for small . Multiplying for gives
using . For this is , close to the exact value . Setting the estimate equal to gives . With possible values instead of , a collision becomes likely after roughly draws, which is why hash collisions arrive much sooner than 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.
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 (), by building the event as a set.
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 , and which barely move at ?
With seed 7 the first cell printed , , and heads for : frequencies , , and , which is the pattern of the simulator. Other seeds give other numbers.
Common mistakes
Check your understanding
For two events, , and . What is ?
The overlap formula gives . The value adds the two probabilities and forgets that the overlap was counted twice. The value subtracts the overlap twice. The value is the product , which is not what a union is.
Which of these lists cannot be the probabilities of the three outcomes of an experiment?
The list adds to , but the probabilities of all outcomes together must add to . The list and the list are nonnegative and add to . The list also meets both conditions: it describes an experiment that always gives the first outcome, which the rules allow.
Two fair dice are rolled. What is the probability that the sum is ?
Three of the equally likely ordered pairs have sum : , and . The value treats the sums as equally likely, which they are not. The value forgets the pair . The value is the probability of a sum of .
A fair die is rolled three times. What is the probability of at least one six?
Of the equally likely sequences, have no six, so the probability is . The value adds the three probabilities, which overlap. The value is the probability of no six at all. The value is the probability of three sixes, not of at least one.
A club has members and picks a committee of , where the order of the members does not matter. How many different committees are possible?
Order does not matter and nobody is picked twice, so the count is . The value is , which counts ordered lists, each committee times. The value allows repeated picks. The value is , which has no counting rule behind it.
Exercises
- A coin is flipped and then a die is rolled. Write the sample space and say how many outcomes it has.
Show answer
The outcomes are pairs such as and : outcomes, by the multiplication rule. - On a die, and . Find , , and the probability of each.
Show answer
with probability . with probability . with probability . Check: . - Two events have and . Can they be mutually exclusive? What is the smallest possible value of ?
Show answer
No: mutually exclusive events would give , which is more than . Since , the overlap formula gives . The value is reached when . - Two fair dice are rolled. Find the probability that the sum is , and that the sum is at most .
Show answer
Sum : , so . Sum at most : , so . - 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 .
Show answer
Different: . Larger is : one die shows and the other shows , or ( pairs, counting both orders), or both show ( pair). That is pairs, so . - A lock has wheels with the digits to . 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 answer
codes. Three different digits: . The probability is . - 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 answer
President and secretary: order matters, so . Co-chairs: order does not matter, so , half as many. - A five-card hand is dealt from a well-shuffled -card deck. What is the probability that all five cards are hearts?
Show answer
, about one hand in . - A fair coin is flipped times. What is the probability of at least one head?
Show answer
The only sequence with no head is all tails, one of the . So the probability is . - Three people are in a room. What is the probability that at least two share a birthday (same assumptions as the lesson)?
Show answer
. - A fair coin gives heads in the first flips. If you flip more times, what is the expected frequency of heads over all flips? Did the extra heads get “paid back”?
Show answer
Expected heads in the new flips: . Expected total: out of , a frequency of . The surplus of heads over stays, but it is now a small part of the total, so the frequency is closer to . - A model outputs the numbers for three classes. Why are they not probabilities, and what is one way to fix them?
Show answer
They add to , but probabilities of a partition must add to . Divide each by to get .
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.