## AI Project #2: Bayes Nets

#### Due: Monday February 1415, 2011, 11:59 PM

(Last Update Feb 14, 5:00 PM)

In this project, you will perform exact and approximate inference in Bayesian Networks.

All variables are assumed binary.

Calling Convention
You will need to produce a file `inference.py` that will compute conditional probabilities (either exactly, or approximately via sampling). It will be called like so:

`python inference.py egnet.txt EXACT 1 1 0 0 2 1 3 0`
The input arguments are:

• The name of a file containing the specification of a Bayesian network. (Described below.)
• EXACT or SAMPLING, the mode that should be used to compute the probabilities
• The variable and value that we are computing the probability of. (In this example,`X1=1`).
• The variables and values that are being conditioned on. (In this example, `X0=0`, `X2=1`, and `X3=0`.)

So, in summary, the above call asks you to compute `P(X1=1 | X0=0, X2=1, X3=0)` by exact (brute-force) inference, using the bayes net specified in egnet.txt.

File Format of Bayes Net
An example of the kind of tree you will need to read in is egnet.txt. Here is a visualization of this net. Thank you Haitao!

• The first line of the file contains the number of variables.
• After the first line, each pair of lines specifies the parents and conditional probabilities of one node
• The first line of each pair gives
• The number of the node. (You can assume that the variables are listed in order.)
• The number of parents of the node.
• A list of the parents of the node
For example

`5 3 0 1 3`

would mean that variable `X5` has three parents: `X0`, `X1`, and `X3`.

• The second line of each pair gives the conditional probability that the node has value 1, for each possible configuration of the parents. Continuing the previous example,

`.2 .3 .1 .9 .15 .3 .15 .7`

Would mean that:

• `P(X5=1|X0=0, X1=0, X3=0) = .2`
• `P(X5=1|X0=0, X1=0, X3=1) = .3`
• `P(X5=1|X0=0, X1=1, X3=0) = .1`
• `P(X5=1|X0=0, X1=1, X3=1) = .9`
• `P(X5=1|X0=1, X1=0, X3=0) = .15`
• `P(X5=1|X0=1, X1=0, X3=1) = .3`
• `P(X5=1|X0=1, X1=1, X3=0) = .15`
• `P(X5=1|X0=1, X1=1, X3=1) = .7`

For sampling, you should generate 100,000 samples.

You will again submit your project using mycourses. You need to submit two files, `inference.py`, and `writeup.txt`, which is a plain text file documenting your code. Please submit the two files seperately (please do not put them into a .zip or .tar.gz file).

Your code will be tested using Python 2.6.

• Correctly passes a set of tests : 55%
• Efficiency and clarity of code : 35%
• Clarity and thoroughness of writeup: 10%

Questions:
Q) Is it OK for my program to crash on bad input data?
A) Yes.

Q) What is the output format?
A) Just print out the specified conditional probability in text format.

Q) Can we assume that the parents of a variable always have lower numbers? (I.e. is it impossible for `X5` to be a parent of `X4`?)
A) Yes.

A) For you, anything!

```\$ python inference.py egnet.txt EXACT 1 1 0 0 2 1 3 0
0.994974874372
\$ python inference.py egnet.txt EXACT 0 0
0.5
\$ python inference.py egnet.txt EXACT 1 1 0 1
0.5
\$ python inference.py egnet.txt EXACT 0 0 2 1
0.4
```

Q) How much randomness should we expect in the sampling based inference?
A) It depends on the input query, but here is an example of calling the above function a few times.

```\$ python inference.py egnet.txt SAMPLING 1 1 0 0 2 1 3 0
0.994463809719
\$ python inference.py egnet.txt SAMPLING 1 1 0 0 2 1 3 0
0.994411177645
\$ python inference.py egnet.txt SAMPLING 1 1 0 0 2 1 3 0
0.993524888709
\$ python inference.py egnet.txt SAMPLING 1 1 0 0 2 1 3 0
0.99411995296
\$ python inference.py egnet.txt SAMPLING 1 1 0 0 2 1 3 0
0.995306122449
```