Example #1
0
    def test_observed_complex(self):
        s1 = DiscreteVariableNode('s1', ['a', 'b'])
        s2 = DiscreteVariableNode('s2', ['a', 'b'])
        f = DiscreteFactorNode(
            'f',
            Factor(['s1', 's2'], {
                's1': ['a', 'b'],
                's2': ['a', 'b'],
            }, {
                ('a', 'a'): 1,
                ('a', 'b'): 0.5,
                ('b', 'a'): 0,
                ('b', 'b'): 0.5
            }))

        s1.connect(f)
        s2.connect(f)

        s2.observed({('a', ): 0.7, ('b', ): 0.3})

        s1.send_messages()
        s2.send_messages()

        f.update()
        f.normalize()

        f.send_messages()

        s1.update()
        s1.normalize()

        self.assertClose(s1.belief[('a', )], 0.85)
        self.assertClose(s1.belief[('b', )], 0.15)
        self.assertClose(s2.belief[('a', )], 0.7)
Example #2
0
    def test_observed_complex(self):
        s1 = DiscreteVariableNode('s1', ['a', 'b'])
        s2 = DiscreteVariableNode('s2', ['a', 'b'])
        f = DiscreteFactorNode('f', Factor(
            ['s1', 's2'],
            {
                's1': ['a', 'b'],
                's2': ['a', 'b'],
            },
            {
                ('a', 'a'): 1,
                ('a', 'b'): 0.5,
                ('b', 'a'): 0,
                ('b', 'b'): 0.5
            }))

        s1.connect(f)
        s2.connect(f)

        s2.observed({
            ('a',): 0.7,
            ('b',): 0.3
        })

        s1.send_messages()
        s2.send_messages()

        f.update()
        f.normalize()

        f.send_messages()

        s1.update()
        s1.normalize()

        self.assertClose(s1.belief[('a',)], 0.85)
        self.assertClose(s1.belief[('b',)], 0.15)
        self.assertClose(s2.belief[('a',)], 0.7)
Example #3
0
    def test_network(self):
        # Create network.
        hid = DiscreteVariableNode("hid", ["save", "del"])
        obs = DiscreteVariableNode("obs", ["osave", "odel"])
        fact_h1_o1 = DiscreteFactorNode(
            "fact_h1_o1",
            Factor(['hid', 'obs'], {
                "hid": ["save", "del"],
                "obs": ["osave", "odel"]
            }, {
                ("save", "osave"): 0.3,
                ("save", "odel"): 0.6,
                ("del", "osave"): 0.7,
                ("del", "odel"): 0.4
            }))

        # Add edges.
        obs.connect(fact_h1_o1)
        fact_h1_o1.connect(hid)

        # 1. Without observations, send_messages used.
        obs.send_messages()
        fact_h1_o1.send_messages()

        hid.update()
        hid.normalize()
        self.assertClose(hid.belief[("save", )], 0.45)

        # 2. Observed value, message_to and update_belief used.
        obs.observed({("osave", ): 1})
        obs.message_to(fact_h1_o1)
        fact_h1_o1.update()
        fact_h1_o1.message_to(hid)
        hid.update()

        hid.normalize()
        self.assertClose(hid.belief[("save", )], 0.3)

        # 3. Without observations, send_messages used.
        obs.observed(None)
        obs.send_messages()
        fact_h1_o1.send_messages()

        hid.update()
        hid.normalize()
        self.assertClose(hid.belief[("save", )], 0.45)
Example #4
0
    def test_network(self):
        # Create network.
        hid = DiscreteVariableNode("hid", ["save", "del"])
        obs = DiscreteVariableNode("obs", ["osave", "odel"])
        fact_h1_o1 = DiscreteFactorNode("fact_h1_o1", Factor(
            ['hid', 'obs'],
            {
                "hid": ["save", "del"],
                "obs": ["osave", "odel"]
            },
            {
                ("save", "osave"): 0.3,
                ("save", "odel"): 0.6,
                ("del", "osave"): 0.7,
                ("del", "odel"): 0.4
            }))

        # Add edges.
        obs.connect(fact_h1_o1)
        fact_h1_o1.connect(hid)

        # 1. Without observations, send_messages used.
        obs.send_messages()
        fact_h1_o1.send_messages()

        hid.update()
        hid.normalize()
        self.assertClose(hid.belief[("save",)], 0.45)

        # 2. Observed value, message_to and update_belief used.
        obs.observed({("osave",): 1})
        obs.message_to(fact_h1_o1)
        fact_h1_o1.update()
        fact_h1_o1.message_to(hid)
        hid.update()

        hid.normalize()
        self.assertClose(hid.belief[("save",)], 0.3)

        # 3. Without observations, send_messages used.
        obs.observed(None)
        obs.send_messages()
        fact_h1_o1.send_messages()

        hid.update()
        hid.normalize()
        self.assertClose(hid.belief[("save",)], 0.45)
Example #5
0
    def test_layers(self):
        f_h_o = {
            ("save", "osave"): 0.8,
            ("del",  "osave"): 0.2,
            ("save", "odel"): 0.2,
            ("del",  "odel"): 0.8,
        }

        f_h_h = {
            ("save", "save"): 0.9,
            ("del",  "save"): 0.1,
            ("save", "del"): 0.1,
            ("del",  "del"): 0.9
        }
        # Create nodes.
        hid1 = DiscreteVariableNode("hid1", ["save", "del"])
        obs1 = DiscreteVariableNode("obs1", ["osave", "odel"])
        fact_h1_o1 = DiscreteFactorNode("fact_h1_o1", Factor(
            ['hid1', 'obs1'],
            {
                "hid1": ["save", "del"],
                "obs1": ["osave", "odel"]
            },
            f_h_o))

        hid2 = DiscreteVariableNode("hid2", ["save", "del"])
        obs2 = DiscreteVariableNode("obs2", ["osave", "odel"])
        fact_h2_o2 = DiscreteFactorNode("fact_h2_o2", Factor(
            ['hid2', 'obs2'],
            {
                "hid2": ["save", "del"],
                "obs2": ["osave", "odel"]
            },
            f_h_o))

        fact_h1_h2 = DiscreteFactorNode("fact_h1_h2", Factor(
            ['hid1', 'hid2'],
            {
                "hid1": ["save", "del"],
                "hid2": ["save", "del"],
            },
            f_h_h))

        hid3 = DiscreteVariableNode("hid3", ["save", "del"])
        obs3 = DiscreteVariableNode("obs3", ["osave", "odel"])
        fact_h3_o3 = DiscreteFactorNode("fact_h3_o3", Factor(
            ['hid3', 'obs3'],
            {
                "hid3": ["save", "del"],
                "obs3": ["osave", "odel"]
            },
            f_h_o))

        fact_h2_h3 = DiscreteFactorNode("fact_h2_h3", Factor(
            ['hid2', 'hid3'],
            {
                "hid2": ["save", "del"],
                "hid3": ["save", "del"],
            },
            f_h_h))

        # Connect nodes.
        obs1.connect(fact_h1_o1)
        obs2.connect(fact_h2_o2)
        obs3.connect(fact_h3_o3)

        fact_h1_o1.connect(hid1)
        fact_h2_o2.connect(hid2)
        fact_h3_o3.connect(hid3)

        hid1.connect(fact_h1_h2)
        hid2.connect(fact_h1_h2)

        hid2.connect(fact_h2_h3)
        hid3.connect(fact_h2_h3)

        # Set observations.
        obs1.observed({('osave',): 1})

        # Add nodes to lbp.
        lbp = LBP(strategy='layers')
        lbp.add_layers([
            [obs1, hid1, fact_h1_o1],
            [obs2, hid2, fact_h2_o2, fact_h1_h2],
            [obs3, hid3, fact_h3_o3, fact_h2_h3],
        ])

        lbp.run()

        self.assertAlmostEqual(hid1.belief[('save',)], 0.8)
        self.assertAlmostEqual(hid2.belief[('save',)], 0.8 * 0.9 + 0.2 * 0.1, places=6)
        # @DM: The original test that fails at my computer.
        # self.assertAlmostEqual(hid3.belief[('save',)], hid2.belief[('save',)] * 0.9 + hid2.belief[('del',)] * 0.1)
        # MK: This one passes.
        self.assertAlmostEqual(
            hid3.belief[('save',)],
            hid2.belief[('save',)] * 0.9 + hid2.belief[('del',)] * 0.1,
            places=6)

        lbp = LBP(strategy='layers')
        lbp.add_layers([
            [obs1, hid1, fact_h1_o1]
        ])

        lbp.run()
        self.assertAlmostEqual(hid1.belief[('save',)], 0.8)

        lbp.add_layer([obs2, hid2, fact_h2_o2, fact_h1_h2])
        lbp.run(from_layer=0)
        self.assertAlmostEqual(hid2.belief[('save',)], 0.8 * 0.9 + 0.2 * 0.1, places=6)

        lbp.add_layers([
            [obs3, hid3, fact_h3_o3, fact_h2_h3]
        ])
        lbp.run(from_layer='last')
        self.assertAlmostEqual(hid3.belief[('save',)], hid2.belief[('save',)] * 0.9 + hid2.belief[('del',)] * 0.1)
Example #6
0
    def test_single_linked(self):
        f_h_o = {
            ("save", "osave"): 0.8,
            ("del",  "osave"): 0.2,
            ("save", "odel"): 0.2,
            ("del",  "odel"): 0.8,
        }

        f_h_h = {
            ("save", "save"): 0.9,
            ("del",  "save"): 0.1,
            ("save", "del"): 0.1,
            ("del",  "del"): 0.9
        }
        # Create nodes.
        hid1 = DiscreteVariableNode("hid1", ["save", "del"])
        obs1 = DiscreteVariableNode("obs1", ["osave", "odel"])
        fact_h1_o1 = DiscreteFactorNode("fact_h1_o1", Factor(
            ['hid1', 'obs1'],
            {
                "hid1": ["save", "del"],
                "obs1": ["osave", "odel"]
            },
            f_h_o))

        hid2 = DiscreteVariableNode("hid2", ["save", "del"])
        obs2 = DiscreteVariableNode("obs2", ["osave", "odel"])
        fact_h2_o2 = DiscreteFactorNode("fact_h2_o2", Factor(
            ['hid2', 'obs2'],
            {
                "hid2": ["save", "del"],
                "obs2": ["osave", "odel"]
            },
            f_h_o))

        fact_h1_h2 = DiscreteFactorNode("fact_h1_h2", Factor(
            ['hid1', 'hid2'],
            {
                "hid1": ["save", "del"],
                "hid2": ["save", "del"],
            },
            f_h_h))

        hid3 = DiscreteVariableNode("hid3", ["save", "del"])
        obs3 = DiscreteVariableNode("obs3", ["osave", "odel"])
        fact_h3_o3 = DiscreteFactorNode("fact_h3_o3", Factor(
            ['hid3', 'obs3'],
            {
                "hid3": ["save", "del"],
                "obs3": ["osave", "odel"]
            },
            f_h_o))

        fact_h2_h3 = DiscreteFactorNode("fact_h2_h3", Factor(
            ['hid2', 'hid3'],
            {
                "hid2": ["save", "del"],
                "hid3": ["save", "del"],
            },
            f_h_h))

        # Connect nodes.
        obs1.connect(fact_h1_o1)
        obs2.connect(fact_h2_o2)
        obs3.connect(fact_h3_o3)

        fact_h1_o1.connect(hid1)
        fact_h2_o2.connect(hid2)
        fact_h3_o3.connect(hid3)

        hid1.connect(fact_h1_h2)
        hid2.connect(fact_h1_h2)

        hid2.connect(fact_h2_h3)
        hid3.connect(fact_h2_h3)

        # Add nodes to lbp.
        lbp = LBP(strategy='tree')
        lbp.add_nodes([
            obs1, obs2, obs3,
            fact_h1_o1, fact_h2_o2, fact_h3_o3,
            hid1, hid2, hid3,
            fact_h1_h2, fact_h2_h3
        ])

        obs1.observed({('osave',): 1})
        lbp.run()

        self.assertAlmostEqual(hid1.belief[('save',)], 0.8)
        self.assertAlmostEqual(hid2.belief[('save',)], 0.8 * 0.9 + 0.2 * 0.1, places=6)
        self.assertAlmostEqual(hid3.belief[('save',)],
                               hid2.belief[('save',)] * 0.9 + hid2.belief[('del',)] * 0.1,
                               places=6)
Example #7
0
    def test_network(self):
        # Create nodes.
        hid1 = DiscreteVariableNode("hid1", ["save", "del"])
        obs1 = DiscreteVariableNode("obs1", ["osave", "odel"])
        fact_h1_o1 = DiscreteFactorNode("fact_h1_o1", Factor(
            ['hid1', 'obs1'],
            {
                "hid1": ["save", "del"],
                "obs1": ["osave", "odel"]
            },
            {
                ("save", "osave"): 0.8,
                ("save", "odel"): 0.3,
                ("del", "osave"): 0.2,
                ("del", "odel"): 0.7,
            }))

        hid2 = DiscreteVariableNode("hid2", ["save", "del"])
        obs2 = DiscreteVariableNode("obs2", ["osave", "odel"])
        fact_h2_o2 = DiscreteFactorNode("fact_h2_o2", Factor(
            ['hid2', 'obs2'],
            {
                "hid2": ["save", "del"],
                "obs2": ["osave", "odel"]
            },
            {
                ("save", "osave"): 0.8,
                ("save", "odel"): 0.2,
                ("del", "osave"): 0.2,
                ("del", "odel"): 0.8,
            }))

        fact_h1_h2 = DiscreteFactorNode("fact_h1_h2", Factor(
            ['hid1', 'hid2'],
            {
                "hid1": ["save", "del"],
                "hid2": ["save", "del"],
            },
            {
                ("save", "save"): 0.9,
                ("save", "del"): 0.1,
                ("del", "save"): 0,
                ("del", "del"): 1
            }))

        # Connect nodes.
        obs1.connect(fact_h1_o1)
        fact_h1_o1.connect(hid1)

        obs2.connect(fact_h2_o2)
        fact_h2_o2.connect(hid2)

        hid1.connect(fact_h1_h2)
        hid2.connect(fact_h1_h2)

        # Add nodes to lbp.
        lbp = LBP()
        lbp.add_nodes([
            obs1, fact_h1_o1, hid1,
            obs2, fact_h2_o2, hid2,
            fact_h1_h2
        ])

        obs1.observed({('osave',): 1})
        lbp.run(n_iterations=1)
        self.assertAlmostEqual(hid1.belief[('save',)], 0.8)

        obs2.observed({('odel',): 1})
        lbp.run(n_iterations=1)
        self.assertAlmostEqual(hid1.belief[('save',)], 0.56521738)
Example #8
0
    def test_single_linked(self):
        f_h_o = {
            ("save", "osave"): 0.8,
            ("del", "osave"): 0.2,
            ("save", "odel"): 0.2,
            ("del", "odel"): 0.8,
        }

        f_h_h = {
            ("save", "save"): 0.9,
            ("del", "save"): 0.1,
            ("save", "del"): 0.1,
            ("del", "del"): 0.9
        }
        # Create nodes.
        hid1 = DiscreteVariableNode("hid1", ["save", "del"])
        obs1 = DiscreteVariableNode("obs1", ["osave", "odel"])
        fact_h1_o1 = DiscreteFactorNode(
            "fact_h1_o1",
            Factor(['hid1', 'obs1'], {
                "hid1": ["save", "del"],
                "obs1": ["osave", "odel"]
            }, f_h_o))

        hid2 = DiscreteVariableNode("hid2", ["save", "del"])
        obs2 = DiscreteVariableNode("obs2", ["osave", "odel"])
        fact_h2_o2 = DiscreteFactorNode(
            "fact_h2_o2",
            Factor(['hid2', 'obs2'], {
                "hid2": ["save", "del"],
                "obs2": ["osave", "odel"]
            }, f_h_o))

        fact_h1_h2 = DiscreteFactorNode(
            "fact_h1_h2",
            Factor(['hid1', 'hid2'], {
                "hid1": ["save", "del"],
                "hid2": ["save", "del"],
            }, f_h_h))

        hid3 = DiscreteVariableNode("hid3", ["save", "del"])
        obs3 = DiscreteVariableNode("obs3", ["osave", "odel"])
        fact_h3_o3 = DiscreteFactorNode(
            "fact_h3_o3",
            Factor(['hid3', 'obs3'], {
                "hid3": ["save", "del"],
                "obs3": ["osave", "odel"]
            }, f_h_o))

        fact_h2_h3 = DiscreteFactorNode(
            "fact_h2_h3",
            Factor(['hid2', 'hid3'], {
                "hid2": ["save", "del"],
                "hid3": ["save", "del"],
            }, f_h_h))

        # Connect nodes.
        obs1.connect(fact_h1_o1)
        obs2.connect(fact_h2_o2)
        obs3.connect(fact_h3_o3)

        fact_h1_o1.connect(hid1)
        fact_h2_o2.connect(hid2)
        fact_h3_o3.connect(hid3)

        hid1.connect(fact_h1_h2)
        hid2.connect(fact_h1_h2)

        hid2.connect(fact_h2_h3)
        hid3.connect(fact_h2_h3)

        # Add nodes to lbp.
        lbp = LBP(strategy='tree')
        lbp.add_nodes([
            obs1, obs2, obs3, fact_h1_o1, fact_h2_o2, fact_h3_o3, hid1, hid2,
            hid3, fact_h1_h2, fact_h2_h3
        ])

        obs1.observed({('osave', ): 1})
        lbp.run()

        self.assertAlmostEqual(hid1.belief[('save', )], 0.8)
        self.assertAlmostEqual(hid2.belief[('save', )],
                               0.8 * 0.9 + 0.2 * 0.1,
                               places=6)
        self.assertAlmostEqual(hid3.belief[('save', )],
                               hid2.belief[('save', )] * 0.9 +
                               hid2.belief[('del', )] * 0.1,
                               places=6)
Example #9
0
    def test_ep(self):
        # Create nodes.
        hid1 = DiscreteVariableNode("hid1", ["save", "del"])
        obs1 = DiscreteVariableNode("obs1", ["osave", "odel"])
        fact_h1_o1 = DirichletFactorNode("fact_h1_o1")
        theta_h1_o1 = DirichletParameterNode(
            'theta_h1_o1',
            Factor(['hid1', 'obs1'], {
                "hid1": ["save", "del"],
                "obs1": ["osave", "odel"]
            }, {
                ("save", "osave"): 1,
                ("save", "odel"): 1,
                ("del", "osave"): 1,
                ("del", "odel"): 1,
            }))

        hid2 = DiscreteVariableNode("hid2", ["save", "del"])
        obs2 = DiscreteVariableNode("obs2", ["osave", "odel"])
        fact_h2_o2 = DirichletFactorNode("fact_h2_o2")
        theta_h2_o2 = DirichletParameterNode(
            'theta_h2_o2',
            Factor(['hid2', 'obs2'], {
                "hid2": ["save", "del"],
                "obs2": ["osave", "odel"]
            }, {
                ("save", "osave"): 1,
                ("save", "odel"): 1,
                ("del", "osave"): 1,
                ("del", "odel"): 1,
            }))

        fact_h1_h2 = DiscreteFactorNode(
            "fact_h1_h2",
            Factor(['hid1', 'hid2'], {
                "hid1": ["save", "del"],
                "hid2": ["save", "del"],
            }, {
                ("save", "save"): 0.9,
                ("save", "del"): 0.1,
                ("del", "save"): 0,
                ("del", "del"): 1
            }))

        # Connect nodes.
        obs1.connect(fact_h1_o1, parent=False)
        fact_h1_o1.connect(hid1)
        fact_h1_o1.connect(theta_h1_o1)

        obs2.connect(fact_h2_o2, parent=False)
        fact_h2_o2.connect(hid2)
        fact_h2_o2.connect(theta_h2_o2)

        hid1.connect(fact_h1_h2)
        hid2.connect(fact_h1_h2)

        # Add nodes to lbp.
        lbp = LBP(strategy='tree')
        lbp.add_nodes([
            obs1, obs2, fact_h1_o1, fact_h2_o2, theta_h1_o1, theta_h2_o2, hid1,
            hid2, fact_h1_h2
        ])

        obs1.observed({('osave', ): 1})
        obs2.observed({('osave', ): 1})
        hid1.observed({('save', ): 1})
        hid2.observed({('save', ): 1})

        for i in range(100):
            lbp.run()
            print theta_h1_o1.alpha.pretty_print(precision=5)
            lbp.init_messages()
Example #10
0
    def test_layers(self):
        f_h_o = {
            ("save", "osave"): 0.8,
            ("del", "osave"): 0.2,
            ("save", "odel"): 0.2,
            ("del", "odel"): 0.8,
        }

        f_h_h = {
            ("save", "save"): 0.9,
            ("del", "save"): 0.1,
            ("save", "del"): 0.1,
            ("del", "del"): 0.9
        }
        # Create nodes.
        hid1 = DiscreteVariableNode("hid1", ["save", "del"])
        obs1 = DiscreteVariableNode("obs1", ["osave", "odel"])
        fact_h1_o1 = DiscreteFactorNode(
            "fact_h1_o1",
            Factor(['hid1', 'obs1'], {
                "hid1": ["save", "del"],
                "obs1": ["osave", "odel"]
            }, f_h_o))

        hid2 = DiscreteVariableNode("hid2", ["save", "del"])
        obs2 = DiscreteVariableNode("obs2", ["osave", "odel"])
        fact_h2_o2 = DiscreteFactorNode(
            "fact_h2_o2",
            Factor(['hid2', 'obs2'], {
                "hid2": ["save", "del"],
                "obs2": ["osave", "odel"]
            }, f_h_o))

        fact_h1_h2 = DiscreteFactorNode(
            "fact_h1_h2",
            Factor(['hid1', 'hid2'], {
                "hid1": ["save", "del"],
                "hid2": ["save", "del"],
            }, f_h_h))

        hid3 = DiscreteVariableNode("hid3", ["save", "del"])
        obs3 = DiscreteVariableNode("obs3", ["osave", "odel"])
        fact_h3_o3 = DiscreteFactorNode(
            "fact_h3_o3",
            Factor(['hid3', 'obs3'], {
                "hid3": ["save", "del"],
                "obs3": ["osave", "odel"]
            }, f_h_o))

        fact_h2_h3 = DiscreteFactorNode(
            "fact_h2_h3",
            Factor(['hid2', 'hid3'], {
                "hid2": ["save", "del"],
                "hid3": ["save", "del"],
            }, f_h_h))

        # Connect nodes.
        obs1.connect(fact_h1_o1)
        obs2.connect(fact_h2_o2)
        obs3.connect(fact_h3_o3)

        fact_h1_o1.connect(hid1)
        fact_h2_o2.connect(hid2)
        fact_h3_o3.connect(hid3)

        hid1.connect(fact_h1_h2)
        hid2.connect(fact_h1_h2)

        hid2.connect(fact_h2_h3)
        hid3.connect(fact_h2_h3)

        # Set observations.
        obs1.observed({('osave', ): 1})

        # Add nodes to lbp.
        lbp = LBP(strategy='layers')
        lbp.add_layers([
            [obs1, hid1, fact_h1_o1],
            [obs2, hid2, fact_h2_o2, fact_h1_h2],
            [obs3, hid3, fact_h3_o3, fact_h2_h3],
        ])

        lbp.run()

        self.assertAlmostEqual(hid1.belief[('save', )], 0.8)
        self.assertAlmostEqual(hid2.belief[('save', )],
                               0.8 * 0.9 + 0.2 * 0.1,
                               places=6)
        # @DM: The original test that fails at my computer.
        # self.assertAlmostEqual(hid3.belief[('save',)], hid2.belief[('save',)] * 0.9 + hid2.belief[('del',)] * 0.1)
        # MK: This one passes.
        self.assertAlmostEqual(hid3.belief[('save', )],
                               hid2.belief[('save', )] * 0.9 +
                               hid2.belief[('del', )] * 0.1,
                               places=6)

        lbp = LBP(strategy='layers')
        lbp.add_layers([[obs1, hid1, fact_h1_o1]])

        lbp.run()
        self.assertAlmostEqual(hid1.belief[('save', )], 0.8)

        lbp.add_layer([obs2, hid2, fact_h2_o2, fact_h1_h2])
        lbp.run(from_layer=0)
        self.assertAlmostEqual(hid2.belief[('save', )],
                               0.8 * 0.9 + 0.2 * 0.1,
                               places=6)

        lbp.add_layers([[obs3, hid3, fact_h3_o3, fact_h2_h3]])
        lbp.run(from_layer='last')
        self.assertAlmostEqual(
            hid3.belief[('save', )],
            hid2.belief[('save', )] * 0.9 + hid2.belief[('del', )] * 0.1)
Example #11
0
    def test_network(self):
        # Create nodes.
        hid1 = DiscreteVariableNode("hid1", ["save", "del"])
        obs1 = DiscreteVariableNode("obs1", ["osave", "odel"])
        fact_h1_o1 = DiscreteFactorNode(
            "fact_h1_o1",
            Factor(['hid1', 'obs1'], {
                "hid1": ["save", "del"],
                "obs1": ["osave", "odel"]
            }, {
                ("save", "osave"): 0.8,
                ("save", "odel"): 0.3,
                ("del", "osave"): 0.2,
                ("del", "odel"): 0.7,
            }))

        hid2 = DiscreteVariableNode("hid2", ["save", "del"])
        obs2 = DiscreteVariableNode("obs2", ["osave", "odel"])
        fact_h2_o2 = DiscreteFactorNode(
            "fact_h2_o2",
            Factor(['hid2', 'obs2'], {
                "hid2": ["save", "del"],
                "obs2": ["osave", "odel"]
            }, {
                ("save", "osave"): 0.8,
                ("save", "odel"): 0.2,
                ("del", "osave"): 0.2,
                ("del", "odel"): 0.8,
            }))

        fact_h1_h2 = DiscreteFactorNode(
            "fact_h1_h2",
            Factor(['hid1', 'hid2'], {
                "hid1": ["save", "del"],
                "hid2": ["save", "del"],
            }, {
                ("save", "save"): 0.9,
                ("save", "del"): 0.1,
                ("del", "save"): 0,
                ("del", "del"): 1
            }))

        # Connect nodes.
        obs1.connect(fact_h1_o1)
        fact_h1_o1.connect(hid1)

        obs2.connect(fact_h2_o2)
        fact_h2_o2.connect(hid2)

        hid1.connect(fact_h1_h2)
        hid2.connect(fact_h1_h2)

        # Add nodes to lbp.
        lbp = LBP()
        lbp.add_nodes(
            [obs1, fact_h1_o1, hid1, obs2, fact_h2_o2, hid2, fact_h1_h2])

        obs1.observed({('osave', ): 1})
        lbp.run(n_iterations=1)
        self.assertAlmostEqual(hid1.belief[('save', )], 0.8)

        obs2.observed({('odel', ): 1})
        lbp.run(n_iterations=1)
        self.assertAlmostEqual(hid1.belief[('save', )], 0.56521738)