Using Bayes’ Rule in Real Life

Simplified Bayes’ Rule

\[P(A|B) \propto P(B|A) \cdot P(A)\]
[22]:
import numpy as np
import pandas as pd

Let’s say that I want to know if I have COVID. The tests do not actually tell us if we COVID or not. These tests have sensitivities and specificities which are conditional probabilities. Because we use these to update our beliefs, we call them likelihoods. The most accepted COVID tests have a specificity >97% and a sensitivity >80%. (I’ll put a link here.)

We are going to go through a real-life example involving an individual named Alex and COVID tests. The events where Alex is or is not infected by COVID-19 will be denoted as \(COVID+\) and \(COVID-\), respectively. The events where Alex tests positive or negative for COVID will be denoted as \(TEST+\) and \(TEST-\), respectively. Specificity and sensitivity represent the true negative rate and true positive rate, respectively. This means specificity = \(P(TEST-|COVID-)\) and sensitivity = \(P(TEST+|COVID+)\). In other words, specificity is the chance of receiving a negative result if the true state of nature is negative, and sensitivity is the chance of receiving a positive result if the true state of nature is positive.

What is our prior belief?

Let’s say that Alex was exposed to COVID yesterday and their friend started displaying symptoms today. Upon hearing this from their friend, Alex will automatically begin forming a prior belief about how likely it was they contracted COVID from their friend. This will depend on their proximity to each other the day before, the length of exposure, immunizations, and trust in their immune system and information about COVID-19. For simplicity, let’s say that after considering these factors, Alex believes he has about 50/50 chance of having contracted COVID and decides that is enough to get tested.

[13]:
prior = .5 # Prior belief of having contracted COVID-19

What is our likelihood?

Alex gets a PCR test done which will have the specificity and sensitivity defined before. These will become part of Bayes’ formula as soon as Alex receives a test result. Let us investigate the more likely case, where Alex’s test result is negative.

[14]:
sens = .8
spec = .97

Using the sensitivity and specifity, we can build a table that represents the joint probability mass function of these two events. Because both random variables are discrete, we will have a 2x2 table. We will put the true states of nature in the columns and the test results in the rows. We can find the probability of each combination of events happening like so:

\[P(\text{TEST- & COVID-}) = P(COVID-) \cdot P(TEST-|COVID-)\]
\[P(\text{TEST+ & COVID-}) = P(COVID-) \cdot P(TEST+|COVID-)\]
\[P(\text{TEST- & COVID+}) = P(COVID+) \cdot P(TEST-|COVID+)\]
\[P(\text{TEST+ & COVID+}) = P(COVID+) \cdot P(TEST+|COVID+)\]

Percent chance of true negatives and true positives are located in the top-left and bottom-right positions, respectively.

[15]:
table = np.zeros((2,2))
[20]:
table[0,0] = (1-prior) * spec
table[1,0] = (1-prior) * (1 - spec)
table[0,1] = prior * (1-sens)
table[1,1] = prior * sens
[24]:
df = pd.DataFrame(table, index=['TEST-', 'TEST+'], columns=['COVID-', 'COVID+'])
df
[24]:
COVID- COVID+
TEST- 0.485 0.1
TEST+ 0.015 0.4

What is the posterior?

Now that we can see the probability of each combination of events and we know that Alex’s test came back negative, we only need to consider the probabilities in the first row. Then the probabilities .485 and .1 become .83 and .17 so that they sum to 1. And after receiving this one test, Alex can conclude that they have only a .17, or 17 percent, chance of having contracted COVID-19.

[27]:
print(.485  / (.485 + .1))
print(.1  / (.485 + .1))
0.8290598290598291
0.17094017094017097
[25]:
posterior = table[0,1] / table[0,:].sum()
[26]:
posterior
[26]:
0.17094017094017092

What to do with Bayes’ Rule?

Using this example, any individual might be able to decide whether it is safe to visit relatives or whether they should social distance or even quarantine. However, for a business owner with employees, it would enable them to decide whether it is too risky to allow a sick employee come in to work. In addition, the business owner should keep in mind the effectiveness of a test, namely the specificity and sensitivity and also consider selecting tests that favor one over the other. In this example, for instance, the tests have a higher specificity so that Alex’s chances of receiving a false positive on the test are close to 0 (0.015), but some employers might prefer more sensitive tests (and less specific) so that false positives are more common. The costs of false positives versus false negatives (and even probabilities of each event) will depend on the individual’s utility function and sometimes decision makers have control over which attributes they get to see in a test or experiment. We will discuss this in the next section.