# Python – Bayes’ theorem – Drug testing example. 99% is not enough!

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.

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.

resulting in:

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

Tagged with: , , , ,