{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Bayes' Rule in Real Life" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Simplified Bayes' Rule**\n", "$$ P(A|B) \\propto P(B|A) \\cdot P(A) $$" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.)\n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is our prior belief?\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "prior = .5 # Prior belief of having contracted COVID-19" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is our likelihood?\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "sens = .8\n", "spec = .97" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:\n", "\n", "$$ P(\\text{TEST- & COVID-}) = P(COVID-) \\cdot P(TEST-|COVID-) $$\n", "$$ P(\\text{TEST+ & COVID-}) = P(COVID-) \\cdot P(TEST+|COVID-) $$\n", "$$ P(\\text{TEST- & COVID+}) = P(COVID+) \\cdot P(TEST-|COVID+) $$\n", "$$ P(\\text{TEST+ & COVID+}) = P(COVID+) \\cdot P(TEST+|COVID+) $$\n", "\n", "Percent chance of true negatives and true positives are located in the top-left and bottom-right positions, respectively." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "table = np.zeros((2,2))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "table[0,0] = (1-prior) * spec\n", "table[1,0] = (1-prior) * (1 - spec)\n", "table[0,1] = prior * (1-sens)\n", "table[1,1] = prior * sens" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | COVID- | \n", "COVID+ | \n", "
---|---|---|
TEST- | \n", "0.485 | \n", "0.1 | \n", "
TEST+ | \n", "0.015 | \n", "0.4 | \n", "