Part 1: Conditional Probability
▶ Part 1: Conditional Probability
▶ Part 3: Bayes' Theorem in AI
This part builds the intuition behind Bayes' theorem, starting from the conditional probability concepts developed in Part 1.
The reader is expected to be familiar with basic probability concepts, conditional probability, independent and dependent events, and the difference between \(P(A|B)\) and \(P(B|A)\).
1. Bayes' Theorem: Turning evidence into belief
We already know how conditional probability works.
means:
Given that B has happened, what is the probability of A?
But sometimes we know:
and want to find:
In other words, we want to reverse the direction of the conditional probability.
That is the basic idea behind Bayes' theorem:
Bayes gives us a way to move from \(P(A \mid B)\) to \(P(B \mid A)\), and vice versa.
The experiment
Let's make this concrete.
We have two boxes:
The experiment happens in two steps:
Step 1: We randomly choose one of the two boxes.
Step 2: From the chosen box, we randomly pick one ball.
So the process is:
Now consider Box B.
Direction 1: Box → Ball
Suppose I tell you:
"Box B was chosen."
Now we pick a ball from Box B.
What is the probability that the ball is red?
Box B contains:
So:
We know the box, and we are predicting something about the ball.
This is:
Direction 2: Ball → Box
Now run the same experiment again.
A box is chosen.
A ball is picked.
But this time I tell you:
"The ball picked was red."
Now I ask:
What is the probability that Box B was chosen?
This is:
We know something about the ball, and we are trying to infer something about the box.
This is:
The reversal
The experiment itself has not changed.
What changes is the direction of the question.
The first question is:
If I know the box, what can I say about the ball?
The second question is:
If I know the ball, what can I say about the box?
The second question is often the interesting one.
We observe an outcome and want to work backwards to the possible explanation.
That is the basic problem Bayes' theorem solves.
A Worked Example
| Box | Red Balls | Blue Balls | Total Balls |
|---|---|---|---|
| Box A | 9 | 1 | 10 |
| Box B | 2 | 8 | 10 |
Each box is equally likely to be selected:
We will use these numbers in both examples.
Example 1: Find Probability of Red Ball When Box B Is Selected
Suppose Box B is selected.
Box B contains:
2 red balls
8 blue balls
10 balls total
We want to find:
Using the definition of conditional probability:
Substitute the values:
Therefore:
Example 2: Find Probability of Box B When the Red Ball Is Picked
Suppose the ball picked was red.
We want to find:
From the definition of conditional probability:
We can write the joint probability as:
Therefore:
This is Bayes' theorem:
Now we calculate each part.
From the table:
and:
We also need \(P(\text{Red})\).
A red ball can come from either Box A or Box B:
Substitute the values:
Now apply Bayes' theorem:
Therefore:
So we have converted:
into:
using Bayes' theorem.
2. Another Example: An Imperfect Test
Let's continue with the mammogram example.
Suppose:
| Cancer | No Cancer | |
|---|---|---|
| Probability | 1% | 99% |
| Positive test | 80% | 9.6% |
| Negative test | 20% | 90.4% |
Now suppose the test is positive.
There are two ways this can happen:
True positive
For a true positive, two things must happen:
- The person has cancer.
- The test is positive given that the person has cancer.
Therefore:
False positive
For a false positive, two things must happen:
- The person does not have cancer.
- The test is positive given that the person does not have cancer.
Therefore:
All positive results
A positive result can come from either group.
So:
Now we can finally ask the question we actually care about:
Among all the positive results, what proportion are from people who have cancer?
So although the test is positive 80% of the time when cancer is present, a positive result corresponds to only about a 7.8% probability of cancer in this example.
The difference comes from the fact that positive results can come from both groups, and cancer is relatively uncommon.
3. From the Calculation to Bayes' Theorem
From the previous section:
We also know:
Therefore:
This is Bayes' theorem in the specific case of the mammogram.
Now replace the specific terms with \(A\) and \(B\):
Or:
Notice what Bayes is doing.
We start with:
and use it to calculate:
But we also need:
and:
The important point is that Bayes is not a new probability concept.
It is the conditional probability relationship rearranged so that we can calculate the probability in the direction we want.
4. Prior, Likelihood and Posterior
Now we can give names to the quantities in Bayes' theorem:
| Term | Name | What does it represent? |
|---|---|---|
| \(P(A)\) | Prior | Probability of A before seeing B |
| \(P(B\mid A)\) | Likelihood | Probability of seeing B if A is true |
| \(P(B)\) | Evidence | Probability of seeing B overall |
| \(P(A\mid B)\) | Posterior | Probability of A after seeing B |
For the mammogram example:
| Term | Name | What does it represent? |
|---|---|---|
| \(P(\text{Cancer})\) | Prior | Probability of cancer before the test result |
| \(P(\text{Positive}\mid\text{Cancer})\) | Likelihood | Probability of a positive test if cancer is present |
| \(P(\text{Positive})\) | Evidence | Probability of a positive test overall |
| \(P(\text{Cancer}\mid\text{Positive})\) | Posterior | Probability of cancer after a positive test |
The key picture is:
with the Likelihood telling us how strongly the evidence fits the situation we are considering.
5. Putting Bayes to Work
A patient gets a positive mammogram.
The question is:
Given this positive result, what is the probability that the patient has cancer?
We know:
| Term | Name | Probability |
|---|---|---|
| \(P(\text{Cancer})\) | Prior | 1% |
| \(P(\text{Positive}\mid\text{Cancer})\) | Likelihood | 80% |
| \(P(\text{Positive}\mid\text{No Cancer})\) | False positive rate | 9.6% |
Step 1: Start with the prior
Before seeing the test result:
So the starting probability is 1%.
Step 2: Look at the positive result
The test is positive.
But a positive result can happen in both situations:
Step 3: Calculate the true positive probability
The probability of:
Cancer and positive test
is:
Step 4: Calculate the false positive probability
The probability of:
No cancer and positive test
is:
Step 5: Find all positive results
A positive result can come from either group:
Step 6: Among positive results, find the proportion with cancer
Therefore:
What happened?
We started with:
Then we received new evidence:
Positive mammogram
After accounting for how the test behaves, our probability became:
So Bayes is a way of updating a probability when new evidence arrives.
The important part is not just the final 7.8%.
It is the process:
Start with what we know
↓
Observe new evidence
↓
Consider how that evidence can occur
↓
Update the probability
6. Base Rate: Why the Starting Point Matters
The mammogram example reveals something important.
The test result was the same, but the final probability depends heavily on where we started.
The prior was:
That 1% is the base rate: how common the disease is before we see the test result.
Change the base rate
Suppose the same test is used in a population where cancer is much more common:
Keep the same test:
The test has not changed.
But the answer will change because the starting probability changed.
Using Bayes:
So:
| Scenario | Prior: \(P(\text{Cancer})\) | Posterior: \(P(\text{Cancer}\mid\text{Positive})\) |
|---|---|---|
| Current example: Original mammogram example | 1% | 7.8% |
| Another example: Another population, with a higher cancer rate | 20% | 67.6% |
Same test.
Same positive result.
Very different answer.
The reason is simple:
Evidence does not act on a blank slate. It updates what we already believed.
That starting belief is the base rate, and Bayes forces us to take it into account.
7. Why Base Rates Matter
The previous example gives us an important lesson.
A positive test result does not have a fixed meaning.
Its meaning depends partly on how common the condition was before the test.
In the first example:
After a positive test:
In the second scenario, the starting probability was higher:
With the same test and the same positive result:
So:
This starting probability is called the base rate.
The common mistake is to look only at the test result and ignore the base rate.
Bayes does not make that mistake.
It combines:
to produce: