Problem Set 5 (21.5 - 4.6.07)
In this problem set we want to implement a simple Bayesian network and do some calculations with it. As a simple example we take the following network (taken from here):
It describes the simple case of an water sprinkler in the garden. All variables have only boolean values.
- Write a general implementation of a Bayesian network. It should be somehow made of nodes and edges. We only consider discrete variables (i.e. no continuous variables). If you use Python it would be good to use an object-orientated design (in Matlab you can use oo as well, see for example the official documentation).
- Recreate the sprinkler network with your implementation and the conditional probabilities given.
- Extend your network implementation to include the calculation of the joint probability (which might internally still use the network to save memory) and other conditional probabilities (this is probably easiest from the joint probability). Using this print out the probability that the sprinkler was on if the grass is wet.
- Write a sampler which creates random samples for the whole network (the distribution of the samples follows the joint probability function).
Python program (author: Gidi Farhi)
This is an example solution from Gidi Farhi. He defines a class for variables/nodes and one for Bayesian networks. Both classes are pretty generic, which was the aim of this exercise. Therefore I think that this is a good solution.
For another take on Bayesian networks you can take a look at the OpenBayes
libray, which is unfortunately no longer maintained.
Note that there are many practical applications for Bayes and Bayesian networks. From diagnostics and the anoying paperclip in MS Office
to spam-filters for email
(e.g. Thunderbird uses a Bayesian spam classifier).