\chapter{Probabilistic representation and reasoning (and burglars)}
\section{Formal description}
In our representation of the model we chose to introduce a \textit{Noisy OR} to
represent the causal independence of \textit{Burglar} and \textit{Earthquake}
on \textit{Alarm}. The visual representation of the network is visible in
Figure~\ref{bnetwork21}

\begin{figure}[H]
	\caption{Bayesian network, visual representation}
	\label{bnetwork21}
	\centering
	\includegraphics[scale=0.5]{d1.eps}
\end{figure}

As for the probabilities for \textit{Burglar} and \textit{Earthquake} we chose
to calculate them using days the unit. Calculation for the probability of a
\textit{Burglar} event happening at some day is then this(assuming a gregorian
calendar and leap days).
$$\frac{1}{365 + 0.25 - 0.01 - 0.0025}=\frac{1}{365.2425}$$

This gives the following probability distributions visible in
Table~\ref{probdist}

\begin{table}[H]
	\label{probdist}
	\begin{tabular}{|l|l|}
		\hline
		& Earthquake\\
		\hline
		T & $0.0027$\\
		F & $0.9973$\\
		\hline
	\end{tabular}
	%
	\begin{tabular}{|l|l|}
		\hline
		& Burglar\\
		\hline
		T & $0.0027$\\
		F & $0.9973$\\
		\hline
	\end{tabular}
	
	\begin{tabular}{|l|ll|}
		\hline
		& \multicolumn{2}{c|}{$I_1$}\\
		Earthquake & T & F\\
		\hline
		T & $0.2$ & $0.8$\\
		F & $0$ & $1$\\
		\hline
	\end{tabular}
	\begin{tabular}{|l|ll|}
		\hline
		& \multicolumn{2}{c|}{$I_2$}\\
		Burglar & T & F\\
		\hline
		T & $0.95$ & $0.05$\\
		F & $0$ & $1$\\
		\hline
	\end{tabular}
	\begin{tabular}{|ll|ll|}
		\hline
		&& \multicolumn{2}{c|}{Alarm}\\
		$I_1$ & $I_2$ & T & F\\
		\hline
		T & T & $1$ & $0$\\
		T & F & $1$ & $0$\\
		F & T & $1$ & $0$\\
		F & F & $0$ & $1$\\
		\hline
	\end{tabular}
	
	\begin{tabular}{|l|ll|}
		\hline
		& \multicolumn{2}{c|}{Watson}\\
		Alarm & T & F\\
		\hline
		T & $0.8$ & $0.2$\\
		F & $0.4$ & $0.6$\\
		\hline
	\end{tabular}
	\begin{tabular}{|l|ll|}
		\hline
		& \multicolumn{2}{c|}{Gibbons}\\
		Alarm & T & F\\
		\hline
		T & $0.99$ & $0.01$\\
		F & $0.04$ & $0.96$\\
		\hline
	\end{tabular}
	\begin{tabular}{|l|ll|}
		\hline
		& \multicolumn{2}{c|}{Radio}\\
		Earthquake & T & F\\
		\hline
		T & $0.9998$ & $0.0002$\\
		F & $0.0002$ & $0.9998$\\
		\hline
	\end{tabular}
\end{table}

\section{Implementation}
This distribution results in the \textit{AILog} code in Listing~\ref{alarm.ail}

\begin{listing}[H]
	\label{alarm.ail}
	\caption{alarm.ail}
	\inputminted[linenos,fontsize=\footnotesize]{prolog}{./src/alarm.ail}
\end{listing}

\section{Queries}
Using the following queries the probabilities or as follows:\\
\begin{enumerate}[a)]
	\item $P(\text{Burglary})=
		0.002737757092501968$
	\item $P(\text{Burglary}|\text{Watson called})=
		0.005321803679438259$
	\item $P(\text{Burglary}|\text{Watson called}\wedge\text{Gibbons called})=
		0.11180941544755249$
	\item $P(\text{Burglary}|\text{Watson called}\wedge\text{Gibbons called}
		\wedge\text{Radio})=0.01179672476662423$
\end{enumerate}

\begin{listing}[H]
	\begin{minted}[fontsize=\footnotesize]{prolog}
ailog: predict burglar.
Answer: P(burglar|Obs)=0.002737757092501968.
  [ok,more,explanations,worlds,help]: ok.

ailog: observe watson.
Answer: P(watson|Obs)=0.4012587986186947.
  [ok,more,explanations,worlds,help]: ok.

ailog: predict burglar.
Answer: P(burglar|Obs)=[0.005321803679438259,0.005321953115441623].
  [ok,more,explanations,worlds,help]: ok.

ailog: observe gibbons.
Answer: P(gibbons|Obs)=[0.04596053565368094,0.045962328885721306].
  [ok,more,explanations,worlds,help]: ok.

ailog: predict burglar.
Answer: P(burglar|Obs)=[0.11180941544755249,0.1118516494624678].
  [ok,more,explanations,worlds,help]: ok.

ailog: observe radio.
Answer: P(radio|Obs)=[0.02582105837443645,0.025915745316785182].
  [ok,more,explanations,worlds,help]: ok.

ailog: predict burglar.
Answer: P(burglar|Obs)=[0.01179672476662423,0.015584580594335082].
  [ok,more,explanations,worlds,help]: ok.
	\end{minted}
\end{listing}

\section{Comparison with manual calculation}
Querying the \textit{Alarm} variable gives the following answer
\begin{minted}{prolog}
	ailog: predict alarm.
	Answer: P(alarm|Obs)=0.0031469965467367292.
	  [ok,more,explanations,worlds,help]: ok.
\end{minted}

Using formula: $P(i_1|C_1)+P(i_2|C_2)(1-P(i_1|C_1))$ we can calculate the
probability of the \textit{Alarm} variable using variable elimination. This
results in the following answer: 
$$0.2*0.0027+0.95*0.0027*(1-0.2*0.0027)=0.00314699654673672941001347$$

There is a slight difference in probability. This is probably due to the
precision of the \textit{AILog} module. The manual calculation was done with
arbitrary precision. Manual calculation takes a lot longer and therefore one
can prefer the \textit{AILog} method when speed is of an essence. When
precision is necessary manual calculation is preferred.
