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_huang_inference_with_multiple_evidence(): """ Tests inference on the Huang graph with a multiple evidence. :return: None. """ bbn = BbnUtil.get_huang_graph() join_tree = InferenceController.apply(bbn) expected = { 'a': [0.5, 0.5], 'b': [0.45, 0.55], 'c': [0.45, 0.55], 'd': [0.68, 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) ev1 = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('a')) \ .with_evidence('on', 1.0) \ .build() ev2 = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('f')) \ .with_evidence('on', 1.0) \ .build() join_tree.unobserve_all() join_tree.update_evidences([ev1, ev2]) expected = { 'a': [1.0, 0.0], 'b': [0.184, 0.816], 'c': [0.798, 0.202], 'd': [0.0370, 0.963], 'e': [0.0206, 0.979], 'f': [1.0, 0.0], 'g': [0.658, 0.342], 'h': [0.941, 0.0588] } __validate_posterior__(expected, join_tree)
def sure_val_func(): global sure_node global sure_val try: sure_val = input("What is the current value of node %s: " % sure_node) # insert an observation evidence ev = EvidenceBuilder() ev = ev.with_node(join_tree.get_bbn_node_by_name(str(sure_node))) ev = ev.with_evidence(str(sure_val), 1.0) ev = ev.build() join_tree.set_observation(ev) # print the marginal probabilities except: print("Node value is not correct.") sure_val_func()
def test_inference_controller(): bbn = BbnUtil.get_huang_graph() join_tree = InferenceController.apply(bbn) print('INIT') print_potentials(join_tree) ev = EvidenceBuilder()\ .with_node(join_tree.get_bbn_node(0))\ .with_evidence('on', 1.0)\ .build() join_tree.set_observation(ev) print('FIRST') print_potentials(join_tree)
def test_huang_inference_with_single_evidence(): """ Tests inference on the Huang graph with a single evidence. :return: None. """ bbn = BbnUtil.get_huang_graph() join_tree = InferenceController.apply(bbn) expected = { 'a': [0.5, 0.5], 'b': [0.45, 0.55], 'c': [0.45, 0.55], 'd': [0.68, 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) ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('a')) \ .with_evidence('on', 1.0) \ .build() join_tree.unobserve_all() join_tree.set_observation(ev) expected = { 'a': [1.0, 0.0], 'b': [0.5, 0.5], 'c': [0.7, 0.3], 'd': [0.7, 0.3], 'e': [0.39, 0.61], 'f': [0.18934, 0.81066], 'g': [0.59, 0.41], 'h': [0.7826, 0.2174] } __validate_posterior__(expected, join_tree)
def get_stock_pct_chg(tscode): a = get_stock_info(tscode) ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('income')) \ .with_evidence(str(int(a["income_sort"])), 1.0) \ .with_node(join_tree.get_bbn_node_by_name('forecast')) \ .with_evidence(str(int(a["forecast_sort"])), 1.0) \ .with_node(join_tree.get_bbn_node_by_name('express')) \ .with_evidence(str(int(a["express_sort"])), 1.0) \ .with_node(join_tree.get_bbn_node_by_name('fina_indicator')) \ .with_evidence(str(int(a["fina_indicator_sort"])), 1.0) \ .with_node(join_tree.get_bbn_node_by_name('cashflow')) \ .with_evidence(str(int(a["cashflow_sort"])), 1.0) \ .with_node(join_tree.get_bbn_node_by_name('transaction')) \ .with_evidence(str(int(a["transaction_sort"])), 1.0) \ .with_node(join_tree.get_bbn_node_by_name('balancesheet')) \ .with_evidence(str(int(a["balancesheet_sort"])), 1.0) \ .build() join_tree.set_observation(ev) for node in join_tree.get_bbn_nodes(): potential = join_tree.get_bbn_potential(node) print(node) print(potential)
.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) join_tree = InferenceController.apply(bbn) ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('ap')) \ .with_evidence('yes', 1) \ .build() ev2 = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('p')) \ .with_evidence('yes', 1) \ .build() ev3 = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('wbc')) \ .with_evidence('high', 1) \ .build() join_tree.set_observation(ev) join_tree.set_observation(ev2) join_tree.set_observation(ev3)
.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 posterior probabilities for node, posteriors in join_tree.get_posteriors().items(): p = ', '.join([f'{val}={prob:.5f}' for val, prob in posteriors.items()]) print(f'{node} : {p}')
if __name__ == '__main__': args = parse_args(sys.argv[1:]) bbn, jt = get_bbn(args.file) while True: value = input(f'command ') if value == 'q': break elif value == 'p': posteriors = jt.get_posteriors() for name, d in posteriors.items(): s = name + ' ' + ' '.join([f'{k}={v:.5f}' for k, v in d.items()]) print(s) elif value == 'c': jt.unobserve_all() else: name, val = value.split('=') ev = EvidenceBuilder() \ .with_node(jt.get_bbn_node_by_name(name)) \ .with_evidence(val, 1.0) \ .build() start = time.time() jt.set_observation(ev) stop = time.time() diff = stop - start print(f'{diff:.5f} : inference time') print('finished')
bbn = BbnUtil.get_huang_graph() # PotentialInitializer.init(bbn) # # ug = Moralizer.moralize(bbn) # cliques = Triangulator.triangulate(ug) # # join_tree = Transformer.transform(cliques) # # Initializer.initialize(join_tree) # Propagator.propagate(join_tree) # # join_tree.set_listener(InferenceController()) 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) join_tree.set_observation(ev) ev2 = EvidenceBuilder().with_node( join_tree.get_bbn_node_by_name('a')).with_evidence('on', 1.0).build() ev3 = EvidenceBuilder().with_node( join_tree.get_bbn_node_by_name('f')).with_evidence('on', 1.0).build() join_tree.update_evidences([ev2, ev3]) # for k, v in join_tree.potentials.items(): # clique = join_tree.get_node(k) # potential = v # print(clique) # print(potential) for node in join_tree.get_bbn_nodes():
def test_inference_libpgm(): """ Tests inference with evidence on libpgm graph. :return: None. """ difficulty = BbnNode(Variable(0, 'difficulty', ['easy', 'hard']), [0.6, 0.4]) intelligence = BbnNode(Variable(1, 'intelligence', ['low', 'high']), [0.7, 0.3]) 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]) sat = BbnNode(Variable(3, 'sat', ['low', 'high']), [0.95, 0.05, 0.2, 0.8]) letter = BbnNode(Variable(4, 'letter', ['weak', 'strong']), [0.1, 0.9, 0.4, 0.6, 0.99, 0.01]) bbn = Bbn() \ .add_node(difficulty) \ .add_node(intelligence) \ .add_node(grade) \ .add_node(sat) \ .add_node(letter) \ .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) ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('sat')) \ .with_evidence('high', 1.0) \ .build() join_tree.unobserve_all() join_tree.set_observation(ev) __validate_posterior__( { 'difficulty': [0.6, 0.4], 'intelligence': [0.127, 0.873], 'grade': [0.671, 0.190, 0.139], 'sat': [0.0, 1.0], 'letter': [0.281, 0.719] }, join_tree) ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('sat')) \ .with_evidence('low', 1.0) \ .build() join_tree.unobserve_all() join_tree.set_observation(ev) __validate_posterior__( { 'difficulty': [0.6, 0.4], 'intelligence': [0.917, 0.0828], 'grade': [0.245, 0.326, 0.430], 'sat': [1.0, 0.0], 'letter': [0.58, 0.42] }, join_tree)
def get_evidence(n, v): return EvidenceBuilder() \ .with_node(self.jt.get_bbn_node_by_name(n)) \ .with_evidence(v, 1.0) \ .build()
.add_edge(Edge(season, rain, EdgeType.DIRECTED)) \ .add_edge(Edge(atmos_pres, rain, EdgeType.DIRECTED)) \ .add_edge(Edge(rain, grass, EdgeType.DIRECTED)) \ .add_edge(Edge(rain, umbrellas, EdgeType.DIRECTED)) \ .add_edge(Edge(rain, dog_bark, EdgeType.DIRECTED)) \ .add_edge(Edge(rain, cat_mood, EdgeType.DIRECTED)) \ .add_edge(Edge(dog_bark, cat_hide, EdgeType.DIRECTED)) \ .add_edge(Edge(cat_mood, cat_hide, EdgeType.DIRECTED)) # new cell join_tree = InferenceController.apply(bbn) # insert an observation evidence ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('season')) \ .with_evidence('winter', 1.0) \ .build() join_tree.set_observation(ev) # print the marginal probabilities for node in join_tree.get_bbn_nodes(): potential = join_tree.get_bbn_potential(node) print(node) print(potential) print('--------------------->') # new cell join_tree = InferenceController.apply(bbn)
# '''(graph, prog='neato')''' # plt.figure(figsize=(20, 10)) # plt.subplot(121) # labels = dict([(k, node.variable.name) for k, node in bbn.nodes.items()]) # nx.draw(graph, pos=pos, with_labels=True, labels=labels) # plt.title('BBN DAG') # 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('PastCall')) \ .with_evidence('0-4', 1) \ .build() join_tree.set_observation(ev) # print the marginal probabilities for node in join_tree.get_bbn_nodes(): potential = join_tree.get_bbn_potential(node) print(node) print(potential)
def bayesian_belief_network(): deadline = BbnNode(Variable(0, 'deadline', ['long', 'medium', 'short']), [0.33, 0.33, 0.33]) goal = BbnNode(Variable(1, 'goal', ['large', 'medium', 'small']), [0.33, 0.33, 0.33]) backers = BbnNode(Variable(3, 'backers', ['many', 'medium', 'few']), [0.6, 0.3, 0.2, 0.3, 0.4, 0.4, 0.1, 0.3, 0.4]) pledges = BbnNode(Variable(4, 'pledges', ['large', 'medium', 'small']), [0.6, 0.3, 0.1, 0.2, 0.5, 0.2, 0.2, 0.2, 0.7]) state = BbnNode(Variable(5, 'state', ['success', 'fail']), [ 0.7, 0.5, 0.2, 0.8, 0.6, 0.2, 0.9, 0.7, 0.3, 0.3, 0.5, 0.8, 0.2, 0.4, 0.8, 0.1, 0.3, 0.7 ]) global Bbn Bbn = Bbn() \ .add_node(deadline) \ .add_node(goal) \ .add_node(backers) \ .add_node(pledges) \ .add_node(state) \ .add_edge(Edge( goal, backers, EdgeType.DIRECTED)) \ .add_edge(Edge( goal, pledges, EdgeType.DIRECTED)) \ .add_edge(Edge( backers, state, EdgeType.DIRECTED)) \ .add_edge(Edge( deadline, state, EdgeType.DIRECTED)) with warnings.catch_warnings(): warnings.simplefilter('ignore') graph = convert_for_drawing(Bbn) pos = nx.nx_agraph.graphviz_layout(graph, prog='dot') #uncomment this section to print BBN #plt.figure(figsize=(8, 8)) #plt.subplot(121) #labels = dict([(k, node.variable.name) for k, node in Bbn.nodes.items()]) #nx.draw(graph, pos=pos, with_labels=True, labels=labels) #plt.title('Bayesian Belief Network Diagram') #plt.show() join_tree = InferenceController.apply(Bbn) print("BBN - Original Probabilities") for node in join_tree.get_bbn_nodes(): potential = join_tree.get_bbn_potential(node) print(node) print(potential) print('--------------------->') # insert an observation evidence ev = EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('goal')) \ .with_evidence('large', 1.0) \ .build() join_tree.set_observation(ev) #print the marginal probabilities print() print() print("BBN - Set Goal = Large") for node in join_tree.get_bbn_nodes(): potential = join_tree.get_bbn_potential(node) print(node) print(potential) print('--------------------->')