Beginner: data foundations · Probability and inference · topic 12 of 62 · 30 min
Conditional probability and Bayes' theorem
Explanation
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).
emails_with_lottery = 50 # B happened
spam_with_lottery = 45 # A and B happened
print(spam_with_lottery / emails_with_lottery)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 positive | Test negative | Total | |
|---|---|---|---|
| Has the disease | 99 | 1 | 100 |
| Healthy | 495 | 9,405 | 9,900 |
| Total | 594 | 9,406 | 10,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.
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))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
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.
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))Practice (0/3 done)
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.
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Random variables and distributionsProbability and inference · same module
- Samples, the central limit theorem and confidence intervalsProbability and inference · same module
- Logistic regression and classificationThe ML workflow, regression and classification
- If I were you: imagining and advice (second conditional)Spoken English: Zero to Fluent · Grammar for real talk
- NoSQL: documents, key-value, wide-column and graphSQL and Databases · Procedures, triggers, SQL engines and NoSQL
- If sentences: zero and first conditionalSpoken English: Zero to Fluent · Polite and useful grammar
Questions people ask about this
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
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.
A game pays ₹50 with probability 0.2 and ₹0 otherwise. What is the expected value?Related
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
- 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.
- Module 3 exam: Probability and inference · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Cleaning, exploring and visualising data. 62 topics left in the course before the final exam.