# Bayesian Network

> This document is written in *Python 3*.
>
> ***GitHub***: https://github.com/czs108

## AND Rule

\begin{equation}
\begin{split}
P(A \cap B) &= P(A) \cdot P(B \mid A) \\
    &= P(B) \cdot P(A \mid B)
\end{split}
\end{equation}

## Two Nodes


```python
from graphviz import Digraph

bayes = Digraph()
bayes.node("A")
bayes.node("B")
bayes.edges(["AB"])

bayes
```




    

    



Suppose we know:

\begin{align}
& P(A) \\
& P(B \mid A) \\
& P(B \mid \overline{A})
\end{align}

We can get:

\begin{equation}
P(A \cap B) = P(A) \cdot P(B \mid A)
\end{equation}

\begin{equation}
P(\overline{A} \cap B) = P(\overline{A}) \cdot P(B \mid \overline{A})
\end{equation}

\begin{equation}
P(B) = P(A \cap B) + P(\overline{A} \cap B)
\end{equation}

\begin{equation}
P(A \mid B) = \frac{P(A \cap B)}{P(B)}
\end{equation}

## Three Nodes on the Same Line


```python
from graphviz import Digraph

bayes = Digraph()
bayes.node("A")
bayes.node("B")
bayes.node("C")
bayes.edges(["AB", "BC"])

bayes
```




    

    



Suppose we know:

\begin{align}
& P(A) \\
& P(B \mid A) \\
& P(B \mid \overline{A}) \\
& P(C \mid B) \\
& P(C \mid \overline{B})
\end{align}

We can get:

\begin{equation}
P(A \cap B \cap C) = P(A) \cdot P(B \mid A) \cdot P(C \mid B)
\end{equation}

\begin{equation}
P(A \cap \overline{B} \cap C) = P(A) \cdot P(\overline{B} \mid A) \cdot P(C \mid \overline{B})
\end{equation}

Event $A$ and $C$ are *conditionally independent* under the status of $B$. Knowing $A$ does not provide any information about $C$.

\begin{equation}
P(A \cap C) = P(A \cap B \cap C) + P(A \cap \overline{B} \cap C)
\end{equation}

\begin{equation}
P(C) = P(A \cap C) + P(\overline{A} \cap C)
\end{equation}

\begin{equation}
P(B \mid A \cap C) = \frac{P(A \cap B \cap C)}{P(A \cap C)}
\end{equation}

\begin{equation}
P(A \mid C) = \frac{P(A \cap C)}{P(C)}
\end{equation}

## Three Nodes as a Binary Tree


```python
from graphviz import Digraph

bayes = Digraph()
bayes.node("A")
bayes.node("B")
bayes.node("C")
bayes.edges(["AB", "AC"])

bayes
```




    

    



Suppose we know:

\begin{align}
& P(A) \\
& P(B \mid A) \\
& P(B \mid \overline{A}) \\
& P(C \mid B) \\
& P(C \mid \overline{B})
\end{align}

We can get:

\begin{equation}
P(A \cap B \cap C) = P(A) \cdot P(B \mid A) \cdot P(C \mid A)
\end{equation}

\begin{equation}
P(\overline{A} \cap B \cap C) = P(\overline{A}) \cdot P(B \mid \overline{A}) \cdot P(C \mid \overline{A})
\end{equation}

\begin{equation}
P(B \cap C) = P(A \cap B \cap C) + P(\overline{A} \cap B \cap C)
\end{equation}

\begin{equation}
\begin{split}
P(C) &= P(A \cap C) + P(\overline{A} \cap C) \\
    &= P(A) \cdot P(C \mid A) + P(\overline{A}) \cdot P(C \mid \overline{A})
\end{split}
\end{equation}

\begin{equation}
P(A \mid B \cap C) = \frac{P(A \cap B \cap C)}{P(B \cap C)}
\end{equation}

\begin{equation}
P(B \mid C) = \frac{P(B \cap C)}{P(C)}
\end{equation}

f2f6b9de1c17604dca625248b2fe22c6b7bd6f6a
ipynb
Jupyter Notebook
exercises/Bayesian Network.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Bayesian Network.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Bayesian Network.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
2022-03-21T05:04:07.000Z
2022-03-21T05:04:07.000Z
Qwen/Qwen-72B
1. YES
2. YES