Conditional probability and Bayes' formula
Conditional probability as a shrunken sample space, the multiplication rule and the law of total probability, Bayes' formula and the base-rate fallacy on a screening test, odds and likelihood ratios, and pairwise, mutual and conditional independence.
When we learn something
The topic Kolmogorov's axioms gave us a probability space: a sample space , a collection of events and a measure that obeys three axioms. Once chosen, stays fixed. But most reasoning under uncertainty is about updating. A spam filter sees a word, a doctor sees a test result, a language model sees the start of a sentence. Each needs probabilities that account for what it has just learned.
Roll two fair dice: all 36 outcomes are equally likely. Someone tells you the sum is 8. Five outcomes remain: , , , , . The other 31 are ruled out, and the five survivors are still equally likely to one another. So the probability that the first die shows 3 is now , not .
Learning that happened does two things. It throws away every outcome outside , and it rescales what is left so that the total is 1 again. That is the definition of the conditional probability of given :
Now swap the roles. Given that the first die shows 3, the sum is 8 only for , so the probability of a sum of 8 is . Both questions share the intersection, with probability , but divide by different events. In general , and mixing the two up is the most common mistake in this topic.
For a fixed , the map satisfies all three axioms. It is a new probability measure that lives on , so every rule from the previous topic holds conditionally, for example . The condition matters. For a single point of under the uniform distribution, the formula divides by zero. Conditioning on such events needs densities and waits for the topic Joint distributions and independence.
The multiplication rule and the chain rule
Multiply the definition by the denominator. This gives the multiplication rule:
It is useful because the conditional is often the number we know directly. Draw two cards from a shuffled deck without putting the first one back. The first is an ace with probability . Given that, 3 aces remain among 51 cards:
Applying the rule again and again gives the chain rule for any number of events:
Each factor asks one question: given everything so far, how likely is the next step? The birthday problem from Outcomes, events and counting is such a product: each factor is the probability that the next birthday is new, given that the earlier ones were all different.
A language model works the same way. Write for the probability that a sentence consists of the tokens in this order. The chain rule splits it into next-token probabilities:
The model never stores the probability of a whole sentence. All it learns is the conditional for the next token, and the chain rule does the rest.
The law of total probability
Sometimes is hard to find directly but easy once we know which of several cases holds. Events form a partition of if they are pairwise disjoint, together cover , and each has positive probability. Every outcome then falls into exactly one case. The event splits into the disjoint pieces . Additivity adds the pieces up, and the multiplication rule rewrites each one. The result is the law of total probability:
It is an average of the conditional probabilities, weighted by how likely each case is.
Now the example that will follow us through the course: a screening test for a rare disease. Let be the event "the person is sick" and the event "the test is positive". Three numbers describe the test:
- the base rate : one person in a hundred is sick;
- the sensitivity : the test catches nine sick people out of ten;
- the false-positive rate : it also fires on 9 healthy people out of a hundred, so its specificity is .
The events and form a partition, so
The same in natural frequencies. Out of people, 100 are sick, and 90 of them test positive. The other are healthy, and 891 of them test positive anyway. That is 981 positives in total. A spam filter or a fraud detector has the same shape: a rare class and a good but imperfect detector.
Bayes' formula and the base rate
Someone tests positive. How likely is it that they are sick? We know , but the question asks for , the conditional the other way round. Write with the multiplication rule in both orders and divide by . This gives Bayes' formula:
The pieces have names. is the prior: how plausible the hypothesis was before the evidence. is the likelihood: how well explains the evidence . is the posterior: how plausible is after the evidence. The denominator comes from the law of total probability.
For our test,
Less than one in ten. The frequencies show why: only 90 of the 981 positives are sick. The test rarely errs on any one healthy person, but healthy people are 99 times more numerous, and their 891 false alarms swamp the 90 true positives.
Intuition usually answers "about 0.9". That answer quietly replaces with and ignores the base rate. The mistake is called the base-rate fallacy. Gigerenzer and Hoffrage (1995) found that people reason much better when the same data are given as natural frequencies, as in the count above.
In ML terms, a detector with recall and false-positive rate has precision on this population: recall is and precision is . The same reversal comes back twice later in the course. In Bayesian inference the hypotheses are values of a parameter. In Hypothesis tests and A/B testing, a p-value is a probability of data given a hypothesis, not of the hypothesis given data.
Independence
Sometimes learning tells us nothing about : . Multiply both sides by to get a symmetric definition that also works when . Events and are independent if
Two rolls of a fair die are the standard case: a 3 followed by a 5 has probability . Independence is not disjointness. If and are disjoint and both have positive probability, learning makes impossible, which is as dependent as two events get.
For more than two events, pairs are not enough. Events are mutually independent if the product rule holds for every subcollection: for any indices ,
Pairwise independence asks this only for pairs, and it is strictly weaker. Toss two fair coins. Let be "the first is heads", "the second is heads" and "the two coins agree". Each event has probability . Every pair is independent: for example, means both heads, with probability . But also means both heads, so its probability is , not . Any two of these events determine the third.
One more distinction. Events and are conditionally independent given , with , if
Run our test twice on the same person, and suppose the two runs err independently once the person's status is fixed. The results are conditionally independent given and given , but not independent overall. A first positive makes disease more likely, and with it a second positive: , while . Conditional independence does not imply independence.
Odds and accumulating evidence
With two competing hypotheses, Bayes' formula is simplest in odds. The odds of an event are . A probability of is odds of . Write Bayes' formula for and for with the same evidence and divide one by the other. The denominator cancels:
Posterior odds equal prior odds times the likelihood ratio. For our test the ratio is . The prior odds become , a probability of , as before.
Now a second test. If the runs are conditionally independent given the person's status, two positives have likelihood for a sick person and for a healthy one, and their likelihood ratio is . The odds become , a probability of about . A third positive gives , about . Each independent piece of evidence multiplies the odds by its own ratio. Logarithms turn this into a sum. For pieces of evidence , conditionally independent given and given ,
This is how a naive Bayes spam filter works. It assumes that the words of an email are conditionally independent given the class, spam or not, and adds one log likelihood ratio per word to the prior log-odds. The assumption is usually false, and the filter often works anyway. Logistic regression outputs a score of exactly this kind, the log-odds of the class. And if a classifier is deployed on a population with a different base rate but the same class-conditional inputs, only the prior term changes.
What exactly we learned
A last caution: we condition on what we actually learned, including how we learned it. The Monty Hall problem shows this. A car is behind one of three doors and goats are behind the other two. You pick door 1. The host knows where the car is. He opens another door with a goat behind it and offers you a switch.
If the car is behind door 1, with probability , switching loses. If it is behind one of the other two, with probability , the host is forced to open the remaining goat door, and switching wins. So switching wins with probability . The host's rule for choosing a door is part of the model. Another rule could produce the same visible event, a goat behind an opened door, and a different answer.
This completes the language of events. Conditional probability, the multiplication rule, the law of total probability, Bayes' formula and independence are the tools of everything that follows. But look at the questions that kept coming up: how many of three test runs are positive, how many suspicious words an email contains, what the two dice add up to. Each answer is a number attached to an outcome. The next topic, Discrete distributions, calls them random variables and starts with the simplest ones. The result of one test run is a Bernoulli variable, and the number of positives in runs that are conditionally independent given the person's status is binomial.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Go deeper — optional
Code and labs do not count toward the main percentage or the certificate.
- Bayes: coin from two urnsoptional
Update P(urn|data) after a heads/tails sequence; match the table.
~20 min · easier on a computer · The matching course topic has been read.
Self-check in the notebook — not part of mastery or the certificate.
Answers are saved on the server and tied to this browser until you sign in.