Esempio n. 1
0
 def test_add_node(self):
     self.bn.clear()
     n = DiscreteNode("Some Node", [True, False])
     self.bn.add_node(n)
     self.assertEqual(n, self.bn.get_node("Some Node"))
     self.assertTrue(n in self.bn.get_nodes(["Some Node"]))
     node_with_same_name = DiscreteNode("Some Node", [True, False])
     self.assertRaises(Exception, self.bn.add_node, node_with_same_name)
Esempio n. 2
0
 def test_add_edge(self):
     self.bn.clear()
     n1 = DiscreteNode("1", [True, False])
     n2 = DiscreteNode("2", [True, False])
     self.bn.add_node(n1)
     self.bn.add_node(n2)
     self.bn.add_edge(n1, n2)
     self.assertTrue(n1 in self.bn.get_parents(n2))
     self.assertTrue(n2 in self.bn.get_children(n1))
Esempio n. 3
0
 def test_remove_edge(self):
     self.bn.clear()
     n1 = DiscreteNode("1", [True, False])
     n2 = DiscreteNode("2", [True, False])
     self.bn.add_node(n1)
     self.bn.add_node(n2)
     self.bn.add_edge(n1, n2)
     self.assertEqual([n1], self.bn.get_parents(n2))
     self.bn.remove_edge(n1, n2)
     self.assertEqual([], self.bn.get_parents(n2))
 def test_temporal_edges(self):
     self.dbn.clear()
     n1 = DiscreteNode("1", [True, False])
     n2 = DiscreteNode("2", [False, False])
     self.dbn.add_node(n1)
     self.dbn.add_node(n2)
     self.assertTrue(self.dbn.is_valid())
     self.dbn.add_edge(n1, n1)
     self.assertFalse(self.dbn.is_valid())
     self.dbn.remove_edge(n1, n1)
     self.dbn.add_edge(n1, n2)
     self.assertTrue(self.dbn.is_valid())
    def test_easy_values(self):
        n1 = DiscreteNode("Some Node", [True, False])
        n2 = DiscreteNode("Second Node", [True, False])

        cpt1 = numpy.array([2, 3])
        cpt2 = numpy.array([5, 7])

        n1.set_probability_table(cpt1, [n1])
        n2.set_probability_table(cpt2, [n2])

        s = n1.get_cpd().multiplication(n2.get_cpd())

        cptN = numpy.array([[10, 14], [15, 21]])

        numpy.testing.assert_array_equal(s.table, cptN)
        self.assertEqual(s.variables[0], n1)
    def test_easy_shape(self):
        n1 = DiscreteNode("Some Node", [True, False])
        n2 = DiscreteNode("Second Node", [True, False])

        s = n1.get_cpd().multiplication(n2.get_cpd())

        self.assertEqual(s.table.shape, (2, 2))

        s = n1.get_cpd().multiplication(n1.get_cpd())
        self.assertEqual(s.table.shape, (2, ))
Esempio n. 7
0
 def test_complicated_multi(self):
     n1 = DiscreteNode("Some Node", [True, False])
     n2 = DiscreteNode("Second Node" , [True, False,"noIdea"])
     
     cpt1 = numpy.array([2,3])
     cpt2 = numpy.array([5,7,9])
     
     n1.set_probability_table(cpt1,[n1])
     n2.set_probability_table(cpt2,[n2])
     
     c3 = n1.get_cpd().multiplication(n2.get_cpd())
     c3 = n1.get_cpd().multiplication(c3)
     
     cptN = numpy.array([[20, 28, 36],[45, 63, 81]])        
     numpy.testing.assert_array_equal(c3.table,cptN)
    def test_easy_marginalize(self):
        n1 = DiscreteNode("Some Node", [True, False])
        n2 = DiscreteNode("Second Node", [True, False, "other"])

        cpt1 = numpy.array([2, 3])
        cpt2 = numpy.array([5, 7, 3])

        n1.set_probability_table(cpt1, [n1])
        n2.set_probability_table(cpt2, [n2])

        s = n1.get_cpd().multiplication(n2.get_cpd())
        s = s.marginalization(n2)

        print s.table

        cptN = numpy.array([30, 45])

        numpy.testing.assert_array_equal(s.table, cptN)
        self.assertEqual(s.variables[0], n1)
Esempio n. 9
0
 def test_is_valid(self):
     self.bn.clear()
     n1 = DiscreteNode("1", [True, False])
     n2 = DiscreteNode("2", [True, False])
     self.bn.add_node(n1)
     self.bn.add_node(n2)
     self.bn.add_edge(n1, n2)
     self.assertTrue(self.bn.is_valid())
     self.bn.add_edge(n1, n1)
     self.assertFalse(self.bn.is_valid())
     self.bn.remove_edge(n1, n1)
     self.assertTrue(self.bn.is_valid())
     n3 = DiscreteNode("3", [True, False])
     n4 = DiscreteNode("4", [True, False])
     self.bn.add_node(n3)
     self.bn.add_node(n4)
     self.assertTrue(self.bn.is_valid())
     self.bn.add_edge(n2, n3)
     self.assertTrue(self.bn.is_valid())
     self.bn.add_edge(n3, n4)
     self.assertTrue(self.bn.is_valid())
     self.bn.add_edge(n4, n1)
     self.assertFalse(self.bn.is_valid())
Esempio n. 10
0
 def test_easy_values(self):
     n1 = DiscreteNode("Some Node", [True, False])
     n2 = DiscreteNode("Second Node" , [True, False])
     
     cpt1 = numpy.array([2,3])
     cpt2 = numpy.array([5,7])
     
     n1.set_probability_table(cpt1,[n1])
     n2.set_probability_table(cpt2,[n2])
     
     s = n1.get_cpd().multiplication(n2.get_cpd())
     
     cptN = numpy.array([[10,14],[15,21]])
     
     numpy.testing.assert_array_equal(s.table,cptN)
     self.assertEqual(s.variables[0],n1)
Esempio n. 11
0
    def test_easy_shape(self):
        n1 = DiscreteNode("Some Node", [True, False])
        n2 = DiscreteNode("Second Node" , [True, False])

        s = n1.get_cpd().multiplication(n2.get_cpd())

        self.assertEqual(s.table.shape, (2,2));

        s = n1.get_cpd().multiplication(n1.get_cpd())
        self.assertEqual(s.table.shape,(2,))
Esempio n. 12
0
 def test_easy_marginalize(self):
     n1 = DiscreteNode("Some Node", [True, False])
     n2 = DiscreteNode("Second Node" , [True, False, "other"])
     
     cpt1 = numpy.array([2,3])
     cpt2 = numpy.array([5,7,3])
     
     n1.set_probability_table(cpt1,[n1])
     n2.set_probability_table(cpt2,[n2])
     
     s = n1.get_cpd().multiplication(n2.get_cpd())
     s =s.marginalization(n2)
     
     print s.table
     
     cptN = numpy.array([30,45])        
     
     numpy.testing.assert_array_equal(s.table,cptN)
     self.assertEqual(s.variables[0],n1)
Esempio n. 13
0
from primo.nodes import UtilityNode
from primo.nodes import DiscreteNode
from primo.inference.decision import MakeDecision

import numpy

'''Example of a Bayesian Decision Network found in 
Barber, David - Bayesian Reasoning and Machine Learning
Page 111ff
'''

bdn = BayesianDecisionNetwork()

education = DecisionNode("education", ["do Phd", "no Phd"])
cost = UtilityNode("cost")
prize = DiscreteNode("prize", ["prize", "no prize"])
income = DiscreteNode("income", ["low", "average", "high"])
benefit = UtilityNode("benefit")
startup = DecisionNode("startUp", ["do startUp", "no startUp"])
costStartup = UtilityNode("costStartup")

#bdn.add_node(startup)
bdn.add_node(education)
bdn.add_node(cost)
bdn.add_node(prize)
bdn.add_node(income)
bdn.add_node(benefit)
bdn.add_node(startup)
bdn.add_node(costStartup)

bdn.add_edge(education, cost)
import primo.inference.particlefilter as pf
from primo.io import XMLBIF

# Construct a DynmaicBayesianNetwork
dbn = DynamicBayesianNetwork()
B0 = BayesianNetwork()
twoTBN = TwoTBN(XMLBIF.read("Robot_Localization.xmlbif"))

# Configure TwoTBN
x0 = twoTBN.get_node("x0")
x = twoTBN.get_node("x")
door = twoTBN.get_node("door")
twoTBN.set_initial_node(x0.name, x.name)

# Configure initial distribution
x0_init = DiscreteNode(
    x0.name, ["p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9"])
B0.add_node(x0_init)
cpt_x0_init = numpy.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
x0_init.set_probability_table(cpt_x0_init, [x0_init])

dbn.B0 = B0
dbn.twoTBN = twoTBN

N = 1000
T = 10

pos = 0
lastPos = 0
evidence = {}

Esempio n. 15
0
    def test_complicated_multi(self):
        n1 = DiscreteNode("Some Node", [True, False])
        n2 = DiscreteNode("Second Node", [True, False, "noIdea"])

        cpt1 = numpy.array([2, 3])
        cpt2 = numpy.array([5, 7, 9])

        n1.set_probability_table(cpt1, [n1])
        n2.set_probability_table(cpt2, [n2])

        c3 = n1.get_cpd().multiplication(n2.get_cpd())
        c3 = n1.get_cpd().multiplication(c3)

        cptN = numpy.array([[20, 28, 36], [45, 63, 81]])
        numpy.testing.assert_array_equal(c3.table, cptN)
Esempio n. 16
0
 def test_remove_node(self):
     self.bn.clear()
     n = DiscreteNode("Some Node to remove", [True, False])
     self.bn.add_node(n)
     self.bn.remove_node(n)
     self.assertFalse(n in self.bn.get_nodes([]))
Esempio n. 17
0
 def test_add_node(self):
     self.dbn.clear()
     n = DiscreteNode("Some_Node", [True, False])
     self.dbn.add_node(n)
     self.assertEqual(n, self.dbn.get_node("Some_Node"))
     self.assertTrue(n in self.dbn.get_nodes(["Some_Node"]))
Esempio n. 18
0
 def setUp(self):
     # Create BayesNet
     self.bn = BayesianNetwork()
     # Create Nodes
     weather0 = DiscreteNode("Weather0", ["Sun", "Rain"])
     weather = DiscreteNode("Weather", ["Sun", "Rain"])
     ice_cream_eaten = DiscreteNode("Ice Cream Eaten", [True, False])
     # Add nodes
     self.bn.add_node(weather0)
     self.bn.add_node(weather)
     self.bn.add_node(ice_cream_eaten)
     # Add edges
     self.bn.add_edge(weather, ice_cream_eaten)
     self.bn.add_edge(weather0, weather)
     # Set probabilities
     cpt_weather0 = numpy.array([0.6, 0.4])
     weather0.set_probability_table(cpt_weather0, [weather0])
     cpt_weather = numpy.array([[0.7, 0.5], [0.3, 0.5]])
     weather.set_probability_table(cpt_weather, [weather0, weather])
     ice_cream_eaten.set_probability(0.9, [(ice_cream_eaten, True), (weather, "Sun")])
     ice_cream_eaten.set_probability(0.1, [(ice_cream_eaten, False), (weather, "Sun")])
     ice_cream_eaten.set_probability(0.2, [(ice_cream_eaten, True), (weather, "Rain")])
     ice_cream_eaten.set_probability(0.8, [(ice_cream_eaten, False), (weather, "Rain")])
Esempio n. 19
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from  primo.networks import BayesianNetwork
from  primo.nodes import DiscreteNode
import numpy

bn = BayesianNetwork()
burglary = DiscreteNode("Burglary", ["Intruder","Safe"])
alarm = DiscreteNode("Alarm", ["Ringing", "Silent","Kaputt"])
earthquake = DiscreteNode("Earthquake", ["Shaking", "Calm"])
john_calls = DiscreteNode("John calls", ["Calling", "Not Calling"])
mary_calls = DiscreteNode("Mary calls", ["Calling", "Not Calling"])


bn.add_node(burglary)
bn.add_node(alarm)
bn.add_node(earthquake)
bn.add_node(john_calls)
bn.add_node(mary_calls)


bn.add_edge(burglary,alarm)
bn.add_edge(earthquake, alarm)
bn.add_edge(alarm, john_calls)
bn.add_edge(alarm, mary_calls)


burglary.set_probability(0.2,[(burglary,"Intruder")])

alarm.set_probability(0.1,[(alarm,"Ringing"),(burglary,"Safe"),(earthquake,"Calm")])
Esempio n. 20
0
This example shows how to create a BayesNet

@author: djohn
"""

import numpy

from  primo.networks import BayesianNetwork
from  primo.nodes import DiscreteNode


#initialize a new BayesNet
bn = BayesianNetwork()

#create Nodes with Name and the possible values
burglary = DiscreteNode("Burglary", ["Intruder","Safe"])
alarm = DiscreteNode("Alarm", ["Ringing", "Silent"])
earthquake = DiscreteNode("Earthquake", ["Shaking", "Calm"])
john_calls = DiscreteNode("John calls", ["Calling", "Not Calling"])
baum_calls = DiscreteNode("Baum calls", ["Calling", "Not Calling"])

# add Nodes to BayesNet
bn.add_node(burglary)
bn.add_node(alarm)
bn.add_node(earthquake)
bn.add_node(john_calls)
bn.add_node(baum_calls)

# Add edges to show dependencies
bn.add_edge(burglary,alarm)
bn.add_edge(earthquake, alarm)
Esempio n. 21
0
 def setUp(self):
     # Create BayesNet
     self.bn = BayesianNetwork()
     # Create Nodes
     weather0 = DiscreteNode("Weather0", ["Sun", "Rain"])
     weather = DiscreteNode("Weather", ["Sun", "Rain"])
     ice_cream_eaten = DiscreteNode("Ice Cream Eaten", [True, False])
     # Add nodes
     self.bn.add_node(weather0)
     self.bn.add_node(weather)
     self.bn.add_node(ice_cream_eaten)
     # Add edges
     self.bn.add_edge(weather, ice_cream_eaten)
     self.bn.add_edge(weather0, weather)
     # Set probabilities
     cpt_weather0 = numpy.array([.6, .4])
     weather0.set_probability_table(cpt_weather0, [weather0])
     cpt_weather = numpy.array([[.7, .5], [.3, .5]])
     weather.set_probability_table(cpt_weather, [weather0, weather])
     ice_cream_eaten.set_probability(.9, [(ice_cream_eaten, True),
                                          (weather, "Sun")])
     ice_cream_eaten.set_probability(.1, [(ice_cream_eaten, False),
                                          (weather, "Sun")])
     ice_cream_eaten.set_probability(.2, [(ice_cream_eaten, True),
                                          (weather, "Rain")])
     ice_cream_eaten.set_probability(.8, [(ice_cream_eaten, False),
                                          (weather, "Rain")])
Esempio n. 22
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from primo.networks import BayesianNetwork
from primo.nodes import DiscreteNode
import numpy

bn = BayesianNetwork()
burglary = DiscreteNode("Burglary", ["Intruder", "Safe"])
alarm = DiscreteNode("Alarm", ["Ringing", "Silent", "Kaputt"])
earthquake = DiscreteNode("Earthquake", ["Shaking", "Calm"])
john_calls = DiscreteNode("John calls", ["Calling", "Not Calling"])
mary_calls = DiscreteNode("Mary calls", ["Calling", "Not Calling"])

bn.add_node(burglary)
bn.add_node(alarm)
bn.add_node(earthquake)
bn.add_node(john_calls)
bn.add_node(mary_calls)

bn.add_edge(burglary, alarm)
bn.add_edge(earthquake, alarm)
bn.add_edge(alarm, john_calls)
bn.add_edge(alarm, mary_calls)

burglary.set_probability(0.2, [(burglary, "Intruder")])

alarm.set_probability(0.1, [(alarm, "Ringing"), (burglary, "Safe"),
                            (earthquake, "Calm")])

cpt = numpy.array([[0.1, 0.9], [0.5, 0.5], [0.4, 0.6]])
john_calls.set_probability_table(cpt, [alarm, john_calls])
Esempio n. 23
0
from primo.inference.mcmc import GibbsTransitionModel
import numpy
import pdb

#About this example:
#This example shows how approximate inference can be used query a purely discrete
#bayesian network. At first that network is being constructed and afterwards it
#is passed to an MCMC object that is used to answer several kinds of questions:
#-Prior marginal
#-Posterior marginal
#-Probability of evidence
#-Maximum a-posteriori hypothesis

#Construct some simple BayesianNetwork
bn = BayesianNetwork()
burglary = DiscreteNode("Burglary", ["Intruder", "Safe"])
alarm = DiscreteNode("Alarm", ["Ringing", "Silent", "Destroyed"])

bn.add_node(burglary)
bn.add_node(alarm)

bn.add_edge(burglary, alarm)

#Parametrize the network
burglary_cpt = numpy.array([0.2, 0.8])
burglary.set_probability_table(burglary_cpt, [burglary])

alarm_cpt = numpy.array([[0.8, 0.15, 0.05], [0.05, 0.9, 0.05]])
alarm.set_probability_table(alarm_cpt, [burglary, alarm])

#Get some inference object
from primo.nodes import DecisionNode
from primo.nodes import UtilityNode
from primo.nodes import DiscreteNode
from primo.inference.decision import MakeDecision

import numpy
'''Example of a Bayesian Decision Network found in 
Barber, David - Bayesian Reasoning and Machine Learning
Page 111ff
'''

bdn = BayesianDecisionNetwork()

education = DecisionNode("education", ["do Phd", "no Phd"])
cost = UtilityNode("cost")
prize = DiscreteNode("prize", ["prize", "no prize"])
income = DiscreteNode("income", ["low", "average", "high"])
benefit = UtilityNode("benefit")
startup = DecisionNode("startUp", ["do startUp", "no startUp"])
costStartup = UtilityNode("costStartup")

#bdn.add_node(startup)
bdn.add_node(education)
bdn.add_node(cost)
bdn.add_node(prize)
bdn.add_node(income)
bdn.add_node(benefit)
bdn.add_node(startup)
bdn.add_node(costStartup)

bdn.add_edge(education, cost)
Esempio n. 25
0
from primo.inference.mcmc import GibbsTransitionModel
import numpy
import pdb

#About this example:
#This example shows how approximate inference can be used query a purely discrete
#bayesian network. At first that network is being constructed and afterwards it
#is passed to an MCMC object that is used to answer several kinds of questions:
#-Prior marginal
#-Posterior marginal
#-Probability of evidence
#-Maximum a-posteriori hypothesis

#Construct some simple BayesianNetwork
bn = BayesianNetwork()
burglary = DiscreteNode("Burglary", ["Intruder","Safe"])
alarm = DiscreteNode("Alarm", ["Ringing", "Silent","Destroyed"])

bn.add_node(burglary)
bn.add_node(alarm)

bn.add_edge(burglary,alarm)


#Parametrize the network
burglary_cpt=numpy.array([0.2,0.8])
burglary.set_probability_table(burglary_cpt, [burglary])

alarm_cpt=numpy.array([[0.8,0.15,0.05],[0.05,0.9,0.05]])
alarm.set_probability_table(alarm_cpt, [burglary,alarm])