Bayes Theorem
Update beliefs with evidence using prior, likelihood, and posterior reasoning.
In plain words
Bayes' rule tells you how to change your mind when you get new evidence, taking into account how likely the thing was in the first place.
Think of it like…
A detective's hunch (prior) gets revised by each clue (likelihood). A clue that would be very common even for innocent people moves the hunch very little, and if the crime is extremely rare to begin with, even good evidence leaves the suspect probably innocent.
Where you’ve seen it
A fraud model flags transactions. Fraud occurs in 1% of transactions (prior). The model catches 95% of fraud (sensitivity: P(flag | fraud) = 0.95) and wrongly flags 5% of legitimate transactions (false positive rate). For a flagged transaction: P(fraud | flag) = (0.95 × 0.01) / (0.95 × 0.01 + 0.05 × 0.99) = 0.0095 / 0.059 ≈ 0.16. Only about 16% of flags are actual fraud, because legitimate transactions are so much more numerous. In 10,000 transactions: 100 fraud (95 flagged) and 9,900 legitimate (495 flagged), so 95 of 590 flags are real.
Words you’ll hear
- Prior P(A)
- Probability of the hypothesis before seeing the evidence (often the base rate).
- Likelihood P(B | A)
- Probability of the evidence if the hypothesis is true.
- Posterior P(A | B)
- Updated probability of the hypothesis after the evidence.
- Base rate fallacy
- Ignoring the prior when interpreting a test result.
Visual diagram
Theory & deep dive
The formula and the natural-frequency method
P(A | B) = P(B | A) P(A) / P(B), where P(B) = P(B | A) P(A) + P(B | not A) P(not A). In the fraud example A = fraud and B = flagged, so P(B) = 0.95 × 0.01 + 0.05 × 0.99 = 0.059, the overall flag rate of 5.9%. Dividing the fraction of flags that come from real fraud (0.0095) by the total flag rate (0.059) gives 0.161. People find this much easier with natural frequencies: imagine 10,000 transactions and count each branch, as in the diagram. The result is a ratio of counts, and the base rate is impossible to forget.
Two further forms are useful: the odds form (posterior odds = prior odds × likelihood ratio: 1:99 × 0.95/0.05 = 19:99 ≈ 0.19, which is a probability of 16%) and sequential updating: after a first flag the posterior (0.16) becomes the prior for the next piece of evidence, such as a second independent alert.
Prior odds 1:99. A flag has a likelihood ratio of 0.95 / 0.05 = 19. Posterior odds = (1/99) × 19 = 0.192, so P = 0.192 / 1.192 ≈ 0.16. Improving the false positive rate from 5% to 0.5% raises the likelihood ratio to 190 and the posterior to about 66%.
Bayes in data science
Naive Bayes classifiers apply the theorem with the simplifying assumption that features are independent given the class: P(class | words) ∝ P(class) Π P(wordᵢ | class). Despite the unrealistic assumption they work well for text (spam filters) and are fast, needing little data. Bayesian inference more generally treats parameters as random: choose a prior, observe data, compute the posterior distribution (often by sampling with MCMC in PyMC or Stan), and read credible intervals directly ("there's a 94% probability the lift is between 0.2% and 1.8%"). Bayesian A/B testing reports the probability that variant B beats A and the expected loss, which stakeholders often find more intuitive than p-values.
Practical cautions: priors matter most with little data, so choose them deliberately (weakly informative priors are a good default) and test sensitivity; the evidence term must use the correct population (a test's accuracy measured on sick patients doesn't apply to the general population); likelihoods of dependent evidence can't simply be multiplied; and calibration matters: a classifier's probability outputs should be checked (calibration curves) before treating them as likelihoods or posteriors.
Prior: conversion rate ~ Beta(2, 38) (about 5%). Observe 30 conversions in 400 visits. The posterior is Beta(2 + 30, 38 + 370) = Beta(32, 408), with mean 32 / 440 ≈ 7.3%, a compromise between the prior (5%) and the data (7.5%).
How it works, step by step
Step 1 of 41. Identify the hypothesis and evidence
Define A (fraud) and B (flagged) and the population they refer to.
- Right
- population
When to use it & trade-offs
What it is
Bayes' theorem reverses a conditional probability: P(A | B) = P(B | A) × P(A) / P(B). In words: posterior = likelihood × prior ÷ evidence. The prior is your belief before seeing data, the likelihood is how probable the data are if the hypothesis is true, and the posterior is the updated belief. The denominator P(B) is found with the law of total probability. The crucial lesson is the base rate: how common something is to begin with changes how much a piece of evidence should move you.
Why it matters: Spam filters, fraud alerts, medical tests, recommendation systems and A/B test analyses all update beliefs from evidence. The theorem explains why a "95% accurate" fraud detector can still be wrong most of the time it raises an alarm when fraud is rare, a mistake called the base rate fallacy. Naive Bayes classifiers and Bayesian modelling are direct applications.
When to reach for it
- Interpreting test and alert accuracy for rare events
- Spam and text classification baselines
- Bayesian A/B testing and uncertainty estimates
When to avoid or be careful
- Ignoring the base rate when reading a "95% accurate" claim
- Multiplying likelihoods of dependent evidence
- Treating uncalibrated model scores as true probabilities
Bayesian vs frequentist views
Strengths: Posterior probabilities; priors encode knowledge; intuitive intervals.
Watch out: Needs priors and (often) sampling computation.
Strengths: Established procedures; no priors.
Watch out: p-values and CIs are easy to misread.
Strengths: Simple, quick, works with little data.
Watch out: Independence assumption; probabilities poorly calibrated.
Interview signals & numbers to know
When this topic comes up
- “The model is 95% accurate but most alerts are false”
- “What's the probability the patient has the disease given a positive test?”
- “Naive Bayes”
- Prior and posterior
- Bayesian A/B testing
Benchmarks & rules of thumb
- ≈ 0.16
- P(fraud | flag) in the example
- 95 of 590
- true fraud among flagged transactions
- 19
- the likelihood ratio 0.95 / 0.05
Common mistakes & follow-ups
Common interview mistakes
- Reporting P(flag | fraud) = 95% as the probability that a flagged transaction is fraud.
- Using accuracy measured on a balanced test set to reason about a rare-event population.
- Setting a strong prior with no sensitivity analysis.
Likely interviewer follow-ups
- Why can a 95%-accurate test still give mostly false positives?
- How would you explain Bayes' theorem to a non-technical stakeholder?
- How does naive Bayes work?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Bayes Theorem with data science interview Q&A and hands-on practice.