def test_deepcopy(): """ Tests deep copy of join tree. :return: None """ a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) lhs = InferenceController.apply(bbn) rhs = copy.deepcopy(lhs) lhs_nodes, rhs_nodes = lhs.get_nodes(), rhs.get_nodes() lhs_edges, rhs_edges = lhs.get_edges(), rhs.get_edges() lhs_neighbors, rhs_neighbors = lhs.neighbors, rhs.neighbors lhs_evidences, rhs_evidences = lhs.evidences, rhs.evidences lhs_potentials, rhs_potentials = lhs.potentials, rhs.potentials assert len(lhs_nodes) == len(rhs_nodes) assert len(lhs_edges) == len(rhs_edges) assert len(lhs_neighbors) == len(rhs_neighbors) assert len(lhs_evidences) == len(rhs_evidences) assert len(lhs_potentials) == len(rhs_potentials) list(lhs.get_nodes())[0].nodes[0].variable.values[0] = 'true' lhs_v = list(lhs.get_nodes())[0].nodes[0].variable.values[0] rhs_v = list(rhs.get_nodes())[0].nodes[0].variable.values[0] assert lhs_v != rhs_v
def test_inference_4(): """ Tests inference on simple customized graph. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.7, 0.3]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.9, 0.1, 0.3, 0.7, 0.5, 0.5, 0.1, 0.9]) e = BbnNode(Variable(4, 'e', ['on', 'off']), [0.6, 0.4, 0.2, 0.8]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(e) \ .add_edge(Edge(a, c, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) \ .add_edge(Edge(c, e, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) expected = { 'a': [0.7, 0.3], 'b': [0.4, 0.6], 'c': [0.456, 0.544], 'e': [0.3824, 0.6176] } __validate_posterior__(expected, join_tree)
def build_bbn(variable_profiles, g, p): """ Builds a BBN from a DAG, g, and paremeters, p. :param variable_profiles: Variable profiles. :param g: DAG. :param p: Parameters. :return: BBN. """ bbn = Bbn() nodes = list(g.nodes) bbn_node_dict = {} for idx in nodes: name = g.nodes[idx]['name'] domain = variable_profiles[name] cpt = p[idx] v = Variable(idx, name, domain) n = BbnNode(v, cpt) bbn.add_node(n) bbn_node_dict[idx] = n edges = list(g.edges) for edge in edges: pa = bbn_node_dict[edge[0]] ch = bbn_node_dict[edge[1]] e = Edge(pa, ch, EdgeType.DIRECTED) bbn.add_edge(e) return bbn
def convert_for_exact_inference(g, p): """ Converts the graph and parameters to a BBN. :param g: Directed acyclic graph (DAG in the form of networkx). :param p: Parameters. :return: BBN. """ bbn = Bbn() bbn_nodes = {} for node in g.nodes: id = node params = p[id]['params'].flatten() states = [ 'state{}'.format(state) for state in range(p[id]['shape'][1]) ] v = Variable(id, str(id), states) n = BbnNode(v, params) bbn.add_node(n) bbn_nodes[id] = n for e in g.edges: pa = bbn_nodes[e[0]] ch = bbn_nodes[e[1]] bbn.add_edge(Edge(pa, ch, EdgeType.DIRECTED)) return bbn
def test_sampling_with_rejection(): """ Tests sampling a serial graph with rejection and evidence set. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) n_samples = 10000 samples = pd.DataFrame( sampler.get_samples(evidence={0: 'on'}, n_samples=n_samples, seed=37)) samples.columns = ['a', 'b', 'c'] assert n_samples == samples.shape[0] assert 3 == samples.shape[1] s_a = samples.a.value_counts() s_b = samples.b.value_counts() s_c = samples.c.value_counts() s_a = s_a / s_a.sum() s_b = s_b / s_b.sum() s_c = s_c / s_c.sum() s_a = s_a.sort_index().values s_b = s_b.sort_index().values s_c = s_c.sort_index().values assert_almost_equal(s_a, np.array([1.0])) assert_almost_equal(s_b, np.array([0.5006, 0.4994])) assert_almost_equal(s_c, np.array([0.5521, 0.4479])) join_tree = InferenceController.apply(bbn) ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('a')) \ .with_evidence('on', 1.0) \ .build() join_tree.set_observation(ev) posteriors = join_tree.get_posteriors() assert_almost_equal(s_a, np.array([posteriors['a']['on']]), decimal=1) assert_almost_equal(s_b, np.array( [posteriors['b']['off'], posteriors['b']['on']]), decimal=1) assert_almost_equal(s_c, np.array( [posteriors['c']['off'], posteriors['c']['on']]), decimal=1)
def test_sampling(): """ Tests sampling a serial graph. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) n_samples = 10000 samples = pd.DataFrame(sampler.get_samples(n_samples=n_samples, seed=37)) samples.columns = ['a', 'b', 'c'] assert n_samples == samples.shape[0] assert 3 == samples.shape[1] s_a = samples.a.value_counts() s_b = samples.b.value_counts() s_c = samples.c.value_counts() s_a = s_a / s_a.sum() s_b = s_b / s_b.sum() s_c = s_c / s_c.sum() s_a = s_a.sort_index() s_b = s_b.sort_index() s_c = s_c.sort_index() assert_almost_equal(s_a.values, np.array([0.4985, 0.5015])) assert_almost_equal(s_b.values, np.array([0.5502, 0.4498])) assert_almost_equal(s_c.values, np.array([0.5721, 0.4279])) join_tree = InferenceController.apply(bbn) posteriors = join_tree.get_posteriors() assert_almost_equal(s_a.values, np.array( [posteriors['a']['off'], posteriors['a']['on']]), decimal=1) assert_almost_equal(s_b.values, np.array( [posteriors['b']['off'], posteriors['b']['on']]), decimal=1) assert_almost_equal(s_c.values, np.array( [posteriors['c']['off'], posteriors['c']['on']]), decimal=1)
def test_trivial_inference(): """ Tests inference on trivial graphs. :return: None. """ a1 = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b1 = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn1 = Bbn().add_node(a1).add_node(b1).add_edge( Edge(a1, b1, EdgeType.DIRECTED)) jt1 = InferenceController.apply(bbn1) a2 = BbnNode(Variable(1, 'a', ['t', 'f']), [0.2, 0.8]) b2 = BbnNode(Variable(0, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn2 = Bbn().add_node(a2).add_node(b2).add_edge( Edge(a2, b2, EdgeType.DIRECTED)) jt2 = InferenceController.apply(bbn2) a3 = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b3 = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9]) bbn3 = Bbn().add_node(a3).add_node(b3) jt3 = InferenceController.apply(bbn3) __validate_posterior__({ 'a': [0.2, 0.8], 'b': [0.74, 0.26] }, jt1, debug=False) __validate_posterior__({ 'a': [0.2, 0.8], 'b': [0.74, 0.26] }, jt2, debug=False) __validate_posterior__({ 'a': [0.2, 0.8], 'b': [0.1, 0.9] }, jt3, debug=False)
def test_reapply(): """ Tests reinitializing join tree after updating CPTs. :return: None. """ a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) lhs = InferenceController.apply(bbn) rhs = InferenceController.reapply(lhs, { 0: [0.3, 0.7], 1: [0.2, 0.8, 0.8, 0.2] }) lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()] rhs_pot = [rhs.get_bbn_potential(n) for n in rhs.get_bbn_nodes()] lhs_d = Potential.to_dict(lhs_pot) rhs_d = Potential.to_dict(rhs_pot) # lhs should not match rhs after CPT update for k, prob in lhs_d.items(): assert k in rhs_d assert prob != rhs_d[k] # now create lhs with same params as param used to update old # should match with rhs since params are now the same a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.3, 0.7]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.2, 0.8, 0.8, 0.2]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) lhs = InferenceController.apply(bbn) lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()] lhs_d = Potential.to_dict(lhs_pot) for k, prob in lhs_d.items(): assert k in rhs_d assert_almost_equals(prob, rhs_d[k], 0.001)
def test_inference_var_permutation(): """ Tests inference on graphs where id are reversed. :return: None. """ a1 = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b1 = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) c1 = BbnNode(Variable(2, 'c', ['t', 'f']), [0.2, 0.8, 0.7, 0.3]) bbn1 = Bbn().add_node(a1).add_node(b1).add_node(c1) \ .add_edge(Edge(a1, b1, EdgeType.DIRECTED)) \ .add_edge(Edge(b1, c1, EdgeType.DIRECTED)) jt1 = InferenceController.apply(bbn1) a2 = BbnNode(Variable(2, 'a', ['t', 'f']), [0.2, 0.8]) b2 = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) c2 = BbnNode(Variable(0, 'c', ['t', 'f']), [0.2, 0.8, 0.7, 0.3]) bbn2 = Bbn().add_node(a2).add_node(b2).add_node(c2) \ .add_edge(Edge(a2, b2, EdgeType.DIRECTED)) \ .add_edge(Edge(b2, c2, EdgeType.DIRECTED)) jt2 = InferenceController.apply(bbn2) __validate_posterior__( { 'a': [0.2, 0.8], 'b': [0.74, 0.26], 'c': [0.33, 0.67] }, jt1, debug=False) __validate_posterior__( { 'a': [0.2, 0.8], 'b': [0.74, 0.26], 'c': [0.33, 0.67] }, jt2, debug=False)
def main(): # defining bbn variable to create a bayesian belief network global machine_name global join_tree # enter 1 or 2 as per your choice machine_type = int( input( "Choose : \n 1. Use Existing Machine \n 2. Configure a new machine \n\n" )) # if existing machine then ask for machine name and open it and process further if machine_type == 1: machine_name = str(input("Please input your machine name: ")) machine_name_file = '%s.sav' % machine_name try: join_tree = pickle.load(open(machine_name_file, 'rb')) for node in join_tree.get_bbn_nodes(): print(node) check(machine_name) potential_func() except: print("Machine name does not exists") main() else: machine_name = str(input("Please input your machine name: ")) globals()['machine_%s' % machine_name] = Bbn() # create the nodes create_bbn_nodes() # create the network structure by edges and nodes create_bbn_edges() join_tree = InferenceController.apply(globals()['machine_%s' % machine_name]) filename = '%s.sav' % machine_name pickle.dump(join_tree, open(filename, 'wb')) print(globals()['machine_%s' % machine_name]) check(machine_name) potential_func()
def test_inference_1(): """ Tests inference on the Huang graph with manual construction. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) d = BbnNode(Variable(3, 'd', ['on', 'off']), [0.9, 0.1, 0.5, 0.5]) e = BbnNode(Variable(4, 'e', ['on', 'off']), [0.3, 0.7, 0.6, 0.4]) f = BbnNode(Variable(5, 'f', ['on', 'off']), [0.01, 0.99, 0.01, 0.99, 0.01, 0.99, 0.99, 0.01]) g = BbnNode(Variable(6, 'g', ['on', 'off']), [0.8, 0.2, 0.1, 0.9]) h = BbnNode(Variable(7, 'h', ['on', 'off']), [0.05, 0.95, 0.95, 0.05, 0.95, 0.05, 0.95, 0.05]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(d) \ .add_node(e) \ .add_node(f) \ .add_node(g) \ .add_node(h) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(a, c, EdgeType.DIRECTED)) \ .add_edge(Edge(b, d, EdgeType.DIRECTED)) \ .add_edge(Edge(c, e, EdgeType.DIRECTED)) \ .add_edge(Edge(d, f, EdgeType.DIRECTED)) \ .add_edge(Edge(e, f, EdgeType.DIRECTED)) \ .add_edge(Edge(c, g, EdgeType.DIRECTED)) \ .add_edge(Edge(e, h, EdgeType.DIRECTED)) \ .add_edge(Edge(g, h, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) expected = { 'a': [0.5, 0.5], 'b': [0.45, 0.55], 'c': [0.45, 0.55], 'd': [0.680, 0.32], 'e': [0.465, 0.535], 'f': [0.176, 0.824], 'g': [0.415, 0.585], 'h': [0.823, 0.177] } __validate_posterior__(expected, join_tree)
def test_github_issue_4(): """ Tests issue #4 https://github.com/vangj/py-bbn/issues/4 :return: None. """ a = BbnNode(Variable(0, 'A', ['T', 'F']), [0.5, 0.5]) b = BbnNode(Variable(1, 'B', ['T', 'F']), [0.2, 0.8, 0.1, 0.9]) c = BbnNode(Variable(2, 'C', ['T', 'F']), [0.5, 0.5, 0.5, 0.5]) d = BbnNode(Variable(3, 'D', ['T', 'F']), [0.5, 0.5, 0.5, 0.5]) e = BbnNode(Variable(4, 'E', ['T', 'F']), [0.5, 0.5, 0.5, 0.5]) f = BbnNode(Variable(5, 'F', ['T', 'F']), [0.5, 0.5, 0.5, 0.5]) g = BbnNode(Variable(6, 'G', ['T', 'F']), [ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 ]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(d) \ .add_node(e) \ .add_node(f) \ .add_node(g) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(a, c, EdgeType.DIRECTED)) \ .add_edge(Edge(b, d, EdgeType.DIRECTED)) \ .add_edge(Edge(b, e, EdgeType.DIRECTED)) \ .add_edge(Edge(c, f, EdgeType.DIRECTED)) \ .add_edge(Edge(e, g, EdgeType.DIRECTED)) \ .add_edge(Edge(d, g, EdgeType.DIRECTED)) \ .add_edge(Edge(f, g, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) expected = { 'A': [0.5, 0.5], 'B': [0.15, 0.85], 'C': [0.5, 0.5], 'D': [0.5, 0.5], 'E': [0.5, 0.5], 'F': [0.5, 0.5], 'G': [0.5, 0.5] } __validate_posterior__(expected, join_tree) __print_potentials__(join_tree)
def test_from_data_simple(): """ Tests create BBN from data. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) bbn1 = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn1) samples = sampler.get_samples(n_samples=10000, seed=37) i2n = {n.variable.id: n.variable.name for n in bbn1.get_nodes()} samples = pd.DataFrame(samples).rename(columns=i2n) parents = { 'a': [], 'b': ['a'], 'c': ['b'] } bbn2 = Factory.from_data(parents, samples) join_tree1 = InferenceController.apply(bbn1) join_tree2 = InferenceController.apply(bbn2) posteriors1 = join_tree1.get_posteriors() posteriors2 = join_tree2.get_posteriors() for k, v1 in posteriors1.items(): assert k in posteriors2 v2 = posteriors2[k] assert len(v1) == len(v2) for k2 in v1: assert k2 in v2 diff = abs(v1[k2] - v2[k2]) assert diff < 0.01
def test_toplogical_sort_mixed(): """ Tests topological sort of diverging structure. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(b, a, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) assert_almost_equal([1, 0, 2], sampler.nodes)
def test_toplogical_sort_reversed(): """ Tests topological sorting of graph with nodes in reverse order. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.5, 0.5]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(c, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, a, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) assert_almost_equal([2, 1, 0], sampler.nodes)
def test_inference_2(): """ Tests inference on customized graph. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.7, 0.3]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.9, 0.1, 0.3, 0.7, 0.5, 0.5, 0.1, 0.9]) d = BbnNode(Variable(3, 'd', ['on', 'off']), [0.3, 0.7, 0.8, 0.2]) e = BbnNode(Variable(4, 'e', ['on', 'off']), [0.6, 0.4, 0.2, 0.8]) f = BbnNode(Variable(5, 'f', ['on', 'off']), [0.7, 0.3, 0.1, 0.9]) g = BbnNode(Variable(6, 'g', ['on', 'off']), [0.4, 0.6, 0.9, 0.1]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(d) \ .add_node(e) \ .add_node(f) \ .add_node(g) \ .add_edge(Edge(a, c, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) \ .add_edge(Edge(c, d, EdgeType.DIRECTED)) \ .add_edge(Edge(c, e, EdgeType.DIRECTED)) \ .add_edge(Edge(d, f, EdgeType.DIRECTED)) \ .add_edge(Edge(d, g, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) expected = { 'a': [0.7, 0.3], 'b': [0.4, 0.6], 'c': [0.456, 0.544], 'd': [0.572, 0.428], 'e': [0.382, 0.618], 'f': [0.443, 0.557], 'g': [0.614, 0.386] } __validate_posterior__(expected, join_tree)
def test_inference_libpgm2(): """ Tests libpgm graph where ordering messes up computation. :return: None. """ letter = BbnNode(Variable(4, 'Letter', ['weak', 'strong']), [0.1, 0.9, 0.4, 0.6, 0.99, 0.01]) grade = BbnNode( Variable(2, 'Grade', ['a', 'b', 'c']), [0.3, 0.4, 0.3, 0.9, 0.08, 0.02, 0.05, 0.25, 0.7, 0.5, 0.3, 0.2]) intelligence = BbnNode(Variable(3, 'Intelligence', ['low', 'high']), [0.7, 0.3]) sat = BbnNode(Variable(1, 'SAT', ['low', 'high']), [0.95, 0.05, 0.2, 0.8]) difficulty = BbnNode(Variable(0, 'Difficulty', ['easy', 'hard']), [0.6, 0.4]) bbn = Bbn() \ .add_node(letter) \ .add_node(grade) \ .add_node(intelligence) \ .add_node(sat) \ .add_node(difficulty) \ .add_edge(Edge(difficulty, grade, EdgeType.DIRECTED)) \ .add_edge(Edge(intelligence, grade, EdgeType.DIRECTED)) \ .add_edge(Edge(intelligence, sat, EdgeType.DIRECTED)) \ .add_edge(Edge(grade, letter, EdgeType.DIRECTED)) join_tree = InferenceController.apply(bbn) __validate_posterior__( { 'Difficulty': [0.6, 0.4], 'Intelligence': [0.7, 0.3], 'Grade': [0.362, 0.288, 0.350], 'SAT': [0.725, 0.275], 'Letter': [0.498, 0.502] }, join_tree, debug=False)
def test_simple_serde(): """ Tests join tree serde with only 1 clique. :return: None. """ a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) lhs = InferenceController.apply(bbn) d = JoinTree.to_dict(lhs) rhs = JoinTree.from_dict(d) rhs = InferenceController.apply_from_serde(rhs) lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()] rhs_pot = [rhs.get_bbn_potential(n) for n in rhs.get_bbn_nodes()] lhs_pot = Potential.to_dict(lhs_pot) rhs_pot = Potential.to_dict(rhs_pot) assert len(lhs_pot) == len(rhs_pot)
def test_sampler_tables(): """ Tests sampler creation of tables. :return: None. """ a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) assert_almost_equal([0, 1, 2], sampler.nodes) tables = sampler.tables assert 3 == len(tables) assert 0 in tables assert 1 in tables assert 2 in tables lhs = np.array(tables[0].probs) rhs = np.array([0.5, 1.0]) assert_almost_equal(lhs, rhs) lhs = np.array(list(tables[1].probs.values())) rhs = np.array([[0.5, 1.0], [0.4, 1.0]]) assert_almost_equal(lhs, rhs) lhs = np.array(list(tables[2].probs.values())) rhs = np.array([[0.7, 1.0], [0.2, 1.0]]) assert_almost_equal(lhs, rhs)
def get_drug_network(): gender_probs = [0.49, 0.51] drug_probs = [ 0.23323615160349853, 0.7667638483965015, 0.7563025210084033, 0.24369747899159663 ] recovery_probs = [ 0.31000000000000005, 0.69, 0.27, 0.73, 0.13, 0.87, 0.06999999999999995, 0.93 ] X = BbnNode(Variable(1, 'drug', ['false', 'true']), drug_probs) Y = BbnNode(Variable(2, 'recovery', ['false', 'true']), recovery_probs) Z = BbnNode(Variable(0, 'gender', ['female', 'male']), gender_probs) bbn = Bbn() \ .add_node(X) \ .add_node(Y) \ .add_node(Z) \ .add_edge(Edge(Z, X, EdgeType.DIRECTED)) \ .add_edge(Edge(Z, Y, EdgeType.DIRECTED)) \ .add_edge(Edge(X, Y, EdgeType.DIRECTED)) return bbn
def test_forest_inference(): """ Tests inference on a disconnected DAG; sub-DAGs are a -> b, c -> d and e -> f. :return: None. """ a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) c = BbnNode(Variable(2, 'c', ['t', 'f']), [0.2, 0.8]) d = BbnNode(Variable(3, 'd', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) e = BbnNode(Variable(4, 'e', ['t', 'f']), [0.2, 0.8]) f = BbnNode(Variable(5, 'f', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b).add_node(c).add_node(d).add_node(e).add_node(f) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(c, d, EdgeType.DIRECTED)) \ .add_edge(Edge(e, f, EdgeType.DIRECTED)) jt = InferenceController.apply(bbn) pot = [jt.get_bbn_potential(n) for n in jt.get_bbn_nodes()] o = Potential.to_dict(pot) e = { '0=t': 0.2, '0=f': 0.8, '1=t': 0.7400000000000001, '1=f': 0.26, '2=t': 0.2, '2=f': 0.8, '3=t': 0.7400000000000001, '3=f': 0.26, '4=t': 0.2, '4=f': 0.8, '5=t': 0.7400000000000001, '5=f': 0.26 } for k, p in e.items(): assert_almost_equals(p, o[k], 0.001)
d = BbnNode(Variable(3, 'd', ['on', 'off']), [0.9, 0.1, 0.5, 0.5]) e = BbnNode(Variable(4, 'e', ['on', 'off']), [0.3, 0.7, 0.6, 0.4]) f = BbnNode(Variable(5, 'f', ['on', 'off']), [0.01, 0.99, 0.01, 0.99, 0.01, 0.99, 0.99, 0.01]) g = BbnNode(Variable(6, 'g', ['on', 'off']), [0.8, 0.2, 0.1, 0.9]) h = BbnNode(Variable(7, 'h', ['on', 'off']), [0.05, 0.95, 0.95, 0.05, 0.95, 0.05, 0.95, 0.05]) # create the network structure bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(d) \ .add_node(e) \ .add_node(f) \ .add_node(g) \ .add_node(h) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(a, c, EdgeType.DIRECTED)) \ .add_edge(Edge(b, d, EdgeType.DIRECTED)) \ .add_edge(Edge(c, e, EdgeType.DIRECTED)) \ .add_edge(Edge(d, f, EdgeType.DIRECTED)) \ .add_edge(Edge(e, f, EdgeType.DIRECTED)) \ .add_edge(Edge(c, g, EdgeType.DIRECTED)) \ .add_edge(Edge(e, h, EdgeType.DIRECTED)) \ .add_edge(Edge(g, h, EdgeType.DIRECTED)) # convert the BBN to a join tree join_tree = InferenceController.apply(bbn) # insert an observation evidence ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('a')) \
def from_libpgm_discrete_object(bn): """ Converts a libpgm discrete network object into a py-bbn one. :param bn: libpgm discrete BBN. :return: py-bbn BBN. """ def get_nodes(bn, domain_spaces=True): def get_parent_domains(name, bn): parents = bn.Vdata[name]['parents'] domains = [] if parents is None or len(parents) == 0: return domains for parent in parents: domain = bn.Vdata[parent]['vals'][:] domains.append(domain) return domains def cross_product(domains): products = [] if domains is None or len(domains) == 0: return products for e in itertools.product(*domains): products.append(e) return products def stringify_cross_product(pa_domains, domain_spaces=True): joiner_delim = ', ' if domain_spaces is True else ',' s = [] for pa_domain in pa_domains: r = joiner_delim.join( ["'{}'".format(v) for v in pa_domain]) r = '[{}]'.format(r) s.append(r) return s def get_cond_probs(name, bn, domain_spaces=True): probs = [] pa_domains = stringify_cross_product( cross_product(get_parent_domains(name, bn)), domain_spaces) if len(pa_domains) == 0: probs = bn.Vdata[name]['cprob'][:] else: for pa_domain in pa_domains: cprob = bn.Vdata[name]['cprob'][pa_domain] probs.extend(cprob) return probs nodes = {} for name in bn.V: domain = bn.Vdata[name]['vals'][:] order = bn.Vdata[name]['ord'] probs = get_cond_probs(name, bn, domain_spaces) node = BbnNode(Variable(order, name, domain), probs) nodes[name] = node return nodes def get_edges(bn, nodes): edges = [] for k, v in bn.Vdata.items(): ch = nodes[k] if v['parents'] is not None and len(v['parents']) > 0: parents = [nodes[pa] for pa in v['parents']] for pa in parents: edge = Edge(pa, ch, EdgeType.DIRECTED) edges.append(edge) return edges nodes = get_nodes(bn) edges = get_edges(bn, nodes) bbn = Bbn() for node in sorted(nodes.values(), key=lambda n: n.id): bbn.add_node(node) for e in edges: bbn.add_edge(e) return bbn
def from_data(structure, df): """ Creates a BBN. :param structure: A dictionary where keys are names of children and values are list of parent names. :param df: A dataframe. :return: BBN. """ def get_profile(df): profile = {} for c in df.columns: values = sorted(list(df[c].value_counts().index)) profile[c] = values return profile def get_n2i(parents): g = nx.DiGraph() for k in parents: g.add_node(k) for ch, pas in parents.items(): for pa in pas: g.add_edge(pa, ch) nodes = list(topological_sort(g)) return {n: i for i, n in enumerate(nodes)} def get_cpt(name, parents, n2v, df): parents = sorted(parents) n2v = {k: sorted(v) for k, v in n2v.items()} n = df.shape[0] cpts = [] if len(parents) == 0: for v in n2v[name]: c = df[df[name] == v].shape[0] p = c / n cpts.append(p) else: domains = [(n, d) for n, d in n2v.items() if n in parents] domains = sorted(domains, key=lambda tup: tup[0]) domain_names = [tup[0] for tup in domains] domain_values = [tup[1] for tup in domains] domains = list(product(*domain_values)) for values in domains: probs = [] denom_q = ' and '.join( [f'{n}=="{v}"' for n, v in zip(domain_names, values)]) for v in n2v[name]: numer_q = f'{name}=="{v}" and {denom_q}' numer = df.query(numer_q).shape[0] / n denom = df.query(denom_q).shape[0] / n prob = numer / denom probs.append(prob) probs = pd.Series(probs) probs = probs / probs.sum() probs = list(probs) cpts.extend(probs) return cpts n2v = get_profile(df) n2i = get_n2i(df) n2c = {n: get_cpt(n, structure[n], n2v, df) for n in structure} bbn = Bbn() nodes = {} for name in n2v: idx = n2i[name] values = n2v[name] cpts = n2c[name] v = Variable(idx, name, values) node = BbnNode(v, cpts) nodes[name] = node bbn.add_node(node) for ch, parents in structure.items(): ch_node = nodes[ch] for pa in parents: pa_node = nodes[pa] edge = Edge(pa_node, ch_node, EdgeType.DIRECTED) bbn.add_edge(edge) return bbn
from pybbn.graph.dag import Bbn from pybbn.graph.edge import Edge, EdgeType from pybbn.graph.node import BbnNode from pybbn.graph.variable import Variable # create graph a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8]) b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1]) bbn = Bbn().add_node(a).add_node(b) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) # serialize Bbn.to_csv(bbn, 'simple-bbn.csv')
from pybbn.graph.dag import Bbn from pybbn.graph.edge import Edge, EdgeType from pybbn.graph.node import BbnNode from pybbn.graph.variable import Variable from pybbn.sampling.sampling import LogicSampler a = BbnNode(Variable(0, 'a', ['on', 'off']), [0.5, 0.5]) b = BbnNode(Variable(1, 'b', ['on', 'off']), [0.5, 0.5, 0.4, 0.6]) c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.2, 0.8]) bbn = Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_edge(Edge(a, b, EdgeType.DIRECTED)) \ .add_edge(Edge(b, c, EdgeType.DIRECTED)) sampler = LogicSampler(bbn) samples = sampler.get_samples(n_samples=10000, seed=37)
def test_to_dict(): """ Tests serializing join tree to dictionary. :return: None. """ n0 = BbnNode(Variable(0, 'n0', ['t', 'f']), [0.2, 0.8]) n1 = BbnNode(Variable(1, 'n1', ['t', 'f']), [0.9, 0.1, 0.9, 0.1]) n2 = BbnNode(Variable(2, 'n2', ['t', 'f']), [0.6, 0.4, 0.4, 0.6]) bbn = Bbn().add_node(n0).add_node(n1).add_node(n2) \ .add_edge(Edge(n0, n1, EdgeType.DIRECTED)) \ .add_edge(Edge(n1, n2, EdgeType.DIRECTED)) jt = InferenceController.apply(bbn) d = JoinTree.to_dict(jt) lhs = json.dumps(d, sort_keys=True, indent=2) rhs = """{ "bbn_nodes": { "0": { "probs": [ 0.2, 0.8 ], "variable": { "id": 0, "name": "n0", "values": [ "t", "f" ] } }, "1": { "probs": [ 0.9, 0.1, 0.9, 0.1 ], "variable": { "id": 1, "name": "n1", "values": [ "t", "f" ] } }, "2": { "probs": [ 0.6, 0.4, 0.4, 0.6 ], "variable": { "id": 2, "name": "n2", "values": [ "t", "f" ] } } }, "jt": { "edges": [ "0-1-1-1-2" ], "nodes": { "0-1": { "node_ids": [ 0, 1 ], "type": "clique" }, "0-1-1-1-2": { "left": "0-1", "right": "1-2", "type": "sepset" }, "1-2": { "node_ids": [ 1, 2 ], "type": "clique" } }, "parent_info": { "0": [], "1": [ 0 ], "2": [ 1 ] } } }""" assert lhs == rhs
c = BbnNode(Variable(2, 'c', ['on', 'off']), [0.7, 0.3, 0.5, 0.5]) d = BbnNode(Variable(3, 'd', ['on', 'off']), [0.8, 0.2, 0.1, 0.9]) e = BbnNode(Variable(4, 'e', ['on', 'off']), [0.1, 0.9, 0.8, 0.2]) f = BbnNode(Variable(5, 'f', ['on', 'off']), [0.01, 0.99, 0.01, 0.99, 0.01, 0.99, 0.99, 0.01]) g = BbnNode(Variable(6, 'g', ['on', 'off']), [0.8, 0.2, 0.1, 0.9]) h = BbnNode(Variable(7, 'h', ['on', 'off']), [0.05, 0.95, 0.95, 0.05, 0.95, 0.05, 0.95, 0.05]) # create the network structure bbn = Bbn().add_node(a).add_node(b).add_node(c).add_node(d).add_node( e).add_node(f).add_node(g).add_node(h).add_edge( Edge(a, b, EdgeType.DIRECTED)).add_edge( Edge(a, c, EdgeType.DIRECTED)).add_edge( Edge(b, d, EdgeType.DIRECTED)).add_edge( Edge(c, e, EdgeType.DIRECTED)).add_edge( Edge(d, f, EdgeType.DIRECTED)).add_edge( Edge(e, f, EdgeType.DIRECTED)).add_edge( Edge(c, g, EdgeType.DIRECTED)).add_edge( Edge(e, h, EdgeType.DIRECTED)).add_edge( Edge(g, h, EdgeType.DIRECTED)) # convert the BBN to a join tree join_tree = InferenceController.apply(bbn) # insert an observation evidence ev = EvidenceBuilder().with_node( join_tree.get_bbn_node_by_name('a')).with_evidence('on', 1.0).build() join_tree.set_observation(ev) # print the marginal probabilities
from pybbn.graph.variable import Variable from pybbn.pptc.inferencecontroller import InferenceController import networkx as nx # for drawing graphs import matplotlib.pyplot as plt # for drawing graphs if __name__ == '__main__': ap = BbnNode(Variable(0, 'ap', ['yes', 'no']), [0.3, 0.7]) p = BbnNode(Variable(1, 'p', ['yes', 'no']), [0.6, 0.4]) sir = BbnNode(Variable(2, 'sir', ['yes', 'no']), [0.7, 0.3, 0.45, 0.55, 0.55, 0.45, 0.2, 0.8]) wbc = BbnNode(Variable(3, 'wbc', ['high', 'low']), [0.6, 0.4, 0.3, 0.7]) bbn = Bbn() \ .add_node(ap) \ .add_node(p) \ .add_node(sir) \ .add_node(wbc) \ .add_edge(Edge(ap, sir, EdgeType.DIRECTED)) \ .add_edge(Edge(p, sir, EdgeType.DIRECTED)) \ .add_edge(Edge(sir, wbc, EdgeType.DIRECTED)) options = { "font_size": 16, "node_size": 3000, "node_color": "white", "edgecolors": "black", "edge_color": "red", "linewidths": 5, "width": 5, } n, d = bbn.to_nx_graph() nx.draw(n, with_labels=True, labels=d, **options)
def test_from_dict(): """ Tests deserializing from dictionary. :return: """ s = """{ "bbn_nodes": { "0": { "probs": [ 0.2, 0.8 ], "variable": { "id": 0, "name": "n0", "values": [ "t", "f" ] } }, "1": { "probs": [ 0.9, 0.1, 0.9, 0.1 ], "variable": { "id": 1, "name": "n1", "values": [ "t", "f" ] } }, "2": { "probs": [ 0.6, 0.4, 0.4, 0.6 ], "variable": { "id": 2, "name": "n2", "values": [ "t", "f" ] } } }, "jt": { "edges": [ "0-1-1-1-2" ], "nodes": { "0-1": { "node_ids": [ 0, 1 ], "type": "clique" }, "0-1-1-1-2": { "left": "0-1", "right": "1-2", "type": "sepset" }, "1-2": { "node_ids": [ 1, 2 ], "type": "clique" } }, "parent_info": { "0": [], "1": [ 0 ], "2": [ 1 ] } } }""" d = json.loads(s) lhs = JoinTree.from_dict(d) lhs = InferenceController.apply_from_serde(lhs) n0 = BbnNode(Variable(0, 'n0', ['t', 'f']), [0.2, 0.8]) n1 = BbnNode(Variable(1, 'n1', ['t', 'f']), [0.9, 0.1, 0.9, 0.1]) n2 = BbnNode(Variable(2, 'n2', ['t', 'f']), [0.6, 0.4, 0.4, 0.6]) bbn = Bbn().add_node(n0).add_node(n1).add_node(n2) \ .add_edge(Edge(n0, n1, EdgeType.DIRECTED)) \ .add_edge(Edge(n1, n2, EdgeType.DIRECTED)) rhs = InferenceController.apply(bbn) lhs_pot = [lhs.get_bbn_potential(n) for n in lhs.get_bbn_nodes()] rhs_pot = [rhs.get_bbn_potential(n) for n in rhs.get_bbn_nodes()] lhs_pot = Potential.to_dict(lhs_pot) rhs_pot = Potential.to_dict(rhs_pot) assert len(lhs_pot) == len(rhs_pot) for k, p in lhs_pot.items(): assert_almost_equals(p, rhs_pot[k], 0.001)