Advanced level

Conditional probability and Bayes’ theorem

Information changes the odds. Once you know that B happened, the space of possibilities shrinks to B alone — and every probability has to be recomputed inside it. This lesson shows how, how to reverse the condition with Bayes’ theorem, and why a positive screening result almost never means what it looks like.

Before you start

This topic builds on earlier ideas. Before you start, it's worth working through the lessons below — they'll make everything click:

Where this is used

Real situations where you count exactly the way this lesson teaches:

  • Reading a screening result
    A test detects the disease in 99 of every 100 patients who have it and misfires on 2 of every 100 who do not, while 1 person in 100 is actually ill. A positive result reaches 0.01 · 0.99 = 0.0099 of the ill and 0.99 · 0.02 = 0.0198 of the healthy, so 2.97 per cent of everyone tested. The probability of being ill after a positive result is 0.0099 / 0.0297 — exactly one third, not 99 per cent. That is why a positive screening result is always repeated with a confirmatory test.
  • A spam filter
    A filter learns how often a word appears in spam and how often in ordinary mail. If the word “invoice” shows up in 40 per cent of spam and 2 per cent of legitimate messages, and 30 per cent of all mail is spam, then seeing that word puts the probability of spam at 0.3 · 0.4 / (0.3 · 0.4 + 0.7 · 0.02) = 0.12 / 0.134, about 90 per cent. A naive Bayes classifier is this same computation repeated for every word in the message.
  • Quality control across two lines
    A plant makes 70 per cent of its units on line A, where 1 in 100 is faulty, and 30 per cent on line B, where 4 in 100 are. The overall defect rate is 0.7 · 0.01 + 0.3 · 0.04 = 0.019, that is 1.9 per cent. When a complaint concerns a faulty unit, the probability that it came off line B is 0.012 / 0.019, about 63 per cent — and that is the number an audit starts from.
  • Tomorrow’s weather forecast
    A service forecasts rain on 30 per cent of days. When it does, rain follows 80 per cent of the time; when it does not, rain still falls 10 per cent of the time. Rain on a randomly chosen day therefore has probability 0.3 · 0.8 + 0.7 · 0.1 = 0.31. If it rained and you cannot recall the forecast, the chance that rain had been predicted is 0.24 / 0.31, about 77 per cent.

All formulas

  • Conditional probability

    P(AB)=P(AB)P(B)P(A|B) = \frac{P(A \cap B)}{P(B)}

    defined only for P(B) > 0

  • Multiplication rule

    P(AB)=P(B)P(AB)P(A \cap B) = P(B) \cdot P(A|B)

    the definition times P(B)

  • Counting form

    P(AB)=ABBP(A|B) = \frac{|A \cap B|}{|B|}

    when the outcomes are equally likely

  • Law of total probability

    P(B)=i=1nP(Ai)P(BAi)P(B) = \sum_{i=1}^{n} P(A_i) \cdot P(B|A_i)

    A₁, …, Aₙ split Ω into disjoint cases

  • Bayes’ theorem

    P(AkB)=P(Ak)P(BAk)i=1nP(Ai)P(BAi)P(A_k|B) = \frac{P(A_k) \cdot P(B|A_k)}{\sum_{i=1}^{n} P(A_i) \cdot P(B|A_i)}

    reverses the condition: P(B|A) into P(A|B)

  • Independence

    P(AB)=P(A)    P(AB)=P(A)P(B)P(A|B) = P(A) \iff P(A \cap B) = P(A) \cdot P(B)

    the condition changes nothing about A

The classical probability lesson said that drawing without replacement is not independent, because the first draw changes what is left in the urn. We wrote that down as P(AB)P(A|B) and left it without a computation. This lesson supplies it.

The starting point is simple: information changes the odds. Once you know that BB has happened, outcomes outside BB are no longer possible — the sample space has shrunk from all of Ω\Omega to BB alone, and everything has to be recomputed inside it.

The definition

The conditional probability of AA given BB is the number

P(AB)=P(AB)P(B),P(B)>0P(A|B) = \frac{P(A \cap B)}{P(B)}, \qquad P(B) > 0

The numerator holds what favours AA and lies in the new space; the denominator holds the whole new space. It is exactly the construction of the classical formula, with BB taking over the role of Ω\Omega.

The proviso P(B)>0P(B) > 0 is not a formality: conditioning on an impossible event makes no sense, because there is nothing to divide by.

When all outcomes are equally likely, Ω|\Omega| cancels out of numerator and denominator and you can work with counts directly:

P(AB)=ABBP(A|B) = \frac{|A \cap B|}{|B|}

The two-way table

The handiest tool for conditional probabilities is the two-way table: one criterion down the rows, the other across the columns, counts in the cells.

A hundred students before an exam — half of them studied, half did not:

passedfailedtotal
studied45550
did not study153550
total6040100

Every conditional probability is read off this table as a cell divided by a row or a column total — without a single formula.

From the table above, read off P(passed | studied) and P(studied | passed).

P(AB)P(A|B) is not P(BA)P(B|A)

The example above was not a curiosity but the single most important warning of this lesson. Both quotients divide the same P(AB)P(A \cap B), but by different things, so they agree only in the special case P(A)=P(B)P(A) = P(B).

The everyday versions of this mistake sound entirely innocent:

  • “Most people with flu run a fever” does not mean “most people with a fever have flu”.
  • “Nearly every crash involves excessive speed” does not mean “nearly every instance of speeding ends in a crash”.

Both sentences swap the condition for the conclusion. The tool that performs that swap correctly comes two sections below.

The multiplication rule

It is worth clearing the denominator once, because that is the form the definition is used in most often:

P(AB)=P(B)P(AB)P(A \cap B) = P(B) \cdot P(A|B)

It reads as a recipe for a two-stage experiment: first BB happened, then, in the narrowed world, AA happened.

Two cards are drawn from a 52-card deck without replacement. What is the probability that both are aces?

The law of total probability

Often it is the other way round: we do not know P(B)P(B), but we do know it separately in each case. Then split Ω\Omega into disjoint events A1,,AnA_1, \dots, A_n covering everything, and add:

P(B)=i=1nP(Ai)P(BAi)P(B) = \sum_{i=1}^{n} P(A_i) \cdot P(B|A_i)

Each summand is one application of the multiplication rule. On the drawing each corresponds to one path: probabilities multiply along it, and finished paths are added.

urnU₁ 1/3white 2/52/15black 3/53/15U₂ 1/3white 3/53/15black 2/52/15U₃ 1/3white 1/51/15black 4/54/15
Three urns chosen with equal probability, each with a different mix. The number at the end of a path is the product of the probabilities along its branches. Three paths end in a white ball — their sum is the total probability.
Three urns are chosen with equal probability. The first holds 2 white balls out of 5, the second 3 out of 5, the third 1 out of 5. What is the probability of drawing a white ball?

Bayes’ theorem

Now the reversal. Since P(AkB)P(A_k \cap B) can be written two ways,

P(AkB)=P(AkB)P(B)=P(Ak)P(BAk)i=1nP(Ai)P(BAi)P(A_k|B) = \frac{P(A_k \cap B)}{P(B)} = \frac{P(A_k) \cdot P(B|A_k)}{\sum_{i=1}^{n} P(A_i) \cdot P(B|A_i)}

This is Bayes’ theorem, and the denominator is precisely the total probability of the previous section. On a tree it has a short description: one path divided by the sum of every path ending in the same result.

personill 0.01test + 0.990.0099test − 0.010.0001healthy 0.99test + 0.020.0198test − 0.980.9702
Two paths end in a positive result: an ill person detected (0.0099) and a healthy one misclassified (0.0198). The second is twice as thick as the first — the test rarely errs, but there are simply very many healthy people.
A test detects the disease in 99% of patients and raises a false alarm in 2% of healthy people. One per cent of the population is ill. What is the probability that a person with a positive result is ill?

Why so low? Because there are 9999 times more healthy people than ill ones, so even a two-per-cent error rate produces twice as many false alarms as there are true detections in total. You can see it on the tree: the thin 0.00990.0099 branch against the thick 0.01980.0198 one.

Base rates and the prosecutor’s fallacy

The number P(C)=0.01P(C) = 0.01 in the example has a name of its own: the base rate — the probability of the hypothesis before any evidence is seen. It enters both the numerator and the denominator of Bayes’ formula, so it governs the answer.

Here is what happens when only the base rate moves and the test stays the same:

base rateprobability of illness after a positive result
0.1%0.1\%4.7%4.7\%
1%1\%33%33\%
10%10\%85%85\%
50%50\%98%98\%

The same test, the same sensitivity, four completely different answers. Base-rate neglect — reading the sensitivity of a test as though it answered the question about the patient — is exactly the error this table puts into numbers.

Its courtroom version is called the prosecutor’s fallacy. It goes: the trace matches one person in a thousand, therefore the accused is guilty with probability 99.9%99.9\%. That is false, because the condition has been swapped with the conclusion.

A trace matches 1 person in 1000. Any of a million residents could have left it, and there is no other evidence. What can be said about the guilt of a person who matches?

Independence as a special case

Since conditioning is supposed to change the odds, it is worth naming the situation where it changes nothing:

P(AB)=P(A)P(A|B) = P(A)

Substituting that into the definition immediately gives the formula you know from the classical-probability lesson:

P(AB)=P(A)P(B)P(A \cap B) = P(A) \cdot P(B)

Independence is therefore not a separate idea but a special case of conditional probability — the one where knowing about BB turns out to be useless. The student table is not independent: P(passed)=60100P(\text{passed}) = \tfrac{60}{100} while P(passedstudied)=4550P(\text{passed}|\text{studied}) = \tfrac{45}{50}, so studying does help after all.

Exercises

The set asks three things, in the order of the lesson. The P(AB)P(A|B) prompt gives the probabilities P(AB)P(A \cap B) and P(B)P(B) — not counts — and asks for the quotient. The P(B)P(B) prompt gives a split into cases: pairs P(Ai)P(A_i) and P(BAi)P(B|A_i) from which the total probability has to be assembled. The P(A1B)P(A_1|B) prompt gives the same data and asks for the reversal, that is for Bayes’ theorem.

In all three cases the answer is a fraction in lowest terms — the convention from the classical-probability lesson still holds.

Practice

Work through a set of exercises — they get harder as you go. At the end you'll see your score and the mistakes worth reviewing.

Exercise 1 of 8Score: 0
P(A|B): P(A∩B) = 1/10, P(B) = 4/5

The generator does not ask about interpretation — about whether, at a given base rate, a positive result is already cause for concern. That is a piece of reasoning rather than a number, so it is laid out in the worked examples above.

Common mistakes

  • Confusing P(AB)P(A|B) with P(BA)P(B|A) — same numerator, different denominators. Always check which event is the condition: it is the one under the fraction bar.
  • Neglecting the base rate — the sensitivity of a test is not an answer to the question about the patient. For a rare disease most alarms are false even when the test is good.
  • Dividing by P(A)P(A) instead of P(B)P(B) — the denominator holds the probability of the condition, the event already known to have occurred.
  • A split that does not cover Ω\Omega — the law of total probability needs cases that are disjoint and exhaustive; if the P(Ai)P(A_i) do not add up to 11, the computation is meaningless.
  • Assuming independence without checking — multiplying P(A)P(B)P(A) \cdot P(B) is legitimate only when the condition really changes nothing. Drawing without replacement never qualifies.
  • Conditioning on an event of probability zero — there is nothing to divide by, so P(AB)P(A|B) is simply undefined.

Formula card

Topic: Conditional probability

  • Conditional probability

    P(AB)=P(AB)P(B)P(A|B) = \frac{P(A \cap B)}{P(B)}

    defined only for P(B) > 0

  • Multiplication rule

    P(AB)=P(B)P(AB)P(A \cap B) = P(B) \cdot P(A|B)

    the definition times P(B)

  • Counting form

    P(AB)=ABBP(A|B) = \frac{|A \cap B|}{|B|}

    when the outcomes are equally likely

  • Law of total probability

    P(B)=i=1nP(Ai)P(BAi)P(B) = \sum_{i=1}^{n} P(A_i) \cdot P(B|A_i)

    A₁, …, Aₙ split Ω into disjoint cases

  • Bayes’ theorem

    P(AkB)=P(Ak)P(BAk)i=1nP(Ai)P(BAi)P(A_k|B) = \frac{P(A_k) \cdot P(B|A_k)}{\sum_{i=1}^{n} P(A_i) \cdot P(B|A_i)}

    reverses the condition: P(B|A) into P(A|B)

  • Independence

    P(AB)=P(A)    P(AB)=P(A)P(B)P(A|B) = P(A) \iff P(A \cap B) = P(A) \cdot P(B)

    the condition changes nothing about A

personill 0.01test + 0.990.0099test − 0.010.0001healthy 0.99test + 0.020.0198test − 0.980.9702
A screening test in a population where 1 person in 100 has the disease. The four paths exhaust every possibility and their probabilities add up to one. Two of them end in a positive result — and it is the second, thicker one that dominates.

Frequently asked questions

Related articles