2.7.9Statistics & Probability — Intermediate

Bayes' theorem — derivation and applications

1,554 words7 min readdifficulty · medium6 backlinks

WHY do we need it?

Often the probability we can measure is the "wrong way round".

  • A medical test gives us P(positivedisease)P(\text{positive} \mid \text{disease}) — how the test behaves when someone is sick.
  • But the patient wants P(diseasepositive)P(\text{disease} \mid \text{positive}) — given my positive result, am I actually sick?

These two are not equal. Bayes' theorem is the bridge between them.


WHAT are the ingredients?

The definition of conditional probability is: P(AB)=P(AB)P(B),P(B)0.P(A\mid B) = \frac{P(A\cap B)}{P(B)}, \qquad P(B)\ne 0.


HOW: derivation from scratch

Expanding the denominator (Law of Total Probability)

If causes A1,,AnA_1,\dots,A_n are mutually exclusive and exhaustive (a partition of the sample space), then any evidence BB can only happen through one of them: P(B)=i=1nP(BAi)P(Ai).P(B) = \sum_{i=1}^{n} P(B\mid A_i)\,P(A_i).

Figure — Bayes' theorem — derivation and applications

Worked Example 1 — The classic false-positive shock

A disease affects 1%1\% of a population. A test is 99%99\% sensitive (P(+D)=0.99P(+\mid D)=0.99) and 95%95\% specific (P(Dˉ)=0.95P(-\mid \bar D)=0.95, so P(+Dˉ)=0.05P(+\mid\bar D)=0.05). You test positive. What is P(D+)P(D\mid +)?

Step 1 — list priors and likelihoods. P(D)=0.01,  P(Dˉ)=0.99,  P(+D)=0.99,  P(+Dˉ)=0.05.P(D)=0.01,\; P(\bar D)=0.99,\; P(+\mid D)=0.99,\; P(+\mid \bar D)=0.05. Why? We separate what we believe before (prior) from how the test behaves (likelihood).

Step 2 — total probability of a positive. P(+)=(0.99)(0.01)+(0.05)(0.99)=0.0099+0.0495=0.0594.P(+) = (0.99)(0.01) + (0.05)(0.99) = 0.0099 + 0.0495 = 0.0594. Why? A positive can come from a true sick person OR a healthy false positive.

Step 3 — apply Bayes. P(D+)=0.00990.05940.1667=16.7%.P(D\mid +) = \frac{0.0099}{0.0594} \approx 0.1667 = 16.7\%. Why the surprise? Because the disease is rare, the huge healthy group produces many false positives that swamp the few true positives.


Worked Example 2 — Two factories (partition)

Factory X makes 60%60\% of bulbs with 2%2\% defect rate; Factory Y makes 40%40\% with 5%5\% defect rate. A bulb is defective. From which factory did it likely come?

Step 1 — priors: P(X)=0.6, P(Y)=0.4P(X)=0.6,\ P(Y)=0.4. Likelihoods: P(DX)=0.02, P(DY)=0.05P(D\mid X)=0.02,\ P(D\mid Y)=0.05.

Step 2 — evidence: P(D)=(0.02)(0.6)+(0.05)(0.4)=0.012+0.020=0.032.P(D)=(0.02)(0.6)+(0.05)(0.4)=0.012+0.020=0.032. Why? Sum over both factories (they partition all bulbs).

Step 3 — posteriors: P(XD)=0.0120.032=0.375,P(YD)=0.0200.032=0.625.P(X\mid D)=\frac{0.012}{0.032}=0.375,\qquad P(Y\mid D)=\frac{0.020}{0.032}=0.625. Why? Even though X makes more bulbs, Y's higher defect rate makes it the more likely source of a defective one.


Forecast-then-Verify


Common Mistakes


Recall Feynman: explain to a 12-year-old

Imagine a box that flashes "DANGER!" whenever a monster is near — and it's really good, flashing 9999 out of 100100 times a monster comes. But monsters are super rare. So most of the time it flashes, it's actually a squirrel that tricked it. Before you panic, you must think: "How many monsters are there in the first place?" Bayes' theorem mixes how good the alarm is with how rare monsters really are to tell you the true chance. New clue → update your guess.


Active Recall Flashcards

State the definition of conditional probability P(AB)P(A\mid B).
P(AB)=P(AB)P(B)P(A\mid B)=\dfrac{P(A\cap B)}{P(B)}, for P(B)0P(B)\neq 0.
Write Bayes' theorem in its simplest form.
P(AB)=P(BA)P(A)P(B)P(A\mid B)=\dfrac{P(B\mid A)\,P(A)}{P(B)}.
What is the "prior" in Bayes' theorem?
P(A)P(A) — belief in the cause before seeing the evidence.
What is the "likelihood"?
P(BA)P(B\mid A) — probability of the evidence given the cause is true.
How do you expand P(B)P(B) for a partition A1,,AnA_1,\dots,A_n?
P(B)=iP(BAi)P(Ai)P(B)=\sum_i P(B\mid A_i)P(A_i) (Law of Total Probability).
Why can a 99%-accurate test still give a low P(disease+)P(\text{disease}\mid +)?
Because a low prior (rare disease) means false positives from the large healthy group dominate.
Name the fallacy of swapping P(AB)P(A\mid B) and P(BA)P(B\mid A).
The prosecutor's fallacy / transposed conditional.
In Example 2, why is the defective bulb more likely from the smaller factory Y?
Y's higher defect rate (0.05) outweighs X's larger share, once we condition on "defective".

Connections

  • Conditional Probability — the definition Bayes is built from.
  • Law of Total Probability — supplies the denominator P(B)P(B).
  • Independent Events — special case where P(AB)=P(A)P(A\mid B)=P(A).
  • Naive Bayes Classifier — machine-learning application.
  • Prior and Posterior Distributions — the continuous generalisation.
  • Tree Diagrams — visual bookkeeping for the joint probabilities.

Concept Map

written both ways

equate and divide

input

input

normaliser

yields

Law of Total Probability

flips

applied to

reveals

updates

Conditional probability

Shared joint P A and B

Bayes theorem

Prior P A

Likelihood P B given A

Evidence P B

Posterior P A given B

Partition of causes

Wrong-way-round conditional

Medical test example

False-positive shock

Updated belief with new data

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Bayes' theorem ka core idea simple hai: conditional probability ko ulta karna. Bahut baar humein pata hota hai P(evidencecause)P(\text{evidence}\mid \text{cause}) — jaise test positive aane ka chance jab bimari hai. Par patient ko chahiye P(causeevidence)P(\text{cause}\mid \text{evidence}) — positive aaya, ab sach mein bimari hai kya? Ye dono barabar nahi hote, aur Bayes theorem in dono ke beech ka pul hai.

Formula banta hai bahut seedhe se: P(AB)P(A\cap B) ko dono taraf se likho, phir barabar karke divide karo. Result: posterior == (likelihood ×\times prior) ÷\div evidence. Yaad rakhne ke liye "PLE over E" bol lo. Denominator P(B)P(B) ko hum saare causes pe sum karke nikalte hain — isko Law of Total Probability kehte hain.

Sabse bada lesson base rate ka hai. Ek 99%99\% accurate test bhi galat lag sakta hai agar bimari rare hai. Kyunki healthy log itne zyada hain ki unke false positives, real sick logon ke true positives ko dabaa dete hain. Isliye Example 1 mein answer sirf 16.7%16.7\% aata hai — chaunkane wala, par sahi. Jab prevalence 30%30\% kar do, wahi test 89.5%89.5\% trustworthy ho jaata hai. Matlab prior aur evidence dono important hain.

Exam tip: kabhi bhi P(AB)P(A\mid B) aur P(BA)P(B\mid A) ko mat mix karo (prosecutor's fallacy), aur P(B)P(B) nikalte waqt saare causes include karo warna posterior sum 11 nahi hoga. Bas prior, likelihood, evidence — teeno neatly likh lo, aur answer khud aa jaata hai.

Go deeper — visual, from zero

Test yourself — Statistics & Probability — Intermediate

Connections