Bayes and base rates
Bayes’ rule updates a probability after evidence arrives. In a fictional factory, 10% of items are defective; a detector alerts on 80% of defective items and 20% of good items. A positive alert does not mean an 80% chance of a defect. The updated probability is 0.08/(0.08 + 0.18) = 4/13, about 30.77%. The alert is evidence, not proof.
Build the denominator from both possible sources
The prior P(D) is the probability before observing the alert. The likelihood P(alert | D) is the probability of the evidence under the defect hypothesis. Here it is also the sensitivity or true-positive rate. The false-positive rate P(alert | not D) describes alerts among good items. The posterior P(D | alert) is the updated probability. Imagine 1,000 items with exactly the stated rates: 100 defective, 900 good; 80 true alerts and 180 false alerts. Among all 260 alerts, only 80 are actual defects. The denominator must include both sources of alerts.
Apply Bayes’ rule
The general formula is P(D | alert) = P(alert | D) × P(D) / P(alert). For these two mutually exclusive and exhaustive possibilities, P(alert) = P(alert | D) × P(D) + P(alert | not D) × (1 − P(D)). This is the total probability of an alert. No independence assumption between defect status and alert is required. The denominator must be positive.
def posterior(prior, sensitivity, false_positive): if not all(0 <= p <= 1 for p in (prior, sensitivity, false_positive)): raise ValueError("Probabilities must be between 0 and 1") true_alert = prior * sensitivity false_alert = (1 - prior) * false_positive total = true_alert + false_alert return true_alert / total if total else None
print(round(posterior(0.1, 0.8, 0.2), 4))print(round(posterior(0.01, 0.8, 0.2), 4))print(posterior(0.5, 0, 0))The first output is the original example. The second shows that if only 1% of items are defective, the same detector yields a posterior of about 3.88%. The third result is undefined because alerts never occur under that model. The helper accepts finite probabilities in [0, 1], not percentages like 80. Do not round intermediate calculations unnecessarily.
Compare two factories using counts
for defective in [100, 10]: good = 1000 - defective true_alerts = defective * 8 // 10 false_alerts = good * 2 // 10 alerts = true_alerts + false_alerts print(true_alerts, false_alerts, round(true_alerts / alerts, 4))The counts are exact for these particular synthetic populations; for arbitrary populations, expected counts may be fractional, so floor division is not a general probability estimator. A lower prior can make false alerts dominate even when sensitivity is unchanged. A factory should estimate rates on representative labeled data and use review costs, missed-defect costs and safety requirements when deciding how to respond. A posterior calculated under incorrect rates can be misleading.
Common mistakes and practice
Do not confuse P(alert | D) with P(D | alert), or P(not D | alert) with the false-positive rate. The latter conditions on good items. Do not treat an unvalidated model score as a calibrated probability. Practice: with prior 0.5, sensitivity 0.8 and false-positive rate 0.2, compute the posterior: 0.4/(0.4 + 0.1) = 0.8. Then explain why this differs from 4/13 even though the detector has not changed. Next, compare variation across measurements.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bayes and base rates — understanding check
Reason about uncertainty using synthetic factory observations.
What is the prior in the 10% defect example?
How many false alerts occur among 900 good items at a 20% false-positive rate?
Of 1,000 items, 100 are defective. The detector raises 80 true alerts and 180 false alerts. What is the probability of a defect given an alert in this fixture?
With sensitivity 0.8 and false-positive rate 0.2, what happens to the posterior after an alert when the defect prior falls from 0.10 to 0.01?
What does posterior(0.5, 0, 0) return?