Bayes' Theorem Examples: 45 Problems with Answers

Worked examples of Bayes' Theorem to try before you open each solution, then practice problems with answers.

Worked Examples

Example 1

medium
Email spam filter: P(spam)=0.3. The word 'free' appears in 80% of spam emails and 10% of legitimate emails. An email contains 'free'. Find P(spam∣free) using Bayes' theorem.
Show solution

Answer

P(spam∣free)≈0.774. There's a 77.4% chance the email is spam.

First step

1.
Prior: P(spam)=0.3, P(legit)=0.7

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Free: full solutions on 3 topics a day. No card.

Every topic, every day: the Family plan

Example 2

hard
Drug testing: P(user)=0.05. Test sensitivity P(+∣user)=0.99. Specificity P(−∣non-user)=0.95 (so P(+∣non-user)=0.05). Find P(user∣+).

Example 3

medium
A box contains 40% red and 60% blue marbles. Red marbles are 'shiny' 30% of the time; blue marbles are shiny 10% of the time. A drawn marble is shiny. Find P(red∣shiny).

Example 4

medium
A communication channel sends 0 with probability 0.6 and 1 with probability 0.4. Each bit is flipped with probability 0.1. The receiver sees 1. Find P(sent 1∣received 1).

Example 5

medium
A store has 3 suppliers A (50%), B (30%), C (20%). Defect rates: 2%, 4%, 5%. A randomly chosen item is defective. Find P(B∣defective).

Example 6

hard
You have three biased coins with P(H)=0.2,0.5,0.9 chosen uniformly at random, flipped once, lands heads. Find the posterior probability that the chosen coin has P(H)=0.9.

Example 7

challenge
You suspect a coin is biased toward heads. Prior: P(biased)=0.1 with P(H∣biased)=0.75; otherwise fair. You observe 8 heads in 10 flips. Find the posterior probability the coin is biased.

Practice Problems

Try each one, then open the solution to compare your method.

Example 1

easy
Write out Bayes' theorem and explain each component: P(A∣B)=P(B∣A)P(A)P(B).

Example 2

hard
A coin is either fair (p=0.5, probability 0.7) or biased (p=0.8, probability 0.3). You flip it once and get heads. Update the probability that the coin is biased using Bayes' theorem.

Example 3

easy
State Bayes' theorem for P(H∣E).

Example 4

easy
Are P(A∣B) and P(B∣A) generally equal?

Example 5

easy
Given P(H)=0.5, P(E∣H)=0.8, and P(E)=0.4, find P(H∣E).

Example 6

easy
If the prior P(H)=0, what is the posterior P(H∣E) (for any evidence with P(E)>0)?

Example 7

easy
Compute P(E) by the law of total probability if P(E∣H)=0.6, P(H)=0.5, P(E∣Hc)=0.2, P(Hc)=0.5.

Example 8

easy
In Bayes' theorem, which term is the 'prior'?

Example 9

easy
Which term, P(E∣H) or P(H∣E), is the 'likelihood' in Bayes' theorem?

Example 10

easy
A disease has prior P(D)=0.01. A 99%-accurate test still yields many false positives mainly because of what?

Example 11

medium
A test is 90% sensitive (P(+∣D)=0.9) and the disease prior is P(D)=0.2. Also P(+∣Dc)=0.1. Find P(D∣+).

Example 12

medium
Prior P(H)=0.3, P(E∣H)=0.5, P(E∣Hc)=0.25. Find P(H∣E).

Example 13

medium
Two factories: A makes 60% of parts with a 5% defect rate; B makes 40% with a 10% defect rate. A part is defective. Find P(A∣defective).

Example 14

medium
A spam filter: 40% of email is spam. 'Free' appears in 80% of spam and 10% of non-spam. An email contains 'free'. Find P(spam∣free).

Example 15

medium
A box has 70% fair coins (P(H)=0.5) and 30% biased coins (P(H)=0.9). A drawn coin flips heads. Find P(biased∣H).

Example 16

medium
Prior odds of H to Hc are 1:3. The likelihood ratio P(E∣H)P(E∣Hc)=6. Find the posterior odds, then P(H∣E).

Example 17

medium
A rare disease has prior 0.001. The test is 99% sensitive (P(+∣D)=0.99) and 95% specific (P(+∣Dc)=0.05). Find P(D∣+).

Example 18

medium
Using the law of total probability, find P(E) where H1,H2,H3 partition the space: priors 0.2,0.3,0.5 and P(E∣Hi)=0.1,0.4,0.6.

Example 19

challenge
With three hypotheses, priors 0.2,0.3,0.5 and likelihoods 0.1,0.4,0.6 (so P(E)=0.44), find P(H2∣E).

Example 20

challenge
A test on a disease with prior 0.04 has P(+∣D)=0.95 and P(+∣Dc)=0.1. A patient tests positive TWICE (independent tests). Find P(D∣++).

Example 21

challenge
Prior P(H)=0.5. Evidence E has P(E∣H)=0.3 and P(E∣Hc)=0.6. After observing E, is H more or less likely than before? Compute P(H∣E).

Example 22

medium
A coin is fair with prior 0.5 or two-headed with prior 0.5. It is flipped once and lands heads. Find P(two-headed∣H).

Example 23

easy
Given P(H)=0.25, P(E∣H)=0.8, P(E)=0.5. Find P(H∣E).

Example 24

easy
If P(H∣E)=0.6 and P(E)=0.2 and P(H)=0.3, find P(E∣H).

Example 25

medium
A weather model says P(rain)=0.2. If it rains, the forecaster predicts rain 90% of the time; if it doesn't, the forecaster predicts rain 20% of the time. The forecaster predicts rain. Find P(rain∣predicted rain).

Example 26

medium
At a school, 30% of students play sports. Among sport-players, 70% own gym shoes; among non-players, 20% own gym shoes. A student owns gym shoes. Find P(plays sports∣gym shoes).

Example 27

medium
A jar has 3 fair coins and 1 two-headed coin. A random coin is flipped and lands heads. Find P(two-headed∣H).

Example 28

medium
An urn has 3 fair coins and 1 two-headed coin. A random coin is flipped TWICE and lands heads both times. Find P(two-headed∣HH).

Example 29

medium
A patient's prior probability of disease is 10%. A test has sensitivity 80% and specificity 80%. Find P(D∣+).

Example 30

medium
Express Bayes' theorem in odds form: the posterior odds of H given E equal the prior odds times the ____.

Example 31

medium
Prior odds for spam vs not-spam are 1:3. The word 'lottery' has likelihood ratio 9 for spam. Find the posterior odds and P(spam∣lottery).

Example 32

medium
In the previous question, find P(A∣defective).

Example 33

hard
A disease affects 1 in 1000. A test has sensitivity 99% and specificity 99%. Find P(D∣+).

Example 34

hard
Continuing the previous problem: after one positive test, the prior becomes ≈0.0902. A second independent test (same sensitivity/specificity) also returns positive. Find the updated posterior.

Example 35

hard
A taxi is in a hit-and-run. 85% of city cabs are Green, 15% Blue. A witness identifies a Blue cab and is right 80% of the time. Find P(actually Blue∣witness says Blue).

Example 36

hard
Two hypotheses have prior odds 1:2 and the evidence has likelihood ratio 5 for H1 over H2. After observing the evidence twice (independent), find the posterior probability of H1.

Example 37

hard
A factory has 4 machines producing equal shares; defect rates 1%, 2%, 3%, 4%. A defective is found. What is P(machine 4∣defective)?

Example 38

challenge
A genetic test for a recessive trait has sensitivity 98%, specificity 97%. Prevalence is 0.5%. After ONE positive, you re-test using a different independent test of sensitivity 95%, specificity 99%, also positive. Find P(carrier∣+,+).

What to focus on

Core idea: Bayes' theorem flips a conditional, turning P(E∣H) and a prior P(H) into the posterior P(H∣E).

Common stuck point: The procedure for bayes' theorem is the easy part; the trap is treating P(H∣E) as equal to P(E∣H). Asking "Am I given P(E∣H) and a prior, and asked for the flipped P(H∣E)?" first is what keeps a correct-looking calculation from being attached to the wrong concept.

Ask yourself: Am I given P(E∣H) and a prior, and asked for the flipped P(H∣E)?

Concept recap

Bayes' theorem gives the posterior probability of a hypothesis given evidence: P(H∣E)=P(E∣H)⋅P(H)P(E).

Bayes works like a detective weighing suspects. Before any clue, some suspects are more likely than others: the prior. A clue such as muddy boots fits some suspects better than others: the likelihood. Multiply the two for each suspect and rescale so the chances add to 1. A clue that fits a rare suspect perfectly can still point to a common suspect it fits less well.

Read the full lesson on Bayes' Theorem

Background knowledge

These ideas may be useful before you work through the harder examples.

conditional probabilityprobabilitysample space