Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 2 · Bayes’ rule with natural frequencies

Level: Levels 1–2 (Going deeper: Level 3) · Prerequisites: L1-04 (count within a condition), L2-04 (conditional probability), L2-05 (reverse a condition with Bayes’ rule)

What you’ll learn:

  • Why P(positive∣condition)P(\text{positive}\mid\text{condition}) and P(condition∣positive)P(\text{condition}\mid\text{positive}) are different numbers.

  • How to compute a posterior by counting an imaginary group of 10,000 people.

  • Why a rare condition makes most positive results false alarms. This is the base-rate fallacy.

  • How to update twice with two tests, and why that needs a conditional-independence assumption.

Open in Colab

1 · Predict → Run → Explain: one positive test

A condition affects 1 person in 100. A test finds it in 90 % of people who have it. This is the test’s sensitivity. The test also wrongly flags 5 % of people who do not have it. This is its false-positive rate.

Predict. Someone tests positive. Is the chance they have the condition closer to 90 %, 50 %, or 15 %?

engine:  P(positive) = 0.0585,  P(condition | positive) = 0.1538
counts:  of 10,000 people, 100 have the condition and 90 of them test positive;
         9,900 do not, and 495 of them test positive.
         posterior by counting = 90 / (90 + 495) = 0.1538
Natural-frequency checks passed.

Explain. There are 585 positive results. Only 90 of them come from people with the condition. So the posterior is 90/585≈0.15490/585\approx0.154. This is a conditional probability: you count only inside the positive group Illowsky & Dean, 2023. The test is good, but the condition is rare. Among the 9,900 healthy people, 5 % test positive: that is 495 false alarms. Among the 100 sick people, 90 % test positive: that is 90. The false alarms win. The starting rate is called the prior, or base rate. Ignoring it and answering “90 %” is called the base-rate fallacy.

2 · An icon array of 10,000 people

Each small square is one person. The squares are sorted into four groups. Move the prior slider. Use the dropdown to choose the test’s sensitivity and false-positive rate. The title shows the engine’s posterior.

Loading...

Explain. Raise the prior and watch the red block grow next to the orange one. The test has not changed. Yet the posterior rises a lot. The same evidence means more when the condition is common.

3 · The same reasoning as a tree

A natural-frequency tree follows the 10,000 people through two splits. First: condition or not. Then: positive or not. The slider changes the prior (sensitivity 0.9, false-positive rate 0.05).

Loading...

4 · Posterior as a function of the prior

The next figure plots the posterior against the prior (log scale). The slider changes the sensitivity. The dropdown changes the false-positive rate. The dashed diagonal is “posterior = prior”, which is what a useless test gives.

Loading...

Explain. When the prior is small, lowering the false-positive rate moves the curve up far more than raising the sensitivity does. For rare conditions, false alarms from the large healthy group dominate.

5 · Predict → Run → Explain: a second test

The person who tested positive takes a second test. It has the same sensitivity and false-positive rate.

Predict. After two positives, is the posterior about 2×0.1542\times0.154, or much higher?

after one positive: 0.1538;  after two positives: 0.7660;  odds form: 0.7660
simulation, independent tests: 0.7692
correlated false alarms: each test alone still has false-positive rate 0.049,
  but P(condition | two positives) = 0.1406 (simulated 0.1404), not 0.7660
Sequential-updating checks passed.

Explain. With independent errors, each positive multiplies the odds by the likelihood ratio 0.9/0.05=180.9/0.05=18. So two positives raise the posterior to about 0.766. Now suppose healthy people’s false alarms come from a shared cause. Then the second positive mostly repeats the first one. In this extreme example it even lowers the posterior a little. Why? People with the condition can miss the second test, but the shared-trait false alarms never do. The formula is only as good as its independence assumption.

<Figure size 1300x494 with 2 Axes>

Going deeper

Bayes’ rule

For condition DD and positive result ++,

P(D∣+)=P(+∣D) P(D)P(+∣D) P(D)+P(+∣Dˉ) P(Dˉ),P(+)>0.P(D\mid +)=\frac{P(+\mid D)\,P(D)}{P(+\mid D)\,P(D)+P(+\mid \bar D)\,P(\bar D)}, \qquad P(+)>0 .

Odds form

With prior odds O(D)=P(D)/P(Dˉ)O(D)=P(D)/P(\bar D) and likelihood ratio Λ+=P(+∣D)/P(+∣Dˉ)\Lambda_+=P(+\mid D)/P(+\mid\bar D),

O(D∣+)=Λ+ O(D).O(D\mid +)=\Lambda_+\,O(D).

Two tests

In general O(D∣+1,+2)=P(+1,+2∣D)P(+1,+2∣Dˉ) O(D)O(D\mid +_1,+_2)=\dfrac{P(+_1,+_2\mid D)}{P(+_1,+_2\mid\bar D)}\,O(D). The joint likelihood factorizes only if +1⊥+2∣D+_1\perp +_2\mid D and +1⊥+2∣Dˉ+_1\perp +_2\mid\bar D. Then O(D∣+1,+2)=Λ+,1Λ+,2 O(D)O(D\mid +_1,+_2)=\Lambda_{+,1}\Lambda_{+,2}\,O(D). In the shared-trait example, P(+1,+2∣Dˉ)=0.05P(+_1,+_2\mid\bar D)=0.05 instead of 0.052. So the joint likelihood ratio is 0.81/0.05=16.20.81/0.05=16.2, smaller than the single-test value 18.

Negative results

P(D∣−)=(1−sens)P(D)(1−sens)P(D)+(1−fp)P(Dˉ)P(D\mid -)=\dfrac{(1-\text{sens})P(D)}{(1-\text{sens})P(D)+(1-\text{fp})P(\bar D)}, which is s.bayes(prior, 1 - sens, 1 - fp).

Exercises

Solution

0.00099/(0.00099+0.00999)≈0.0900.00099/(0.00099+0.00999)\approx0.090. When the base rate is 1 in 1,000, even an accurate-looking test gives mostly false alarms.

Solution

Set true and false positives equal: 0.01×0.9=0.99 f0.01\times0.9=0.99\,f, so f=0.009/0.99≈0.0091f=0.009/0.99\approx0.0091.

Solution

s.bayes(0.01, 0.1, 0.95) gives 0.001/(0.001+0.9405)≈0.001060.001/(0.001+0.9405)\approx0.00106. That is about a tenth of the prior.

Check yourself

All checks passed.
References
  1. Illowsky, B., & Dean, S. (2023). Introductory Statistics 2e. https://openstax.org/books/introductory-statistics-2e/pages/1-introduction