Probability Foundations: How Likely Is This?
The Doctor’s Dilemma
Dr. Sarah runs a routine test on a patient. The test comes back positive for a rare disease that affects 1 in 1000 people. The test is 99% accurate:- If you HAVE the disease, it correctly says “positive” 99% of the time
- If you DON’T have it, it correctly says “negative” 99% of the time
Difficulty: Beginner
Prerequisites: Basic Python, Module 1 (Describing Data)
What You’ll Build: A spam email detector using Bayes’ theorem
What Is Probability?
At its core, probability answers: “How likely is something to happen?” Analogy: Probability is the language your ML model speaks. When a spam classifier says “92% chance this is spam,” it is giving you a probability. When a self-driving car decides to brake, it is reasoning about the probability of a pedestrian stepping into the road. Every prediction an ML model makes is, at its foundation, a probability statement.The Coin Flip
- Over many flips, about half will be heads
- Any single flip is unpredictable
- Probability is about long-run frequency
Basic Probability Rules
Rule 1: Probability is Between 0 and 1
- P(A) = 0: Impossible (rolling a 7 on a standard die)
- P(A) = 1: Certain (rolling 1-6 on a standard die)
- P(A) = 0.5: Equally likely to happen or not
Rule 2: Complement Rule
The probability something doesn’t happen is 1 minus the probability it does.Rule 3: Addition Rule
For mutually exclusive events (can’t happen together):Rule 4: Multiplication Rule for Independent Events
Events are independent if one doesn’t affect the other.Conditional Probability: The Game Changer
Here’s where it gets interesting. Conditional probability answers: “What’s the probability of A, GIVEN that B already happened?” Notation: reads as “probability of A given B”The Job Interview Example
You’re applying for jobs. Here’s some data:Bayes’ Theorem: The Most Important Formula
Now we can solve that medical test problem from the beginning. Bayes’ Theorem lets you flip conditional probabilities: In words: The probability of A given B equals the probability of B given A, times the probability of A, divided by the probability of B.Solving the Medical Test Problem
Let’s set up what we know:- P(Disease) = 0.001 (1 in 1000 people have it)
- P(Positive | Disease) = 0.99 (test catches 99% of sick people)
- P(Positive | No Disease) = 0.01 (1% false positive rate)
Why Is This So Low?
Let’s think about 10,000 people:- Start with a concrete population — pick 10,000 people (or 100,000, whatever makes the math clean).
- Split by the base rate — how many actually have the condition? At 0.1%, that is 10 out of 10,000.
- Apply the test to both groups — of the 10 sick people, 99% test positive = ~10 true positives. Of the 9,990 healthy people, 1% test positive = ~100 false positives.
- Look at the positive pile — 10 true positives out of 110 total positives = ~9%.
Bayes’ Theorem in Machine Learning
This isn’t just medical trivia. Bayes’ theorem is the foundation of:- Spam filters (Naive Bayes classifier)
- Recommendation systems
- Medical diagnosis AI
- Text classification
- Bayesian neural networks
🚀 Mini-Project: Spam Email Detector
The Problem
You receive an email with the word “FREE” in it. What’s the probability it’s spam?The Data
Step 1: Calculate Prior Probabilities
Step 2: Calculate Likelihoods
Step 3: Apply Bayes’ Theorem
Step 4: Multiple Words (Naive Assumption)
The “naive” in Naive Bayes assumes words are independent:Complete Spam Classifier
🎯 Practice Exercises
Exercise 1: Card Probability
Solution
Solution
Exercise 2: Weather Prediction
Solution
Solution
Exercise 3: Two-Test Diagnosis
Solution
Solution
Key Takeaways
Basic Rules
- Probability is between 0 and 1
- P(not A) = 1 - P(A)
- For exclusive events: P(A or B) = P(A) + P(B)
- For independent events: P(A and B) = P(A) × P(B)
Conditional Probability
- P(A|B) = P(A and B) / P(B)
- “Given B” means we’re only looking at cases where B happened
- This changes everything!
Bayes' Theorem
- Lets you flip P(A|B) to P(B|A)
- Prior × Likelihood = Posterior (after normalizing)
- Foundation of spam filters, medical diagnosis, ML
Key Insight
- Base rates matter enormously
- “99% accurate” doesn’t mean what you think
- Always consider: what’s the prior probability?
Common Mistakes to Avoid
Interview Questions
Question 1: The Two-Child Problem (Google)
Question 1: The Two-Child Problem (Google)
Question 2: Birthday Problem (Amazon)
Question 2: Birthday Problem (Amazon)
Question 3: Monty Hall Problem (Classic)
Question 3: Monty Hall Problem (Classic)
Question 4: Spam Classifier (Tech Companies)
Question 4: Spam Classifier (Tech Companies)
Practice Challenge
Challenge: Build a Complete Bayesian Classifier
Challenge: Build a Complete Bayesian Classifier
📝 Practice Exercises
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Connection to Machine Learning
Interview Prep: Common Questions
Probability Interview Questions
Probability Interview Questions
P(1st ace) = 4/52. P(2nd ace | 1st ace) = 3/51. Total = (4/52) × (3/51) = 12/2652 ≈ 0.45%Q: A test is 95% accurate for a disease affecting 1% of population. If positive, what’s P(have disease)?
Use Bayes: P(D|+) = P(+|D)×P(D) / [P(+|D)×P(D) + P(+|¬D)×P(¬D)] = (0.95×0.01) / (0.95×0.01 + 0.05×0.99) ≈ 16%Q: What’s the difference between independent and mutually exclusive events?
Independent: Occurrence of one doesn’t affect the other (e.g., two coin flips). Mutually exclusive: Both cannot occur simultaneously (e.g., getting heads AND tails on one flip). Note: Mutually exclusive events are NOT independent!Q: You flip a fair coin 10 times and get 10 heads. What’s P(heads on flip 11)?
Still 50%! This is the gambler’s fallacy. Each flip is independent; past results don’t affect future outcomes.
Common Pitfalls
Key Takeaways
- ✅ Basic Probability - P(A) = favorable outcomes / total outcomes
- ✅ Addition Rule - P(A or B) = P(A) + P(B) - P(A and B)
- ✅ Multiplication Rule - P(A and B) = P(A) × P(B|A)
- ✅ Conditional Probability - P(A|B) = P(A and B) / P(B)
- ✅ Bayes’ Theorem - Update beliefs with new evidence; essential for ML
- ✅ Independence - Events where one doesn’t affect the other
Next: Probability Distributions
Interview Deep-Dive
A disease affects 1 in 10,000 people. A test is 99% sensitive and 99% specific. A patient tests positive. Walk me through the actual probability they have the disease.
A disease affects 1 in 10,000 people. A test is 99% sensitive and 99% specific. A patient tests positive. Walk me through the actual probability they have the disease.
- This is a Bayes’ theorem problem, and the answer shocks most people. Let me work through it with a natural frequency approach. Imagine 1,000,000 people. Of those, 100 actually have the disease (1 in 10,000). Of those 100, the test correctly identifies 99 (99% sensitivity). Of the 999,900 healthy people, the test incorrectly flags 1% as positive, giving 9,999 false positives.
- Total positive tests: 99 true positives plus 9,999 false positives equals 10,098. So the probability of actually having the disease given a positive test is 99 / 10,098, which is about 0.98% — less than 1%.
- The intuition: when the disease is very rare, even a highly accurate test produces far more false positives than true positives because the healthy population is so much larger. This is the base rate fallacy in action.
- This has direct real-world consequences. During COVID, rapid antigen tests had different positive predictive values depending on community prevalence. In a low-prevalence area, a positive test was much less reliable than in a high-prevalence area — same test, same accuracy, but radically different interpretation because of the base rate.
What is the difference between independent events and mutually exclusive events? Most candidates confuse these.
What is the difference between independent events and mutually exclusive events? Most candidates confuse these.
- Independent events are those where the occurrence of one does not change the probability of the other. Formally, P(A and B) = P(A) x P(B). Example: flipping a coin and rolling a die. The coin result has no effect on the die.
- Mutually exclusive events are those that cannot occur simultaneously. P(A and B) = 0. Example: rolling a 3 and rolling a 5 on the same die throw.
- Here is the critical point that trips people up: mutually exclusive events are actually dependent, not independent. If I tell you that event A happened (you rolled a 3), then you know with certainty that event B did not happen (you did not roll a 5). That means P(B given A) = 0, which is different from P(B) = 1/6. Since P(B given A) does not equal P(B), the events are dependent.
- The only exception is when one of the events has probability zero. In every other case, mutually exclusive implies dependent. This is a common interview gotcha question precisely because the two concepts sound like they should be related but they are actually in tension.
You flip a fair coin 20 times and get 15 heads. A colleague says the coin must be biased. How do you evaluate this claim statistically?
You flip a fair coin 20 times and get 15 heads. A colleague says the coin must be biased. How do you evaluate this claim statistically?
- The way to evaluate this is to ask: “If the coin truly were fair, how likely is it to see a result this extreme or more extreme?” That is the definition of a p-value.
- For a fair coin, the number of heads in 20 flips follows a Binomial(n=20, p=0.5) distribution. P(X >= 15) is the sum of P(X=15) + P(X=16) + … + P(X=20). Computing this gives approximately 2.1%. Since we should also consider the other tail (5 or fewer heads would be equally surprising), the two-tailed p-value is about 4.1%.
- At a standard alpha of 0.05, this is borderline significant. We would reject the null hypothesis that the coin is fair — but barely. At alpha = 0.01, we would not reject. This tells us that 15 out of 20 is unusual but not overwhelmingly so for a fair coin.
- The key insight: a single run of 20 flips is not very powerful for detecting bias. If the true probability of heads were 0.6, we would need roughly 200 flips to detect that with 80% power. Twenty flips can only reliably detect large biases (like p = 0.8). This is why sample size planning matters before making claims.
Explain Naive Bayes classification. Why is it called 'naive,' and when does it work well despite that assumption?
Explain Naive Bayes classification. Why is it called 'naive,' and when does it work well despite that assumption?
- Naive Bayes uses Bayes’ theorem to compute P(class given features) by flipping it to P(features given class) x P(class). The “naive” part is the assumption that all features are conditionally independent given the class. That means P(word1 and word2 given spam) = P(word1 given spam) x P(word2 given spam). In reality, words are correlated — “Nigerian” and “prince” tend to appear together in spam — so the assumption is violated.
- Despite this, Naive Bayes works surprisingly well in practice for several reasons. First, classification only requires getting the rank ordering of class probabilities correct, not the actual probability values. Even with wrong probability estimates, the argmax (most likely class) is often correct. Second, the independence assumption causes errors that tend to cancel out across many features. Third, with limited training data, Naive Bayes has far fewer parameters to estimate than a model that captures all pairwise dependencies, so it suffers less from overfitting.
- It excels in text classification (spam filtering, sentiment analysis) where you have high-dimensional sparse features and moderate amounts of labeled data. It is fast to train (single pass through data), fast to predict, and handles missing features naturally.
- Where it fails: when feature dependencies actually matter for classification. For example, XOR-type problems where the class depends on the interaction of two features (neither feature alone is predictive). In those cases, you need a model that captures interactions, like logistic regression with interaction terms, or a tree-based model.