AI engineering glossary · Probability

What is bayes' theorem?

Bayes' theorem (also called Bayes' rule): A formula for reversing a conditional probability: P(A | B) = P(B | A) x P(A) / P(B). It updates a starting belief about A in light of evidence B, and underlies spam filters, medical test interpretation and Bayesian machine learning.

Level: beginnerTopic: Probability

Bayes' theorem: an example

A disease affects 1% of people; a test catches 90% of cases and gives 9% false alarms. P(disease | positive) = 0.9 x 0.01 / (0.9 x 0.01 + 0.09 x 0.99), about 0.09.

Bayes' theorem in Python

A short example you can run (Python 3.11). The output below is what it really prints.

# A fraud test: 1% of payments are fraud; the test catches 95% of fraud and flags 3% of genuine payments.
p_fraud = 0.01
p_flag_given_fraud = 0.95
p_flag_given_ok = 0.03
p_flag = p_flag_given_fraud * p_fraud + p_flag_given_ok * (1 - p_fraud)
print(f"P(fraud | flagged) = {p_flag_given_fraud * p_fraud / p_flag:.3f}")

Output

P(fraud | flagged) = 0.242

Related terms

More Probability terms

Where you learn it

Bayes' theorem is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.