{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Lea_COVID19.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "1", "colab_type": "text" }, "source": [ "\n", "# Bayesian Reasoning with Lea: the COVID-19 Case\n", "\n", "by Pierre Denis\n", "\n", "---\n", "The present coronavirus pandemic highlights the question of the detection of the disease and the significance of symptom occurrences for this purpose. Generally speaking, common reasoning is misleading for this detection because it has several psychological biases, which are nowadays increased by alarming mass media news (as for the present crisis). This can cause wrong evaluation of the risks of having false positives or false negatives.\n", "\n", "The present page presents a simple probabilistic model showing the occurrence of symptoms (fever, cough) that may be caused by some diseases (cold, flu, COVID-19). Once this model is set up, several queries can be made for calculating, among others, the probabilities of\n", "\n", "* having given symptom(s) in the presence/absence of given disease\n", "* or having a given disease in the presence/absence of some symptom(s)\n", "\n", "This model uses Bayesian reasoning and noisy-OR techniques. It is largely inspired from the noisy-OR model given in [this presentation of R. Blutner](http://www.blutner.de/Intension/Noisy%20OR.pdf), as well as in the [\"Artificial Intelligence: A Modern Approach\" book (2nd ed.) of S. Russell and P. Norvig](http://aima.cs.berkeley.edu/).\n", "\n", "The model is implemented using [Lea](https://bitbucket.org/piedenis/lea/src/dev_lea3/), a Python package dedicated to probabilistic programming. The present page allows executing the command interactively, experimenting the influence of parameter change, doing what-if analysis and even enriching the model (refer to [Lea tutorials](https://bitbucket.org/piedenis/lea/wiki/Home)).\n", "\n", "***CAVEAT***: *The present model is merely a toy model provided for the example. Neither the model nor the probability values it uses have been endorsed by any medical/scientific authority! The numerical results obtained here have therefore no significance for the real world.*\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "FWd6GUCpCNhW" }, "source": [ "## Prerequisites\n", "\n", "Before running the examples below, you have to import a set of functions from the `lea` module:\n", "\n", "**-> to do so, click on the button located beside the command below!**" ] }, { "cell_type": "code", "metadata": { "id": "EAAgkvxE_Tn9", "colab_type": "code", "colab": {} }, "source": [ "from lea import __version__, event, if_, joint, P\n", "print (\"Using Lea\",__version__)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "243", "colab_type": "text" }, "source": [ "## Defining the Probabilistic Model\n", "\n", "For a given patient, the model defines two symptom random variable (fever, cough), each of which can be either true or false. Each of these two symptoms can be caused by three diseases, modeled also by random variables: cold, flu and COVID-19.\n", "![covid19.png](https://bitbucket.org/repo/BpoAoj/images/3544436633-covid19.png)\n", "\n", "The modelling starts by defining *prior* probability distributions of the three diseases (arbitrary values, not found in afore-mentioned references):\n", "\n", "**-> click in sequence on the buttons located beside the commands below!**" ] }, { "cell_type": "code", "metadata": { "id": "246", "colab_type": "code", "colab": {} }, "source": [ "cold = event(0.200)\n", "flu = event(0.100)\n", "covid19 = event(0.005)\n", "print (\"OK (prior probabilities set)\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "aW4Dhannti17", "colab_type": "text" }, "source": [ "At any time, you can check what is the probability of a given random variable by using the `P` function. For instance:" ] }, { "cell_type": "code", "metadata": { "id": "ywDdVJMFtyTy", "colab_type": "code", "colab": {} }, "source": [ "P(cold)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "245", "colab_type": "text" }, "source": [ "Let's now define the conditional probabilities of fever if the occurrence of one single disease:\n" ] }, { "cell_type": "code", "metadata": { "id": "244", "colab_type": "code", "colab": {} }, "source": [ "fever_if_sole_cold = event(0.4)\n", "fever_if_sole_flu = event(0.8)\n", "fever_if_sole_covid19 = event(0.9)\n", "print (\"OK (fever conditional probabilities set)\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "s7blaBLVuNzB", "colab_type": "text" }, "source": [ "... and to the same for the cough symptom:" ] }, { "cell_type": "code", "metadata": { "id": "bhjdyRUquOHG", "colab_type": "code", "colab": {} }, "source": [ "cough_if_sole_cold = event(0.8)\n", "cough_if_sole_flu = event(0.2)\n", "cough_if_sole_covid19 = event(0.6)\n", "print (\"OK (cough conditional probabilities set)\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "247", "colab_type": "text" }, "source": [ "All probability values defined above can be changed as you wish. To take effect, you have just to (re-)execute the statements by clicking the button on the left.\n", "\n", "Now, the following construct defines the `fever` and `cough` random variables as caused by the three diseases, in a so-called *noisy-OR* construct:" ] }, { "cell_type": "code", "metadata": { "id": "248", "colab_type": "code", "colab": {} }, "source": [ "fever_by_cold = if_(cold , fever_if_sole_cold , False)\n", "fever_by_flu = if_(flu , fever_if_sole_flu , False)\n", "fever_by_covid19 = if_(covid19, fever_if_sole_covid19, False)\n", "cough_by_cold = if_(cold , cough_if_sole_cold , False)\n", "cough_by_flu = if_(flu , cough_if_sole_flu , False)\n", "cough_by_covid19 = if_(covid19, cough_if_sole_covid19, False)\n", "fever = fever_by_cold | fever_by_flu | fever_by_covid19\n", "cough = cough_by_cold | cough_by_flu | cough_by_covid19\n", "print (\"OK (probabilistic model set up)\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "249", "colab_type": "text" }, "source": [ "The `fever_by_cold` variable can be interpreted as \"*having fever due to a cold*\". In case of cold, it has some probability to be true, namely the number defined for `prob_fever_if_sole_cold`. If there is no cold, `fever_by_cold` is surely false, i.e. either there is no fever or the fever is not caused by cold.\n", "\n", "Then, the `fever` variable is defined by making a disjunction of three `fever_by_`... variables. The same applies to define `cough` variable as a disjunction of the three `cough_by_`... variables." ] }, { "cell_type": "markdown", "metadata": { "id": "bQ1yDpPv7m4s", "colab_type": "text" }, "source": [ "## Querying the Probabilistic Model\n", "\n", "Now that the model is defined, it can be queried in various ways to get new probabilities, possibly based on new information or assumptions. \n", "\n", "***reminder: the calculated values uses the arbitrary probabilities defined above, hence have no guaranteed significance for the real world!***\n", "\n", "**Note: to run the following queries, you must have executed the statements of the previous section.**" ] }, { "cell_type": "markdown", "metadata": { "id": "sRzIAGjivUHC", "colab_type": "text" }, "source": [ "Q1: What is the probability of having fever?" ] }, { "cell_type": "code", "metadata": { "id": "tQiOPjkQ7s8G", "colab_type": "code", "colab": {} }, "source": [ "P(fever)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "yvr29oaKXPYl", "colab_type": "text" }, "source": [ "Q2: What is the probability of having COVID-19 in the occurrence of fever (presence or absence of cough is unknown)?" ] }, { "cell_type": "code", "metadata": { "id": "00qvaYQ_7x9v", "colab_type": "code", "colab": {} }, "source": [ "P(covid19.given(fever))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "0u0oEr_TWnLc", "colab_type": "text" }, "source": [ "Q3: What is the probability of having COVID-19 in the occurrence of fever and cough?" ] }, { "cell_type": "code", "metadata": { "id": "Z5Hpc3QAz8bG", "colab_type": "code", "colab": {} }, "source": [ "P(covid19.given(fever,cough))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "CcD_UV-JYAPY", "colab_type": "text" }, "source": [ "Q4: What is the probability of having COVID-19 in the occurrence of fever but without cough?" ] }, { "cell_type": "code", "metadata": { "id": "ziyrPMH9YJcw", "colab_type": "code", "colab": {} }, "source": [ "P(covid19.given(fever,~cough))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "91qyk1J5aE0q", "colab_type": "text" }, "source": [ "Q5: What is the probability of having COVID-19 in the absence of fever and cough (ie being *asymptomatic* for this disease)?" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "hmXkosJcaana", "colab": {} }, "source": [ "P(covid19.given(~fever,~cough))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "VVVAnOVEXrdJ", "colab_type": "text" }, "source": [ "Q6: What is the probability of having flu in the occurrence of fever and cough?" ] }, { "cell_type": "code", "metadata": { "id": "-fv4P3MH0NLG", "colab_type": "code", "colab": {} }, "source": [ "P(flu.given(fever,cough))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "2ERWGoa6b_Oh", "colab_type": "text" }, "source": [ "Q7: What is the probability of having COVID-19 in the occurrence of fever and cough, knowing that it's not due to a cold?" ] }, { "cell_type": "code", "metadata": { "id": "DIbz8GWYb_2X", "colab_type": "code", "colab": {} }, "source": [ "P(covid19.given(fever,cough,~cold))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "miZvAngxbTGB", "colab_type": "text" }, "source": [ "Q8: What is the probability of having COVID-19 in the occurrence of fever and cough, knowing that it's due neither to a cold nor to a flu?" ] }, { "cell_type": "code", "metadata": { "id": "4fgl1g06bqQa", "colab_type": "code", "colab": {} }, "source": [ "P(covid19.given(fever,cough,~cold,~flu))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "6RBjQ4kNSkcl", "colab_type": "text" }, "source": [ "For getting more results in one single step, it is also possible to produce tabular data." ] }, { "cell_type": "markdown", "metadata": { "id": "LLsJ8ByiVADf", "colab_type": "text" }, "source": [ "Q9: What are the probabilities of each symptom combination, independently of any information about a possible disease?" ] }, { "cell_type": "code", "metadata": { "id": "5Y-OkOeZVA_k", "colab_type": "code", "colab": {} }, "source": [ "joint(fever,cough)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "UIZMffGzS56R", "colab_type": "text" }, "source": [ "Q10: What are the probabilities of each symptom combination, depending of presence/absence of COVID-19?" ] }, { "cell_type": "code", "metadata": { "id": "ESp4hpG9Si9g", "colab_type": "code", "colab": {} }, "source": [ "joint(covid19,fever,cough)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "ZiamojNnTlab", "colab_type": "text" }, "source": [ "Q11: What are the probabilities of each symptom combination, in case of COVID-19?\n" ] }, { "cell_type": "code", "metadata": { "id": "NaTUrg64Tlpf", "colab_type": "code", "colab": {} }, "source": [ "joint(fever,cough).given(covid19)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "rFMVwa1y7-UU", "colab_type": "text" }, "source": [ "## Enriching the Probabilistic Model\n", "\n", "The model defined above may be enriched to cope with new random variables. \n", "\n", "For instance, one could have partial information like the occurrence of some symptom(s) without knowing which exactly (fever or cough or both). The inclusive OR, expressed as a vertical bar, may be used to define such variable:" ] }, { "cell_type": "code", "metadata": { "id": "vDk4lmroioHJ", "colab_type": "code", "colab": {} }, "source": [ "symptom = fever | cough\n", "print (\"OK (symptom defined)\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "CcFUBzm0pDy_", "colab_type": "text" }, "source": [ "Then, new queries may be done.\n", "\n", "Q12: What is the probability to have some symptom(s) in the occurrence of COVID-19?" ] }, { "cell_type": "code", "metadata": { "id": "Y-T55KxmlEi0", "colab_type": "code", "colab": {} }, "source": [ "P(symptom.given(covid19))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "KEAnVcv5pk1i", "colab_type": "text" }, "source": [ "Q13: What is the probability to have some symptom(s) in the absence of COVID-19?" ] }, { "cell_type": "code", "metadata": { "id": "ZLXhE0f4lRm0", "colab_type": "code", "colab": {} }, "source": [ "P(symptom.given(~covid19))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "dAkip-cqjHkO", "colab_type": "text" }, "source": [ "![covid19-2.png](https://bitbucket.org/repo/BpoAoj/images/690187781-covid19-2.png)\n", "\n", "Another intersting case consists in modeling the probability of death due to COVID-19. Assuming we know that it is 3.4% for a patient having COVID-19 and 0.02% for any other patient (whatever possible other disease). Here is how to express this rule:" ] }, { "cell_type": "code", "metadata": { "id": "E-RfDY6E8dk_", "colab_type": "code", "colab": {} }, "source": [ "death = if_(covid19, event(0.034), event(0.0002))\n", "print (\"OK (death defined)\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "mQ_n8dz2qgkL", "colab_type": "text" }, "source": [ "Then, new types of queries can be made, possibly involving post-mortem analysis.\n", "\n", "Q14: What is the probability of death for a patient having fever and coughing?" ] }, { "cell_type": "code", "metadata": { "id": "256", "colab_type": "code", "colab": {} }, "source": [ "P(death.given(fever,cough))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "OzqR4sRtq9vS", "colab_type": "text" }, "source": [ "Q15: What is the probability that a death is caused by COVID-19?" ] }, { "cell_type": "code", "metadata": { "id": "qSmSIK0LkSfu", "colab_type": "code", "colab": {} }, "source": [ "P(covid19.given(death))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "B9Pz9WMvrHAC", "colab_type": "text" }, "source": [ "Q16: What is the probability that a death is caused by COVID-19, given that there were no symptom?" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "wFidPZLakreU", "colab": {} }, "source": [ "P(covid19.given(death,~symptom))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "Gtkfy7NWWiZq", "colab_type": "text" }, "source": [ "Q17: Which were the probabilities of combinations COVID-19 / symptoms, for a (now) dead patient?" ] }, { "cell_type": "code", "metadata": { "id": "xLxIEHejWi0H", "colab_type": "code", "colab": {} }, "source": [ "joint(covid19,symptom).given(death)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "X1X1eodOPnJv", "colab_type": "text" }, "source": [ "## What Else?\n", "\n", "Independently of the model shown above, you may read a very interesting \n", "[post on coronavirus and probability](\n", "http://towardsdatascience.com/coronovarius-and-probability-the-media-must-learn-how-to-report-statistics-now-973ed2d52959#--responses) from Raphael Sonabend.\n", "\n", "Here are the translation of the use case in Lea where\n", "\n", "* `a_lt_65_if_c` is the probability of being under 65-years-old given dying from COVID-19.\n", "* `c` is the (unconditional) probability of dying from COVID-19\n", "* `a_lt_65_prior` is the (unconditional) probability of being under 65.\n" ] }, { "cell_type": "code", "metadata": { "id": "swV1HahadWOi", "colab_type": "code", "colab": {} }, "source": [ "c = event(0.034)\n", "a_lt_65_if_c = event(0.19)\n", "a_lt_65_prior = event(0.92)\n", "a_lt_65 = if_(c, a_lt_65_if_c, prior_lea=a_lt_65_prior)\n", "print (\"OK (model defined)\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "omb5bgL5d18q", "colab_type": "text" }, "source": [ "Now, the probability of dying from COVID-19 if you’re under 65 is calculated as follows:" ] }, { "cell_type": "code", "metadata": { "id": "f8brxPQDd22-", "colab_type": "code", "colab": {} }, "source": [ "P(c.given(a_lt_65))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "LVVt1l7imScB", "colab_type": "text" }, "source": [ "The result here is perfectly in line with the value 0.7% calculated in the post. You may change the probability values above to do what-if analysis. \n", "\n", "Now, Lea can do more...\n", "\n", "As explained [here](http://bitbucket.org/piedenis/lea/wiki/Lea3_Tutorial_3#markdown-header-symbolic-computation-with-probabilities), Lea enables you to do *symbolic calculation*, that is producing probability formulas instead of numbers. To do so, simply replace actual probability values in the model above by parameter names, like `'a'`, `'b'`, `'c'`:" ] }, { "cell_type": "code", "metadata": { "id": "ym9Z4F3zdkPw", "colab_type": "code", "colab": {} }, "source": [ "c = event('c')\n", "a_lt_65_if_c = event('a')\n", "a_lt_65_prior = event('b')\n", "a_lt_65 = if_(c, a_lt_65_if_c, prior_lea=a_lt_65_prior)\n", "print (\"OK (symbolic model defined)\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "cqwdCevCq-98", "colab_type": "text" }, "source": [ "Then, the calculated conditional probability produces an arithmetic expression" ] }, { "cell_type": "code", "metadata": { "id": "7wOkmm5Nq_06", "colab_type": "code", "colab": {} }, "source": [ "P(c.given(a_lt_65))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "_k7ZZAZGrEtX", "colab_type": "text" }, "source": [ "which is in line with Bayes' theorem. Other expressions include the probability of being under 65-years-old given NOT dying from COVID-19:" ] }, { "cell_type": "code", "metadata": { "id": "I6i_MmADtZny", "colab_type": "code", "colab": {} }, "source": [ "P(a_lt_65.given(~c))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "dMjCYfovuWpM", "colab_type": "text" }, "source": [ "or joint probability distribution of death vs age:" ] }, { "cell_type": "code", "metadata": { "id": "hdc59Ym0rE7a", "colab_type": "code", "colab": {} }, "source": [ "joint(c,a_lt_65)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "pVj8n4lH6w03", "colab_type": "text" }, "source": [ "## For Going Further...\n", "\n", "You are invited to experiment on this \"sandbox\" page, by changing the given probability values and see the effects on the query results or by making your own queries. \n", "\n", "To have a better understanding of the techniques used and possibly refine the model, you are invited to read the [Lea tutorials](http://bitbucket.org/piedenis/lea/wiki/Home).\n", "\n", "Note that a similar medical case study, dedicated to mammography and breast cancer, has been presented in [this post](http://chrisstrelioff.ws/sandbox/2015/05/27/revisiting_the_medical_tests_example_with_python_and_lea.html) of Chris Strelioff. Take care that it uses version 2 of Lea (the current page requires Lea 3, which is not backward compatible).\n", "\n", "Questions or comments can be addressed to pie.denis@skynet.be.\n", "\n", "*last updated on 2020, March 22, 12h00 CET*\n" ] } ] }