def test_get_cardinality_check_cardinality(self): self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]) phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2)) self.graph.add_factors(phi1) self.assertRaises(ValueError, self.graph.get_cardinality, check_cardinality=True) phi2 = DiscreteFactor(['a', 'c'], [1, 2], np.random.rand(2)) self.graph.add_factors(phi2) self.assertRaises(ValueError, self.graph.get_cardinality, check_cardinality=True) phi3 = DiscreteFactor(['c', 'd'], [2, 2], np.random.rand(4)) self.graph.add_factors(phi3) self.assertDictEqual( self.graph.get_cardinality(check_cardinality=True), { 'd': 2, 'c': 2, 'b': 2, 'a': 1 })
def test_remove_factors(self): self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]]) phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.graph.add_factors(phi1, phi2) self.graph.remove_factors(phi1) six.assertCountEqual(self, self.graph.factors, [phi2])
def setUp(self): self.factor1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) self.factor2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.factor3 = DiscreteFactor(['d', 'e'], [2, 2], np.random.rand(4)) self.factor4 = DiscreteFactor(['e', 'f'], [2, 2], np.random.rand(4)) self.factor5 = DiscreteFactor(['a', 'b', 'e'], [2, 2, 2], np.random.rand(8)) self.graph1 = JunctionTree() self.graph1.add_edge(('a', 'b'), ('b', 'c')) self.graph1.add_factors(self.factor1, self.factor2) self.graph2 = JunctionTree() self.graph2.add_nodes_from([('a', 'b'), ('b', 'c'), ('d', 'e')]) self.graph2.add_edge(('a', 'b'), ('b', 'c')) self.graph2.add_factors(self.factor1, self.factor2, self.factor3) self.graph3 = JunctionTree() self.graph3.add_edges_from([(('a', 'b'), ('b', 'c')), (('d', 'e'), ('e', 'f'))]) self.graph3.add_factors(self.factor1, self.factor2, self.factor3, self.factor4) self.graph4 = JunctionTree() self.graph4.add_edges_from([(('a', 'b', 'e'), ('b', 'c')), (('a', 'b', 'e'), ('e', 'f')), (('d', 'e'), ('e', 'f'))]) self.graph4.add_factors(self.factor5, self.factor2, self.factor3, self.factor4)
def test_remove_multiple_factors(self): self.graph.add_nodes_from(['a', 'b', 'c']) phi1 = DiscreteFactor(['a', 'b'], [2, 2], range(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], range(4)) self.graph.add_factors(phi1, phi2) self.graph.remove_factors(phi1, phi2) six.assertCountEqual(self, self.graph.factors, [])
def test_partition_function(self): self.graph.add_nodes_from(['a', 'b', 'c']) phi1 = DiscreteFactor(['a', 'b'], [2, 2], range(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], range(4)) self.graph.add_factors(phi1, phi2) self.graph.add_edges_from([('a', 'b'), ('b', 'c')]) self.assertEqual(self.graph.get_partition_function(), 22.0)
def test_copy_with_factors(self): self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]]) phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.graph.add_factors(phi1, phi2) graph_copy = self.graph.copy() self.assertIsInstance(graph_copy, JunctionTree) self.assertIsNot(self.graph, graph_copy) self.assertEqual(hf.recursive_sorted(self.graph.nodes()), hf.recursive_sorted(graph_copy.nodes())) self.assertEqual(hf.recursive_sorted(self.graph.edges()), hf.recursive_sorted(graph_copy.edges())) self.assertTrue(graph_copy.check_model()) self.assertEqual(self.graph.get_factors(), graph_copy.get_factors()) self.graph.remove_factors(phi1, phi2) self.assertTrue(phi1 not in self.graph.factors and phi2 not in self.graph.factors) self.assertTrue(phi1 in graph_copy.factors and phi2 in graph_copy.factors) self.graph.add_factors(phi1, phi2) self.graph.factors[0] = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) self.assertNotEqual(self.graph.get_factors()[0], graph_copy.get_factors()[0]) self.assertNotEqual(self.graph.factors, graph_copy.factors)
def test_get_factors(self): self.graph.add_nodes_from(['a', 'b', 'c']) phi1 = DiscreteFactor(['a', 'b'], [2, 2], range(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], range(4)) six.assertCountEqual(self, self.graph.get_factors(), []) self.graph.add_factors(phi1, phi2) six.assertCountEqual(self, self.graph.get_factors(), [phi1, phi2])
def setUp(self): self.junction_tree = JunctionTree([(('A', 'B'), ('B', 'C')), (('B', 'C'), ('C', 'D'))]) phi1 = DiscreteFactor(['A', 'B'], [2, 3], range(6)) phi2 = DiscreteFactor(['B', 'C'], [3, 2], range(6)) phi3 = DiscreteFactor(['C', 'D'], [2, 2], range(4)) self.junction_tree.add_factors(phi1, phi2, phi3) self.bayesian_model = BayesianModel([('A', 'J'), ('R', 'J'), ('J', 'Q'), ('J', 'L'), ('G', 'L')]) cpd_a = TabularCPD('A', 2, values=[[0.2], [0.8]]) cpd_r = TabularCPD('R', 2, values=[[0.4], [0.6]]) cpd_j = TabularCPD('J', 2, values=[[0.9, 0.6, 0.7, 0.1], [0.1, 0.4, 0.3, 0.9]], evidence=['A', 'R'], evidence_card=[2, 2]) cpd_q = TabularCPD('Q', 2, values=[[0.9, 0.2], [0.1, 0.8]], evidence=['J'], evidence_card=[2]) cpd_l = TabularCPD('L', 2, values=[[0.9, 0.45, 0.8, 0.1], [0.1, 0.55, 0.2, 0.9]], evidence=['J', 'G'], evidence_card=[2, 2]) cpd_g = TabularCPD('G', 2, values=[[0.6], [0.4]]) self.bayesian_model.add_cpds(cpd_a, cpd_g, cpd_j, cpd_l, cpd_q, cpd_r)
def test_get_cardinality(self): self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]) self.assertDictEqual(self.graph.get_cardinality(), {}) phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2)) self.graph.add_factors(phi1) self.assertDictEqual(self.graph.get_cardinality(), {'a': 1, 'b': 2}) self.graph.remove_factors(phi1) self.assertDictEqual(self.graph.get_cardinality(), {}) phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['c', 'd'], [1, 2], np.random.rand(2)) self.graph.add_factors(phi1, phi2) self.assertDictEqual(self.graph.get_cardinality(), { 'd': 2, 'a': 2, 'b': 2, 'c': 1 }) phi3 = DiscreteFactor(['d', 'a'], [1, 2], np.random.rand(2)) self.graph.add_factors(phi3) self.assertDictEqual(self.graph.get_cardinality(), { 'd': 1, 'c': 1, 'b': 2, 'a': 2 }) self.graph.remove_factors(phi1, phi2, phi3) self.assertDictEqual(self.graph.get_cardinality(), {})
def test_add_multiple_factors(self): phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2), ('c', phi2)]) self.graph.add_factors(phi1, phi2) six.assertCountEqual(self, self.graph.factors, [phi1, phi2])
def setUp(self): # A test Bayesian model diff_cpd = TabularCPD('diff', 2, [[0.6], [0.4]]) intel_cpd = TabularCPD('intel', 2, [[0.7], [0.3]]) grade_cpd = TabularCPD('grade', 3, [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25, 0.08, 0.3], [0.3, 0.7, 0.02, 0.2]], evidence=['diff', 'intel'], evidence_card=[2, 2]) self.bayesian_model = BayesianModel() self.bayesian_model.add_nodes_from(['diff', 'intel', 'grade']) self.bayesian_model.add_edges_from([('diff', 'grade'), ('intel', 'grade')]) self.bayesian_model.add_cpds(diff_cpd, intel_cpd, grade_cpd) # A test Markov model self.markov_model = MarkovModel([('A', 'B'), ('C', 'B'), ('B', 'D')]) factor_ab = DiscreteFactor(['A', 'B'], [2, 3], [1, 2, 3, 4, 5, 6]) factor_cb = DiscreteFactor(['C', 'B'], [4, 3], [3, 1, 4, 5, 7, 8, 1, 3, 10, 4, 5, 6]) factor_bd = DiscreteFactor(['B', 'D'], [3, 2], [5, 7, 2, 1, 9, 3]) self.markov_model.add_factors(factor_ab, factor_cb, factor_bd) self.gibbs = GibbsSampling(self.bayesian_model)
def test_get_variable_nodes(self): phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2), ('c', phi2)]) self.graph.add_factors(phi1, phi2) six.assertCountEqual(self, self.graph.get_variable_nodes(), ['a', 'b', 'c'])
def test_get_factors(self): self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]]) phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) six.assertCountEqual(self, self.graph.get_factors(), []) self.graph.add_factors(phi1, phi2) self.assertEqual(self.graph.get_factors(node=('b', 'a')), phi1) self.assertEqual(self.graph.get_factors(node=('b', 'c')), phi2) six.assertCountEqual(self, self.graph.get_factors(), [phi1, phi2])
def test_check_model1(self): self.graph.add_edges_from([(('a', 'b'), ('a', 'c')), (('a', 'c'), ('a', 'd'))]) phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2)) self.graph.add_factors(phi1) self.assertRaises(ValueError, self.graph.check_model) phi2 = DiscreteFactor(['a', 'c'], [1, 2], np.random.rand(2)) self.graph.add_factors(phi2) self.assertRaises(ValueError, self.graph.check_model)
def test_cardinality_mismatch_raises_error(self): self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]) factor_list = [ DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in self.graph.edges() ] self.graph.add_factors(*factor_list) self.graph.add_factors( DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6))) self.assertRaises(ValueError, self.graph.triangulate)
def test_check_model(self): self.graph.add_edges_from([(('a', 'b'), ('a', 'c'))]) phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2)) phi2 = DiscreteFactor(['a', 'c'], [1, 2], np.random.rand(2)) self.graph.add_factors(phi1, phi2) self.assertTrue(self.graph.check_model()) self.graph.remove_factors(phi2) phi2 = DiscreteFactor(['a', 'c'], [1, 2], np.random.rand(2)) self.graph.add_factors(phi2) self.assertTrue(self.graph.check_model())
def test_triangulation_h6_create_new(self): self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]) phi1 = DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6)) phi2 = DiscreteFactor(['b', 'c'], [3, 4], np.random.rand(12)) phi3 = DiscreteFactor(['c', 'd'], [4, 5], np.random.rand(20)) phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10)) self.graph.add_factors(phi1, phi2, phi3, phi4) H = self.graph.triangulate(heuristic='H6', inplace=True) self.assertListEqual( hf.recursive_sorted(H.edges()), [['a', 'b'], ['a', 'd'], ['b', 'c'], ['b', 'd'], ['c', 'd']])
def test_check_model3(self): self.graph.add_nodes_from(['a', 'b', 'c']) phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) phi3 = DiscreteFactor(['a', 'c'], [2, 2], np.random.rand(4)) self.graph.add_nodes_from([phi1, phi2]) self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2), ('c', phi2)]) self.graph.add_factors(phi1, phi2, phi3) self.assertRaises(ValueError, self.graph.check_model) self.graph.remove_factors(phi3) self.assertTrue(self.graph.check_model())
def test_to_junction_tree(self): phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2), ('c', phi2)]) self.graph.add_factors(phi1, phi2) jt = self.graph.to_junction_tree() self.assertIsInstance(jt, JunctionTree) self.assertListEqual(hf.recursive_sorted(jt.nodes()), [['a', 'b'], ['b', 'c']]) self.assertEqual(len(jt.edges()), 1)
def test_bayesian_model(self): self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]) phi1 = DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6)) phi2 = DiscreteFactor(['b', 'c'], [3, 4], np.random.rand(12)) phi3 = DiscreteFactor(['c', 'd'], [4, 5], np.random.rand(20)) phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10)) self.graph.add_factors(phi1, phi2, phi3, phi4) bm = self.graph.to_bayesian_model() self.assertIsInstance(bm, BayesianModel) self.assertListEqual(sorted(bm.nodes()), ['a', 'b', 'c', 'd']) self.assertTrue(nx.is_chordal(bm.to_undirected()))
def test_junction_tree(self): self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]) phi1 = DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6)) phi2 = DiscreteFactor(['b', 'c'], [3, 4], np.random.rand(12)) phi3 = DiscreteFactor(['c', 'd'], [4, 5], np.random.rand(20)) phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10)) self.graph.add_factors(phi1, phi2, phi3, phi4) junction_tree = self.graph.to_junction_tree() self.assertListEqual(hf.recursive_sorted(junction_tree.nodes()), [['a', 'b', 'd'], ['b', 'c', 'd']]) self.assertEqual(len(junction_tree.edges()), 1)
def test_to_markov_model(self): phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2), ('c', phi2)]) self.graph.add_factors(phi1, phi2) mm = self.graph.to_markov_model() self.assertIsInstance(mm, MarkovModel) self.assertListEqual(sorted(mm.nodes()), ['a', 'b', 'c']) self.assertListEqual(hf.recursive_sorted(mm.edges()), [['a', 'b'], ['b', 'c']]) self.assertListEqual(sorted(mm.get_factors(), key=lambda x: x.scope()), [phi1, phi2])
def test_check_model(self): self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]) phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2)) phi2 = DiscreteFactor(['c', 'b'], [3, 2], np.random.rand(6)) phi3 = DiscreteFactor(['c', 'd'], [3, 4], np.random.rand(12)) phi4 = DiscreteFactor(['d', 'a'], [4, 1], np.random.rand(4)) self.graph.add_factors(phi1, phi2, phi3, phi4) self.assertTrue(self.graph.check_model()) self.graph.remove_factors(phi1, phi4) phi1 = DiscreteFactor(['a', 'b'], [4, 2], np.random.rand(8)) self.graph.add_factors(phi1) self.assertTrue(self.graph.check_model())
def test_copy_with_factorchanges(self): self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]]) phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.graph.add_factors(phi1, phi2) graph_copy = self.graph.copy() self.graph.factors[0].reduce([('a', 0)]) self.assertNotEqual(self.graph.factors[0].scope(), graph_copy.factors[0].scope()) self.assertNotEqual(self.graph, graph_copy) self.graph.factors[1].marginalize(['b']) self.assertNotEqual(self.graph.factors[1].scope(), graph_copy.factors[1].scope()) self.assertNotEqual(self.graph, graph_copy)
def test_get_factors(self): phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2), ('c', phi2)]) six.assertCountEqual(self, self.graph.get_factors(), []) self.graph.add_factors(phi1, phi2) self.assertEqual(self.graph.get_factors(node=phi1), phi1) self.assertEqual(self.graph.get_factors(node=phi2), phi2) six.assertCountEqual(self, self.graph.get_factors(), [phi1, phi2]) self.graph.remove_factors(phi1) self.assertRaises(ValueError, self.graph.get_factors, node=phi1)
def test_check_model(self): self.graph.add_nodes_from(['a', 'b', 'c']) phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4)) phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4)) self.graph.add_nodes_from([phi1, phi2]) self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2), ('c', phi2)]) self.graph.add_factors(phi1, phi2) self.assertTrue(self.graph.check_model()) self.graph.remove_factors(phi1) self.graph.remove_node(phi1) phi1 = DiscreteFactor(['a', 'b'], [4, 2], np.random.rand(8)) self.graph.add_factors(phi1) self.graph.add_edges_from([('a', phi1)]) self.assertTrue(self.graph.check_model())
def test_factor_graph(self): phi1 = DiscreteFactor(['Alice', 'Bob'], [3, 2], np.random.rand(6)) phi2 = DiscreteFactor(['Bob', 'Charles'], [2, 2], np.random.rand(4)) self.graph.add_edges_from([('Alice', 'Bob'), ('Bob', 'Charles')]) self.graph.add_factors(phi1, phi2) factor_graph = self.graph.to_factor_graph() self.assertIsInstance(factor_graph, FactorGraph) self.assertListEqual( sorted(factor_graph.nodes()), ['Alice', 'Bob', 'Charles', 'phi_Alice_Bob', 'phi_Bob_Charles']) self.assertListEqual( hf.recursive_sorted(factor_graph.edges()), [['Alice', 'phi_Alice_Bob'], ['Bob', 'phi_Alice_Bob'], ['Bob', 'phi_Bob_Charles'], ['Charles', 'phi_Bob_Charles']]) self.assertListEqual(factor_graph.get_factors(), [phi1, phi2])
def __init__(self, intersection_set_variables, cluster_potential): """ Initialization of the current cluster """ # The variables with which the cluster is made of. self.cluster_variables = frozenset(cluster_potential.scope()) # The cluster potentials must be specified before only. self.cluster_potential = copy.deepcopy(cluster_potential) # Generate intersection sets for this cluster; S(c) self.intersection_sets_for_cluster_c = [intersect.intersection(self.cluster_variables) for intersect in intersection_set_variables if intersect.intersection(self.cluster_variables)] # Initialize messages from this cluster to its respective intersection sets # \lambda_{c \rightarrow \s} = 0 self.message_from_cluster = {} for intersection in self.intersection_sets_for_cluster_c: # Present variable. It can be a node or an edge too. (that is ['A'] or ['A', 'C'] too) present_variables = list(intersection) # Present variables cardinality present_variables_card = cluster_potential.get_cardinality(present_variables) present_variables_card = [present_variables_card[var] for var in present_variables] # We need to create a new factor whose messages are blank self.message_from_cluster[intersection] = \ DiscreteFactor(present_variables, present_variables_card, np.zeros(np.prod(present_variables_card)))
def test_check_model1(self): self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]) phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2)) phi2 = DiscreteFactor(['b', 'c'], [3, 3], np.random.rand(9)) self.graph.add_factors(phi1, phi2) self.assertRaises(ValueError, self.graph.check_model) self.graph.remove_factors(phi2) phi3 = DiscreteFactor(['c', 'a'], [4, 4], np.random.rand(16)) self.graph.add_factors(phi3) self.assertRaises(ValueError, self.graph.check_model) self.graph.remove_factors(phi3) phi2 = DiscreteFactor(['b', 'c'], [2, 3], np.random.rand(6)) phi3 = DiscreteFactor(['c', 'd'], [3, 4], np.random.rand(12)) phi4 = DiscreteFactor(['d', 'a'], [4, 3], np.random.rand(12)) self.graph.add_factors(phi2, phi3, phi4) self.assertRaises(ValueError, self.graph.check_model) self.graph.remove_factors(phi2, phi3, phi4) phi2 = DiscreteFactor(['a', 'b'], [1, 3], np.random.rand(3)) self.graph.add_factors(phi1, phi2) self.assertRaises(ValueError, self.graph.check_model) self.graph.remove_factors(phi2)
def test_check_model2(self): self.graph.add_edges_from([(('a', 'b'), ('a', 'c')), (('a', 'c'), ('a', 'd'))]) phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2)) phi2 = DiscreteFactor(['a', 'c'], [3, 3], np.random.rand(9)) phi3 = DiscreteFactor(['a', 'd'], [4, 4], np.random.rand(16)) self.graph.add_factors(phi1, phi2, phi3) self.assertRaises(ValueError, self.graph.check_model) self.graph.remove_factors(phi2) phi2 = DiscreteFactor(['a', 'c'], [1, 3], np.random.rand(3)) self.graph.add_factors(phi2) self.assertRaises(ValueError, self.graph.check_model) self.graph.remove_factors(phi3) phi3 = DiscreteFactor(['a', 'd'], [1, 4], np.random.rand(4)) self.graph.add_factors(phi3) self.assertTrue(self.graph.check_model())