COVID-19, Bayes’ theorem and taking probabilistic decisions. | by Archit Ranjan | Towards Data Science

Note from the editors: Towards Data Science is a Medium publication primarily based on the study of data science and machine learning. We are not health professionals or epidemiologists, and the opinions of this article should not be interpreted as professional advice. To learn more about the coronavirus pandemic, you can click here.
No test is 100% accurate to detect the novel coronavirus! However, we feel satisfied when we are told that the tests are 98.5% accurate in detecting COVID infections. But what does this accuracy actually mean?
Before we answer this question, let’s review some basic concepts.
True positive: A person with COVID-19 tests positive for COVID-19False positive: A person without COVID-19 tests positive for COVID-19False negative: A person with COVID-19 tests negative for COVID-19True negative: A person without COVID-19 tests negative for COVID-19
True positive: A person with COVID-19 tests positive for COVID-19
False positive: A person without COVID-19 tests positive for COVID-19
False negative: A person with COVID-19 tests negative for COVID-19
True negative: A person without COVID-19 tests negative for COVID-19
Now let’s review what we mean by accuracy, precision, sensitivity and specificity.
Accuracy = (true positives + true negatives) / all results
Precision = true positives / (true positives + false positives)
Sensitivity = true positives / (true positives + false negatives)
Specificity = true negatives / (true negatives + false positives)
Now suppose there are 200 patients. Lets say 100 patients are infected and 100 patients are not. Suppose 99 of the 100 infected patients were tested positive and 2 healthy patients were also tested positive. In other words, 99 people were truly positive, and 2 people were falsely positive. 1 infected person was falsely negative, and 98 not infected patients were truly negative. Now let’s calculate what we just studied.
Accuracy = (99 + 98) / 200 = 0.985Precision = 99 / (99 + 2) = 0.98Sensitivity = 99 / (99 + 1) = 0.99Specificity = 98 / (98 + 2) = 0.98
Looks good till now, right? Now let’s try to understand more about specificity. It means that out of 100 patients who did not have covid-19, only 98 people tested negative. So, given that a person doesn’t have the virus, probability that he will test negative is 0.98.
Now let’s dive deeper! Probability that given a person who has has covid, tests positive will be given by the conditional probability, P(+|C+) = sensitivity = 0.99.Similarly, Probability that a healthy patient will test negative will beP(-|C−) = specificity = 0.98.
We got the conditional probabilities, but what use is that? Now let’s say we pick up a random person from a population and test him. He tested positive, but what is the probability that he actually has the novel coronavirus. In other words, here we are interested to find P(C+|+).
Now according to Bayes’ theorem, P(A|B) = P(A)P(B|A) / (P(A)P(B|A) + P(not A)P(B|not A) )
So, if we know P(C+), we can easily calculate P(C+|+) by plugging in the values in the above equation. So, to calculate P(C+), you can just just divide the number of cases in your country by the total population. In my case, it is less than 0.0001 which is quite rare. So lets calculate P(C+|+).
0.005! Did you guess it? I bet not! This means that even if a random person is tested positive, there’s a less than 0.5% chance that he actually has the virus.
Now let’s say you are living in a more prone area and now your chances of catching coronavirus has increased 10 fold. Even then, P(C+|+) = 0.047, i.e, if you test positive, you have a less than 5% chance that you are actually infected.
This is a problem with all rare diseases.
Okay, so we understood why it would be a dumb idea to test random people for COVID-19. Also, if you are living in an area where you are more likely to catch the virus, still testing positive would mean a less than 5% chance that you are infected. But wait, what about pooled testing?
Let the probability that a person living in red zone has COVID-19 is 0.001 to 0.005. Recently a news came that Wuhan is planning to test all 11 million residents after fresh cases began to arrive. According to the current protocol, 40 samples can be tested in 16 RT-PCR tests (up to 3 positives). This can go up to 961 samples that can be tested in 93 RT-PCR tests (up to 10 positives).
Suppose we pool x samples and probability that a person has Covid is p. Then, probability that atleast 1 person will have Covid is given by
P =  1 - (1-p)^x  
Now, let’s make some rules. We want to chose x such that if a pool of x samples is tested positive, we want the probability that at least 1 person has Covid-19 to be more than 90%. I’ll use R as a programming tool.
Here, pc denotes the probability that at least one of x samples is infected.prob is P(C+|+), calculated using Bayes’ theorem. Now, we plot x(number of samples) vs p(probability that a person has covid).
So, after surveying the number of confirmed cases in a locality, if we want to test the entire population for covid-19, we can use this model to chose the number of samples for pool testing.
So, this helps us to take probabilistic decisions to chose the number of samples. I have chosen p to be 90% which might be less for doctors to have confidence. On the other hand, increasing the number of samples raises other issues. So, parameters can be adjusted by experts before taking their final decision. Thanks for reading!