示例#1
0
class TestMPE(unittest.TestCase):
    """
    Test cases for Most Probable Explanation(MPE)
    """
    
    def setUp(self):
        self.Net = BayesNet()
        network_structure = {
            "node1" : {
                "state1" : 0.3,
                "state2" : 0.7,
            },
            "node2" : {
                "state1" : 0.7,
                "state2" : 0.3,
            },
            "node3" : {
                "state1" : 0.5,
                "state2" : 0.5
            },
        }    
        
        self.Net.create_network(network_structure)
        
        
        
    def test_MPE_connected(self):
        """
        testing Net.getMPE(NodeName1,NodeName2) between parent and child node(i.e. connected nodes)
        """

        self.Net.createEdge("node1",["node2","node3"])
        node1 = self.Net.getNode("node1")
        node2 = self.Net.getNode("node2")
        node3 = self.Net.getNode("node3")
        
        self.Net.setProbability(node2,["state1", 'state1'],0.3)
        self.Net.setProbability(node2,["state1", 'state2'],0.1)
        self.Net.setProbability(node2,["state2", 'state1'],0.7)
        self.Net.setProbability(node2,["state2", 'state2'],0.9)
        self.Net.setProbability(node3,["state1", 'state1'],0.4)
        self.Net.setProbability(node3,["state1", 'state2'],0.0)
        self.Net.setProbability(node3,["state2", 'state1'],0.6)
        self.Net.setProbability(node3,["state2", 'state2'],1.0)
        
        expected_outcome = {'node1': 'state2', 'node2': 'state2'}
        
        mpe = self.Net.getMPE("node1","node2")
        self.assertDictEqual(expected_outcome,mpe)

        
    def test_MPE_disconnected(self):
        """
        testing Net.getMPE(NodeName1,NodeName2) between disconnected nodes
        """
        
        self.Net.createEdge(["node1","node2"],"node3")
        node1 = self.Net.getNode("node1")
        node2 = self.Net.getNode("node2")
        node3 = self.Net.getNode("node3")

        self.Net.setProbability(node3,["state1", 'state1','state1'],0.3)
        self.Net.setProbability(node3,["state1", 'state1','state2'],0.1)
        self.Net.setProbability(node3,["state1", 'state2','state1'],0.5)
        self.Net.setProbability(node3,["state1", 'state2','state2'],0.7)
        self.Net.setProbability(node3,["state2", 'state1','state1'],0.7)
        self.Net.setProbability(node3,["state2", 'state1','state2'],0.9)
        self.Net.setProbability(node3,["state2", 'state2','state1'],0.5)
        self.Net.setProbability(node3,["state2", 'state2','state2'],0.3)
        
        expected_outcome = {'node1': 'state2', 'node2': 'state1'}
        mpe = self.Net.getMPE("node1","node2")
        self.assertDictEqual(expected_outcome,mpe)
示例#2
0
class TestMPE(unittest.TestCase):
    """
    Test cases for Most Probable Explanation(MPE)
    """
    def setUp(self):
        self.Net = BayesNet()
        network_structure = {
            "node1": {
                "state1": 0.3,
                "state2": 0.7,
            },
            "node2": {
                "state1": 0.7,
                "state2": 0.3,
            },
            "node3": {
                "state1": 0.5,
                "state2": 0.5
            },
        }

        self.Net.create_network(network_structure)

    def test_MPE_connected(self):
        """
        testing Net.getMPE(NodeName1,NodeName2) between parent and child node(i.e. connected nodes)
        """

        self.Net.createEdge("node1", ["node2", "node3"])
        node1 = self.Net.getNode("node1")
        node2 = self.Net.getNode("node2")
        node3 = self.Net.getNode("node3")

        self.Net.setProbability(node2, ["state1", 'state1'], 0.3)
        self.Net.setProbability(node2, ["state1", 'state2'], 0.1)
        self.Net.setProbability(node2, ["state2", 'state1'], 0.7)
        self.Net.setProbability(node2, ["state2", 'state2'], 0.9)
        self.Net.setProbability(node3, ["state1", 'state1'], 0.4)
        self.Net.setProbability(node3, ["state1", 'state2'], 0.0)
        self.Net.setProbability(node3, ["state2", 'state1'], 0.6)
        self.Net.setProbability(node3, ["state2", 'state2'], 1.0)

        expected_outcome = {'node1': 'state2', 'node2': 'state2'}

        mpe = self.Net.getMPE("node1", "node2")
        self.assertDictEqual(expected_outcome, mpe)

    def test_MPE_disconnected(self):
        """
        testing Net.getMPE(NodeName1,NodeName2) between disconnected nodes
        """

        self.Net.createEdge(["node1", "node2"], "node3")
        node1 = self.Net.getNode("node1")
        node2 = self.Net.getNode("node2")
        node3 = self.Net.getNode("node3")

        self.Net.setProbability(node3, ["state1", 'state1', 'state1'], 0.3)
        self.Net.setProbability(node3, ["state1", 'state1', 'state2'], 0.1)
        self.Net.setProbability(node3, ["state1", 'state2', 'state1'], 0.5)
        self.Net.setProbability(node3, ["state1", 'state2', 'state2'], 0.7)
        self.Net.setProbability(node3, ["state2", 'state1', 'state1'], 0.7)
        self.Net.setProbability(node3, ["state2", 'state1', 'state2'], 0.9)
        self.Net.setProbability(node3, ["state2", 'state2', 'state1'], 0.5)
        self.Net.setProbability(node3, ["state2", 'state2', 'state2'], 0.3)

        expected_outcome = {'node1': 'state2', 'node2': 'state1'}
        mpe = self.Net.getMPE("node1", "node2")
        self.assertDictEqual(expected_outcome, mpe)
示例#3
0
class TestJPD(unittest.TestCase):
    """
    Test cases for Joint Probability Distribution
    """
    
    def setUp(self):
        self.Net = BayesNet()
        network_structure = {
            "node1" : {
                "state1" : 0.3,
                "state2" : 0.7,
            },
            "node2" : {
                "state1" : 0.7,
                "state2" : 0.3,
            },
            "node3" : {
                "state1" : 0.5,
                "state2" : 0.5
            },
        }    
        
        self.Net.create_network(network_structure)
        

        
    def test_JPD_connected(self):
        """
        testing Net.getJPD(NodeName1,NodeName2) between parent and child node(i.e. connected nodes)
        """

        self.Net.createEdge("node1",["node2","node3"])
        node1 = self.Net.getNode("node1")
        node2 = self.Net.getNode("node2")
        node3 = self.Net.getNode("node3")
        
        self.Net.setProbability(node2,["state1", 'state1'],0.3)
        self.Net.setProbability(node2,["state1", 'state2'],0.1)
        self.Net.setProbability(node2,["state2", 'state1'],0.7)
        self.Net.setProbability(node2,["state2", 'state2'],0.9)
        self.Net.setProbability(node3,["state1", 'state1'],0.4)
        self.Net.setProbability(node3,["state1", 'state2'],0.0)
        self.Net.setProbability(node3,["state2", 'state1'],0.6)
        self.Net.setProbability(node3,["state2", 'state2'],1.0)
        
        jpd = self.Net.getJPD("node1","node2")
        self.assertAlmostEqual(0.07000000029802322,jpd[(('node2', 'state2'), ('node1', 'state1'))])
        self.assertAlmostEqual(0.09000000357627869,jpd[(('node2', 'state1'), ('node1', 'state1'))])
        self.assertAlmostEqual(0.21000000834465027,jpd[(('node2', 'state1'), ('node1', 'state2'))])
        self.assertAlmostEqual(0.6299999952316284,jpd[(('node2', 'state2'), ('node1', 'state2'))])
        
        
    def test_JPD_disconnected(self):
        """
        testing Net.getJPD(NodeName1,NodeName2) between disconnected nodes
        """
        
        self.Net.createEdge(["node1","node2"],"node3")
        node1 = self.Net.getNode("node1")
        node2 = self.Net.getNode("node2")
        node3 = self.Net.getNode("node3")

        self.Net.setProbability(node3,["state1", 'state1','state1'],0.3)
        self.Net.setProbability(node3,["state1", 'state1','state2'],0.1)
        self.Net.setProbability(node3,["state1", 'state2','state1'],0.5)
        self.Net.setProbability(node3,["state1", 'state2','state2'],0.7)
        self.Net.setProbability(node3,["state2", 'state1','state1'],0.7)
        self.Net.setProbability(node3,["state2", 'state1','state2'],0.9)
        self.Net.setProbability(node3,["state2", 'state2','state1'],0.5)
        self.Net.setProbability(node3,["state2", 'state2','state2'],0.3)
        
        jpd = self.Net.getJPD("node1","node2")
        self.assertAlmostEqual(0.4899999797344208,jpd[(('node2', 'state2'), ('node1', 'state1'))])
        self.assertAlmostEqual(0.21000000834465027,jpd[(('node2', 'state1'), ('node1', 'state1'))])
        self.assertAlmostEqual(0.09000001102685928,jpd[(('node2', 'state1'), ('node1', 'state2'))])
        self.assertAlmostEqual(0.21000000834465027,jpd[(('node2', 'state2'), ('node1', 'state2'))])
示例#4
0
class TestJPD(unittest.TestCase):
    """
    Test cases for Joint Probability Distribution
    """
    def setUp(self):
        self.Net = BayesNet()
        network_structure = {
            "node1": {
                "state1": 0.3,
                "state2": 0.7,
            },
            "node2": {
                "state1": 0.7,
                "state2": 0.3,
            },
            "node3": {
                "state1": 0.5,
                "state2": 0.5
            },
        }

        self.Net.create_network(network_structure)

    def test_JPD_connected(self):
        """
        testing Net.getJPD(NodeName1,NodeName2) between parent and child node(i.e. connected nodes)
        """

        self.Net.createEdge("node1", ["node2", "node3"])
        node1 = self.Net.getNode("node1")
        node2 = self.Net.getNode("node2")
        node3 = self.Net.getNode("node3")

        self.Net.setProbability(node2, ["state1", 'state1'], 0.3)
        self.Net.setProbability(node2, ["state1", 'state2'], 0.1)
        self.Net.setProbability(node2, ["state2", 'state1'], 0.7)
        self.Net.setProbability(node2, ["state2", 'state2'], 0.9)
        self.Net.setProbability(node3, ["state1", 'state1'], 0.4)
        self.Net.setProbability(node3, ["state1", 'state2'], 0.0)
        self.Net.setProbability(node3, ["state2", 'state1'], 0.6)
        self.Net.setProbability(node3, ["state2", 'state2'], 1.0)

        jpd = self.Net.getJPD("node1", "node2")
        self.assertAlmostEqual(0.07000000029802322,
                               jpd[(('node2', 'state2'), ('node1', 'state1'))])
        self.assertAlmostEqual(0.09000000357627869,
                               jpd[(('node2', 'state1'), ('node1', 'state1'))])
        self.assertAlmostEqual(0.21000000834465027,
                               jpd[(('node2', 'state1'), ('node1', 'state2'))])
        self.assertAlmostEqual(0.6299999952316284,
                               jpd[(('node2', 'state2'), ('node1', 'state2'))])

    def test_JPD_disconnected(self):
        """
        testing Net.getJPD(NodeName1,NodeName2) between disconnected nodes
        """

        self.Net.createEdge(["node1", "node2"], "node3")
        node1 = self.Net.getNode("node1")
        node2 = self.Net.getNode("node2")
        node3 = self.Net.getNode("node3")

        self.Net.setProbability(node3, ["state1", 'state1', 'state1'], 0.3)
        self.Net.setProbability(node3, ["state1", 'state1', 'state2'], 0.1)
        self.Net.setProbability(node3, ["state1", 'state2', 'state1'], 0.5)
        self.Net.setProbability(node3, ["state1", 'state2', 'state2'], 0.7)
        self.Net.setProbability(node3, ["state2", 'state1', 'state1'], 0.7)
        self.Net.setProbability(node3, ["state2", 'state1', 'state2'], 0.9)
        self.Net.setProbability(node3, ["state2", 'state2', 'state1'], 0.5)
        self.Net.setProbability(node3, ["state2", 'state2', 'state2'], 0.3)

        jpd = self.Net.getJPD("node1", "node2")
        self.assertAlmostEqual(0.4899999797344208,
                               jpd[(('node2', 'state2'), ('node1', 'state1'))])
        self.assertAlmostEqual(0.21000000834465027,
                               jpd[(('node2', 'state1'), ('node1', 'state1'))])
        self.assertAlmostEqual(0.09000001102685928,
                               jpd[(('node2', 'state1'), ('node1', 'state2'))])
        self.assertAlmostEqual(0.21000000834465027,
                               jpd[(('node2', 'state2'), ('node1', 'state2'))])