
\documentclass[12pt]{article}

\fontfamily{lmss}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{url}
\usepackage{multicol}
\usepackage{hyperref}

\setlength{\parskip}{\baselineskip}%
\setlength{\parindent}{0pt}%

\newenvironment{sol}[1]{
	{\bf Solution:}
}{
}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{arrows}
\usetikzlibrary{shapes}

\title{CS 486/686 Assignment 3}
\author{Due Date: 11:59pm on Thursday, November 15, 2018}
\date{}

\begin{document}

\maketitle

\section*{Instructions}

\begin{itemize}
\item
Submit the assignment in the Dropbox labeled Assignment 3 Submissions on Learn.
\item
No late assignment will be accepted.
\item
This assignment is to be done individually.
\item
For any programming question, you may use the language of your choice.  We highly recommend using Python if you are familiar with it.  
\item
Lead TA: Colin Vandenhof (cm5vande@uwaterloo.ca).  Colin's office hours will be posted on the course website.
\end{itemize}


\newpage
\section{Inference in Bayesian Networks (93 points)}
Your dog Fido has been howling for the last three hours.  In the past, Fido may howl when he is genuinely sick, but he also howls sometimes when there is a full moon or when the neighbour's dog is howling.  You are trying to figure out if you should take him to the vet.  You will implement a system to help you decide what to do.  First, you will implement the variable elimination algorithm.  Second, you will define a Bayesian network to model the reasons for Fido to howl and to compute the probability that Fido is genuinely sick.  

You should perform the computations in your Bayesian network by using your variable elimination algorithm.  However, if you are not able to complete your implementation, you will get partial marks if you perform the computations by hand.

\begin{enumerate}
\item
(5 points)
Implement the variable elimination algorithm.  The implementation is only worth 5 points if it is complete and well-documented.  However, you will earn 1/3 of the marks in part 2 (b, c, d, and e) if you perform the computations using your implementation rather than by hand.

You may assume that all the random variables are Boolean.  You may use the language of your choice, but we will give you tips on the implementation if you use Python.

Please include documentation for every function in your program. If a function is long, include documentation within the function.  In addition, please include detailed instructions for the TA to run your program.  You will earn 5 points if your program is complete and well-documented. 

Factors are essentially multi-dimensional arrays.  Hence, use numpy multi-dimensional arrays as your main data structure.  If you are unfaimilar with numpy, go through the following tutorial: \url{https://docs.scipy.org/doc/numpy-1.15.1/user/quickstart.html}.

Please implement the following five functions.  

[Tip: Test each function individually using examples from the lecture slides.  If you wait till the end to test your entire program, it will be much harder to debug.]

\begin{enumerate}
\item
{\bf restrictedFactor = restrict(factor, variable, value):} This function restricts the given variable in the given factor to the given value.  [Tip: use slicing operations to implement this function.]

\item
{\bf productFactor = multiply(factor1, factor2):} This function performs pointwise multiplication of the given two factors. [Tip: take advantage of the numpy broadcasting rules to multiply multi-dimensional arrays of different shapes.]

\item
{\bf resultFactor = sumout(factor, variable):}  This function sums out a variable in a given factor.  [Tip: use the sum operation to implement this function.]

\item
{\bf normalizedFactor = normalize(factor):} This function normalizes a factor by dividing each entry by the sum of all. the entries.  This function is useful when the factor is a distribution (i.e. the sum of the probabilities must be $1$.).

\item
{\bf resultFactor = inference(factorList, queryVariables, orderedListOfHiddenVariables, evidenceList):} This function computes \\
$Pr( \text{queryVariables} | \text{evidenceList})$ by the variable elimination algorithm. This function should do the following.
\begin{enumerate}
\item
Restrict the factors in factorList according to the evidence in evidenceList.
\item
Based on the order of the hidden variables in orderedListOfHiddenVariables, sum out each hidden variable from the product of the factors in factorList.  
\item
If the result is a product of multiple factors, then multiply them to produce a single factor.
\item
If the resulting factor should denote a probability distribution (i.e. the probabilities should sum up to 1), then normalize the resulting factor.
\end{enumerate}

\end{enumerate}

\item
(88 points) To decide on whether to take Fido to the vet or not, you have the following information available to you.

\begin{itemize}
\item
Fido often howls if he is genuinely sick.  However, Fido is a healthy dog and is only sick 5\% of the time.  If Fido is really sick, then he probably has not eaten much of his dinner and has food left in his bowl. In the past, when Fido is sick then he does not eat his dinner 60\% of the time. However, when Fido is healthy he still does not eat his dinner 10\% of the time.

\item
Fido often howls if there is a full moon or your neighbour’s dog howls.  Your neighbour’s dog sometimes howls at the full moon and sometimes howls when your neighbour is away. However, your neighbour’s dog is never affected by Fido’s howls. Since you live on Earth, there is a full moon once out of every twenty-eight days.
You have observed that your neighbour travels three days out of every ten.  

\item
If there is no full moon and your neighbour is at home then your neighbour's dog never howls.  If there is no full moon and your neighbour is away then your neighbour's dog howls half of the time. If there is a full moon and your neighbour is at home then your neighbour's dog howls 40\% of the time.  Finally, if there is a full moon and your neighbour is away then your neighbour's dog howls 80\% of the time.

\item
If all three triggers are there (Fido sick, full moon, and your neighbour's dog howling), Fido howls 99\% of the time.   If none of the three triggers are there, Fido will not howl.

\item
If Fido is sick he is more likely to howl.  If being sick is the only trigger present, Fido will howl half of the time.  If Fido is sick and your neighbour's dog is howling then Fido will howl 75\% of the time.  If Fido is sick and there is a full moon, then Fido will howl 90\% of the time.  

\item
If Fido is not sick then he is less likely to howl.  The full moon and your neighbour’s dog will cause Fido to howl 65\% of the time.  If there is only a full moon then Fido will howl 40\% of the time.  If there is no full moon, but your neighbour’s dog is howling, then Fido will howl 20\% of the time.
\end{itemize}

\begin{enumerate}
\item
(40 points) Construct a Bayesian network based on the above information about Fido.  
\vspace{0.5em}

Tip: You should be able to directly take the probabilities given in the text above and insert them into the corresponding conditional probability tables.  There is no need to perform any computation to obtain the conditional probabilities.
\vspace{0.5em}

{\bf What to hand in:} Show the graph defining the network and the conditional probability tables associated with each node in the graph. The network should encode all the information stated
above. It should contain six nodes corresponding to the following binary random variables.

\begin{itemize}
\item
$FH$: Fido howls.
\item
$S$: Fido is sick.
\item
$B$: There is food left in Fido's food bowl.
\item
$M$: There is a full moon.
\item
$NA$: Your neighbour's away.
\item
$NH$: Your neighbour's dog howls.
\end{itemize}

The arcs in your Bayesian network should accurately capture the probabilistic dependencies between the variables.

\item
(15 points) What is the prior probability that Fido howls?

{\bf What to hand in:} Indicate what queries (i.e., P r(variables|evidence)) you used to compute those probabilities.  Whether you answer the queries by hand or using the program you wrote part 1,  show or provide a printout of the factors computed at each step of variable elimination (as done in the lecture notes).  {\bf Use the following variable ordering when summing out variables in the variable elimination algorithm: B, FH, M, NA, NH, and S.}

{\bf For parts (b), (c), (d), and (e), a maximum of two thirds of the marks are earned if you answer the question correctly by doing the computations by hand instead of using your program.}

\item
(12 points) Fido is howling.  You look out the window and see that the moon is full. What is probability that Fido is sick?

{\bf What to hand in:} Same as Question 2b.


\item
(12 points) (Fido is howling and the moon is still full.) You walk to the kitchen and see that Fido has not eaten and his food bowl is full.  Given this new information, what is the probability that Fido is sick?

{\bf What to hand in:} Same as Question 2b.


\item
(9 points) (Fido is howling, the moon is full and Fido has no eaten his dinner.) You decide to call your neighbour to see if they are home or not. The phone rings and rings so you conclude that your neighbour is away. Given this information, what is the probability that Fido is sick?

{\bf What to hand in:} Same as Question 2b.


\end{enumerate}

\end{enumerate}

\newpage
\section{Constructing Bayesian Networks (24 points)}

Consider the Bayesian network for the Holmes scenario in Figure 1 below (without the Radio node).  The conditional probabilities are different from the ones shown in class.

\begin{figure}[h]
\label{bnet_1}
\begin{center}
\begin{tikzpicture}
\tikzstyle{every node}=[draw];
\node (tab1) at (-3, 2) {
\begin{tabular}{l}
$P(B) =0.1$
\end{tabular}
};
\node (tab2) at (-3, 0) {
\begin{tabular}{l}
$P(E) = 0.05$
\end{tabular}
};
{\small
\node (tab4) at (3, -2) {
\begin{tabular}{l}
$P(A|\neg E \land \neg B) =0.05$\\
$P(A|E \land \neg B) = 0.1$\\
$P(A|\neg E \land B) = 0.9$\\
$P(A|E \land B) = 0.95$ 
\end{tabular}
};
}
\node (tab5) at (9, 0) {
\begin{tabular}{l}
$P(G|A) =0.4$\\
$P(G|\neg A) =0.05$
\end{tabular}
};
\node (tab4) at (9, 2) {
\begin{tabular}{l}
$P(W| A) =0.8$\\
$P(W| \neg A) = 0.4$\\
\end{tabular}
};
\node[ellipse] (a) at (0, 0) {E};
\node[ellipse] (b) at (0, 2) {B};
\node[ellipse] (c) at (3, 1) {A};
\node[ellipse] (d) at (6, 2) {W};
\node[ellipse] (e) at (6, 0) {G};
\path[draw] [->] (a) -- (c);
\path[draw] [->] (b) -- (c);
\path[draw] [->] (c) -- (d);
\path[draw] [->] (c) -- (e);
\end{tikzpicture}
\end{center}
\caption{Bayesian Network 1}
\end{figure}

If we consider the variables in the order of $W, G, A, B, E$, we will derive the Bayesian network in Figure~\ref{bnet_2} below.

\begin{figure}[h]
\label{bnet_2}
\begin{center}
\begin{tikzpicture}
\tikzstyle{every node}=[draw];
\node[ellipse] (a) at (0, 2) {W};
\node[ellipse] (b) at (0, 0) {G};
\node[ellipse] (c) at (3, 1) {A};
\node[ellipse] (d) at (6, 2) {B};
\node[ellipse] (e) at (6, 0) {E};
\path[draw] [->] (a) -- (c);
\path[draw] [->] (a) -- (b);
\path[draw] [->] (b) -- (c);
\path[draw] [->] (c) -- (d);
\path[draw] [->] (c) -- (e);
\path[draw] [->] (d) -- (e);
\end{tikzpicture}
\end{center}
\caption{Bayesian Network 2}
\end{figure}


You will compute a few probabilities to explain why the structure of the network in Figure~\ref{bnet_2} is the way it is.  You may do the calculations by hand or use the variable elimination algorithm you implemented in Question 1.

\begin{enumerate}
\item 
(6 points) We chose $W$ as the only parent of $G$ because $G$ is not independent of $W$.  Calculate $P(G | W)$ and $P(G | \neg W)$ separately and show that they are not equal.

\item 
(6 points) We chose $A$ as the only parent of $B$ because $B$ is conditionally independent of $W$ and $G$ given $A$.  Calculate $P(B | W \land G \land A)$ and $P(B | A)$ separately and show that they are equal.

\item 
(6 points) We could not have chosen $W$ as the only parent of $B$ because $B$ is not conditionally independent of $A$ and $G$ given $W$.  Calculate $P(B | A \land G \land W)$ and $P(B | W)$ separately and show that they are not equal.

\item 
(6 points) We could not have chosen $A$ as the only parent of $E$ because $E$ is not conditionally independent of $B$ given $A$.  Calculate $P(E | A \land B)$ and $P(E | A)$ separately and show that they are not equal.
\end{enumerate}  



\end{document}





























