('accident', 'traffic_jam'), ('traffic_jam', 'long_queues'), ('traffic_jam', 'late_for_school'), ('getting_up_late', 'late_for_school')]) cpd_rain = TabularCPD('rain', 2, [[0.4], [0.6]]) cpd_accident = TabularCPD('accident', 2, [[0.2], [0.8]]) cpd_traffic_jam = TabularCPD('traffic_jam', 2, [[0.9, 0.6, 0.7, 0.1], [0.1, 0.4, 0.3, 0.9]], evidence=['rain', 'accident'], evidence_card=[2, 2]) cpd_getting_up_late = TabularCPD('getting_up_late', 2, [[0.6], [0.4]]) cpd_late_for_school = TabularCPD('late_for_school', 2, [[0.9, 0.45, 0.8, 0.1], [0.1, 0.55, 0.2, 0.9]], evidence=['getting_up_late', 'traffic_jam'], evidence_card=[2, 2]) cpd_long_queues = TabularCPD('long_queues', 2, [[0.9, 0.2], [0.1, 0.8]], evidence=['traffic_jam'], evidence_card=[2]) model.add_cpds(cpd_rain, cpd_accident, cpd_traffic_jam, cpd_getting_up_late, cpd_late_for_school, cpd_long_queues) junction_tree_bm = model.to_junction_tree() type(junction_tree_bm) junction_tree_bm.nodes() junction_tree_bm.edges()
from pgmpy.models import BayesianModel, MarkovModel from pgmpy.factors import TabularCPD, Factor # Create a bayesian model as we did in the previous chapters model = BayesianModel([('rain', 'traffic_jam'), ('accident', 'traffic_jam'), ('traffic_jam', 'long_queues'), ('traffic_jam', 'late_for_school'), ('getting_up_late', 'late_for_school')]) cpd_rain = TabularCPD('rain', 2, [[0.4], [0.6]]) cpd_accident = TabularCPD('accident', 2, [[0.2], [0.8]]) cpd_traffic_jam = TabularCPD('traffic_jam', 2, [[0.9, 0.6, 0.7, 0.1], [0.1, 0.4, 0.3, 0.9]], evidence=['rain', 'accident'], evidence_card=[2, 2]) cpd_getting_up_late = TabularCPD('getting_up_late', 2, [[0.6], [0.4]]) cpd_late_for_school = TabularCPD( 'late_for_school', 2, [[0.9, 0.45, 0.8, 0.1], [0.1, 0.55, 0.2, 0.9]], evidence=['getting_up_late', 'traffic_jam'], evidence_card=[2, 2]) cpd_long_queues = TabularCPD('long_queues', 2, [[0.9, 0.2], [0.1, 0.8]], evidence=['traffic_jam'], evidence_card=[2]) model.add_cpds(cpd_rain, cpd_accident, cpd_traffic_jam, cpd_getting_up_late, cpd_late_for_school, cpd_long_queues) junction_tree_bm = model.to_junction_tree() type(junction_tree_bm) junction_tree_bm.nodes() junction_tree_bm.edges()
values=[[0.01], [0.99]]) cpd_do = TabularCPD(variable='dog_out', variable_card=2, values=[[0.99, 0.9, 0.97, 0.3], [0.01, 0.1, 0.03, 0.7]], evidence=['family_out', 'bowel_problem'], evidence_card=[2, 2]) cpd_lo = TabularCPD(variable='light_on', variable_card=2, values=[[0.6, 0.05], [0.4, 0.95]], evidence=['family_out'], evidence_card=[2]) cpd_hb = TabularCPD(variable='hear_bark', variable_card=2, values=[[0.7, 0.01], [0.3, 0.99]], evidence=['dog_out'], evidence_card=[2]) #integrity checking model.add_cpds(cpd_fo, cpd_bp, cpd_do, cpd_lo, cpd_hb) model.check_model() junction_tree = model.to_junction_tree() print(junction_tree.nodes()) infer_bp = BeliefPropagation(junction_tree) print( infer_bp.query(['family_out'], evidence={ 'light_on': 0, 'hear_bark': 1 })['family_out'])