Skip to content

Beginner: data foundations · Probability and inference · topic 12 of 62 · 30 min

Conditional probability and Bayes' theorem

Explanation

Conditional probability is the chance of A when you already know B happened, written P(A | B). Bayes' theorem turns P(B | A) around into P(A | B). It explains why a positive result from a good test can still be more likely wrong than right when the condition is rare.

Why "given" matters

The chance that a random email is spam might be 0.2. But if the email contains the words "you won a lottery", the chance is much higher. New information changes probabilities. Conditional probability is how we update a chance after we learn something.

The definition

P(A | B) is read "the probability of A given B". It means: look only at the cases where B happened, and ask how often A also happened. The formula is P(A | B) = P(A and B) ÷ P(B). With counts it is even simpler: (count of A and B) ÷ (count of B).

python
emails_with_lottery = 50          # B happened
spam_with_lottery = 45            # A and B happened
print(spam_with_lottery / emails_with_lottery)
P(spam | "lottery") = 45 ÷ 50 = 0.9.
Careful: P(A | B) is not the same as P(B | A). Most cricketers are good runners, but most good runners are not cricketers. Mixing the two up is one of the most common errors in reading data.

The 2 × 2 table method

Imagine 10,000 people are tested for a disease. 1% have it (the base rate, also called the prior). The test catches 99% of sick people (its sensitivity) but also wrongly flags 5% of healthy people (the false positive rate). Fill a table with whole people instead of percentages.

Test positiveTest negativeTotal
Has the disease991100
Healthy4959,4059,900
Total5949,40610,000

Of the 594 people who test positive, only 99 are sick. So P(sick | positive) = 99 ÷ 594 ≈ 0.167, about 17%. The test is good, but the disease is rare, so most positives come from the huge healthy group. This surprise is called the base rate fallacy.

Bayes' theorem

Bayes' theorem writes the same table as a formula: P(A | B) = P(B | A) × P(A) ÷ P(B). Here A is "sick" and B is "positive". The bottom part, P(B), is every way to get a positive: sick and positive, plus healthy and positive.

python
def bayes(prior, sensitivity, false_positive_rate):
    true_pos = prior * sensitivity
    false_pos = (1 - prior) * false_positive_rate
    return true_pos / (true_pos + false_pos)

print(round(bayes(0.01, 0.99, 0.05), 4))
Shows 0.1667, the same answer as the table.
Think of it like this: Bayes is how a good doctor thinks. A cough alone does not mean tuberculosis, because TB is less common than a cold. But a cough plus weight loss plus contact with a TB patient pushes the chance up step by step. Each new clue updates the earlier belief.

Where data scientists use it

  • Spam filters: the Naive Bayes classifier combines P(word | spam) for many words.
  • Medical and fraud alerts: deciding how many alerts are true and how many staff are needed to check them.
  • A/B tests and Bayesian statistics: updating a belief about which version is better as data comes in.

Common mistakes

  • Ignoring the base rate. Fix: always ask "how common is this to begin with?" and use the 2 × 2 table.
  • Swapping P(A | B) and P(B | A). Fix: say the sentence aloud: "out of the people who tested positive, how many are sick?"
  • Dividing by the wrong total. Fix: in P(A | B), the bottom is always the count of B.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Conditional probability and Bayes' theorem · 1/7

Conditional probability

This lesson is about updating chances when you learn something new.

Prefer a video by a teacher? Find free videos on “bayes theorem explained medical test base rate fallacy” on YouTube ↗

In real life: How many fraud alerts are real?

A bank's fraud model flags 98% of real fraud and wrongly flags 2% of normal payments. Fraud is rare: about 1 in 1,000 payments. Bayes says only about 4.7% of flagged payments are real fraud. That is why banks send a quick "Was this you?" message instead of blocking the card at once; most alerts are false alarms, and the check is cheap.

python
prior = 0.001
sensitivity = 0.98
fpr = 0.02
true_pos = prior * sensitivity
false_pos = (1 - prior) * fpr
print(round(true_pos / (true_pos + false_pos), 3))
Shows 0.047, so roughly 1 alert in 21 is real fraud.

Practice (0/3 done)

Task 1 · Code · Python

Write cond_prob(count_a_and_b, count_b) that returns P(A | B) from counts, rounded to 4 decimal places. If count_b is 0, return 0.0. Example: cond_prob(45, 50) returns 0.9.

Task 2 · Code · Python

Write bayes(prior, sensitivity, false_positive_rate) that returns P(condition | positive test), rounded to 4 decimal places. prior is how common the condition is, sensitivity is P(positive | condition), false_positive_rate is P(positive | no condition). If nobody can test positive (the denominator is 0), return 0.0. Example: bayes(0.01, 0.99, 0.05) returns 0.1667.

Task 3 · Practice

A school screening test for poor eyesight finds 90% of children who need glasses, and wrongly flags 10% of children who do not. 5% of children need glasses. Out of 2,000 children, fill a 2 × 2 table and work out how many flagged children really need glasses. Then write one sentence for the principal explaining the result.

Quick quiz

Question 1

1.What does P(A | B) mean?

Question 2

2.Of 200 customers who opened a promo email, 30 bought something. What is P(bought | opened)?

Question 3

3.A test for a rare condition is 99% sensitive. Why can most positive results still be false?

Question 4

4.In Bayes' theorem, what is the prior?

Question 5

5.Which statement is TRUE?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What does P(A | B) mean?

Answer: The chance of A, looking only at cases where B happened

The bar means "given": we restrict to cases where B happened.

Of 200 customers who opened a promo email, 30 bought something. What is P(bought | opened)?

Answer: 0.15

30 ÷ 200 = 0.15. The bottom is the count of the "given" group.

A test for a rare condition is 99% sensitive. Why can most positive results still be false?

Answer: Because the healthy group is so large that even a small false positive rate gives many false alarms

This is the base rate fallacy: a small rate times a huge group can beat a large rate times a tiny group.

In Bayes' theorem, what is the prior?

Answer: How common the condition is before you see the evidence

The prior is the starting belief, the base rate, before the new evidence is used.

Which statement is TRUE?

Answer: P(A | B) = P(A and B) ÷ P(B)

That is the definition. The first statement is the classic mix-up; the third holds only for independent events.

The chance of rain tomorrow is 0.35. What is the chance of no rain?Related

Answer: 0.65

Complement rule: 1 − 0.35 = 0.65.

From Probability basics

What does the central limit theorem say?Related

Answer: Averages of reasonably large samples are roughly normally distributed

It is about sample averages, not raw data. Raw data can stay skewed.

From Samples, the central limit theorem and confidence intervals

A hospital wants to miss as few sick patients as possible. Compared with 0.5, the threshold should usually be…Related

Answer: Lower

A lower threshold flags more people, catching more true cases at the cost of more false alarms.

From Logistic regression and classification

A game pays ₹50 with probability 0.2 and ₹0 otherwise. What is the expected value?Related

Answer: ₹10

50 × 0.2 + 0 × 0.8 = 10.

From Random variables and distributions

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “Probability and inference” before its exam: Random variables and distributions, Samples, the central limit theorem and confidence intervals, Hypothesis tests, p-values and A/B testing.
  2. Module 3 exam: Probability and inference · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Cleaning, exploring and visualising data. 62 topics left in the course before the final exam.

← Previous: Probability basics