The Bayes’ theorem is interesting, as far as it is both quite challenging to understand and **shocking** once you really realize the simplicity of the numbers behind it.

*It describes the probability of an event, based on prior knowledge of conditions that might be related to the event. For example, if the probability that someone has cancer is related to their age, using Bayes’ theorem the age can be used to more accurately assess the probability of cancer than can be done without knowledge of the age.*

This is the example within Wikipedia, that I liked, thus I have decided to simply write an article about it, “translating” it into Python:

*Suppose that a test for using a particular drug is 99% sensitive and 99% specific. That is, the test will produce 99% true positive results for drug users and 99% true negative results for non-drug users. Suppose that 0.5% of people are users of the drug. *

*What is the probability that a randomly selected individual with a positive test is a drug user?*

So, let’s start with some analysis. “99% true positive results” for drug test users means that in 99 out of 100 times, when the test says “Positive”, it is correct. And “99% true negative results” means, that in 99 out of 100 times, when the test says “Negative”, it is correct.

1 2 3 4 5 6 7 8 9 10 |
# test is n% sensitive and specific: # test will produce n% true positive results for drug tests users # test will produce n% true negative results for drug tests users # m% = 1 - n% n = 0.99 m = 1 - n # 0.5% of the population are users of the drug users_of_drug = 0.005 non_users_of_drug = 1 - users_of_drug |

1 2 |
# probability that a randomly selected person with positive test is a drug user: (n * users_of_drug)/(n * users_of_drug + m * non_users_of_drug) |

After we run the code above, we get exactly the 33.2%, as in the Wikipedia article. So, with other words, if we are tested whether we take a drug, on the condition that 99% of the times the test is correct (in both directions – true positive and true negative) and about 0.5% of the populations are drug users, then the test has will be right only in 1 out of 3 times, when it says **True**. Somehow, the fantastic 99% accuracy seems to be **a complete failure** – 33.2 % is insufficient in all the grading systems I am aware of. This is because only **0.5%** of the population is having the symptoms. And if it was not for the Bayes’ theorem, I would think that a 99% of accuracy is somehow quite ok.

Made a function with calculation, seeing what are the results with 99.9% and 99.99% in sensitivity and specificity.

1 2 3 4 5 6 7 8 9 |
def are_they_on_drugs(n, users_of_drugs): m = 1 - n non_users_of_drugs = 1 - users_of_drugs return (n * users_of_drug)/(n * users_of_drug + m * non_users_of_drug) print(are_they_on_drugs(0.99, 0.005)) print(are_they_on_drugs(0.999, 0.005)) print(are_they_on_drugs(0.9999, 0.005)) print(are_they_on_drugs(0.99999, 0.005)) |

resulting in:

1 2 3 4 |
0.33221476510067094 0.8338898163606009 0.9804863698764484 0.9980139324138296 |

Well, with 99,999% the results look somehow ok.