def test_entails(self): ind1 = Independencies([["A", "B"], ["C", "D"], "E"]) ind2 = Independencies(["A", "C", "E"]) self.assertTrue(ind1.entails(ind2)) self.assertFalse(ind2.entails(ind1)) ind3 = Independencies(("W", ["X", "Y", "Z"])) self.assertTrue(ind3.entails(ind3.closure())) self.assertTrue(ind3.closure().entails(ind3))
def test_entails(self): ind1 = Independencies([['A', 'B'], ['C', 'D'], 'E']) ind2 = Independencies(['A', 'C', 'E']) self.assertTrue(ind1.entails(ind2)) self.assertFalse(ind2.entails(ind1)) ind3 = Independencies(('W', ['X', 'Y', 'Z'])) self.assertTrue(ind3.entails(ind3.closure())) self.assertTrue(ind3.closure().entails(ind3))
def test_build_skeleton(self): ind = Independencies(['B', 'C'], ['A', ['B', 'C'], 'D']) ind = ind.closure() skel1, sep_sets1 = ConstraintBasedEstimator.build_skeleton("ABCD", ind) self.assertTrue( self._edge_list_equal(skel1.edges(), [('A', 'D'), ('B', 'D'), ('C', 'D')])) sep_sets_ref1 = { frozenset({'A', 'C'}): (), frozenset({'A', 'B'}): (), frozenset({'C', 'B'}): () } self.assertEqual(sep_sets1, sep_sets_ref1) model = BayesianModel([('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'E')]) skel2, sep_sets2 = ConstraintBasedEstimator.build_skeleton( model.nodes(), model.get_independencies()) self.assertTrue( self._edge_list_equal(skel2, [('D', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'E')])) sep_sets_ref2 = { frozenset({'D', 'C'}): ('B', ), frozenset({'E', 'B'}): ('C', ), frozenset({'A', 'D'}): (), frozenset({'E', 'D'}): ('C', ), frozenset({'E', 'A'}): ('C', ), frozenset({'A', 'B'}): () } # witnesses/seperators might change on each run, so we cannot compare directly self.assertEqual(sep_sets2.keys(), sep_sets_ref2.keys()) self.assertEqual([len(v) for v in sorted(sep_sets2.values())], [len(v) for v in sorted(sep_sets_ref2.values())])
def test_closure(self): ind1 = Independencies(("A", ["B", "C"], "D")) self.assertEqual( ind1.closure(), Independencies( ("A", ["B", "C"], "D"), ("A", "B", ["C", "D"]), ("A", "C", ["B", "D"]), ("A", "B", "D"), ("A", "C", "D"), ), ) ind2 = Independencies(("W", ["X", "Y", "Z"])) self.assertEqual( ind2.closure(), Independencies( ("W", "Y"), ("W", "Y", "X"), ("W", "Y", "Z"), ("W", "Y", ["X", "Z"]), ("W", ["Y", "X"]), ("W", "X", ["Y", "Z"]), ("W", ["X", "Z"], "Y"), ("W", "X"), ("W", ["X", "Z"]), ("W", ["Y", "Z"], "X"), ("W", ["Y", "X", "Z"]), ("W", "X", "Z"), ("W", ["Y", "Z"]), ("W", "Z", "X"), ("W", "Z"), ("W", ["Y", "X"], "Z"), ("W", "X", "Y"), ("W", "Z", ["Y", "X"]), ("W", "Z", "Y"), ), ) ind3 = Independencies( ("c", "a", ["b", "e", "d"]), (["e", "c"], "b", ["a", "d"]), (["b", "d"], "e", "a"), ("e", ["b", "d"], "c"), ("e", ["b", "c"], "d"), (["e", "c"], "a", "b"), ) self.assertEqual(len(ind3.closure().get_assertions()), 78)
def test_closure(self): ind1 = Independencies(('A', ['B', 'C'], 'D')) self.assertEqual(ind1.closure(), Independencies(('A', ['B', 'C'], 'D'), ('A', 'B', ['C', 'D']), ('A', 'C', ['B', 'D']), ('A', 'B', 'D'), ('A', 'C', 'D'))) ind2 = Independencies(('W', ['X', 'Y', 'Z'])) self.assertEqual(ind2.closure(), Independencies( ('W', 'Y'), ('W', 'Y', 'X'), ('W', 'Y', 'Z'), ('W', 'Y', ['X', 'Z']), ('W', ['Y', 'X']), ('W', 'X', ['Y', 'Z']), ('W', ['X', 'Z'], 'Y'), ('W', 'X'), ('W', ['X', 'Z']), ('W', ['Y', 'Z'], 'X'), ('W', ['Y', 'X', 'Z']), ('W', 'X', 'Z'), ('W', ['Y', 'Z']), ('W', 'Z', 'X'), ('W', 'Z'), ('W', ['Y', 'X'], 'Z'), ('W', 'X', 'Y'), ('W', 'Z', ['Y', 'X']), ('W', 'Z', 'Y'))) ind3 = Independencies(('c', 'a', ['b', 'e', 'd']), (['e', 'c'], 'b', ['a', 'd']), (['b', 'd'], 'e', 'a'), ('e', ['b', 'd'], 'c'), ('e', ['b', 'c'], 'd'), (['e', 'c'], 'a', 'b')) self.assertEqual(len(ind3.closure().get_assertions()), 78)
def test_closure(self): ind1 = Independencies(('A', ['B', 'C'], 'D')) self.assertEqual( ind1.closure(), Independencies(('A', ['B', 'C'], 'D'), ('A', 'B', ['C', 'D']), ('A', 'C', ['B', 'D']), ('A', 'B', 'D'), ('A', 'C', 'D'))) ind2 = Independencies(('W', ['X', 'Y', 'Z'])) self.assertEqual( ind2.closure(), Independencies(('W', 'Y'), ('W', 'Y', 'X'), ('W', 'Y', 'Z'), ('W', 'Y', ['X', 'Z']), ('W', ['Y', 'X']), ('W', 'X', ['Y', 'Z']), ('W', ['X', 'Z'], 'Y'), ('W', 'X'), ('W', ['X', 'Z']), ('W', ['Y', 'Z'], 'X'), ('W', ['Y', 'X', 'Z']), ('W', 'X', 'Z'), ('W', ['Y', 'Z']), ('W', 'Z', 'X'), ('W', 'Z'), ('W', ['Y', 'X'], 'Z'), ('W', 'X', 'Y'), ('W', 'Z', ['Y', 'X']), ('W', 'Z', 'Y'))) ind3 = Independencies(('c', 'a', ['b', 'e', 'd']), (['e', 'c'], 'b', ['a', 'd']), (['b', 'd'], 'e', 'a'), ('e', ['b', 'd'], 'c'), ('e', ['b', 'c'], 'd'), (['e', 'c'], 'a', 'b')) self.assertEqual(len(ind3.closure().get_assertions()), 78)
def test_estimate_from_independencies(self): ind = Independencies(['B', 'C'], ['A', ['B', 'C'], 'D']) ind = ind.closure() model = ConstraintBasedEstimator.estimate_from_independencies("ABCD", ind) self.assertSetEqual(set(model.edges()), set([('B', 'D'), ('A', 'D'), ('C', 'D')])) model1 = BayesianModel([('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'E')]) model2 = ConstraintBasedEstimator.estimate_from_independencies( model1.nodes(), model1.get_independencies()) self.assertTrue(set(model2.edges()) == set(model1.edges()) or set(model2.edges()) == set([('B', 'C'), ('A', 'C'), ('C', 'E'), ('D', 'B')]))
def test_estimate_from_independencies(self): ind = Independencies(['B', 'C'], ['A', ['B', 'C'], 'D']) ind = ind.closure() model = ConstraintBasedEstimator.estimate_from_independencies( "ABCD", ind) self.assertSetEqual(set(model.edges()), set([('B', 'D'), ('A', 'D'), ('C', 'D')])) model1 = BayesianModel([('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'E')]) model2 = ConstraintBasedEstimator.estimate_from_independencies( model1.nodes(), model1.get_independencies()) self.assertTrue( set(model2.edges()) == set(model1.edges()) or set(model2.edges()) == set([('B', 'C'), ('A', 'C'), ('C', 'E'), ('D', 'B')]))
def test_estimate_from_independencies(self): ind = Independencies(["B", "C"], ["A", ["B", "C"], "D"]) ind = ind.closure() model = ConstraintBasedEstimator.estimate_from_independencies( "ABCD", ind) self.assertSetEqual(set(model.edges()), set([("B", "D"), ("A", "D"), ("C", "D")])) model1 = BayesianModel([("A", "C"), ("B", "C"), ("B", "D"), ("C", "E")]) model2 = ConstraintBasedEstimator.estimate_from_independencies( model1.nodes(), model1.get_independencies()) self.assertTrue( set(model2.edges()) == set(model1.edges()) or set(model2.edges()) == set([("B", "C"), ("A", "C"), ("C", "E"), ("D", "B")]))
def test_build_skeleton(self): ind = Independencies(['B', 'C'], ['A', ['B', 'C'], 'D']) ind = ind.closure() skel1, sep_sets1 = ConstraintBasedEstimator.build_skeleton("ABCD", ind) self.assertTrue(self._edge_list_equal(skel1.edges(), [('A', 'D'), ('B', 'D'), ('C', 'D')])) sep_sets_ref1 = {frozenset({'A', 'C'}): (), frozenset({'A', 'B'}): (), frozenset({'C', 'B'}): ()} self.assertEqual(sep_sets1, sep_sets_ref1) model = BayesianModel([('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'E')]) skel2, sep_sets2 = ConstraintBasedEstimator.build_skeleton(model.nodes(), model.get_independencies()) self.assertTrue(self._edge_list_equal(skel2, [('D', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'E')])) sep_sets_ref2 = {frozenset({'D', 'C'}): ('B',), frozenset({'E', 'B'}): ('C',), frozenset({'A', 'D'}): (), frozenset({'E', 'D'}): ('C',), frozenset({'E', 'A'}): ('C',), frozenset({'A', 'B'}): ()} # witnesses/seperators might change on each run, so we cannot compare directly self.assertEqual(sep_sets2.keys(), sep_sets_ref2.keys()) self.assertEqual([len(v) for v in sorted(sep_sets2.values())], [len(v) for v in sorted(sep_sets_ref2.values())])
def test_build_skeleton(self): ind = Independencies(["B", "C"], ["A", ["B", "C"], "D"]) ind = ind.closure() skel1, sep_sets1 = ConstraintBasedEstimator.build_skeleton("ABCD", ind) self.assertTrue( self._edge_list_equal(skel1.edges(), [("A", "D"), ("B", "D"), ("C", "D")])) sep_sets_ref1 = { frozenset({"A", "C"}): (), frozenset({"A", "B"}): (), frozenset({"C", "B"}): (), } self.assertEqual(sep_sets1, sep_sets_ref1) model = BayesianModel([("A", "C"), ("B", "C"), ("B", "D"), ("C", "E")]) skel2, sep_sets2 = ConstraintBasedEstimator.build_skeleton( model.nodes(), model.get_independencies()) self.assertTrue( self._edge_list_equal(skel2, [("D", "B"), ("A", "C"), ("B", "C"), ("C", "E")])) sep_sets_ref2 = { frozenset({"D", "C"}): ("B", ), frozenset({"E", "B"}): ("C", ), frozenset({"A", "D"}): (), frozenset({"E", "D"}): ("C", ), frozenset({"E", "A"}): ("C", ), frozenset({"A", "B"}): (), } # witnesses/seperators might change on each run, so we cannot compare directly self.assertEqual(sep_sets2.keys(), sep_sets_ref2.keys()) self.assertEqual( [len(v) for v in sorted(sep_sets2.values())], [len(v) for v in sorted(sep_sets_ref2.values())], )