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

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

Days were chosen as unit to model the story. Calculation of the probability of a\textit{Burglar} event happening at some day is then (assuming a Gregorian
calendar and leap days):
$$\frac{1}{365 + 0.25 - 0.01 - 0.0025}=\frac{1}{365.2425}$$

The resultant probability distributions can be found in table ~\ref{probdist}, in order to avoid a unclear graph.

\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}

\textit{If there is a burglar present (which could happen once every ten years), the alarm is known to go off 95\% of the time.} We modelled this by setting the value for Burglar True and $I_2$ True on 0,95. \\
\textit{There’s a 40\% chance that Watson is joking and the alarm is in fact off.} This is modelled by putting the value for Watson True and Alarm F on 0,4. As Holmes expects Watson to call in 80\% of the time, the value for alarm True and Watson True is set 0,2. Because the rows have to sum to 1, the other values are easily calculated. \\
\textit{She may not have heard the alarm in 1\% of the cases and is thought to erroneously report an alarm when it is in fact off in 4\% of the cases.} We modelled this by assuming that when Mrs. Gibbons hears the alarm, she calls Holmes. Meaning that the value for Gibbons False and Alarm true is 0,01. As she reports when the alarm is in fact off in 4\% of the cases, the value for Gibbons True and alarm False is 0,04. \\

 

\section{Implementation}
We implemented the distributions in \textit{AILog}, see 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 are 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.00314699654673673$$
TODOOOOOOOOOOO

\newpage
\section{Burglary problem with extended information}
$P(burglary)\cdot\left(
	P(\text{first house is holmes'})+
	P(\text{second house is holmes'})+
	P(\text{third house is holmes'})\right)=\\
0.5102041\cdot\left(
	\frac{1}{10000}+
	\frac{9999}{10000}\cdot\frac{1}{9999}+
	\frac{9999}{10000}\cdot\frac{9998}{9999}\cdot\frac{1}{9998}\right)=
\frac{3}{19600}\approx0.000153$

\section{Bayesian networks}
A bayesian network representation of the burglary problem with a multitude of
houses and burglars is possible but would be very big and tedious because all
the constraints about the burglars must be incorporated in the network.
The network would look something like in figure~\ref{bnnetworkhouses}


\begin{tabular}{|l|l|}
	\hline
	Joe &\\
	\hline
	T & $\nicefrac{5}{7}$\\
	F & $\nicefrac{2}{7}$\\
	\hline
\end{tabular}
\begin{tabular}{|l|l|}
	\hline
	William &\\
	\hline
	T & $\nicefrac{5}{7}$\\
	F & $\nicefrac{2}{7}$\\
	\hline
\end{tabular}
\begin{tabular}{|l|l|}
	\hline
	Jack & \\
	\hline
	T & $\nicefrac{5}{7}$\\
	F & $\nicefrac{2}{7}$\\
	\hline
\end{tabular}
\begin{tabular}{|l|l|}
	\hline
	Averall & \\
	\hline
	T & $\nicefrac{5}{7}$\\
	F & $\nicefrac{2}{7}$\\
	\hline
\end{tabular}

\begin{tabular}{|llll|ll|}
	\hline
	& & & & Burglary &\\
	Joe & William & Jack & Averall & T & F\\
	\hline
	F& F& F& F & $0$ & $1$\\
	F& F& F& T & $0$ & $1$\\
	F& F& T& F & $0$ & $1$\\
	F& F& T& T & $0$ & $1$\\
	F& T& F& F & $0$ & $1$\\
	F& T& F& T & $0$ & $1$\\
	F& T& T& F & $0$ & $1$\\
	F& T& T& T & $0$ & $1$\\
	T& F& F& F & $0$ & $1$\\
	T& F& F& T & $0$ & $1$\\
	T& F& T& F & $1$ & $0$\\
	T& F& T& T & $0$ & $1$\\
	T& T& F& F & $1$ & $0$\\
	T& T& F& T & $0$ & $1$\\
	T& T& T& F & $1$ & $0$\\
	T& T& T& T & $1$ & $0$\\
	\hline
\end{tabular}
\begin{tabular}{|lll|}
	\hline
	& Holmes &\\
	Burglary & T & F\\
	\hline
	T & $0.000153$ & $0.999847$\\
	F & $0$ & $1$\\
	\hline
\end{tabular}

\begin{figure}[H]
	\caption{Bayesian network of burglars and houses}
	\label{bnnetworkhouses}
	\centering
	%\includegraphics[scale=0.5]{d2.eps}
\end{figure}
