Traitement de l'information incertaine
======================================

Réseaux Bayésiens
=================

Exercice 1: Raisonnement probabiliste
-------------------------------------

Informellement, la probabilité que vous soyez malade est toujours proportionnelle à la probabilité de cette maladie. Ainsi, sur 10'000  personnes, l'une d'entre elle sera effectivement malade et sera diagnostiquée comme telle par le test avec 99\% de chance. Cependant, pour les 9'999 personnes restantes, le test se trompera dans 1\% des cas. Environ 100 personnes seront donc positives au test bien que n'étant pas malade. Globalement, vous avez donc seulement 1\% de risque d'être effectivement malade si le test est positif.

Plus formellement, soit :math:`T` la variable représentant le résultat du test, et :math:`M` la variable représentant la maladie. Par définition des taux de faux négatifs et de faux positifs, on a :math:`P(T=1|M=1)= 1-P(T=0|M=1) = 1-0.01=0.99` et :math:`P(T=0|M=0)=1-P(T=1|M=0)=1-0.01=0.99`. Cependant, :math:`P(M=1)=0.0001` puisque la maladie ne frappe qu'une personne sur 10'000. La probabilité qui nous intéresse est :math:`P(M=1|T=1)`, puisque nous souhaitons déterminer le risque que vous soyez effectivement malade sachant que votre test a été positif. Or,

.. math:: 
    \begin{split}
    P(M=1|T=1) &= \frac{P(T=1|M=1) \cdot P(M=1)} {P(T=1|M=1) \cdot P(M=1)+P(T=1|M=0) \cdot P(M=0)}\\
    &= \frac{0.99 \cdot 0.0001} {0.99 \cdot 0.0001+0.01 \cdot 0.9999}\\
    &= 0.009804
    \end{split}

Comme ce chiffre demeure très faible, vous ne devez pas forcément vous inquiéter. Bien sûr, cette conclusion est due à la faible fréquence de la maladie dans la population.

Exercice 2: Causalité
---------------------

Modélisation du problème
~~~~~~~~~~~~~~~~~~~~~~~~

Question 2.1 :
______________

Le réseau bayésien (figure 1) possède les noeuds :

    * :math:`I` = Route-Gelée
    * :math:`H` = Accident-Holmes
    * :math:`W` = Accident-Watson
    * :math:`S` = Professeur-Sauvé

et les arcs: 

* :math:`I \rightarrow H` et :math:`I \rightarrow W`, car l'état des routes influence les accidents de Holmes et de Watson.
* :math:`W \rightarrow S` et :math:`H \rightarrow S`, car les accidents de Holmes et de Watson influencent la résolution de l'affaire.

.. figure:: graph-1.png

    **Figure 1 Le réseau bayésien avec les noeuds: I=Route-Gelée, H=Accident-Holmes, W=Accident-Watson, S=Professeur-Sauvé**

Question 2.2 (Inférence déductive) :
____________________________________
 

* Pour calculer :math:`P(H)`, il nous faut connaître :math:`P(H | I)` :

.. math:: 
    P(H=h) = P(H=h|I=1)P(I=1) + P(H=h|I=0)P(I=0)

Il en va de même de la probabilité :math:`P(W)`, pour laquelle il faut connaître :math:`P(W | I)` :

.. math:: 
    P(W=w) = P(W=w|I=1)P(I=1) + P(W=w|I=0)P(I=0)

* Pour calculer :math:`P(S)`, il nous faut en plus :math:`P(S | H, W)` :

.. math:: 
    P(S=s) = \sum_{i,h,w \in {1, 0}}P(S=s|H=h,W=w)P(H=h|I=i)P(W=w|I=i)P(I=i)

Question 2.3 (Inférence abductive) :
____________________________________
 
La probabilité que nous cherchons est :math:`P(I|W)`. Pour la calculer, il nous faut :math:`P(W|I)`. Par la règle de Bayes, nous avons :

.. math:: 
    P(I=i|W=w) = \frac{P(W=w|I=i)P(I=i)}{P(W=w)}

Question 2.4 (Déduction et abduction) :
_______________________________________

* On cherche :math:`P(H|W)`. Il nous faut donc :math:`P(W|I)` et :math:`P(H|I)`:

.. math:: 
    P(H=h|W=w) = \sum_{i \in {1, 0}} P(H=h|I=i)P(I=i|W=w)

* On cherche :math:`P(H|W,S)`. Il nous faut donc en plus :math:`P(S|H,W)`:

.. math:: 
    P(H=h|W=w, S=s) = \alpha P(S=s|H=h,W=w) P(H=h|W=w)

On détermine :math:`\alpha = \frac{1}{P(S=s|W =w)}` de sorte que:

.. math:: 
    \sum_{h \in {1, 0}}P(H=h|W=w,S=s) = 1

* On cherche :math:`P(H|I,W,S)` :

.. math:: 
    P(H=h|I=i,W=w,S=s) = \alpha P(S=s|H=h,W=w) P(H=h|I=i)

On détermine :math:`\alpha = \frac{1}{P(S=s|I=i,W=w)}` de sorte que :

.. math:: 
    \sum_{h \in {1, 0}}P(H=h|I=i,W=w,S=s) = 1

Calcul probabiliste
~~~~~~~~~~~~~~~~~~~

Question 2.5 :
______________

+--------------+-----------+-----------+--------------+-----------+-----------+
+:math:`P(H|I)`|:math:`I=1`|:math:`I=0`|:math:`P(W|I)`|:math:`I=1`|:math:`I=0`+
+--------------+-----------+-----------+--------------+-----------+-----------+
+:math:`H=1`   |0.9        |0.1        |:math:`W=1`   |0.7        |0.5        +
+--------------+-----------+-----------+--------------+-----------+-----------+
+:math:`H=0`   |0.1        |0.9        |:math:`W=0`   |0.3        |0.5        +
+--------------+-----------+-----------+--------------+-----------+-----------+

Question 2.6 :
______________

.. math:: 
    \begin{split}
    P(H=1) &= P(H=1|I=1)P(I=1) + P(H=1|I=0)P(I=0) \\
           &= 0.9 \cdot 0.7 + 0.1 \cdot 0.3 = 0.66\\
    P(W=1) &= P(W=1|I=1)P(I=1) + P(W=1|I=0)P(I=0) \\
           &= 0.7 \cdot 0.7 + 0.5 \cdot 0.3 = 0.64
    \end{split}

Question 2.7 :
______________

.. math:: 
    \begin{split}
    P(I=1|W=1) &= \frac{P(W=1|I=1) \cdot P(I=1)}{P(W=1)} \\
               &= \frac{0.7 \cdot 0.7}{0.64} \cong 0.766
    \end{split}

Il en résulte que :math:`P(I=0|W=1) \cong 0.234`.\\

Question 2.8 (Dépendance) :
___________________________

.. math:: 
    \begin{split}
    P(H=1| W=1) &=P(H=1|I=1) \cdot P(I=1|W=1)\\
                &+ P(H=1|I=0) \cdot P(I=0|W=1)\\
                &= 0.9 \cdot 0.766 + 0.1 \cdot 0.234 \cong 0.713
    \end{split}

On peut constater que la probabilité d'accident de Holmes a augmenté suite à la connaissance de l'accident de Watson, car cet accident laisse supposer que la route peut être gelée. :math:`H` et :math:`W` sont deux événements dépendants.

Question 2.9 (Indépendance conditionnelle) :
____________________________________________
Si l'on sait que la route n'est pas gelée, :math:`I=0` et l'accident de Watson n'a plus d'influence sur la probabilité d'accident de Holmes. En effet, l'accident de Watson n'a pas d'effet sur la probabilité que la route soit gelée puisque celle-ci est connue avec certitude. Ainsi, on a: :math:`P(H=1|I=0, W=1) = P(H=1|I=0) = 0.1`, sachant que :math:`I`, :math:`H` et :math:`W` sont deux événements indépendants.

Question 2.10 :
_______________

+----------------+---------------+---------------+---------------+---------------+
+:math:`P(S|W,H)`|:math:`H=1,W=1`|:math:`H=1,W=0`|:math:`H=0,W=1`|:math:`H=0,W=0`+
+----------------+---------------+---------------+---------------+---------------+
+:math:`S=1`     |0.1            |0.2            |0.8            |1              +
+----------------+---------------+---------------+---------------+---------------+
+:math:`S=0`     |0.9            |0.8            |0.2            |0              +
+----------------+---------------+---------------+---------------+---------------+

Question 2.11 (Causes multiples) :
__________________________________

.. math:: 
    \begin{split}
    P(S=s) &= \sum_{i,h,w \in {1, 0}}P(S=s|H=h,W=w)P(H=h|I=i)P(W=w|I=i)P(I=i)\\
           & = 0.1 \cdot (0.9 \cdot 0.7 \cdot 0.7 + 0.1 \cdot 0.5 \cdot 0.3) \\
           & + 0.2 \cdot (0.9 \cdot 0.3 \cdot 0.7 + 0.1 \cdot 0.5 \cdot 0.3)\\
           & + 0.8 \cdot (0.1 \cdot 0.7 \cdot 0.7 + 0.9 \cdot 0.5 \cdot 0.3)\\
           & + 1 \cdot (0.1 \cdot 0.3 \cdot 0.7 + 0.9 \cdot 0.5 \cdot 0.3)\\
           &= 0.3896
    \end{split}

Question 2.12 :
_______________

.. math:: 
    \begin{split}
    P(H=1|W=1,S=1) &= \alpha P(S=1|H=1,W=1) P(H=1|W=1)\\
                   &= \alpha \cdot 0.1 \cdot 0.713 = \alpha \cdot 0.0713\\
    P(H=0|W=1,S=1) &= \alpha P(S=1|H=0,W=1) P(H=0|W=1)\\
                   &= \alpha \cdot 0.8 \cdot 0.287 = \alpha \cdot 0.23 
    \end{split}

Il faut que :math:`\alpha \cdot (0.0713 + 0.23) = 1`, donc on trouve :math:`\alpha \cong 3.319`, :math:`P(H=1 | W=1, S=1) \cong 0.237` et :math:`P(H=0 | W=1, S=1) \cong 0.763`.

Question 2.13 (Abduction avec plusieurs conséquences) :
_______________________________________________________

.. math:: 
    \begin{split}
    P(I=1|W=1,S=1) &= \sum_{h \in {1,0}} P(I=1|H=h,W=1) P(H=h|W=1,S=1)\\ 
                   & = \sum_{h \in {1,0}} \frac{P(H=h|I=1)P(W=1|I=1)P(I=1)}{P(H=h|W=1)P(W=1)}P(H=h|W=1,S=1)\\
                   & = \frac{0.9 \cdot 0.7 \cdot 0.7}{0.713 \cdot 0.64}\cdot0.237 + \frac{0.1 \cdot 0.7 \cdot 0.7}{0.287 \cdot 0.64} \cdot 0.763\\
                   & = 0.229 + 0.204 = 0.433
    \end{split}

Question 2.14 :
_______________

.. math:: 
    \begin{split}
    P(H=1|I=0,W=1,S=1) &= \alpha P(S=1|H=1,W=1) P(H=1|I=0)\\
                       & = \alpha \cdot 0.1 \cdot 0.1 =  \alpha \cdot 0.01\\
    P(H=0|I=0,W=1,S=1) &= \alpha P(S=1|H=0,W=1) P(H=0|I=0)\\
                       & = \alpha \cdot 0.8 \cdot 0.9 =  \alpha \cdot 0.72\\
    \end{split}

Il faut que :math:`\alpha \cdot (0.01 + 0.72) = 1`, donc on trouve :math:`\alpha = 1.370` et :math:`P(H=1 | I=0, W=1, S=1) = 0.014` et :math:`P(H=0 |I=0, W=1, S=1) = 0.986`.

Question 2.15 (Dépendance conditionnelle) :
___________________________________________

.. math:: 
    \begin{split}
    P(H=1|I=0, S=1) &= \alpha P(S=1|H=1,I=0) P(H=1|I=0)\\
                    & = \alpha \cdot \sum_{w \in {1,0}}P(S=1|H=1,W=w) \cdot P(W=w|I=0) \cdot P(H=1|I=0)\\
                    & = \alpha \cdot (0.1 \cdot 0.5 + 0.2 \cdot 0.5) \cdot 0.1 = \alpha \cdot 0.015\\
    P(H=0|I=0,S=1) &= \alpha P(S=1|H=0,I=0) P(H=0|I=0)\\
                   & = \alpha \cdot \sum_{w \in {1,0}}P(S=1|H=0,W=w) \cdot P(W=w|I=0) \cdot P(H=0|I=0)\\
                   & = \alpha \cdot (0.8 \cdot 0.5 + 1 \cdot 0.5) \cdot 0.9 = \alpha \cdot 0.81
    \end{split}

Il faut que :math:`\alpha \cdot (0.015 + 0.81) = 1`, donc on trouve :math:`\alpha = 1.21` et :math:`P(H=1|I=0,S=1) = 0.02` et :math:`P(H=0|I=0,S=1) = 0.98`. Donc, si en plus on sait que Watson a eu un accident, la probabilité que Holmes en ait eu un aussi redescend un peu. Ainsi, :math:`H` et :math:`W` sont des événements dépendants sachant que :math:`S` et :math:`I`.

.. figure:: graph-2.png

    **Figure 2 Le nouveau réseau bayésien avec les noeuds: I=Route-Gelée, H=Accident-Holmes, W=Accident-Watson, S=Professeur-Sauvé, V=Vieux-Pneus-Watson**

Question 2.16 :
_______________

On ajoute le noeud :math:`V` = Vieux-Pneus-Watson et l'arc :math:`V \rightarrow W` (figure 2).

Question 2.17 :
_______________
 
On cherche :math:`P(V|I,W)`. Il nous faut donc :math:`P(W|I,V)` :

.. math:: 
    \begin{split}
    P(V=v|I=i,W=w) = \alpha P(W=w|I=i,V=v) P(V=v)
    \end{split}

On détermine :math:`\alpha = \frac{1}{P(W=w|I=i)}` de sorte que :

.. math:: 
    \sum_{v \in {1, 0}}P(V=v|I=i,W=w) = 1

Inférence à chaînage avant avec facteurs de certitude
=====================================================

Module ``.../moteur_avec_variables_fc/facteurs_certitude.py`` :

.. include::  ../../solutions/inference_facteurs_certitude/moteur_avec_variables_fc/facteurs_certitude.py
    :code:

Module ``.../moteur_avec_variables_fc/regle_avec_variables_fc.py`` :

.. include::  ../../solutions/inference_facteurs_certitude/moteur_avec_variables_fc/regle_avec_variables_fc.py
    :code:

Module ``.../moteur_avec_variables_fc/connaissance_fc.py`` :

.. include::  ../../solutions/inference_facteurs_certitude/moteur_avec_variables_fc/connaissance_fc.py
    :code:

Module ``.../moteur_avec_variables_fc/chainage_avant_avec_variables_fc.py`` :

.. include::  ../../solutions/inference_facteurs_certitude/moteur_avec_variables_fc/chainage_avant_avec_variables_fc.py
    :code:
