← SciSim / Mathematics

Probability Spaces & Bayes Theorem

Prior, likelihood, posterior -- medical testing, base-rate neglect, and frequency tree visualization.
🎓 Tier: Standard Undergraduate -- Probability & Statistics

📊 Section 1 -- Interactive Simulation

P(A)
--
P(B|A)
--
P(B|Ac)
--
P(B)
--
P(A|B) Bayes
--
Likelihood ratio
--

Controls

Scenario

Display

💡 Section 2 -- The Idea, Step by Step

Picture a rare disease that only about 1 person in 100 actually has, and a test that is "pretty good." If your own test comes back positive, should you panic? Surprisingly, often not. Because the healthy crowd is so much larger than the sick crowd, most of the positive results in the whole population come from healthy people who were flagged by mistake. The real question is not "how accurate is the test?" but "out of everyone who tests positive, what share are truly sick?"

Three numbers settle it. The prior $P(A)$ is how common the condition is before any testing — here 1%. The likelihood (or sensitivity) $P(B|A)$ is how often a sick person tests positive — say 99%. The false-positive rate $P(B|A^c)$ is how often a healthy person tests positive — say 5%. Now count out 10,000 people. About 100 are sick, and 99 of them test positive. Of the 9,900 healthy people, 5% — about 495 — also test positive. So out of $99+495=594$ positive results, only 99 are real: $99/594 \approx 16.7\%$.

The idea in one line

$$P(A|B)=\frac{\text{truly positive}}{\text{everyone who tests positive}}=\frac{99}{99+495}\approx 0.167.$$

Written exactly, that fraction is Bayes' theorem: $P(A|B)=\dfrac{P(B|A)\,P(A)}{P(B|A)\,P(A)+P(B|A^c)\,P(A^c)}$. The denominator is just every way of testing positive — the sick branch plus the healthy branch — each weighted by how likely that group is to begin with (the law of total probability). The three sliders map straight onto the inputs: Prior $P(A)$, Sensitivity $P(B|A)$, and FP rate $P(B|A^c)$. The frequency tree on the canvas splits the 10,000 people exactly as above, and the posterior readout updates live as you drag.

Try this in the sim above. First, keep sensitivity at 0.99 but drag the prior from 0.01 up toward 0.5 and watch the posterior leap from about 17% to over 95% — the base rate, not the test, is doing most of the work. Second, set the FP rate down to 0.001 and see how a single positive becomes almost conclusive even for a rare disease. Third, press Animate to sweep the prior across its whole range and watch the posterior curve climb — gentle at first, then steeply once the condition stops being rare.

📐 Section 3 -- Probability Spaces & Bayes

Bayes' Theorem

$$P(A|B)=\frac{P(B|A)P(A)}{P(B)}=\frac{P(B|A)P(A)}{P(B|A)P(A)+P(B|A^c)P(A^c)}.$$

TermMeaningExample (disease)
$P(A)$Prior probability of eventPrevalence: 1%
$P(B|A)$Likelihood / sensitivityTest+|Disease: 99%
$P(B|A^c)$False positive rateTest+|No disease: 5%
$P(A|B)$Posterior probabilityDisease|Test+: ~16.7%
Step 1 -- Probability Space

$(\Omega,\mathcal{F},P)$ where $\Omega$ = sample space, $\mathcal{F}$ = $\sigma$-algebra of events, $P$ = probability measure. Axioms: $P(\Omega)=1$, $P(A)\geq0$, countable additivity $P(igcup A_i)=\sum P(A_i)$ for disjoint $A_i$.

Step 2 -- Conditional Probability

$P(A|B)=P(A\cap B)/P(B)$ for $P(B)>0$. Intuition: restrict the sample space to $B$ and re-normalize. This is NOT the same as $P(B|A)$ -- the classic base-rate neglect error.

Step 3 -- Law of Total Probability

If $\{B_i\}$ is a partition of $\Omega$: $P(A)=\sum_i P(A|B_i)P(B_i)$. For two events: $P(B)=P(B|A)P(A)+P(B|A^c)P(A^c)$. This is the denominator in Bayes.

Step 4 -- Base Rate Neglect (Worked Example)

Disease prevalence 1%, sensitivity 99%, false positive 5%. Positive test: $P(D|+)=0.99 imes0.01/(0.99 imes0.01+0.05 imes0.99)=0.0099/0.0099+0.0495)=0.0099/0.0594pprox16.7\%$. Despite 99% sensitivity, most positives are false -- because disease is rare. Move the slider!

Step 5 -- Independence

$A,B$ independent iff $P(A\cap B)=P(A)P(B)$ iff $P(A|B)=P(A)$ (knowing $B$ gives no information about $A$). Independence is symmetric: $A\perp B\Rightarrow B\perp A$. Pairwise independence does NOT imply mutual independence (see Bernstein example).

Step 6 -- Odds Form of Bayes

Odds $O(A)=P(A)/P(A^c)$. Posterior odds $=$ prior odds $ imes$ likelihood ratio: $ rac{P(A|B)}{P(A^c|B)}= rac{P(A)}{P(A^c)}\cdot rac{P(B|A)}{P(B|A^c)}$. The likelihood ratio $P(B|A)/P(B|A^c)$ is how much the evidence updates the odds.

Reference: Ross -- A First Course in Probability, §3; Grimmett & Stirzaker -- Probability & Random Processes §1-2; MIT OCW 18.650.

❓ Section 4 -- FAQ

🧮Conceptual  What is the difference between P(A|B) and P(B|A)?

They are completely different. P(Disease|Test+) is what you want (probability of disease given a positive test). P(Test+|Disease) is the sensitivity -- probability of a positive test given you have disease. Confusing them is the "inverse probability fallacy" or "prosecutor's fallacy." Bayes converts one into the other.

Key takeaway: P(A|B) and P(B|A) are different quantities. Bayes theorem converts between them using P(A) and P(B).
🔬Simulation  What does the main canvas show?

A frequency tree (natural frequencies): 10000 people, P(A)*10000 have condition A. Of those, P(B|A)*[count A] test positive. Of the others P(A^c)*10000, P(B|Ac)*[count Ac] test positive. The posterior P(A|B) is the fraction of all positives who truly have A. Visualizing natural frequencies dramatically reduces base-rate neglect errors.

Key takeaway: Natural frequency visualization: out of 10000 people, count who are truly positive vs false positive.
🌍Applied  Where is Bayes theorem used?

Medical diagnosis (as shown). Spam filters: P(spam|contains "free") updated by each word. Bayesian inference in ML: update parameter distribution as data arrives. Search and rescue: update location probability as new evidence arrives. Credit scoring: update default probability with payment history. DNA forensics.

Key takeaway: Medical diagnosis, spam filters, Bayesian ML, search/rescue, DNA forensics -- all Bayes theorem.
💡Non-Obvious  A 99% accurate test gives only 50% posterior probability?

Yes -- when prevalence is 1% and false positive rate is ~1%. Example: 99% sensitivity, 1% FPR, 1% prevalence. P(D|+) = 0.99*0.01/(0.99*0.01+0.01*0.99) = 0.5. The test is 50-50! This is why screening tests for rare diseases require confirmatory testing.

Key takeaway: With 1% prevalence and 1% FPR, even a 99% sensitive test gives only 50% posterior -- base rate dominates.
📐Computational  P(A)=0.3, P(B|A)=0.7, P(B|Ac)=0.2. Find P(A|B).

P(B) = 0.7*0.3 + 0.2*0.7 = 0.21+0.14 = 0.35. P(A|B) = 0.7*0.3/0.35 = 0.21/0.35 = 0.6. So knowing B occurred increases probability of A from 30% to 60%.

Key takeaway: Bayes: posterior = 0.21/0.35 = 0.6. Prior 30% updated to posterior 60% upon observing B.
🎓Deep  What is Bayesian vs frequentist probability?

Frequentist: probability = long-run frequency in repeated experiments. P(heads) = fraction of heads in many flips. Cannot assign probability to one-off events. Bayesian: probability = degree of belief. Can say P(hypothesis|data). Uses prior distribution updated by likelihood to get posterior. Machine learning largely uses Bayesian thinking; classical statistics uses frequentist. Both are self-consistent frameworks.

Key takeaway: Frequentist: probability = long-run frequency. Bayesian: probability = degree of belief, updated by Bayes theorem.
Best resources: 3Blue1Brown -- Bayes theorem visual; MIT OCW 18.650; Ross -- First Course in Probability.

⚠️ Section 5 -- Misconceptions & Common Errors

A · Conceptual Misconceptions
❌ Misconception: P(A|B) = P(B|A) -- the inverse probability fallacy.✅ Correction: P(A|B) and P(B|A) are completely different. P(positive|disease) is sensitivity; P(disease|positive) is what we want. Bayes converts between them using base rates.🔍 Students (and prosecutors) confuse conditional probability direction.📖 Ross -- A First Course in Probability §3.
❌ Misconception: Independent events are mutually exclusive.✅ Correction: They are opposite concepts. Mutually exclusive: P(A and B)=0. Independent: P(A and B)=P(A)P(B). If A and B are mutually exclusive with positive probability, they are dependent (knowing A occurred means B cannot).🔍 Students conflate independence with mutual exclusion.📖 Grimmett & Stirzaker §1.5.
❌ Misconception: P(A|B) + P(Ac|B) = 1 and P(A|B) + P(A|Bc) = 1.✅ Correction: Only the first is true -- given B, the probabilities sum to 1. But P(A|B) + P(A|Bc) need not equal 1 (they are from different conditional spaces).🔍 Students incorrectly apply the complement rule across different conditioning events.📖 Ross §3.4.
B · Common Procedural Errors
❌ Error: Writing P(A and B) = P(A) * P(B) for all events.✅ Correct: P(A and B) = P(A|B)*P(B) = P(B|A)*P(A). Equals P(A)*P(B) ONLY when A and B are independent.🔍 Students apply the multiplication rule for independent events to all events.📖 Ross §3.
❌ Error: Denominator of Bayes: P(B) = P(B|A) + P(B|Ac) without weighting.✅ Correct: P(B) = P(B|A)P(A) + P(B|Ac)P(Ac). Must weight by prior probabilities.🔍 Students add likelihoods without multiplying by priors.📖 Grimmett & Stirzaker §1.6.
❌ Error: After drawing two cards without replacement, assuming independence.✅ Correct: P(2nd Ace | 1st Ace) = 3/51 ≠ 4/52 = P(Ace). Sampling without replacement destroys independence.🔍 Students assume independence in sequential sampling without replacement.📖 Ross §3.4.
Education research: Kahneman, Slovic, Tversky -- Judgment under Uncertainty (1982).