pie
ExamplesDocs

Exampless

  • Canvas
  • Reactivity
  • Broadcasting
  • Autodiff
  • Animation
  • UIs
  • Sorting
  • Line fit
  • Convex hull
  • Live convex hull
  • Dijkstra's algorithm
  • k-means
  • Gaussian mixture
  • PCA
  • SVM
  • Differential equations
  • Python
  • Matplotlib on the canvas

What does a positive test mean?

  • Counting people
  • The formula
  • Rare diseases are harder
  • Testing twice

This page has a glossary. Hover over a word with a dotted underline to see its definition, or tap it on a touch screen. The "more" link in the popup opens the full entry at the end of the page. The docs show how to add one to your own page.

A disease affects 1 in 100 people, so its prevalence is 1%. A test for it has a sensitivity of 90% and a specificity of 95%. You take the test and it comes back positive.

What is the chance you have the disease? Many people say about 90%. The answer is closer to 15%.


Counting people

The easiest way to see why is to count. Take 10,000 people and split them by what the test says. All editors on this page share their variables, so run them in order.

def count_positives(population, prevalence, sensitivity, specificity):
    sick = population * prevalence
    healthy = population - sick
    true_positives = sick * sensitivity
    false_positives = healthy * (1 - specificity)
    return true_positives, false_positives

sick_positive, healthy_positive = count_positives(10_000, 0.01, 0.90, 0.95)
print(f"sick and positive:    {sick_positive:.0f}")
print(f"healthy and positive: {healthy_positive:.0f}")

The test finds 90 of the 100 sick people. It also wrongly flags 5% of the 9,900 healthy people, which is 495 false positives. So 585 people test positive, and only 90 of them are sick.

The test is good. But healthy people outnumber sick people 99 to 1, so a small share of them is still a large crowd.


The formula

Bayes' theorem does the same counting in one line:

P(sick∣positive)=sick and positivesick and positive+healthy and positiveP(\text{sick} \mid \text{positive}) = \frac{\text{sick and positive}}{\text{sick and positive} + \text{healthy and positive}}P(sick∣positive)=sick and positive+healthy and positivesick and positive​

In Python, we can reuse count_positives with a population of 1:

def chance_sick(prevalence, sensitivity, specificity):
    sick_positive, healthy_positive = count_positives(1, prevalence, sensitivity, specificity)
    return sick_positive / (sick_positive + healthy_positive)

print(f"{chance_sick(0.01, 0.90, 0.95):.1%}")

Rare diseases are harder

Keep the same test and change only the prevalence:

for prevalence in [0.001, 0.01, 0.1, 0.5]:
    print(f"prevalence {prevalence:6.1%}: chance sick {chance_sick(prevalence, 0.90, 0.95):6.1%}")

The same positive result means about 2% for a very rare disease and about 95% for a common one. That is why doctors usually test people who already have symptoms: among them, the disease is far more common.


Testing twice

After one positive test, your chance of being sick is about 15%. That number can stand in for the prevalence of a second test. This assumes the two tests make their mistakes independently, which real tests may not.

chance = 0.01
for test in range(1, 4):
    chance = chance_sick(chance, 0.90, 0.95)
    print(f"after positive test {test}: {chance:.1%}")

One positive test is weak evidence here. Three in a row are strong evidence.

#math
Glossary
Prevalence

The fraction of a group that has a disease at a given time. If 1 in 100 people have it, the prevalence is 1%.

Sensitivity

The fraction of sick people that a test correctly flags as positive.

A sensitivity of 90% means the test finds 90 of every 100 sick people and misses the other 10. Those 10 are false negatives.

Specificity

The fraction of healthy people that a test correctly reports as negative.

A specificity of 95% means 95 of every 100 healthy people get a negative result, and the other 5 get a false positive.

False positive

A positive result for someone who doesn't have the disease.

The fraction of healthy people who get a false positive is 1 minus the specificity.

Bayes' theorem

A rule for updating a probability after seeing new evidence.

P(A∣B)=P(B∣A) P(A)P(B)P(A \mid B) = \frac{P(B \mid A)\, P(A)}{P(B)}P(A∣B)=P(B)P(B∣A)P(A)​

Here AAA is "sick" and BBB is "tests positive". P(B∣A)P(B \mid A)P(B∣A) is the sensitivity and P(A)P(A)P(A) is the prevalence. P(B)P(B)P(B) is the share of everyone who tests positive, sick or healthy.