Skip to content
EN - Language: English
MathematicsHigh school

Conditional probability - tree diagrams, total probability and Bayes' theorem

· 16 min read

In short

A conditional probability P(A | B) is the chance of A when you already know that B has happened. Only the outcomes in B are still possible, so P(A | B) = P(A ∩ B)/P(B). On a tree diagram you multiply along a path and add the paths that end in A - the law of total probability. Bayes' theorem uses both to turn P(A | B) into P(B | A).

Words to know

Conditional probability
P(A | B), read "the probability of A given B" - the chance of A among the outcomes in which B happens.
Tree diagram
A picture of an experiment in stages, with one branch for each result and its probability on the branch.
Law of total probability
P(A) is the sum of the probabilities of all the paths that end in A.
Bayes' theorem
P(B | A) is the probability of the path through B to A, divided by P(A).
Independent events
Events for which knowing one does not change the chance of the other, P(A | B) = P(A).
Sensitivity and specificity
The share of ill people for whom a test is positive, P(+ | ill), and the share of healthy people for whom it is negative, P(− | healthy).

What do you think?

A test for an illness is positive for 90% of the people who have it. For healthy people it is wrong 10% of the time: it comes back positive although they are well. 10 in every 100 people have the illness. Your test is positive. How likely is it that you are ill?

The idea: a condition shrinks the group

“Given that” changes the question. Without a condition you look at everyone. With one, you look only at the cases in which the condition holds, and ask how often the event happens among them.

That is all a conditional probability is. P(A∣B)P(A \mid B), read “the probability of A given B”, is the share of the outcomes in BB that are also in AA. Once you know that BB has happened, every outcome outside BB is ruled out.

The order matters. P(ill∣+)P(\text{ill} \mid +) asks how many of the people with a positive test are ill. P(+∣ill)P(+ \mid \text{ill}) asks how many of the ill people test positive. The two are counted in different groups, so they are usually different numbers.

This page counts people before it uses percentages, and not by chance. In studies, people answered questions like the one above correctly much more often when the data were given as counts of people than as percentages.

A condition, step by step

Go through the animation with the buttons or the arrow keys. Each figure is one person: the red ones are ill, and an amber plus marks a positive test.

P(ill) = 10/100 = 0.1

healthyill

Step 1 of 6
Steps of the animation. Arrow keys move between steps.

Here are 100 people, and 10 of them are ill (red). Pick one person at random, and the probability that they are ill is 10/100 = 0.1.

Counting and the tree always agree. Counting is quicker when the numbers are small and simple, and the tree works with any probabilities.

Try it yourself: change the test

The first slider sets how common the illness is. The other two set how often the test is positive for ill people and for healthy people. Every branch keeps the thickness of its probability, and the amber paths are the positive results.

0.10.90.90.10.10.9illhealthy+0.09−0.01+0.09−0.81
10%
90%
10%
A positive result (total probability)
P(+) = 0.1 · 0.9 + 0.9 · 0.1 = 0.18
Ill, given a positive result (Bayes)
P(ill | +) = 0.09/0.18 = 50%
Out of 10,000 people
positive: 1,800 · of them ill: 900

What to notice:

  • when only 1% of people are ill, P(ill∣+)P(\text{ill} \mid +) drops to about 8%: of 10,000 people, 1080 test positive, but only 90 of them are ill,
  • with no false positives (the last slider at 0%), a positive test means that the person is ill,
  • when the illness is rare, the false positives change the answer much more than the middle slider does,
  • doctors call P(+∣ill)P(+ \mid \text{ill}) the sensitivity of a test, and P(−∣healthy)P(- \mid \text{healthy}), the chance of a negative result for a healthy person, its specificity.

Definitions and formulas

Conditional probability. For events AA and BB with P(B)>0P(B) > 0, the probability of AA given BB is

P(A∣B)=P(A∩B)P(B)P(A \mid B) = \frac{P(A \cap B)}{P(B)}

where A∩BA \cap B means that both AA and BB happen.

Equally likely outcomes. When all outcomes are equally likely, count only the outcomes in BB. With n(B)n(B) for the number of outcomes in BB,

P(A∣B)=n(A∩B)n(B)P(A \mid B) = \frac{n(A \cap B)}{n(B)}

The multiplication rule. Multiplying both sides of the definition by P(B)P(B) gives

P(A∩B)=P(B)⋅P(A∣B)P(A \cap B) = P(B) \cdot P(A \mid B)

On a tree, the branches that follow BB carry probabilities given BB. So the probability of a whole path is the product of the probabilities along it.

The law of total probability. Let B1,…,BnB_1, \ldots, B_n be events that do not overlap, that together cover every outcome, and that each have a probability above 0. Then for any event AA

P(A)=P(B1)⋅P(A∣B1)+…+P(Bn)⋅P(A∣Bn)\begin{aligned} P(A) &= P(B_1) \cdot P(A \mid B_1) \\ &+ \ldots \\ &+ P(B_n) \cdot P(A \mid B_n) \end{aligned}

On a tree, you add the probabilities of all the paths that end in AA. In the animation, B1B_1 was “ill”, B2B_2 was “healthy” and AA was a positive test.

Bayes’ theorem. Under the same conditions, and with P(A)>0P(A) > 0,

P(Bi∣A)=P(Bi)⋅P(A∣Bi)P(A)P(B_i \mid A) = \frac{P(B_i) \cdot P(A \mid B_i)}{P(A)}

In words: one path to AA, divided by all the paths to AA. The theorem turns P(A∣Bi)P(A \mid B_i) round into P(Bi∣A)P(B_i \mid A). It is named after Thomas Bayes. After Bayes died in 1761, his friend Richard Price edited his essay and sent it to the Royal Society in London, which published it.

Independent events. AA and BB are independent when knowing that BB has happened does not change the chance of AA:

P(A∣B)=P(A)P(A \mid B) = P(A)

For P(B)>0P(B) > 0 this is the same as P(A∩B)=P(A)⋅P(B)P(A \cap B) = P(A) \cdot P(B). Only for independent events may you multiply the plain probabilities.

Example 1: two dice

Two fair dice are rolled, and at least one of them shows a 6. What is the probability that the sum is 8?

Step 1. Name the events

We call the event we ask about AA and the condition BB.

A:the sum is 8B:at least one 6\begin{aligned} A&: \text{the sum is } 8 \\ B&: \text{at least one } 6 \end{aligned}

Step 2. Count the outcomes in B

There are 6 outcomes with a 6 on the first die and 6 with a 6 on the second. The roll (6, 6) is in both groups, so we subtract it once.

n(B)=6+6−1=11n(B) = 6 + 6 - 1 = \boxed{11}

Step 3. Count the outcomes in both A and B

A sum of 8 with a 6 needs a 2 on the other die, which gives two outcomes.

A∩B={(2,6), (6,2)}A \cap B = \{(2, 6),\ (6, 2)\}
n(A∩B)=2n(A \cap B) = \boxed{2}

Step 4. Divide by the outcomes in B

Only the 11 outcomes in BB are possible now, so we divide by 11, not by 36.

P(A∣B)=211P(A \mid B) = \frac{2}{11}

Answer

P(A∣B)=211≈0.18P(A \mid B) = \frac{2}{11} \approx 0.18. Knowing that there is a 6 makes a sum of 8 a little more likely than without the condition, when the probability is 536≈0.14\frac{5}{36} \approx 0.14.

Example 2: which machine made the faulty part?

A factory has two machines. Machine M1M_1 makes 60% of the parts, and 2% of its parts are faulty. Machine M2M_2 makes the other 40%, and 5% of its parts are faulty. A part picked at random turns out to be faulty. What is the probability that M2M_2 made it?

Step 1. Write down the data

FF stands for “the part is faulty”. The tree splits first by machine, then by whether the part is faulty.

P(M1)=0.6P(F∣M1)=0.02P(M2)=0.4P(F∣M2)=0.05\begin{aligned} P(M_1) &= 0.6 \\ P(F \mid M_1) &= 0.02 \\ P(M_2) &= 0.4 \\ P(F \mid M_2) &= 0.05 \end{aligned}

Step 2. Multiply along the paths to a faulty part

Each path is the product of its two branches.

0.6⋅0.02=0.0120.6 \cdot 0.02 = \boxed{0.012}
0.4⋅0.05=0.020.4 \cdot 0.05 = \boxed{0.02}

Step 3. Add the two paths

The law of total probability gives the share of all parts that are faulty.

P(F)=0.012+0.02=0.032P(F) = 0.012 + 0.02 = \boxed{0.032}

Step 4. Divide the path through the second machine by the sum

By Bayes’ theorem, we divide the path through M2M_2 by all the paths to a faulty part.

P(M2∣F)=0.020.032=0.625\begin{aligned} P(M_2 \mid F) &= \frac{0.02}{0.032} \\ &= 0.625 \end{aligned}

Answer

P(M2∣F)=0.625P(M_2 \mid F) = 0.625. The second machine makes fewer parts, but it made 62.5% of the faulty ones, because its parts are faulty more often.

Your turn

Urn I holds 3 white and 2 black balls, and urn II holds 1 white and 3 black balls. You toss a coin to choose an urn, then draw one ball from it. What is the probability that the ball is white? And if it is white, what is the probability that it came from urn I?

Hintshow

Draw a tree: first the urn (1/2 each), then the colour. Add the paths that end in a white ball, then divide the path through urn I by that sum.

Solutiontry it on your own first

Step 1. Multiply along the paths to a white ball

WW stands for “the ball is white”, and U1U_1 and U2U_2 for the two urns. Each urn is chosen with probability 12\frac{1}{2}, and then the ball is white with the share of white balls in that urn.

12⋅35=310\frac{1}{2} \cdot \frac{3}{5} = \boxed{\frac{3}{10}}
12⋅14=18\frac{1}{2} \cdot \frac{1}{4} = \boxed{\frac{1}{8}}

Step 2. Add the two paths

We write both fractions over 40 and add them.

P(W)=1240+540=1740\begin{aligned} P(W) &= \frac{12}{40} + \frac{5}{40} \\ &= \boxed{\frac{17}{40}} \end{aligned}

Step 3. Divide the path through urn I by the sum

By Bayes’ theorem, we divide the path through U1U_1 by both paths to a white ball.

P(U1∣W)=1240÷1740=1217\begin{aligned} P(U_1 \mid W) &= \frac{12}{40} \div \frac{17}{40} \\ &= \frac{12}{17} \end{aligned}

Answer

P(W)=1740P(W) = \frac{17}{40} and P(U1∣W)=1217≈0.71P(U_1 \mid W) = \frac{12}{17} \approx 0.71. A white ball most likely came from urn I, which holds more white balls.

Common mistakes

  • Turning the condition round. P(+∣ill)=0.9P(+ \mid \text{ill}) = 0.9 does not mean that P(ill∣+)=0.9P(\text{ill} \mid +) = 0.9: in the animation it is 0.5. This mix-up is sometimes called the confusion of the inverse.
  • Forgetting how rare the condition is. If only 1 in 100 people is ill, the same test gives P(ill∣+)≈0.08P(\text{ill} \mid +) \approx 0.08. Leaving out how common something is to begin with is called the base rate fallacy.
  • Dividing by all the outcomes. Given BB, only the outcomes in BB are possible. In Example 1 the answer is 211\frac{2}{11}, not 236\frac{2}{36}.
  • Multiplying probabilities that depend on each other. Draw two balls from urn I without putting the first one back. Both are white with probability 35⋅24=310\frac{3}{5} \cdot \frac{2}{4} = \frac{3}{10}, not 35⋅35=925\frac{3}{5} \cdot \frac{3}{5} = \frac{9}{25}: after one white ball, only 2 of the 4 balls left are white.
  • Adding along a path. Along a path you multiply. You add only different paths that end in the same result.

In exams and at university

In England, GCSE Mathematics asks you to calculate the probability of independent and dependent combined events, using tree diagrams, and to calculate and interpret conditional probabilities with two-way tables, tree diagrams and Venn diagrams. A level Mathematics adds the formula P(A∣B)=P(A∩B)P(B)P(A \mid B) = \frac{P(A \cap B)}{P(B)}.

In the United States, the Common Core high-school standards describe P(A∣B)P(A \mid B) as P(A and B)P(A \text{ and } B) divided by P(B)P(B), and ask students to read conditional probabilities from two-way frequency tables. The general multiplication rule is marked (+), as a standard for courses that go further.

At university, conditional probability comes early in a first course in probability or statistics. In the open textbook Introduction to Statistics by Diez, Barr and Çetinkaya-Rundel (OpenIntro), for example, it is part of the chapter on probability, together with probability trees and Bayes’ theorem, before random variables and statistical inference.

Check yourself

Test yourself

A fair die shows an even number. What is the probability that it is a 6?

P(A∩B)=0.2P(A \cap B) = 0.2 and P(B)=0.5P(B) = 0.5. What is P(A∣B)P(A \mid B)?

P(B)=0.3P(B) = 0.3, P(A∣B)=0.5P(A \mid B) = 0.5 and P(A∣B′)=0.2P(A \mid B') = 0.2, where B′B' means that BB does not happen. What is P(A)P(A)?

P(A)=0.4P(A) = 0.4, P(B)=0.5P(B) = 0.5 and P(A∩B)=0.2P(A \cap B) = 0.2. Are AA and BB independent?

A test is positive for 99% of the people who have an illness. Does a positive result mean a 99% chance of being ill?

Flashcards for review

Flip a card and say whether you knew the answer. The ones you did not know come back at the end of the deck. Why it pays to come back to them in a few days is explained in the guide Spaced repetition.

Flashcards

  1. What does P(A∣B)P(A \mid B) mean?

    The probability of AA when you know that BB has happened. Only the outcomes in BB count.
  2. What is the formula for P(A∣B)P(A \mid B)?

    P(A∣B)=P(A∩B)P(B)P(A \mid B) = \frac{P(A \cap B)}{P(B)}, for P(B)>0P(B) > 0.
  3. How do you find P(A∣B)P(A \mid B) when all outcomes are equally likely?

    Count the outcomes in both AA and BB, and divide by the number of outcomes in BB: n(A∩B)n(B)\frac{n(A \cap B)}{n(B)}.
  4. How do you get the probability of a path on a tree diagram?

    Multiply the probabilities along it. For two branches that is the multiplication rule, P(A∩B)=P(B)⋅P(A∣B)P(A \cap B) = P(B) \cdot P(A \mid B).
  5. What does the law of total probability say?

    If B1,…,BnB_1, \ldots, B_n do not overlap and cover every outcome, P(A)P(A) is the sum of all the paths that end in AA: P(B1)⋅P(A∣B1)+…+P(Bn)⋅P(A∣Bn)P(B_1) \cdot P(A \mid B_1) + \ldots + P(B_n) \cdot P(A \mid B_n).
  6. What does Bayes’ theorem say?

    P(Bi∣A)=P(Bi)⋅P(A∣Bi)P(A)P(B_i \mid A) = \frac{P(B_i) \cdot P(A \mid B_i)}{P(A)}: one path to AA, divided by all the paths to AA.
  7. Is P(A∣B)P(A \mid B) the same as P(B∣A)P(B \mid A)?

    No. In the animation P(+∣ill)=0.9P(+ \mid \text{ill}) = 0.9, but P(ill∣+)=0.5P(\text{ill} \mid +) = 0.5: the two are counted in different groups.
  8. When are AA and BB independent?

    When P(A∣B)=P(A)P(A \mid B) = P(A), which for P(B)>0P(B) > 0 is the same as P(A∩B)=P(A)⋅P(B)P(A \cap B) = P(A) \cdot P(B).
  9. Why can a positive test for a rare illness still mean that you are probably healthy?

    There are so many more healthy people that their false positives can outnumber the true positives. With 1 in 100 people ill, the test from this page gives P(ill∣+)≈0.08P(\text{ill} \mid +) \approx 0.08.
  10. What are the sensitivity and the specificity of a test?

    The sensitivity is P(+∣ill)P(+ \mid \text{ill}), the share of ill people who test positive. The specificity is P(−∣healthy)P(- \mid \text{healthy}), the share of healthy people who test negative.

Sources

We checked the facts on this page against these sources. All of them may be reused freely: they carry an open licence or are not covered by copyright.

  1. David Diez, Christopher Barr, Mine Çetinkaya-Rundel - Introduction to Statistics (OpenIntro, 4th edition), 3.2 Conditional Probability (P(A | B) = P(A and B)/P(B), the general multiplication rule, probability trees, Bayes' theorem) · CC BY-SA 3.0
  2. Kyle Siegrist - Probability, Mathematical Statistics, and Stochastic Processes, 2.4 Conditional Probability (definition, multiplication rule, the law of total probability for a partition, Bayes' theorem) · CC BY 2.0
  3. David Lippman - Math in Society, 12.4 Bayes' Theorem (a medical test worked with counts of people, and the same answer from Bayes' theorem) · CC BY-SA 3.0
  4. Karin Binder, Stefan Krauss, Ralf Schmidmaier, Leah Braun - Natural frequency trees improve diagnostic efficiency in Bayesian reasoning (Advances in Health Sciences Education, 2021) · CC BY 4.0
  5. Wikipedia - Conditional probability (definition, independence, confusion of the inverse) · CC BY-SA 4.0
  6. Wikipedia - Bayes' theorem (the theorem, the law of total probability, Thomas Bayes and Richard Price) · CC BY-SA 4.0
  7. Wikipedia - Sensitivity and specificity (true and false positives) · CC BY-SA 4.0
  8. Wikipedia - Base rate fallacy · CC BY-SA 4.0

More explainers