Conditional probability
Overview
Conditional probability measures how the probability of an event changes when we learn that another event has occurred. It's the foundation of Bayesian reasoning, naive Bayes classifiers, Hidden Markov Models, and essentially all probabilistic inference in ML.
[!intuition] The Core Insight
When you gain information, you shrink the universe of possible outcomes. Conditional probability asks: "Given that I now know B happened, what fraction of those B-outcomes also include A?"
Physical analogy: You have 100 emails. 60 are spam, 40 are legitimate. Among spam, 50 contain the word "free". You open an email and see "free" — your probability estimate that it's spam updates because you've restricted your universe to only "free"-containing emails.
The formula reads as "probability of A given B" — it's the probability that A occurs in the world where B is already true.
[!definition] Formal Definition
The conditional probability of event given event (where ) is:
Why this formula?
- Numerator : The probability both A and B happen (the overlap).
- Denominator : Normalizes by treating B as the new "total universe" (renormalization).
- Result: The fraction of B-outcomes that also satisfy A.
Derivation from first principles (frequency interpretation):
- Run trials. Event occurs in trials.
- Among those trials, event occurs in trials.
- Empirical conditional probability: .
- Express in probabilities: .
[!formula] Key Formulas & Derivations
1. Multiplication Rule (Chain Rule)
Rearranging the definition:
Why? If you want both A and B to happen:
- First, B must happen: probability .
- Then, given B happened, A must also happen: probability .
- Multiply because these are sequential restrictions.
General chain rule (for multiple events):
Derivation: Apply multiplication rule recursively.
2. Law of Total Probability
If partition the sample space (mutually exclusive, exhaustive):
Why? Event A can happen through any of the "pathways". Each pathway contributes (probability of reaching that pathway times probability A happens there).
Derivation:
- Since partition the space: (disjoint union).
- By additivity: .
- By multiplication rule: .
- Combine: .
3. Bayes' Theorem
Derivation from multiplication rule:
- From definition: .
- Also: (symmetry).
- Equate: .
- Solve for : .
Expanded form (using total probability for denominator):
Why this matters: You often know (likelihood) and (prior) but want (posterior). Bayes flips the conditioning.
[!example] Worked Example 1: Medical Diagnosis
Setup: A disease affects 1% of the population. A test correctly identifies the disease 95% of the time (sensitivity) and correctly identifies healthy patients 90% of the time (specificity). You test positive. What's the probability you have the disease?
Define events:
- : Has disease. , .
- : Tests positive.
- (sensitivity).
- → (false positive rate).
Want: .
Step 1: Apply Bayes' theorem.
Step 2: Calculate using total probability (partition by and ).
Why this step? We need to account for both ways to test positive: truly sick and falsely positive.
Step 3: Compute posterior.
Interpretation: Even with a positive test, you only have ~9% chance of disease because the disease is rare (low prior). Most positives are false positives from the healthy majority.
[!example] Worked Example 2: Spam Filter (Naive Bayes Foundation)
Setup:
- 60% of emails are spam, 40% are ham.
- Word "free" appears in 80% of spam, 10% of ham.
- An email contains "free". Is it spam?
Events:
- : Spam. , (ham).
- : Contains "free".
- , .
Want: .
Step 1: Apply Bayes.
Step 2: Calculate .
Why this step? "Free" can appear in spam or ham; weight by their prior probabilities.
Step 3: Compute.
Interpretation: Seeing "free" strongly suggests spam because it's much more common in spam (80% vs 10%) and spam is already the majority class.
[!example] Worked Example 3: Independence Check
Setup: Two coin flips. = first is heads, = second is heads.
Check independence: Events are independent if .
Calculate:
- (fair coin).
- (both heads).
- .
- .
Why this step? If learning B doesn't change the probability of A, they're independent. The second flip doesn't affect the first.
Alternative check: ?
Both checks confirm independence.
[!mistake] Common Mistakes
Mistake 1: Confusing with
Wrong thinking: "If 90% of people with the disease test positive, then 90% of positive tests mean disease."
Why it feels right: The numbers sound similar; our brains conflate the two directions.
The fix: in general. They're related by Bayes' theorem but weighted by priors. In medical testing, (sensitivity) is NOT the same as (posterior).
Steel-man: The confusion arises because in symmetric scenarios (like fair coins), . But with asymmetric priors (rare disease, common test), they diverge wildly.
Mistake 2: Ignoring the Base Rate (Neglecting Priors)
Wrong thinking: "The test is 95% accurate, so a positive result means 95% chance of disease."
Why it feels right: We focus on the test's accuracy and ignore population prevalence.
The fix: Always include the prior in Bayes' theorem. A 95% sensitive test on a 1% prevalence disease yields only ~9% posterior (Example 1).
Steel-man: Test accuracy is the most salient information presented, so our attention anchors there. The base rate is abstract and easy to forget.
Mistake 3: Forgetting to Normalize
Wrong thinking: "Both events have 50% probability conditional on B, so they're equally likely."
Why it feels right: We compare likelihoods directly without renormalizing.
The fix: requires dividing by , the total probability of the conditioning event. Without normalization, you're not measuring fractions of the new universe.
Steel-man: In many contexts (like comparing hypotheses), ratios of unnormalized probabilities (likelihoods) suffice for ranking, so we skip normalization. But for actual probabilities, you must normalize.
[!recall]- Explain to a 12-Year-Old
Imagine you have a bag with 10 marbles: 6 red, 4 blue. Normally, the chance of grabbing red is 6/10 = 60%.
Now, your friend peeks and says, "I see it's a marble from the left half of the bag." Suppose the left half has 4 red and 1 blue. Now your universe shrunk to just those 5 marbles. The chance of red given it's from the left half is 4/5 = 80%. The "given left half" updated your probability because you have new information that changes what's possible.
Conditional probability is just: When you learn something new, recalculate chances in the smaller world where that new thing is true.
[!mnemonic] Remember It
"B is the New Total": When you see , imagine B is now 100% (the whole universe). A is a fraction of that. Divide the overlap by B's total.
"Bayes Flips the Pipe": vs — Bayes' theorem is the flipping tool. Prior → Likelihood → Posterior.
Properties of Conditional Probability
- Range: (still a probability).
- Certainty: (if B happened, B definitely happened).
- Additivity: if .
- Independence: If and are independent, (learning B doesn't change A).
Connections
- Bayes-Theorem — Direct application and inversion of conditioning.
- Law-of-Total-Probability — Partitioning for marginalization.
- Independence — Special case where .
- Naive-Bayes-Classifier — Uses via Bayes + independence assumptions.
- Hidden-Markov-Models — Transition probabilities are conditional: .
- Bayesian-Inference — Updating beliefs: prior → likelihood → posterior.
- Mutual-Information — Measures how much learning Y reduces uncertainty about X.
- Chain-Rule-of-Probability — Factorizing joint distributions via conditionals.
Flashcards
What is the formula for conditional probability ?
Why do we divide by in conditional probability?
State the multiplication rule derived from conditional probability.
What is the law of total probability?
State Bayes' theorem.
In Bayes' theorem, what are the prior, likelihood, and posterior?
If , what does this imply?
A disease has 1% prevalence, test sensitivity 95%, specificity 90%. You test positive. Why isn't ?
What common mistake is "confusing with "?
Why must you always include the prior when computing via Bayes?
Given , , where is not-, what is ?
What does indicate?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, conditional probability ka core idea bahut simple hai — jab tumhe koi nayi information milti hai, tab tumhara "universe of possibilities" chota ho jata hai. Jaise 100 emails hain, usme 60 spam hain. Ab tumne ek email mein "free" word dekha — to ab tum sirf un emails ke bare mein soch rahe ho jisme "free" hai, baaki sab hata do. Iss chote universe mein spam hone ki probability change ho jaati hai. Bas yahi hai P(A|B) — matlab "A hone ki probability, jab hume pehle se pata hai ki B ho chuka hai". Formula bhi isi logic se aata hai: P(A|B) = P(A ∩ B) / P(B) — upar overlap (dono events ek saath), aur neeche P(B) se divide karke hum B ko naya total universe bana dete hain.
Yeh concept isliye important hai kyunki isse Bayes' Theorem nikalta hai, jo real-world ML mein sab jagah use hota hai. Medical diagnosis wala example dekho — disease sirf 1% logon ko hai, test bhi 95% accurate hai, phir bhi agar tum positive aate ho to disease hone ki actual probability surprisingly kam nikalti hai! Iska reason hai ki bahut saare healthy log bhi false positive dete hain (kyunki healthy population itni badi hai). Bayes' Theorem tumhe yeh "prior" knowledge (1% base rate) ko naye evidence (positive test) ke saath sahi tarike se combine karna sikhata hai.
ML mein yeh foundation hai — naive Bayes classifier, Hidden Markov Models, aur har probabilistic inference iske upar khada hai. Machine ko jab bhi data se seekhna hota hai, wo basically yahi kar rahi hoti hai: "yeh evidence dekh kar, mera belief kitna update hona chahiye?" To agar tumhe conditional probability aur Bayes ka intuition clear ho gaya, to aage ka poora probabilistic ML samajhna aasaan ho jayega. Yaad rakho — information milne par universe shrink karo, phir fraction nikalo. Bas itni si baat hai!