gl/ripetizioni
Learn Visually · Maths

Probabilities, to be seen on the tree.

Conditional probability, total probability and Bayes' theorem look like three formulas to learn by heart. In fact they are three ways of reading the same drawing: a tree in which every branch carries a number. Here you move the numbers yourself, and you watch the products along the paths, the sum of the leaves and the reading «backwards» change together.

On a probability tree you multiply along each path and add across different paths; to read the probability «backwards» you divide one leaf by all the leaves that end in the same place.

The principle

Three formulas, one single drawing.

The interactive figure

Use the arrows on the figure (or swipe) to follow the reasoning one piece at a time, then drag the pills on the branches up and down, or use the three sliders under the figure, or the ready-made examples. On a keyboard: Tab reaches a pill, and there the arrow keys change the value by one percentage point, Page Up / Page Down by ten, Home and End take it to 0% and 100%. When focus is elsewhere in the figure, ← and → change step.

Interactive probability treeTwo-level tree about a spam filter. First level: spam 10%, normal mail 90%. 10%

Hover (or tap) a part of the drawing — a fork, a leaf, the sum — to find out what it is.

Step 1 of 7 · The tree

A tree for reasoning in stages

A spam filter looks at incoming emails. The first level describes the mail: at the start 10 emails out of 100 are spam and 90 are normal. The two branches leaving the same point always make 100%: one or the other happens. Drag the value on the branch up or down, or use the arrows: the other branch is always the rest.

Examples
Transcript

The reasoning, step by step

The same 7 steps as the figure, all together and in the same order, to read straight through or to revise without touching anything.
  1. Step 1 · The tree

    A tree for reasoning in stages

    A spam filter looks at incoming emails. The first level describes the mail: at the start 10 emails out of 100 are spam and 90 are normal. The two branches leaving the same point always make 100%: one or the other happens. Drag the value on the branch up or down, or use the arrows: the other branch is always the rest.

  2. Step 2 · Second level

    After each branch, the filter decides

    From each point a new pair of branches starts: the filter flags the email or it does not. There are two independent numbers, because the filter behaves differently on spam (it flags 90 out of 100) and on normal mail (it flags 10 out of 100 by mistake). These are conditional probabilities: they only apply once you have taken that branch.

  3. Step 3 · The paths

    Every path is a scenario, and you multiply

    From the root to the leaves there are 2 × 2 = 4 paths: that is the counting principle, one choice after another. The probability of a path is the product of the branches it crosses: spam and flagged makes 10% × 90% = 9%. Out of 1000 emails, 100 are spam and 90 of those get flagged. Hover over a leaf, or select it, to see its path light up.

  4. Step 4 · The sum is 1

    The four leaves exhaust every case

    The four paths do not overlap and cover everything that can happen, so their probabilities always add up to 100%: 9% + 1% + 9% + 81% = 100%. It is the first check to make: if it does not add up on the tree, a branch is wrong. Move a value: the leaves change, the sum does not.

  5. Step 5 · Total

    Add the paths that lead to the same outcome

    How likely is it that an email gets flagged, whatever its nature? Two paths end in «flagged»: flagged spam (9%) and normal mail flagged by mistake (9%). Different paths that lead to the same outcome are added: 9% + 9% = 18%. This is the total probability formula, read off the tree.

  6. Step 6 · Reading backwards

    Turn the question round: where does a flagged email come from?

    Now you know the email was flagged: how likely is it to be really spam? Among the paths that end in «flagged», count how many go through spam: 9% out of a total of 18%, that is 50%. The filter flags 90% of spam, and yet a flagged email is spam only one time in two: there is so much normal mail that even the 10% of mistakes weighs as much as the real spam. This is Bayes’ theorem.

  7. Step 7 · Try it

    Change the numbers and watch the surprise move

    Bring spam down from 10% to 1%: a flagged email is spam one time in twelve. Take it up to 50%: it goes up to 9 times out of 10. With «No false alarms» a flag becomes certainty; with «Useless filter» (50% and 50%) flagging tells you nothing and the probability stays where it started. The backwards reading depends on how common the event is, not only on how good the filter is.

What each element of the drawing is

In the figure these explanations appear when you hover over the element. Here they are all written out.

A fork
The branches leaving the same point are all the ways the story can go on, so their probabilities always add up to 100%. That is why you set only one of them, by dragging it, and the other is the rest. The thicker the branch, the more likely it is.
The second level: conditional probabilities
Each point of the first level has its own pair of branches, with its own numbers: the probability that the filter flags, already knowing whether the email is spam or not. They are not two versions of the same number, they are two different numbers to set.
The leaves: a whole path
A leaf is the end of a path. Its probability is the product of the two branches crossed, written next to it: «10% × 90% = 9%». The four leaves are all and only the possible scenarios, so they add up to 100%.
The total probability
You add the leaves that end in the same outcome, here «flagged», even if they come from different branches. They are highlighted in blue: you multiply along the path, you add across the paths.
The backwards reading (Bayes)
You already know how it ended and want to work back to the start. The numerator is the leaf you care about, highlighted in orange; the denominator is the sum of all the leaves that end in the same outcome. It is a fraction, not a branch of the tree.
Self-check

Three questions to see if it has sunk in.

Try to answer by looking at the figure first, then open the answer.

1. Spam is 20% of the mail, the filter flags 50% of spam and 10% of normal mail. What is the «normal and flagged» path worth?

Normal mail is the rest of 20%, that is 80%, and the filter flags 10% of it by mistake: 80% × 10% = 8%. Out of 1000 emails that is 80 false alarms.

2. With the same numbers, how likely is an email to be flagged? And if it is flagged, how likely is it to be spam?

Spam and flagged: 20% × 50% = 10%. Normal and flagged: 8%. Flagged in all: 10% + 8% = 18%. Backwards reading: 10% / 18% ≈ 55.6%. More than half of the flagged emails are spam, even though the filter catches only half of it: here spam is frequent enough to withstand the false alarms.

3. Spam is 1% of the mail, the filter flags 95% of spam and 5% of normal mail. An email is flagged: is it spam with 95% probability?

No: 95% is the probability of being flagged knowing it is spam, a branch of the tree, while the question asks for the backwards reading. Spam and flagged: 1% × 95% = 0.95%. Normal and flagged: 99% × 5% = 4.95%. Flagged in all: 5.9%. So 0.95 / 5.9 ≈ 16.1%: about one flagged email in six is spam.

FAQ

The questions we always get.

When do you multiply and when do you add on the tree?

You multiply along a path, because it is one thing «and then» another: spam and then flagged. You add across different paths that lead to the same outcome, because it is one thing «or» another: flagged because it is spam, or flagged by mistake. If you come off the tree with a result above 100%, you have added where you should have multiplied or counted the same path twice.

What is the difference between P(A|B) and P(B|A)?

P(B|A) asks: if the email is spam, how likely is it to be flagged? It is a branch of the second level and it is worth 90%. P(A|B) asks the opposite: if the email is flagged, how likely is it to be spam? It is not on any branch: you get it by dividing one leaf by the total, and it is worth 50%. They are different numbers, and mixing them up is the most common mistake in conditional probability problems and in diagnostic tests.

How do you read Bayes' theorem on the tree?

The theorem says P(A|B) = P(B|A) · P(A) / P(B). On the tree the numerator is the leaf of the path «A then B» (10% × 90% = 9%) and the denominator is the sum of all the leaves that end in B (9% + 9% = 18%). You do not need to remember the formula: just draw the tree, multiply along the branches and divide.

Does the tree also work for draws without replacement, or with more than two branches?

Yes, with two precautions. There can be as many branches as you need, as long as those leaving the same point add up to 100%. And in draws without replacement the second-level probabilities change from branch to branch: from an urn with 3 red balls and 2 blue ones, the first red is worth 3/5 and the second red, after the first, 2/4, so the red-red path is 3/5 × 2/4 = 3/10. The tree makes it obvious why the two draws are not independent.

Keep going from here

Does probability not add up for you?

When the tree becomes a drawing you can redo on your own, the formulas for conditional probability, total probability and Bayes stop being a list to remember and become things you can work out. You catch up in a few lessons, with the right problems at the right time.