Bayes rule
Quick answer
Bayes rule updates a probability after observing evidence. It combines a prior belief with the probability of that evidence under each possible hypothesis. The updated value is the posterior. In ML, this distinction matters when alerts are common but the condition being detected is rare.
From joint probability to a posterior
Let F mean faulty and A mean alerted. Since P(F and A)=P(A | F)P(F)=P(F | A)P(A), division gives P(F | A)=P(A | F)P(F)/P(A), provided P(A)>0. If fault and no fault exhaust the possibilities, P(A)=P(A | F)P(F)+P(A | not F)P(not F). A synthetic batch has 1000 parts: 20 faulty, 16 of which alert, and 980 good, 98 of which alert. Select a part uniformly.
fault, good = 20, 980true_alert, false_alert = 16, 98print(fault/(fault+good))print(true_alert/fault)print(round(true_alert/(true_alert+false_alert), 4))Expected output (Python 3.13.5):
0.020.80.1404The prior is 2%, sensitivity is 80%, yet the posterior given an alert is only about 14.04%. False alerts from the large good group dominate.
Change the prior, hold the mechanism fixed
For a binary hypothesis with prior p and likelihoods t=P(A | F), f=P(A | not F), the posterior is pt/(pt+(1−p)f). Here likelihood is a function of the hypothesis for fixed observed evidence; it is not a normalized probability over hypotheses. Compare two settings with the same alert mechanism but different assumed fault prevalence.
def posterior(p, t, f): evidence = p*t + (1-p)*f return p*t/evidence if evidence else Nonefor p in [0.02, 0.2]: print(round(posterior(p, 0.8, 0.1), 4))Expected output (Python 3.13.5):
0.14040.6667Raising the prior to 20% increases the posterior to about 66.67%. This assumes the conditional alert probabilities remain unchanged.
Common mistakes
Do not substitute sensitivity for the posterior. Do not use an arbitrary prior or claim a posterior is calibrated to real equipment without validation. If evidence has zero probability, return an explicit undefined result rather than a fabricated probability. Never multiply evidence likelihoods unless the required conditional independence assumptions are justified.
Practice and next steps
With prior 0.1, true-alert probability 0.9 and false-alert probability 0.1, compute the posterior and explain why it is not 0.9. Next, describe the outcomes themselves using distributions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bayes rule — knowledge check
Apply probability concepts to original finite and simulated examples.
What is a prior?
An alert occurs for 8 faulty and 32 good parts. What is P(fault | alert)?
For fixed evidence E, which expression is the likelihood of H?
When does the elementary Bayes ratio require special handling?
Holding true and false alert rates fixed and positive, what happens as the fault prior increases?