Skip to main content
Bayesian Statistics

Bayesian Statistics

The Doctor’s Dilemma (Revisited)

Remember from our probability module: A test for a rare disease (1 in 1000 people) comes back positive. The test is 99% accurate. What’s the probability you actually have the disease? Most people say “99%.” The real answer: About 9%. This counterintuitive result is the essence of Bayesian thinking. Let’s understand it deeply.
Estimated Time: 4-5 hours
Difficulty: Intermediate
Prerequisites: Probability and Distributions modules
What You’ll Build: Spam filter, A/B test analyzer, and diagnostic tool

Bayes’ Theorem: The Core Formula

P(AB)=P(BA)P(A)P(B)P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)} In plain English:
  • P(A|B): Probability of A given we observed B (posterior)
  • P(B|A): Probability of observing B if A is true (likelihood)
  • P(A): Prior probability of A before seeing evidence
  • P(B): Total probability of observing B
Analogy: Think of Bayes’ theorem as learning from experience. You walk into a restaurant with a prior belief: “There is a 50% chance the food is good” (maybe you know nothing about it). Then you observe evidence: the restaurant is packed on a Tuesday night. Your likelihood reasoning kicks in: “Packed restaurants tend to have good food.” You update your belief to a posterior: “Now I think there is an 80% chance the food is good.” Bayes’ theorem is the mathematically precise version of this everyday reasoning process. And it is exactly how ML models update their parameters during training — each batch of data is new evidence that updates the model’s beliefs about the right weights.

The Medical Test Example

Let’s define:
  • A: You have the disease
  • B: Test is positive
Given:
  • P(disease) = 1/1000 = 0.001 (prior)
  • P(positive|disease) = 0.99 (sensitivity)
  • P(positive|no disease) = 0.01 (false positive rate)
Output:

Why Is It So Low?

Key Insight: When the disease is rare, even a small false positive rate produces many false alarms that swamp the true positives!
ML Application — Why Priors Matter in Practice: In ML, your training data distribution acts as an implicit prior. If 99% of your training emails are not spam, your model starts with a strong prior toward “not spam.” This is why class imbalance is such a pervasive problem. Bayesian thinking gives you principled tools to handle it: you can explicitly adjust the prior (class weights), oversample the minority class (effectively strengthening its prior), or use Bayesian models that naturally incorporate uncertainty. Understanding the math behind the medical test example helps you reason about why your fraud detection model misses rare fraud cases — it is the same base rate problem.

Frequentist vs Bayesian: Two Philosophies

The Coin Flip Example

You flip a coin 10 times and get 7 heads. What’s the probability of heads? Frequentist Answer:
  • The “true” probability is a fixed (unknown) number
  • Our best estimate is 7/10 = 0.70
  • Confidence interval: roughly 0.35 to 0.93 (wide because small sample)
Bayesian Answer:
  • We start with a prior belief (maybe 50-50)
  • We update based on evidence
  • We get a posterior distribution over possible values
Key Observations:
  1. Strong priors resist change (you need lots of data to override them)
  2. Weak priors let the data speak
  3. With enough data, all priors converge to the same answer
Step-by-step reasoning for choosing priors:
  1. Do you have genuine domain knowledge? If an expert says “this coin is almost certainly fair,” a Beta(50, 50) prior encodes that belief. This is equivalent to saying “I have seen 100 previous flips and they were 50-50.”
  2. No strong opinion? Use a weak/uninformative prior like Beta(1, 1) — uniform over all possibilities. This lets the data dominate from the first observation.
  3. Worried about overfitting with small data? A mildly informative prior acts as regularization. This is precisely what L2 regularization (Ridge regression) does — it is equivalent to placing a Gaussian prior centered at zero on the model weights.
  4. Have historical data from similar problems? Use those statistics as your prior. This is empirical Bayes, and it is one of the most practical approaches in industry.
Statistical Mistake in ML — Ignoring the Prior/Regularization Connection: Many ML practitioners think of regularization as “just a tuning parameter” without understanding its Bayesian interpretation. L2 regularization (Ridge) is equivalent to a Gaussian prior on weights. L1 regularization (Lasso) is a Laplacian prior. The regularization strength lambda is the inverse of the prior variance. Understanding this connection helps you choose regularization strength principally: when you have strong prior knowledge that weights should be small, use stronger regularization. When you have lots of data, regularization matters less — because the likelihood overwhelms the prior, just like observation 3 above.

Building a Bayesian Spam Filter

This is exactly how the original spam filters worked!

Bayesian A/B Testing

The Problem with Frequentist A/B Tests

Traditional A/B tests give you a yes/no answer: “statistically significant” or not. Bayesian A/B testing gives you richer information:
  • Probability that B is better than A
  • Expected improvement
  • Distribution of possible outcomes
Analogy: Frequentist A/B testing is like a judge who can only say “guilty” or “not guilty.” Bayesian A/B testing is like a judge who says “I believe there is a 73% chance the defendant is guilty, and if they are, the crime was probably between X and Y in severity.” You get the full picture, not just a binary verdict. For business decisions, this richer output is often exactly what stakeholders need — “there is an 85% probability variant B is better, with an expected lift of 3-7%” is far more actionable than “p = 0.06, not significant.”

Bayesian Linear Regression

Unlike regular linear regression (which gives point estimates), Bayesian regression gives distributions over parameters.

When to Use Bayesian vs Frequentist


Practice Exercises

Problem: You think a coin is fair (prior: Beta(10, 10)). After 30 flips, you see 22 heads. What’s your posterior belief about P(heads)?Calculate the posterior distribution and 95% credible interval.
Problem: You’re testing 3 different ad creatives. After day 1:
  • Ad A: 50 clicks, 5 conversions
  • Ad B: 50 clicks, 8 conversions
  • Ad C: 50 clicks, 3 conversions
Use Bayesian inference to decide which ad to show more tomorrow (Thompson Sampling).
Problem: You have conversion rates from 5 different stores. Build a hierarchical Bayesian model that shares information across stores to get better estimates for stores with little data.

Summary

Key Takeaway: Bayesian statistics lets you formally combine prior knowledge with data to get calibrated uncertainty estimates. It’s especially valuable when data is limited or when you need to make decisions under uncertainty.