Sub topic 14
Updating beliefs with Bayes' formula
This chapter assumes you know the addition and multiplication rules, conditional probability, and the total probability rule from the previous chapters. Bayes' formula answers one question: after you see new evidence, how should you revise a probability you held before?
The idea: reversing a conditional probability
You often know how likely the evidence is when a hypothesis is true. You usually want the opposite: how likely the hypothesis is, given that you saw the evidence. These are different numbers.
- P(B|A) is the probability of the evidence B if event A is true. This is often called the likelihood.
- P(A|B) is the probability of A once you have seen B. This is the updated, or posterior, probability.
- P(A) is the prior probability, held before seeing the evidence.
Bayes' formula links them:
P(A|B) = P(B|A) × P(A) / P(B)
The denominator P(B) is the unconditional probability of the evidence. You get it from the total probability rule, summing over every way B can happen:
P(B) = P(B|A) × P(A) + P(B|not A) × P(not A)
Bayes' formula is the multiplication rule written two ways. P(A and B) equals P(A|B) × P(B) and also P(B|A) × P(A). Setting them equal and dividing by P(B) gives the formula.
A worked example: a screening test
Suppose 1% of firms in a sample are fraudulent. A screening model flags 90% of fraudulent firms (P(flag|fraud) = 0.90). It also wrongly flags 5% of honest firms (P(flag|honest) = 0.05). A firm is flagged. What is the probability it is fraudulent?
- Prior: P(fraud) = 0.01, so P(honest) = 0.99.
- Probability of a flag: 0.90 × 0.01 + 0.05 × 0.99 = 0.009 + 0.0495 = 0.0585.
- Posterior: 0.009 / 0.0585 ≈ 0.1538.
# Bayes' formula: P(A|B) = P(B|A) * P(A) / P(B)
prior = 0.01
p_b_given_a = 0.90
p_b_given_not_a = 0.05
p_b = p_b_given_a * prior + p_b_given_not_a * (1 - prior) # total probability rule
posterior = p_b_given_a * prior / p_b
print(round(p_b, 4)) # 0.0585
print(round(posterior, 4)) # 0.1538A flag from a model that catches 90% of fraud still leaves only a 15.4% chance of fraud. Most flagged firms are honest, because honest firms are so numerous that even a 5% false-alarm rate produces more flags than the rare fraudulent firms do.
Seeing it as a tree and as counts
The tree below shows the two branches that lead to a flag. Bayes' formula takes the fraud branch's share of all flagged outcomes.
A second way to check the answer is to use counts. Imagine 10,000 firms.
| Group | Firms | Flagged |
|---|---|---|
| Fraudulent (1%) | 100 | 90 |
| Honest (99%) | 9,900 | 495 |
| Total | 10,000 | 585 |
Of 585 flagged firms, 90 are fraudulent: 90 / 585 ≈ 15.4%. This matches the formula. On the exam, drawing the tree or the count table is a reliable way to avoid mixing up the conditional probabilities.
A finance example: analyst upgrades
Suppose 30% of stocks in a universe go on to outperform the market. Among outperformers, 60% received an analyst upgrade beforehand. Among non-outperformers, 20% did. A stock has just been upgraded. What is the probability it outperforms?
- Prior: P(outperform) = 0.30.
- P(upgrade) = 0.60 × 0.30 + 0.20 × 0.70 = 0.18 + 0.14 = 0.32.
- Posterior: P(outperform | upgrade) = 0.18 / 0.32 = 0.5625.
The upgrade raises the probability from 30% to about 56%. The evidence helps because the likelihood ratio, 0.60 / 0.20 = 3, is well above 1.
Updating more than once
Each posterior can serve as the prior for the next piece of evidence. This works if the pieces of evidence are conditionally independent, meaning that once you know whether A is true, one result tells you nothing about the other. Suppose a second, independent test of the same quality also flags the firm.
def bayes_update(prior, p_evidence_given_a, p_evidence_given_not_a):
p_evidence = p_evidence_given_a * prior + p_evidence_given_not_a * (1 - prior)
return p_evidence_given_a * prior / p_evidence
p = 0.01
for test_number in (1, 2):
p = bayes_update(p, 0.90, 0.05)
print(test_number, round(p, 4))
# 1 0.1538
# 2 0.766A second flag lifts the probability from about 15.4% to about 76.6%. The order of the tests does not change the final answer.
Common errors
- Confusing P(B|A) with P(A|B). A 90% detection rate does not mean a flagged firm is 90% likely to be fraudulent. The two numbers differ unless the prior and P(B) happen to line up.
- Ignoring the prior (base rate neglect). A rare event stays fairly unlikely even after moderately strong evidence. The 1% prior is what holds the posterior to 15.4%.
- Forgetting the complement term. P(B) needs both P(B|A) × P(A) and P(B|not A) × P(not A). Using only the first term gives a posterior of 1.
- Using probabilities that do not match. The false-alarm rate P(B|not A) is not 1 − P(B|A). The two come from different groups, so they need not sum to 1.
- Chaining dependent evidence. Repeated updating is valid only when the evidence is conditionally independent. Two tests that fail for the same reason are not independent.
Choosing the method on exam day
Use Bayes' formula when the question gives you the probability of the evidence under each hypothesis and asks for the probability of a hypothesis given the evidence. Phrases such as "given that the test is positive" or "after the upgrade" are cues. If the question instead asks for the overall probability of the evidence, the total probability rule alone is enough.
Exercise
A fund manager's research flags a stock as "undervalued." Historically, 10% of stocks are truly undervalued. The research flags 80% of truly undervalued stocks and also flags 15% of stocks that are not undervalued. A stock is flagged. What is the probability it is truly undervalued?
Show answer
First find P(flag) with the total probability rule: 0.80 × 0.10 + 0.15 × 0.90 = 0.08 + 0.135 = 0.215. Then apply Bayes' formula: P(undervalued | flag) = 0.08 / 0.215 ≈ 0.3721, or about 37.2%.
Exercise
In the analyst upgrade example, suppose a second, conditionally independent upgrade arrives from another analyst with the same likelihoods (0.60 given outperformance, 0.20 given non-outperformance). Starting from the posterior of 0.5625, what is the new probability of outperformance?
Show answer
P(second upgrade) = 0.60 × 0.5625 + 0.20 × 0.4375 = 0.3375 + 0.0875 = 0.425. The posterior is 0.3375 / 0.425 ≈ 0.7941, or about 79.4%.