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:
X1=1
).
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!
5 3 0 1 3
would mean that
variable X5
has three parents: X0
, X1
, and X3
.
.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.
Your project grade will be assigned as follows:
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.
Q) Can you please, please, please give us a couple test cases?
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