Sub topic 10
Total Probability and Bayes' Theorem
What this chapter assumes
You should already be comfortable with these ideas from earlier chapters:
- Classical probability (favourable over total).
- The complement rule.
- Conditional probability, P(A | B).
- The multiplication rule, P(A and B) = P(B) × P(A | B).
- Independence.
This chapter turns those pieces into one reusable method for two-stage problems. In a two-stage problem, something is chosen first (a bag, a machine, a patient group), and then an outcome is observed (a colour, a defect, a test result). CAT asks two kinds of questions about such setups:
- Forward: what is the chance of the final outcome?
- Backward: given the outcome, which first-stage choice most likely produced it?
The forward question uses the law of total probability. The backward question uses Bayes' theorem.
Partitions: the condition that makes it work
A set of events B₁, B₂, …, Bₙ is a partition of the sample space if two things hold:
- They are mutually exclusive: no two can happen together.
- They are exhaustive: at least one of them must happen.
Together, their probabilities add up to 1. Examples are "which of three machines made this item" and "which bag was picked." "Has the disease" and "does not have the disease" also form a partition.
If the events are not a partition, the formulas below give wrong answers. Checking that the priors sum to 1 is the quickest sanity test.
The law of total probability
Suppose you want P(A), and A can occur only through the partition events. Then:
P(A) = P(B₁)·P(A | B₁) + P(B₂)·P(A | B₂) + … + P(Bₙ)·P(A | Bₙ)
Each term is the probability of one path: reach Bᵢ, then produce A. You add the paths because they are mutually exclusive.
Worked example: two bags
Bag 1 holds 2 red and 3 blue balls. Bag 2 holds 3 red and 1 blue ball. A fair coin picks the bag, and then one ball is drawn. What is P(red)?
- Path through bag 1: 1/2 × 2/5 = 1/5.
- Path through bag 2: 1/2 × 3/4 = 3/8.
- P(red) = 1/5 + 3/8 = 8/40 + 15/40 = 23/40.
Read the tree like this. The first-stage branch labels are the priors, P(Bᵢ). The second-stage labels are the conditional probabilities, P(A | Bᵢ). Multiplying along a path gives the probability of that whole path. The four leaf values sum to 1: 1/5 + 3/10 + 3/8 + 1/8 = 8/40 + 12/40 + 15/40 + 5/40 = 40/40.
Bayes' theorem: reversing the condition
Now ask the backward question. A red ball was drawn. What is the probability it came from bag 1?
By the definition of conditional probability:
P(B₁ | A) = P(B₁ and A) / P(A)
The numerator is one path from the tree. The denominator is the total probability of A, which is the sum of all paths that end in A. Written out, this is Bayes' theorem:
P(Bᵢ | A) = P(Bᵢ)·P(A | Bᵢ) / [ Σ P(Bⱼ)·P(A | Bⱼ) ]
For the bags:
P(bag 1 | red) = (1/5) / (23/40) = 8/23.
So P(bag 2 | red) = 15/23, and the two posteriors sum to 1.
from fractions import Fraction
via_bag1 = Fraction(1, 2) * Fraction(2, 5)
via_bag2 = Fraction(1, 2) * Fraction(3, 4)
total_red = via_bag1 + via_bag2
print(total_red) # 23/40
print(via_bag1 / total_red) # 8/23
print(via_bag2 / total_red) # 15/23The exam shortcut
You rarely need to write the formula in full. Use this routine:
- Draw the first-stage branches and write the priors.
- Multiply down each branch that ends in the observed outcome.
- The answer is the wanted path divided by the sum of all paths that end in that outcome.
The denominator is always the sum of the numerators. If your posteriors do not add to 1, you have made an arithmetic slip.
More than two branches
The method does not change with three or more first-stage events. A factory has three machines:
| Machine | Share of output | Defect rate |
|---|---|---|
| A | 50% | 2% |
| B | 30% | 3% |
| C | 20% | 5% |
An item is found defective. Which machine most likely made it?
- A: 0.50 × 0.02 = 0.010
- B: 0.30 × 0.03 = 0.009
- C: 0.20 × 0.05 = 0.010
P(defective) = 0.029. The posteriors are 10/29 for A, 9/29 for B and 10/29 for C. Machine C makes only 20% of the items, but it accounts for as large a share of the defects as machine A. A high defect rate raises the posterior even when the prior is small.
This computation can be written as a small function that works for any partition:
from fractions import Fraction
def bayes(priors, likelihoods):
joint = [p * l for p, l in zip(priors, likelihoods)]
total = sum(joint)
return total, [j / total for j in joint]
priors = [Fraction(1, 2), Fraction(3, 10), Fraction(1, 5)]
likes = [Fraction(2, 100), Fraction(3, 100), Fraction(5, 100)]
total, posterior = bayes(priors, likes)
print(total) # 29/1000
print([str(p) for p in posterior]) # ['10/29', '9/29', '10/29']The base-rate trap
The most common conceptual error is confusing P(A | B) with P(B | A). A medical-test question shows the difference.
A disease affects 2% of a population. A test is positive for 95% of people who have the disease. It is also positive for 5% of people who do not. A person tests positive. What is the probability they have the disease?
- Sick and positive: 0.02 × 0.95 = 0.019.
- Healthy and positive: 0.98 × 0.05 = 0.049.
- P(positive) = 0.068.
- P(disease | positive) = 0.019 / 0.068 = 19/68, about 0.28.
Many people expect an answer near 95%. The test is accurate, but healthy people vastly outnumber sick people. Their 5% false-positive rate therefore produces more positives than the true cases do.
A reliable check is to use natural frequencies. Imagine 10,000 people:
- 200 are sick, and 190 of them test positive.
- 9,800 are healthy, and 490 of them test positive.
- Among 680 positives, 190 are sick. 190/680 = 19/68.
Problems where the first stage changes the second
Some CAT problems do not pick a bag. They draw an item and move it, then draw again. The first draw outcome is the partition.
Bag 1 has 3 red and 2 blue balls. Bag 2 has 2 red and 2 blue balls. One ball moves from bag 1 to bag 2 at random. Then a ball is drawn from bag 2. What is P(red)?
- If the moved ball is red (probability 3/5), bag 2 has 3 red out of 5. The path gives 3/5 × 3/5 = 9/25.
- If the moved ball is blue (probability 2/5), bag 2 has 2 red out of 5. The path gives 2/5 × 2/5 = 4/25.
P(red) = 13/25.
The same structure answers the backward question. Given that the second ball is red, P(first ball was red) = (9/25) / (13/25) = 9/13.
Common errors
| Error | What goes wrong | Fix | Column | Column |
|---|---|---|---|---|
| Using the prior as the answer | Ignores the observed outcome | Divide the wanted path by all paths to the outcome | ||
| Swapping P(A \ | B) and P(B \ | A) | Reports the likelihood as the posterior | Ask which event is "given" in the question |
| Forgetting a branch in the denominator | Posteriors sum to more than 1 | List every first-stage event, including ones with a zero-looking contribution | ||
| Treating non-exhaustive events as a partition | Priors sum to less than 1 | Add a "none of these" branch, or re-read the problem | ||
| Assuming equal priors | Wrong weights | Use equal priors only when the problem says the choice is random and fair | ||
| Reusing the original proportions after the first draw | Ignores how the first stage changes the second | Recompute the second-stage contents for each branch |
When to reach for this method
Use total probability or Bayes' theorem when you see these signals:
- Something is selected first (a bag, machine, person, coin, or die number), and an outcome follows.
- The wording says "given that," "if it is found that," or "what is the probability that it came from."
- Different first-stage choices have different success probabilities.
If the problem has a single stage with equally likely outcomes, plain counting is faster. If the stages are not linked, so that the first outcome does not change the second, the multiplication rule alone is enough.
Exercise
A fair die is rolled. If it shows 1 or 2, a ball is drawn from Urn A (4 white, 6 black). Otherwise a ball is drawn from Urn B (7 white, 3 black). (a) Find the probability that the ball is white. (b) Given that the ball is white, find the probability that it came from Urn A.
Show answer
(a) P(white) = 1/3 × 2/5 + 2/3 × 7/10 = 2/15 + 7/15 = 9/15 = 3/5. (b) P(A | white) = (2/15) / (3/5) = 2/9.
Exercise
In a town, 40% of vehicles are two-wheelers, 50% are cars and 10% are trucks. The probabilities that a vehicle of each type is over-speeding are 10%, 20% and 30% respectively. A vehicle is found to be over-speeding. What is the probability that it is a car?
Show answer
The paths are 0.4 × 0.1 = 0.04, 0.5 × 0.2 = 0.10 and 0.1 × 0.3 = 0.03. Their sum is 0.17. P(car | over-speeding) = 0.10 / 0.17 = 10/17.
Exercise
A box has 3 red and 2 blue balls. One ball is drawn at random and put into a second box that holds 2 red and 2 blue balls. A ball is then drawn from the second box. It is red. What is the probability that the transferred ball was red?
Show answer
Transferred red: 3/5 × 3/5 = 9/25. Transferred blue: 2/5 × 2/5 = 4/25. P(red) = 13/25. P(transferred red | second red) = (9/25) / (13/25) = 9/13.