Distribution Test Tables

This example demonstrates how to create some conditional probability tables and a bayesian network.


In [1]:
from pomegranate import *
import math

First let's define some conditional probability tables.


In [2]:
c_table = [[0, 0, 0, 0.6],
           [0, 0, 1, 0.4],
           [0, 1, 0, 0.7],
           [0, 1, 1, 0.3],
           [1, 0, 0, 0.2],
           [1, 0, 1, 0.8],
           [1, 1, 0, 0.9],
           [1, 1, 1, 0.1]]

d_table = [[ 0, 0, 0.5 ],
           [ 0, 1, 0.5 ],
           [ 1, 0, 0.3 ],
           [ 1, 1, 0.7 ]]

f_table = [[ 0, 0, 0, 0.8 ],
           [ 0, 0, 1, 0.2 ],
           [ 0, 1, 0, 0.3 ],
           [ 0, 1, 1, 0.7 ],
           [ 1, 0, 0, 0.6 ],
           [ 1, 0, 1, 0.4 ],
           [ 1, 1, 0, 0.9 ],
           [ 1, 1, 1, 0.1 ]]

e_table = [[ 0, 0, 0.7 ],
           [ 0, 1, 0.3 ],
           [ 1, 0, 0.2 ],
           [ 1, 1, 0.8 ]]

g_table = [[ 0, 0, 0, 0.34 ],
           [ 0, 0, 1, 0.66 ],
           [ 0, 1, 0, 0.83 ],
           [ 0, 1, 1, 0.17 ],
           [ 1, 0, 0, 0.77 ],
           [ 1, 0, 1, 0.23 ],
           [ 1, 1, 0, 0.12 ],
           [ 1, 1, 1, 0.88 ]]

Then let's convert them into distribution objects.


In [3]:
a = DiscreteDistribution({ 0: 0.5, 1: 0.5 })
b = DiscreteDistribution({ 0: 0.7, 1: 0.3 })
e = ConditionalProbabilityTable( e_table, [b] )
c = ConditionalProbabilityTable( c_table, [a,b] )
d = ConditionalProbabilityTable( d_table, [c] )
f = ConditionalProbabilityTable( f_table, [c,e] )
g = ConditionalProbabilityTable( g_table, [c,e] )

Next we can convert these distributions into states.


In [4]:
a_s = State( a, "a" )
b_s = State( b, "b" )
c_s = State( c, "c" )
d_s = State( d, "d" )
e_s = State( e, "e" )
f_s = State( f, "f" )
g_s = State( g, "g" )

Now that we have our states created, we can finally start making our bayesian network.


In [5]:
model = BayesianNetwork( "derp" )
model.add_nodes( a_s, b_s, c_s, d_s, e_s, f_s, g_s )

Then we define the edges.


In [6]:
model.add_edge( a_s, c_s )
model.add_edge( b_s, c_s )
model.add_edge( c_s, d_s )
model.add_edge( c_s, f_s )
model.add_edge( b_s, e_s )
model.add_edge( e_s, f_s )
model.add_edge( c_s, g_s )
model.add_edge( e_s, g_s )

We finish by baking the network to finalize its structure.


In [7]:
model.bake()

Now we can check on the structure of our bayesian network.


In [8]:
print("\n".join( "{:10.10} : {}".format( state.name, belief.parameters[0] ) for state, belief in zip( model.states, model.predict_proba({}, max_iterations=100) ) ))


a          : {0: 0.5, 1: 0.5}
b          : {0: 0.6999999999999997, 1: 0.3000000000000003}
c          : {0: 0.52, 1: 0.4799999999999999}
d          : {0: 0.4040000000000001, 1: 0.596}
e          : {0: 0.5499999999999998, 1: 0.45000000000000034}
f          : {0: 0.6518, 1: 0.34820000000000007}
g          : {0: 0.52066, 1: 0.4793399999999999}

In [ ]: