Probability, conditional probability and Bayes' theorem

A test catches 99% of the people who have a disease. Your result is positive. How likely is it that you really have the disease? It is tempting to say 99%. If the disease is rare, the real answer is much lower. Draw 1000 people and count for yourself.

Short answer. Probability is a number from 0 to 1 that says how likely something is. Conditional probability is the probability when you already know something. You keep only the cases that match what you know, and count again inside them. Bayes' theorem is the math for updating a belief when you see new evidence. Take your old belief, multiply it by the chance of this evidence if that belief is true, and divide by the overall chance of this evidence.

Share with the disease (prevalence) % Positive if sick (sensitivity) % Negative if healthy (specificity) %
  • True positive (sick, positive):
  • False negative (sick, negative):
  • False positive (healthy, positive):
  • True negative (healthy, negative):

Drag a slider or type a number. Circles are people with the disease, squares are people without it. Filled means the test said positive, hollow means it said negative. The shapes are rounded to whole people, and the percentages are worked out before rounding.

Definitions

The text below explains by counting 1000 people. The textbook definitions are as follows. The set of all possible outcomes (the sample space) is S, the complement of an event A is Ac, "A and B" is A∩B, and "A or B" is A∪B.

conditional probability
The probability that event A happens given that event B happened, where P(B) > 0. It is the probability of A ∩ B divided by the probability of B.

P(A∣B)=P(A∩B)P(B)

multiplication rule
The definition of conditional probability written another way. If P(A) > 0 and P(B) > 0, then

P(A∩B)=P(B)P(A∣B)=P(A)P(B∣A)

independence
Two events A and B are independent if the line below holds. When P(B) > 0, this is the same as P(A | B) = P(A).

P(A∩B)=P(A)P(B)

law of total probability
Suppose the events A₁, …, Aₙ do not overlap (Ai∩Aj=∅, i≠j), together make up the sample space (A1∪⋯∪An=S), and P(Aᵢ) > 0 for every i. Then for any event B, the line below holds. In the test example, n = 2, A₁ is "has the disease", A₂ is "no disease", and B is "positive".

P(B)=∑i=1nP(Ai)P(B∣Ai)

Bayes' theorem
If P(A) > 0 and P(B) > 0, the line below holds. P(A) is called the prior probability, P(B | A) the likelihood, and P(A | B) the posterior probability. The bottom, P(B), comes from the law of total probability above.

P(A∣B)=P(A)P(B∣A)P(B)

Written with the partition A₁, …, Aₙ from above, it is

P(Aj∣B)=P(Aj)P(B∣Aj)∑i=1nP(Ai)P(B∣Ai)

sensitivity, specificity, positive predictive value
Let D be the event "has the disease" and T the event "the test is positive". Sensitivity is the share of people with the disease who test positive. Specificity is the share of people without the disease who test negative. The positive predictive value (PPV) is the share of people who test positive who have the disease.

sensitivity=P(T∣D),specificity=P(Tc∣Dc)

The positive predictive value comes from the prevalence P(D), the sensitivity and the specificity by Bayes' theorem. Note that P(T∣Dc)=1−specificity.

PPV=P(D∣T)=P(D)P(T∣D)P(D)P(T∣D)+P(Dc)P(T∣Dc)

1. Probability: how likely, from 0 to 1

Probability is a number from 0 to 1 that says how likely something is to happen. 0 means it never happens. 1 means it always happens. 0.25 means that if you repeat the same thing many times, it happens about 1 time in 4. Multiply by 100 and you can also write it as 25%.

When every case is equally likely, you can count.

probability=number of cases where it happensnumber of all cases

Roll one die. The chance of an even number is 3 ÷ 6 = 0.5, because three faces are even: 2, 4 and 6.

Something that may or may not happen, like "the roll is even" or "this person has the disease", is called an event. The probability of event A is written P(A). P stands for probability.

2. The basic rules

RuleFormulaExample with a die
Between 0 and 10 ≤ P(A) ≤ 1The chance of rolling a 7 is 0. The chance of rolling one of 1 to 6 is 1
Complement (it does not happen)P(not A) = 1 − P(A)The chance of not rolling a 6 = 1 − 1/6 = 5/6
OrP(A or B) = P(A) + P(B) − P(A and B)Even, or 4 and up: 3/6 + 3/6 − 2/6 = 4/6. 4 and 6 were counted twice, so take them off once
Multiplication ruleP(A and B) = P(B) × P(A|B)4 and up, and even: 3/6 × 2/3 = 2/6. Of 4, 5 and 6, two are even
Multiplying when independentP(A and B) = P(A) × P(B)Two dice both show 6: 1/6 × 1/6 = 1/36

P(A|B) is the conditional probability from the next section. Read it as "the probability of A given B".

Independent means that knowing one thing does not change the probability of the other. As a formula, P(A|B) = P(A). Two dice do not affect each other, so they are independent. That is why the multiplication rule turns into a plain multiplication.

"Even" and "4 and up" are not independent. P(even) is 1/2, but once you know the roll is 4 or more, it becomes 2/3. If you multiply anyway, you get it wrong. 1/2 × 1/2 = 1/4, but the real chance of "4 and up, and even" is 2/6.

3. Conditional probability: when you already know something

Conditional probability is the probability when you already know something. The probability of A when you know that B happened is written P(A|B). Whatever sits to the right of the bar | is what you already know.

Knowing something shrinks what you count. Throw away the cases where B did not happen, keep only the cases where it did, and count A inside them. As a formula:

P(A∣B)=P(A∩B)P(B)

Dividing by P(B) is the "keep only B and count again" step. If P(B) is 0, there are no B cases, so it cannot be worked out.

Count with the starting picture of the lab above. Of 1000 people, 60 test positive, and 10 of those 60 have the disease.

P(disease∣positive)=10/100060/1000=1060≈0.167

"Show only positive people" does exactly this. It removes the 940 people who did not test positive and counts inside the 60 who are left. The other way round, P(positive | disease) keeps only the people with the disease and counts the positives among them. That is the sensitivity, 99%.

Try this. ① With the starting values (prevalence 1%, sensitivity 99%, specificity 95%), press "Show only positive people". 60 people are left, and 10 of them have the disease. That is about 16.7%. ② Raise the prevalence to 20%. Now 198 of the 238 positive people have the disease: 83.2%. With the same test, a positive result means more when the disease is common. ③ Set the prevalence back to 1% and raise the specificity to 99%. Now 10 of the 20 positive people have the disease: 50%. For a rare disease, cutting false positives helps a lot. ④ Lower the prevalence to 0. All 50 positive people are false positives, and the chance of disease after a positive result is 0%.

4. Bayes' theorem: updating a belief with evidence

The multiplication rule works in either order.

P(A∩B)=P(A)×P(B∣A)=P(B)×P(A∣B)

Divide the two right-hand sides by P(B) and you get Bayes' theorem.

P(A∣B)=P(A)×P(B∣A)P(B)

Read A as "has the disease" and B as "tested positive".

P(disease∣positive)=P(disease)×P(positive∣disease)P(positive)

In plain words: old belief × chance of this evidence if that belief is true ÷ overall chance of this evidence = belief after seeing the evidence.

To watch this formula being put together one piece at a time on a picture of 1000 people, see How the formula is built just below.

Part of the formulaNameMeaningIn the test example
P(disease)PriorYour belief before the evidencePrevalence 1%
P(positive | disease)LikelihoodThe chance of this evidence if that case is trueSensitivity 99%
P(positive)Overall chance of the evidenceThe chance of a positive result, sick or not, all added up0.0594
P(disease | positive)PosteriorYour belief after the evidenceAbout 16.7%

The bottom part, P(positive), adds up the two ways to get a positive result: being sick and testing positive, or being healthy and testing positive anyway.

P(positive)=P(disease)×P(positive∣disease)+P(no disease)×P(positive∣no disease)

P(positive | no disease) is the share of healthy people who wrongly test positive, so it is 1 − specificity. With the starting values, 0.01 × 0.99 + 0.99 × 0.05 = 0.0099 + 0.0495 = 0.0594.

How the formula is built: Bayes' theorem

Here we put the formula above together one piece at a time, on a picture of 1000 people. At each step one line joins the formula, and the people that line counts get darker in the picture. The numbers are the starting values of the area picture in section 5 (prevalence 10%, sensitivity 90%, specificity 80%). With these values every count is a whole number of people. With the starting values of the first lab (prevalence 1%) you would get 9.9 people, which cannot be drawn.

    Press "Next" to go one step at a time. A line joins the formula on top (the orange background marks the new line), the people it counts get darker, and the reason shows up below. The shapes are the same as in the first lab: circles have the disease, squares do not, and filled means positive. You cannot change the values in this picture. To try other values, use the first lab.

    Written as formulas only, the steps above are:

    P(disease)

    P(disease∩positive)=P(disease)×P(positive∣disease)

    P(no disease∩positive)=P(no disease)×P(positive∣no disease)

    P(positive)=P(disease∩positive)+P(no disease∩positive)

    P(disease∣positive)=P(disease∩positive)P(positive)=P(disease)×P(positive∣disease)P(positive)

    To sum up: the top is the share of people who are sick and positive, and the bottom is the share of all positive people. Both are shares of the same 1000 people, so dividing cancels the 1000 and leaves "the share of positive people who have the disease". In test problems you are usually not given P(positive) directly. You are given the prevalence, sensitivity and specificity. That is why steps ③ and ④ count the two ways to get a positive result and add them.

    5. Seeing it as area

    Bayes' formula is easy to see as area. A square with sides of length 1 has an area of 1. Put every possible case into that area of 1.

    1. Split the width by the prior. The left column is "has the disease" (width P(disease)). The right column is "no disease" (width 1 − P(disease)).
    2. Split each column's height by the conditional probability inside that column. The top is positive, the bottom is negative.
    3. The area of each box = width × height = the chance of being in that group. This is the multiplication rule.
    Share with the disease (prevalence) % Positive if sick (sensitivity) % Negative if healthy (specificity) %

    Drag a slider or type a number. This lab moves on its own, apart from the first lab at the top. When a box is too narrow, its name is left out. The numbers are in the text box.

    The green area (true positive) is the top of the formula, P(disease) × P(positive | disease). Everything inside the black outline, green plus orange (false positive), is the bottom, P(positive). The posterior is the green share of the positive area. The bar under the picture takes just the positive area and lays it out in one line.

    Try this. ① The starting values (prevalence 10%, sensitivity 90%, specificity 80%) give a top of 0.09, a bottom of 0.09 + 0.18 = 0.27, and a posterior of 33.3%. ② Lower the prevalence to 1%. The green column gets as thin as a thread, and the posterior drops to 4.3%. The orange area barely changes, from 0.18 to 0.198. ③ Raise the specificity to 100%. The orange disappears and the posterior becomes 100%. If a test never gives false positives, a positive result always means the disease.

    6. Work it out by hand: count 1000 people

    Take the starting values of the first lab (prevalence 1%, sensitivity 99%, specificity 95%) and turn them into 1000 people. Counts of people are easier to think about than probabilities.

    1. People with the disease. 1000 × 1% = 10 people. The other 990 do not have it.
    2. Results for the 10 sick people. 10 × 99% = 9.9 test positive (true positives) and 0.1 test negative (false negatives). The picture rounds these to 10 and 0.
    3. Results for the 990 healthy people. 990 × 95% = 940.5 test negative (true negatives). The other 990 × 5% = 49.5 test positive (false positives). The picture shows 940 and 50.
    4. All positives: 9.9 + 49.5 = 59.4, about 60 people.
    5. Share of positives who have the disease: 9.9 ÷ 59.4 ≈ 0.167. With the rounded counts, 10 ÷ 60 = 1/6.
    6. Check with the formula: P(disease | positive) = 0.01 × 0.99 ÷ (0.01 × 0.99 + 0.99 × 0.05) = 0.0099 ÷ 0.0594 ≈ 0.167

    The same working, written as formulas. The chance of a positive result without the disease is 1 − specificity.

    P(disease)=0.01,P(positive∣disease)=0.99,P(positive∣no disease)=1−0.95=0.05

    P(disease∩positive)=0.01×0.99=0.0099

    P(no disease∩positive)=0.99×0.05=0.0495

    P(positive)=0.0099+0.0495=0.0594

    P(disease∣positive)=0.00990.0594≈0.167

    The test catches 99% of sick people. Yet about 83% of its positive results are false. Healthy people outnumber sick people 99 to 1, so a small 5% error rate turns into five times as many people (49.5) as the true positives (9.9).

    7. A common mistake: swapping P(A|B) and P(B|A)

    P(positive | disease) = 99% and P(disease | positive) ≈ 16.7% answer different questions. The first counts among sick people. The second counts among positive people. They divide by different things. "The test catches 99% of cases, so a positive result means a 99% chance of disease" swaps the two.

    An everyday example. When it rains, the sky is almost always cloudy. That does not mean it almost always rains when the sky is cloudy. Cloudy days are far more common than rainy days.

    The two are linked by Bayes' formula: P(A|B) ÷ P(B|A) = P(A) ÷ P(B). So you can swap them only when P(A) and P(B) are equal. In the test example, P(disease) = 0.01 and P(positive) = 0.0594, so P(disease | positive) is about 1/6 of P(positive | disease).

    This mistake happens when you forget the prior (the prevalence). In a courtroom, turning "an innocent person would match this evidence by chance only very rarely" into "this person is very unlikely to be innocent" is the same mistake. It is called the prosecutor's fallacy.

    8. Seeing evidence again and again

    The belief you get after one piece of evidence (the posterior) becomes the prior for the next piece. If you take the same test again and get another positive result, you start from 16.7% this time, not 1%, and do the same math.

    Share with the disease (prevalence) % Positive if sick (sensitivity) % Negative if healthy (specificity) %

      Each press of "One more positive" adds a row. The bar length is the posterior at that point. You can go up to six times. If you change a slider, every row is worked out again with the new values.

      As a ratio, it is just one multiplication. Instead of a probability, write the belief as a "disease : no disease" ratio. Each positive result multiplies this ratio by sensitivity ÷ (1 − specificity). That number is called the likelihood ratio. With the starting values it is 0.99 ÷ 0.05 = 19.8. A ratio of a : b is a probability of a ÷ (a + b).

      When"disease : no disease"Probability
      Before the test1 : 991%
      1 positive19.8 : 99 = 1 : 51 ÷ 6 ≈ 16.7%
      2 positives19.8 : 5 = 3.96 : 13.96 ÷ 4.96 ≈ 79.8%
      3 positives78.4 : 178.4 ÷ 79.4 ≈ 98.7%

      The independence assumption. This math treats the two tests as independent. That means: for a person who is sick (or for one who is healthy), the first result does not affect the second. If you give the same person the same test again, whatever fooled the test the first time may still be there and fool it again. Then the second positive tells you less than this math says. That is one reason to confirm with a test that works in a different way.

      Try this. ① At the start you see 1% before the test and 16.7% after one positive. Each press of "One more positive" raises it to 79.8%, then 98.7%, then 99.9%. ② Lower the specificity to 80%. The likelihood ratio drops to 4.95, and even three positives only reach 55.1%. ③ Raise the specificity to 100%. One positive takes it to 100%. ④ Set both sensitivity and specificity to 50%. No matter how many times you press, it stays at 1%. If a test gives a positive result just as often whether you are sick or not, it tells you nothing.

      9. Key properties

      PropertyIn plain wordsWhere to see it
      P(A|B) and P(B|A) are usually differentThey count inside different groups. Their ratio is P(A) ÷ P(B)Section 7
      A small prior makes a positive result hard to trustWhen you look for something rare, false positives easily outnumber true ones. The specificity has to be very highLab 1, ② and ③
      A prior of 0 or 1 never movesIf you start out sure something is impossible (or certain), no evidence can change thatLab 1, ④
      Equal likelihoods leave the belief unchangedIf P(positive | disease) = P(positive | no disease), the result tells you nothingLab in section 8, ④
      Today's posterior is tomorrow's priorUpdating one piece of evidence at a time gives the same answer as using it all at once (when the pieces are independent)Lab in section 8

      Where is it used?

      Common questions

      How is conditional probability different from plain probability?

      Plain probability counts among all the cases. Conditional probability P(A|B) keeps only the cases where B happened and counts A among them. As a formula, it is P(A and B) ÷ P(B).

      A test catches 99% of cases and my result is positive. Why can the chance of disease still be low?

      When a disease is rare, there are far more healthy people than sick people. So even a small share of false positives among the healthy can outnumber the true positives. With a prevalence of 1%, a sensitivity of 99% and a specificity of 95%, only about 17% of the people who test positive have the disease.

      Where does the prior come from?

      From data when you have it. For a medical test, it is the prevalence in the group being tested. For a spam filter, it is the share of spam in the mail you have received so far. Without data, try several values and see how much the answer changes. As long as the prior is not 0 or 1, it matters less and less as evidence piles up.

      Is independent the same as mutually exclusive?

      No. If two events are mutually exclusive, one happening means the other cannot happen. Knowing one tells you about the other, so they are tightly linked. Independent means knowing one does not change the probability of the other. If both have a probability above 0, two mutually exclusive events cannot be independent.

      When are P(A|B) and P(B|A) equal?

      If A and B can happen together (P(A and B) is above 0), only when P(A) and P(B) are equal. That is because Bayes' theorem gives P(A|B) ÷ P(B|A) = P(A) ÷ P(B).

      Words

      Word used hereWord used in textbooks
      how likely, from 0 to 1probability
      something that may or may not happenevent
      A does not happencomplement
      A and Bintersection, written A ∩ B. Its probability is the joint probability
      A or Bunion, written A ∪ B
      the probability of A given Bconditional probability
      knowing one does not change the otherindependent
      cannot both happenmutually exclusive, disjoint
      belief before the evidenceprior probability, prior
      chance of this evidence if that case is truelikelihood
      belief after the evidenceposterior probability, posterior
      overall chance of the evidencemarginal probability, evidence. Adding up the two ways is the law of total probability
      share of people with the diseaseprevalence, base rate
      chance of a positive result if sicksensitivity, true positive rate
      chance of a negative result if healthyspecificity, true negative rate
      chance of disease after a positive resultpositive predictive value (PPV)
      "disease : no disease" ratioodds
      sensitivity ÷ (1 − specificity)positive likelihood ratio
      the mistake of forgetting the priorbase rate neglect, base rate fallacy
      a Bayes classifier that treats words as independentnaive Bayes

      Sources

      ← Jacobian and HessianBinomial and Poisson distributions →