Personal tools
You are here: Home Tutorials Inference in the WaterSprinkler Bayesian Network
Document Actions

Inference in the WaterSprinkler Bayesian Network

by Kosta Gaitanis last modified 2006-04-06 13:58

Here we will learn how to perform inference into a simply bayesian network using the JoinTree (aka Junction Tree) algorithm and the MCMC Sampling Engine. The code of this example can be found in ./Examples/EasyInference.py

Create the WaterSprinkler example

To create this we simply have to run the script found in the Examples directory :

from OpenBayes import JoinTree, MCMCEngine
from WaterSprinkler import *

print G


Choose an Inference Engine

Then we have to choose an inference engine to perform calculations for us. For the moment, only two inference engines are available : JoinTree and MCMCEngine.

JoinTree is an exact inference method that transforms the bayesian network into an equivalent structure (called join/junction tree) by eliminating any undirected loops. For example, the water-sprinkler network forms an undirected loop with the nodes c, s, r and w.
MCMCEngine is an approximate inference engine that samples a big number of times the network and then simply counts how many times each event has occurred.

ie = JoinTree(G)		or
ie = MCMCEngine(G)


Perform Inference without evidence

To calculate the marginal of a certain node (i.e. Wet Grass) simply use

>>> print ie.Marginalise('w')
Multinomial Distribution for node : w
Conditional Probability Table (CPT) :
array([ 0.34909999, 0.65090001], type=Float32)

This syntax is independant of the inference engine used and will return a distribution class with the answer. This example used the JoinTree inference. The results with the MCMCEngine are similar :

Multinomial Distribution for node : w
Conditional Probability Table (CPT) :
array([ 0.33500001, 0.66500002], type=Float32)

To marginalise all the nodes in a bayesian network in one single command use :

ie.MarginaliseAll()

Add some evidence

Suppose now that you know that the sprinkler is on and that it is not cloudy, and you wonder what's the probability of the grass being wet : Pr(w|s=1,c=0). This is called evidence. Evidence is entered using a dictionary var.name:value

ie.SetObs({'s':1,'c':0})

and then perform inference in the same way

>>> print ie.Marginalise('w')
Multinomial Distribution for node : w
Conditional Probability Table (CPT) :
array([ 0.08000001,  0.92000002], type=Float32)

The grass is much more likely to be wet because the sprinkler is on !


Powered by Plone, the Open Source Content Management System

This site conforms to the following standards: