Bayes Theorem

Bayes Theorem#

A doctor runs a test on a patient for a rare desease and the results is positive.

The test has an efficiency detection of 99% and a false positive rate of 1%.

Should the patient be worried ? The wrong impressions is that the patient has 99% probability to be sick.

What you need to have is some prior knowledge about the desease. Looking in literature it turns out that the disease affects the population only one person every thousand.

Let’s try to apply the Bayes theorem:

# P(positive | sick)
likelihood = 0.99 

# P(sick)
prior = 0.001

# Normalization = P(positive | sick)P(sick) + P(positive | healthy)P(healthy)
normalization = 0.99 * 0.001 + 0.01 * 0.999

# Posterior = P(sick | positive) 
# This is what you really want to know: given that you have been tested positive, 
# what is the probability that you are really sick
P = likelihood * prior / normalization

print (f"Probability to be sick being tested positive = {P}")
Probability to be sick being tested positive = 0.09016393442622951

One way to look at the apparently surprising result: a test with a false positive rate of 1% means that every 1000 people tested 10 will be positive, while the number of sick people in a population of 1000 should be only 1. You are among the 10 testes positive. The probability to be the sick one is 10%.

What if the doctor repeats the test and gets a positive a second time ? The prior now is different: you’ve being tested positive before so you should use as prior the posterior of the previous test

# P(positive | sick)
likelihood = 0.99 

# P(sick)
prior = 0.09

# Normalization = P(positive | sick)P(sick) + P(positive | healthy)P(healthy)
normalization = 0.99 * 0.09 + 0.01 * 0.91

# Posterior = P(sick | positive) 
# This is what you really want to know: given that you have been tested positive, 
# what is the probability that you are really sick
P = likelihood * prior / normalization

print(f"Probability to be sick being tested positive a second time = {P}")
Probability to be sick being tested positive a second time = 0.9073319755600815

The extra knowledge shifts dramatically the probability.