Skip to content
BIASED

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 Ω\Omega, a collection of events and a measure PP that obeys three axioms. Once chosen, PP 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: (2,6)(2,6), (3,5)(3,5), (4,4)(4,4), (5,3)(5,3), (6,2)(6,2). 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 1/51/5, not 1/61/6.

Learning that BB happened does two things. It throws away every outcome outside BB, and it rescales what is left so that the total is 1 again. That is the definition of the conditional probability of AA given BB:

P(A∣B)=P(A∩B)P(B),P(B)>0.P(A \mid B) = \frac{P(A \cap B)}{P(B)}, \qquad P(B) > 0.

Now swap the roles. Given that the first die shows 3, the sum is 8 only for (3,5)(3,5), so the probability of a sum of 8 is 1/61/6. Both questions share the intersection, with probability 1/361/36, but divide by different events. In general P(A∣B)≠P(B∣A)P(A \mid B) \ne P(B \mid A), and mixing the two up is the most common mistake in this topic.

For a fixed BB, the map A↦P(A∣B)A \mapsto P(A \mid B) satisfies all three axioms. It is a new probability measure that lives on BB, so every rule from the previous topic holds conditionally, for example P(Ac∣B)=1−P(A∣B)P(A^c \mid B) = 1 - P(A \mid B). The condition P(B)>0P(B) > 0 matters. For a single point of [0,1][0, 1] 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:

P(A∩B)=P(A) P(B∣A).P(A \cap B) = P(A)\, P(B \mid A).

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 4/524/52. Given that, 3 aces remain among 51 cards:

P(two aces)=452⋅351=1221≈0.0045.P(\text{two aces}) = \frac{4}{52} \cdot \frac{3}{51} = \frac{1}{221} \approx 0.0045.

Applying the rule again and again gives the chain rule for any number of events:

P(A1∩⋯∩An)=P(A1) P(A2∣A1)⋯P(An∣A1∩⋯∩An−1).P(A_1 \cap \dots \cap A_n) = P(A_1)\, P(A_2 \mid A_1) \cdots P(A_n \mid A_1 \cap \dots \cap A_{n-1}).

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 P(w1,…,wn)P(w_1, \dots, w_n) for the probability that a sentence consists of the tokens w1,…,wnw_1, \dots, w_n in this order. The chain rule splits it into next-token probabilities:

P(w1,…,wn)=∏t=1nP(wt∣w1,…,wt−1).P(w_1, \dots, w_n) = \prod_{t=1}^{n} P(w_t \mid w_1, \dots, w_{t-1}).

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 P(A)P(A) is hard to find directly but easy once we know which of several cases holds. Events B1,…,BnB_1, \dots, B_n form a partition of Ω\Omega if they are pairwise disjoint, together cover Ω\Omega, and each has positive probability. Every outcome then falls into exactly one case. The event AA splits into the disjoint pieces A∩BiA \cap B_i. Additivity adds the pieces up, and the multiplication rule rewrites each one. The result is the law of total probability:

P(A)=∑i=1nP(A∣Bi) P(Bi).P(A) = \sum_{i=1}^{n} P(A \mid B_i)\, P(B_i).

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 DD be the event "the person is sick" and ++ the event "the test is positive". Three numbers describe the test:

  • the base rate P(D)=0.01P(D) = 0.01: one person in a hundred is sick;
  • the sensitivity P(+∣D)=0.9P(+ \mid D) = 0.9: the test catches nine sick people out of ten;
  • the false-positive rate P(+∣Dc)=0.09P(+ \mid D^c) = 0.09: it also fires on 9 healthy people out of a hundred, so its specificity is P(−∣Dc)=0.91P(- \mid D^c) = 0.91.

The events DD and DcD^c form a partition, so

P(+)=0.9⋅0.01+0.09⋅0.99=0.0981.P(+) = 0.9 \cdot 0.01 + 0.09 \cdot 0.99 = 0.0981.

The same in natural frequencies. Out of 10,00010{,}000 people, 100 are sick, and 90 of them test positive. The other 9,9009{,}900 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 P(+∣D)P(+ \mid D), but the question asks for P(D∣+)P(D \mid +), the conditional the other way round. Write P(A∩B)P(A \cap B) with the multiplication rule in both orders and divide by P(A)P(A). This gives Bayes' formula:

P(B∣A)=P(A∣B) P(B)P(A),P(A)=∑iP(A∣Bi) P(Bi).P(B \mid A) = \frac{P(A \mid B)\, P(B)}{P(A)}, \qquad P(A) = \sum_i P(A \mid B_i)\, P(B_i).

The pieces have names. P(B)P(B) is the prior: how plausible the hypothesis BB was before the evidence. P(A∣B)P(A \mid B) is the likelihood: how well BB explains the evidence AA. P(B∣A)P(B \mid A) is the posterior: how plausible BB is after the evidence. The denominator comes from the law of total probability.

For our test,

P(D∣+)=0.9⋅0.010.0981≈0.092.P(D \mid +) = \frac{0.9 \cdot 0.01}{0.0981} \approx 0.092.

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 P(D∣+)P(D \mid +) with P(+∣D)P(+ \mid D) 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 0.90.9 and false-positive rate 0.090.09 has precision 0.0920.092 on this population: recall is P(+∣D)P(+ \mid D) and precision is P(D∣+)P(D \mid +). 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 BB tells us nothing about AA: P(A∣B)=P(A)P(A \mid B) = P(A). Multiply both sides by P(B)P(B) to get a symmetric definition that also works when P(B)=0P(B) = 0. Events AA and BB are independent if

P(A∩B)=P(A) P(B).P(A \cap B) = P(A)\, P(B).

Two rolls of a fair die are the standard case: a 3 followed by a 5 has probability 1/36=(1/6)⋅(1/6)1/36 = (1/6) \cdot (1/6). Independence is not disjointness. If AA and BB are disjoint and both have positive probability, learning BB makes AA impossible, which is as dependent as two events get.

For more than two events, pairs are not enough. Events A1,…,AnA_1, \dots, A_n are mutually independent if the product rule holds for every subcollection: for any indices i1<⋯<iki_1 < \dots < i_k,

P(Ai1∩⋯∩Aik)=P(Ai1)⋯P(Aik).P(A_{i_1} \cap \dots \cap A_{i_k}) = P(A_{i_1}) \cdots P(A_{i_k}).

Pairwise independence asks this only for pairs, and it is strictly weaker. Toss two fair coins. Let AA be "the first is heads", BB "the second is heads" and CC "the two coins agree". Each event has probability 1/21/2. Every pair is independent: for example, A∩CA \cap C means both heads, with probability 1/4=(1/2)⋅(1/2)1/4 = (1/2) \cdot (1/2). But A∩B∩CA \cap B \cap C also means both heads, so its probability is 1/41/4, not 1/81/8. Any two of these events determine the third.

One more distinction. Events AA and BB are conditionally independent given CC, with P(C)>0P(C) > 0, if

P(A∩B∣C)=P(A∣C) P(B∣C).P(A \cap B \mid C) = P(A \mid C)\, P(B \mid C).

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 DD and given DcD^c, but not independent overall. A first positive makes disease more likely, and with it a second positive: P(second +∣first +)≈0.164P(\text{second } + \mid \text{first } +) \approx 0.164, while P(+)=0.0981P(+) = 0.0981. 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 O(A)=P(A)/P(Ac)O(A) = P(A) / P(A^c). A probability of 0.010.01 is odds of 1:991 : 99. Write Bayes' formula for DD and for DcD^c with the same evidence and divide one by the other. The denominator P(+)P(+) cancels:

O(D∣+)=O(D)⋅P(+∣D)P(+∣Dc).O(D \mid +) = O(D) \cdot \frac{P(+ \mid D)}{P(+ \mid D^c)}.

Posterior odds equal prior odds times the likelihood ratio. For our test the ratio is 0.9/0.09=100.9 / 0.09 = 10. The prior odds 1:991 : 99 become 10:9910 : 99, a probability of 10/109≈0.09210/109 \approx 0.092, as before.

Now a second test. If the runs are conditionally independent given the person's status, two positives have likelihood 0.920.9^2 for a sick person and 0.0920.09^2 for a healthy one, and their likelihood ratio is 10210^2. The odds become 100:99100 : 99, a probability of about 0.5030.503. A third positive gives 1000:991000 : 99, about 0.9100.910. Each independent piece of evidence multiplies the odds by its own ratio. Logarithms turn this into a sum. For pieces of evidence E1,…,EmE_1, \dots, E_m, conditionally independent given DD and given DcD^c,

log⁡O(D∣E1∩⋯∩Em)=log⁡O(D)+∑j=1mlog⁡P(Ej∣D)P(Ej∣Dc).\log O(D \mid E_1 \cap \dots \cap E_m) = \log O(D) + \sum_{j=1}^{m} \log \frac{P(E_j \mid D)}{P(E_j \mid D^c)}.

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 log⁡O(D)\log O(D) 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 1/31/3, switching loses. If it is behind one of the other two, with probability 2/32/3, the host is forced to open the remaining goat door, and switching wins. So switching wins with probability 2/32/3. 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 nn 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.