Description
Handing in this Assignment
What to hand in on paper: No paper submission required.
What to hand in electronically: You must submit all your answers and code electronically. You must submit the following:
1. A file of python code called bnet.py. This file will contain your implementation of the factor
functions and variable elimination.
To submit these files electronically, use the CDF secure Web site
https://www.cdf.toronto.edu/students/
or use the CDF submit command. Type man submit for more information. The name of the assignment for submit will be “A3”
Since we will test your code electronically, you must
• make certain that your code runs on CDF using python3 (version 3.4.1 (installed as “python3”
on CDF, note that “python” with no “3” invokes the wrong version of python).
• not add any non-standard pythons imports from within the python files you submit (the imports
that are already in the template files must remain).
Introduction
In this assignment you will implement variable elimination on Bayes Nets.
What is supplied. You will be supplied with python code implementing Variable, Factor, and BN objects. The file bnet.py contains the class definitions for these objects. The code supports representing
factors as tables of values indexed by various settings of the variables in the factor’s scope.
The template file bnet.py also contains function prototypes for the functions you must implement.
Question 1. Implement Variable Elimination
Implement a collection of functions that operate on Factor objects and then use these functions to implement VE (variable elimination).
multiply factors Take as input a list of Factor objects; create and return a new factor that is equal
to the product of the factors in the list. Do not modify any of the input factors.
restrict factor Take as input a single factor, a variable V and a value d from the domain of that
variable; create and return a new factor that is the restriction of the input factor to the assignment
V = d. Do not modify the input factor.
sum out variable Take as input a single factor, and a variable V ; create and return a new factor that is
the result of summing V out of the input factor. Do not modify the input factor.
VE Take as input a Bayes Net object (object of class BN), a variable that is the query variable Q, and a list
of variables E that are the evidence variables (all of which have had some value set as evidence using
the variable’s set evidence interface). Compute the probability of every possible assignment
2
to Q given the evidence specified by the evidence settings of the evidence variables. Return these
probabilities as a list where every number corresponds the probability of one of Q’s possible values.
Do not modify any factor of the input bayes net.
3

