Bayes' Theorem
---------------

This page is inspired from:

* `MathIsFun <https://www.mathsisfun.com/data/bayes-theorem.html>`__
* `Wikipedia <https://en.wikipedia.org/wiki/Bayes%27_theorem>`__

Theorem
=========

The Bayes's theorem describes the probability of an event, based on prior knowledge of conditions
that might be related to the event.
To compute :math:`P(A|B)`, in other words, the probability of the event :math:`A` to happend, knowing that :math:`B`
already happend, the following drawing can be used:

.. image:: figures/bayes_theorem.svg
    :align: center
    :width: 400px

|

The red area represents :math:`P(B)=a3+a4`. To compute :math:`P(A|B)` we do:

.. math::
   P(A|B)=\frac{a3}{a3+a4}=\frac{P(A)P(B|A)}{P(A)P(B|A)+P(\neg A)P(B|\neg A)}=\frac{P(A)P(B|A)}{P(B)}

This final formula is the Baye's Theorem.
See the `3Blue1Brown video <https://www.youtube.com/watch?v=HZGCoVF3YvM>`__ for a better understanding.

.. note::
   The following notation is also used:
   
   .. math::
      P(A|B)=\frac{a3}{a3+a4}=\frac{P(A \cap B)}{P(B)}

Example
=========

Problem
^^^^^^^^

.. image:: figures/cat.jpg
   :align: center
   :width: 150px

Anna says she is itchy. There is a test for Allergy to Cats, but this test is not always right:

* For people that do have the allergy, the test says *Yes* 80% of the time
* For people that do not have the allergy, the test says *Yes* 10% of the time *(false positive)*

If 1% of the population have the allergy, and Anna's test says *Yes*, what are the chances that she really has the allergy?

**--- Solution ---**

Let's draw the little diagram:

.. image:: figures/bayes_theorem_example.svg
   :width: 400px
   :align: center


.. math::
   P(Allergy|Yes)&=\frac{a3}{a3+a4}

   &=\frac{P(Allergy)P(Yes|Allergy)}{P(Allergy)P(Yes|Allergy)+P(\neg Allergy)*P(Yes|\neg Allergy)}

   &=\frac{0.01 \times 0.80}{0.01 \times 0.80 + 0.99 \times 0.10} = 0.0747

The chances that Anna really has allergy is about 7%.