예제 #1
0
    def bayes_net(self, conditional_dict, current_node):
        """recursively back propagate through nodes"""
        if current_node in conditional_dict.keys():
            return

        if current_node == self.harm.top_layer.source:
            conditional_dict[current_node] = DiscreteDistribution({
                True: 1,
                False: 0
            })
            return

        if len(list(self.harm.top_layer.predecessors_iter(current_node))) == 0:
            conditional_dict[current_node] = DiscreteDistribution({
                True:
                current_node.probability,
                False:
                1 - current_node.probability
            })
            return

        parent_list = []
        for parent in self.harm.top_layer.predecessors_iter(current_node):
            self.bayes_net(conditional_dict, parent)
            parent_list.append(conditional_dict[parent])

        conditional_dict[current_node] = ConditionalProbabilityTable(
            self.table_generator(len(parent_list), current_node.probability),
            parent_list)
from pomegranate import *
import numpy as np

# Let's create the bayesian network for our aridcity first. You can think of an arid city as a place like Pheonix, Arizona.

# In[2]:
from pomegranate.distributions import DiscreteDistribution, ConditionalProbabilityTable

arid_rain = DiscreteDistribution({'T': 0.1, 'F': 0.9})

# The probability that people will use a sprinkler is dependent upon the probability that it has rained.

# In[3]:

arid_sprinkler = ConditionalProbabilityTable(
    [['T', 'T', 0.01], ['T', 'F', 0.99], ['F', 'T', 0.5], ['F', 'F', 0.6]],
    [arid_rain])

# Finally there is the probability that the grass is wet, which is dependent upon both the rain and whether the sprinklers are on.

# In[4]:

arid_grass = ConditionalProbabilityTable(
    [['T', 'T', 'T', 0.99], ['T', 'T', 'F', 0.01], ['T', 'F', 'T', 0.8],
     ['T', 'F', 'F', 0.3], ['F', 'T', 'T', 0.9], ['F', 'T', 'F', 0.1],
     ['F', 'F', 'T', 0.0], ['F', 'F', 'F', 1.0]], [arid_rain, arid_sprinkler])

# Next we need to create the states for our "arid" bayesian network.

# In[5]:

monty = ConditionalProbabilityTable(
	[[ 'A', 'A', 'A', 0.0 ],
	 [ 'A', 'A', 'B', 0.5 ],
	 [ 'A', 'A', 'C', 0.5 ],
	 [ 'A', 'B', 'A', 0.0 ],
	 [ 'A', 'B', 'B', 0.0 ],
	 [ 'A', 'B', 'C', 1.0 ],
	 [ 'A', 'C', 'A', 0.0 ],
	 [ 'A', 'C', 'B', 1.0 ],
	 [ 'A', 'C', 'C', 0.0 ],
	 [ 'B', 'A', 'A', 0.0 ],
	 [ 'B', 'A', 'B', 0.0 ],
	 [ 'B', 'A', 'C', 1.0 ],
	 [ 'B', 'B', 'A', 0.5 ],
	 [ 'B', 'B', 'B', 0.0 ],
	 [ 'B', 'B', 'C', 0.5 ],
	 [ 'B', 'C', 'A', 1.0 ],
	 [ 'B', 'C', 'B', 0.0 ],
	 [ 'B', 'C', 'C', 0.0 ],
	 [ 'C', 'A', 'A', 0.0 ],
	 [ 'C', 'A', 'B', 1.0 ],
	 [ 'C', 'A', 'C', 0.0 ],
	 [ 'C', 'B', 'A', 1.0 ],
	 [ 'C', 'B', 'B', 0.0 ],
	 [ 'C', 'B', 'C', 0.0 ],
	 [ 'C', 'C', 'A', 0.5 ],
	 [ 'C', 'C', 'B', 0.5 ],
	 [ 'C', 'C', 'C', 0.0 ]], [guest, prize] )

# In[2]:
from pomegranate.distributions import DiscreteDistribution, ConditionalProbabilityTable

guest = DiscreteDistribution({'A': 1. / 3, 'B': 1. / 3, 'C': 1. / 3})
prize = DiscreteDistribution({'A': 1. / 3, 'B': 1. / 3, 'C': 1. / 3})

# Now let's create the conditional probability table for our Monty. The table is dependent on both the guest and the prize.

# In[3]:

monty = ConditionalProbabilityTable(
    [['A', 'A', 'A', 0.0], ['A', 'A', 'B', 0.5], ['A', 'A', 'C', 0.5],
     ['A', 'B', 'A', 0.0], ['A', 'B', 'B', 0.0], ['A', 'B', 'C', 1.0],
     ['A', 'C', 'A', 0.0], ['A', 'C', 'B', 1.0], ['A', 'C', 'C', 0.0],
     ['B', 'A', 'A', 0.0], ['B', 'A', 'B', 0.0], ['B', 'A', 'C', 1.0],
     ['B', 'B', 'A', 0.5], ['B', 'B', 'B', 0.0], ['B', 'B', 'C', 0.5],
     ['B', 'C', 'A', 1.0], ['B', 'C', 'B', 0.0], ['B', 'C', 'C', 0.0],
     ['C', 'A', 'A', 0.0], ['C', 'A', 'B', 1.0], ['C', 'A', 'C', 0.0],
     ['C', 'B', 'A', 1.0], ['C', 'B', 'B', 0.0], ['C', 'B', 'C', 0.0],
     ['C', 'C', 'A', 0.5], ['C', 'C', 'B', 0.5], ['C', 'C', 'C', 0.0]],
    [guest, prize])

# Now lets create the states for the bayesian network.

# In[4]:

s1 = State(guest, name="guest")
s2 = State(prize, name="prize")
s3 = State(monty, name="monty")

# Then the bayesian network itself, adding the states in after.
from pomegranate import *

# We'll create the discrete distribution for our friend first.

# In[13]:
from pomegranate.distributions import DiscreteDistribution, ConditionalProbabilityTable

friend = DiscreteDistribution({True: 0.5, False: 0.5})

# The emissions for our guest are completely random.

# In[14]:

guest = ConditionalProbabilityTable(
    [[True, 'A', 0.50], [True, 'B', 0.25], [True, 'C', 0.25],
     [False, 'A', 0.0], [False, 'B', 0.7], [False, 'C', 0.3]], [friend])

# Then the distribution for the remaining cars.

# In[15]:

remaining = DiscreteDistribution({
    0: 0.1,
    1: 0.7,
    2: 0.2,
})

# The probability of whether the prize is randomized is dependent on the number of remaining cars.

# In[16]:
예제 #6
0
from pomegranate import *
import numpy as np

# First we'll create the bayesian network in which rain influences the occurrence of fog. To do that we first create a discrete distribution for the occurrence of rain.

# In[40]:
from pomegranate.distributions import DiscreteDistribution, ConditionalProbabilityTable

dist_rain = DiscreteDistribution({'T': 0.6, 'F': 0.4})

# Now we can create a conditional probability table for fog which is dependent on rain.

# In[41]:

dist_fog = ConditionalProbabilityTable(
    [['T', 'T', 0.1], ['T', 'F', 0.9], ['F', 'T', 0.4], ['F', 'F', 0.6]],
    [dist_rain])

# And finally the conditional probability table for whether the grass is wet, which is dependent upon rain and fog.

# In[42]:

dist_grass = ConditionalProbabilityTable(
    [['T', 'T', 'T', 0.99], ['T', 'T', 'F', 0.01], ['T', 'F', 'T', 0.9],
     ['T', 'F', 'F', 0.1], ['F', 'T', 'T', 0.7], ['F', 'T', 'F', 0.3],
     ['F', 'F', 'T', 0.1], ['F', 'F', 'F', 0.9]], [dist_rain, dist_fog])

# Now that we have our distributions, we can create states for our bayesian network out of them.

# In[43]:
예제 #7
0
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[10]:

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[11]:

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")