Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 45/ 160
03. Mathematics & Statistics

Bayes Theorem

Update beliefs with evidence using prior, likelihood, and posterior reasoning.

IntermediatePrior · Likelihood · Posterior3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Odds form

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%.

Key takeaway: Posterior ∝ prior × likelihood; count natural frequencies to avoid base-rate mistakes.

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.

A tiny Bayesian conversion estimate

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%).

Key takeaway: Naive Bayes for fast classifiers; Bayesian estimation for uncertainty; choose priors deliberately.

How it works, step by step

Step 1 of 4
Frame

1. Identify the hypothesis and evidence

Define A (fraud) and B (flagged) and the population they refer to.

A = fraud · B = flagged
Key numbers at this step
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

Bayesian
Best for: Updating beliefs, small data, direct probability statements

Strengths: Posterior probabilities; priors encode knowledge; intuitive intervals.

Watch out: Needs priors and (often) sampling computation.

Frequentist
Best for: Standard tests and large-sample estimation

Strengths: Established procedures; no priors.

Watch out: p-values and CIs are easy to misread.

Naive Bayes classifier
Best for: Fast baselines for text and categorical features

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?
Interview tip: Explain with 10,000 transactions and count the branches. Interviewers prefer the natural-frequency walk-through to the formula recited from memory.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Bayes Theorem with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Probability Fundamentals & Conditional Probability
Next topic
Discrete Distributions (Binomial, Poisson)