For our example, we measure the probability of a person to be infected if the test is positive \n",
" $P(Infected|PositiveTest)=\\frac{P(PositiveTest|Infected) \\cdot P(Infected)}{P(PositiveTest)}, P(PositiveTest) \\neq 0$ \n",
"where $P(PositiveTest)=P(PositiveTest|Infected) \\cdot P(Infected)+P(PositiveTest|not Infected) \\cdot P(not Infected)$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "20acd6ae",
"metadata": {},
"outputs": [],
"source": [
"def prob_infected_if_test_positive(p_true_positive,p_infected,p_false_positive):\n",
" numerator=p_true_positive*p_infected\n",
" denominator=numerator+p_false_positive*(1-p_infected)\n",
" if denominator!=0:\n",
" return numerator/denominator\n",
" else:\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "74ae0061",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of person being infected if the test is positive: 0.08411214953271028\n"
]
}
],
"source": [
"p_true_positive=0.9 # if the person is infected, the test is positive for 90% of cases\n",
"p_infected=0.02 # the fraction of people in the population to be infected\n",
"p_false_positive=0.2 # if the person is not infected, the test is positive for 20% of cases\n",
"# compute the probability of being infected if the test is positive\n",
"result=prob_infected_if_test_positive(p_true_positive,p_infected,p_false_positive)\n",
"print(f'Probability of person being infected if the test is positive: {result}')"
]
},
{
"cell_type": "markdown",
"id": "4276ec61",
"metadata": {},
"source": [
"**Law of total probability:**
\n",
"$P(B)=\\sum_i P(B|A_i)P(A_i)$\n",
"
**Example:** We have three vases.\n",
"- vase one has 3 red balls and 6 blue balls.\n",
"- vase two has 2 red balls and 8 blue balls\n",
"- vase three has 6 red balls and 3 blue balls\n",
"\n",
"We may choose each vase with the equal probability. Then, what is the probability of drawing a red ball from vases?"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3c8266f5",
"metadata": {},
"outputs": [],
"source": [
"def law_of_total_prob(p_vases=[1/3,1/3,1/3],reds=[3,2,6],blues=[6,8,3]):\n",
" p_reds=[]; p_drawn_red=0; p_drawn_blue=0\n",
" for red,blue in zip(reds,blues):\n",
" p_reds.append(red/(red+blue))\n",
" for p_red,p_vase in zip(p_reds,p_vases):\n",
" p_drawn_red+=p_red*p_vase\n",
" p_drawn_blue+=(1-p_red)*p_vase\n",
" return p_drawn_red,p_drawn_blue"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "06446a2d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of drawing a red ball: 0.39999999999999997\n"
]
}
],
"source": [
"print(f'Probability of drawing a red ball: {law_of_total_prob()[0]}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ae0ff9e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}