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 do_it(join_tree): InferenceController.reapply(join_tree, {0: [0.5, 0.5]})
from pybbn.pptc.inferencecontroller import InferenceController # you have built a BBN 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)) # you have built a junction tree from the BBN # let's call this "original" junction tree the left-hand side (lhs) junction tree lhs_jt = InferenceController.apply(bbn) # you may just update the CPTs with the original junction tree structure # the algorithm to find/build the junction tree is avoided # the CPTs are updated rhs_jt = InferenceController.reapply(lhs_jt, {0: [0.3, 0.7], 1: [0.2, 0.8, 0.8, 0.2]}) # let's print out the marginal probabilities and see how things changed # print the marginal probabilities for the lhs junction tree print('lhs probabilities') # print the posterior probabilities for node, posteriors in lhs_jt.get_posteriors().items(): p = ', '.join([f'{val}={prob:.5f}' for val, prob in posteriors.items()]) print(f'{node} : {p}') # print the marginal probabilities for the rhs junction tree print('rhs probabilities') for node, posteriors in rhs_jt.get_posteriors().items(): p = ', '.join([f'{val}={prob:.5f}' for val, prob in posteriors.items()]) print(f'{node} : {p}')
import json import time from pybbn.graph.dag import Bbn from pybbn.pptc.inferencecontroller import InferenceController def do_it(join_tree): InferenceController.reapply(join_tree, {0: [0.5, 0.5]}) with open('singly-bbn.json', 'r') as f: s = time.time() bbn = Bbn.from_dict(json.loads(f.read())) e = time.time() d = e - s print('{:.5f} seconds to load'.format(d)) s = time.time() join_tree = InferenceController.apply(bbn) e = time.time() d = e - s print('{:.5f} seconds to create join tree'.format(d)) InferenceController.reapply(join_tree, {0: [0.5, 0.5]})