letx=

Probability and statistics

Bayes' theorem

Reverses a conditional probability: from how likely the evidence is if A is true, to how likely A is given the evidence.

P(A∣B)=P(B∣A)P(A)P(B)
Total probability of BP(B)=P(B∣A)P(A)+P(B∣A¯)P(A¯)

Calculator

The live calculator needs JavaScript. The worked example below shows the same steps it would.

See it move

0.010.99Anot A0.9A and B: 0.0090.1A, not B: 0.0010.09not A, B: 0.08910.91not A, not B: 0.901
  • P(A | B) = 0.009 ÷ (0.009 + 0.0891) = 0.09174: the share of the B branches (thick) that come through A.

What the symbols mean

P(A∣B)
the updated (posterior) probability of A after seeing B
P(A)
the prior probability of A, before the evidence (for example how common a disease is)
P(B∣A)
the probability of the evidence when A is true (a test's sensitivity)
P(B∣A¯)
the probability of the evidence when A is false (a test's false-positive rate)
A¯
"not A"

When to use it

Use it for medical tests, spam filters, fault diagnosis and any "how likely is the cause, given what I observed?" question. The calculator takes the three numbers you usually know (base rate, true-positive rate and false-positive rate) and works out P(B) for you. The result is often surprising: a good test for a rare condition still gives many false alarms.

Worked example

A disease affects 1% of people. A test detects it 90% of the time, but it also gives a positive result for 9% of healthy people. If you test positive, what is the probability you have the disease?

  1. Start from Bayes’ theorem, with P(B) expanded by the law of total probability.

    P(A∣B)=P(B∣A)P(A)P(B),P(B)=P(B∣A)P(A)+P(B∣A¯)(1−P(A))
  2. First, the overall probability of B, true positives plus false positives:

    P(B)=0.9·0.01+0.09·(1−0.01)
    =0.009+0.09·0.99
    =0.009+0.0891
    =0.0981
  3. Then the share of those cases where A is true:

    P(A∣B)=0.9·0.010.0981
    =0.0090.0981
    ≈0.0917431
  4. As a percentage, about 9.174%. It is low because A is rare to begin with: most cases of B come from the much larger group where A is false.

Answer P(disease | positive) ≈ 0.0917

Why it works

P(A∩B) can be written two ways, P(A∣B)P(B) and P(B∣A)P(A). Setting them equal and dividing by P(B) gives the theorem. The tree diagram shows the same thing as counts of branches.

Common mistakes

  • Taking the test's accuracy (90%) as the answer and ignoring how rare the disease is (the base-rate fallacy).
  • Using P(B) = P(B | A) instead of the total probability over both branches.
  • Mixing up the false-positive rate P(B∣A¯) with 1− sensitivity, which is the false-negative rate.