# Probability and Bayes' Rule — Artificial Intelligence

Source: https://www.skillbyai.com/en/artificial-intelligence/u-bayes

> Update beliefs with evidence.

## Prior, likelihood, posterior

**Bayes' rule** updates a belief when evidence arrives: P(H | E) = P(E | H) P(H) / P(E). The **prior** P(H) is the belief before evidence, the **likelihood** P(E | H) says how expected the evidence is if H is true, and the **posterior** P(H | E) is the updated belief. A famous lesson: when a condition is rare, even an accurate test produces many false positives, so a positive result may still mean the condition is unlikely. People routinely misjudge this; computing it explicitly avoids the mistake.

## Probabilities and decisions

Real agents rarely know everything; probability lets them reason and decide anyway.

![Three ideas: Bayes' rule, naive Bayes, expected utility.](assets/figures/artificial-intelligence/section-5-map.svg) — Figure 5.1 — Bayes, naive Bayes and utility.

## A medical test with a 1% base rate, run

I ran this with plain Python 3 (standard library only), with fixed random seeds where randomness is used. With 1% prevalence, 95% sensitivity and a 5% false-positive rate, only 5.9% of people test positive, and a positive result means just a 16.1% chance of having the condition. A second independent positive raises it to 78.5%.

```python
prior = 0.01          # 1% of people have the condition
sensitivity = 0.95    # P(test positive | condition)
false_pos = 0.05      # P(test positive | no condition)
p_pos = sensitivity * prior + false_pos * (1 - prior)
posterior = sensitivity * prior / p_pos
print(f"P(positive test)            = {p_pos:.4f}")
print(f"P(condition | positive test) = {posterior:.3f}")
second = sensitivity * posterior / (sensitivity * posterior + false_pos * (1 - posterior))
print(f"after a second independent positive test: {second:.3f}")
```

Output:

```
P(positive test)            = 0.0590
P(condition | positive test) = 0.161
after a second independent positive test: 0.785
```

## Always ask for the base rate

Before interpreting any alarm or test, ask how common the condition is in the population being tested.

**Quiz:** Why can a positive result from an accurate test still be unlikely to be correct?

- [ ] Probabilities cannot be updated
- [ ] Accurate tests are always wrong
- [ ] Bayes' rule ignores priors
- [x] When the condition is rare, false positives can outnumber true positives

*Answer:* When the condition is rare, false positives can outnumber true positives. Base rates matter.
