1. Example Using the Earthquake network

In this example I will try to create the Alarm Bayesian Network using pgmpy and do some simple queries on the network. This network is mentioned in Bayesian Artificial Intelligence - Section 2.5.1 (https://bayesian-intelligence.com/publications/bai/book/BAI_Chapter2.pdf)

[1]:
#Importing Library
from pgmpy.models import BayesianNetwork
from pgmpy.inference import VariableElimination
[3]:
#Defining network structure

alarm_model = BayesianNetwork([('Burglary', 'Alarm'),
                              ('Earthquake', 'Alarm'),
                              ('Alarm', 'JohnCalls'),
                              ('Alarm', 'MaryCalls')])

#Defining the parameters using CPT
from pgmpy.factors.discrete import TabularCPD

cpd_burglary = TabularCPD(variable='Burglary', variable_card=2,
                      values=[[.999], [0.001]])
cpd_earthquake = TabularCPD(variable='Earthquake', variable_card=2,
                       values=[[0.998], [0.002]])
cpd_alarm = TabularCPD(variable='Alarm', variable_card=2,
                        values=[[0.999, 0.71, 0.06, 0.05],
                                [0.001, 0.29, 0.94, 0.95]],
                        evidence=['Burglary', 'Earthquake'],
                        evidence_card=[2, 2])
cpd_johncalls = TabularCPD(variable='JohnCalls', variable_card=2,
                      values=[[0.95, 0.1], [0.05, 0.9]],
                      evidence=['Alarm'], evidence_card=[2])
cpd_marycalls = TabularCPD(variable='MaryCalls', variable_card=2,
                      values=[[0.1, 0.7], [0.9, 0.3]],
                      evidence=['Alarm'], evidence_card=[2])

# Associating the parameters with the model structure
alarm_model.add_cpds(cpd_burglary, cpd_earthquake, cpd_alarm, cpd_johncalls, cpd_marycalls)
[4]:
# Checking if the cpds are valid for the model
alarm_model.check_model()
[4]:
True
[5]:
# Viewing nodes of the model
alarm_model.nodes()
[5]:
NodeView(('Burglary', 'Alarm', 'Earthquake', 'JohnCalls', 'MaryCalls'))
[6]:
# Viewing edges of the model
alarm_model.edges()
[6]:
OutEdgeView([('Burglary', 'Alarm'), ('Alarm', 'JohnCalls'), ('Alarm', 'MaryCalls'), ('Earthquake', 'Alarm')])
[7]:
#Checking independcies of a node
alarm_model.local_independencies('Burglary')
[7]:
(Burglary ⟂ Earthquake)
[8]:
#Listing all Independencies
alarm_model.get_independencies()
[8]:
(JohnCalls ⟂ Burglary, MaryCalls, Earthquake | Alarm)
(JohnCalls ⟂ MaryCalls, Earthquake | Burglary, Alarm)
(JohnCalls ⟂ Burglary, Earthquake | Alarm, MaryCalls)
(JohnCalls ⟂ Burglary, MaryCalls | Alarm, Earthquake)
(JohnCalls ⟂ Earthquake | Burglary, MaryCalls, Alarm)
(JohnCalls ⟂ MaryCalls | Burglary, Earthquake, Alarm)
(JohnCalls ⟂ Burglary | Alarm, MaryCalls, Earthquake)
(MaryCalls ⟂ JohnCalls, Burglary, Earthquake | Alarm)
(MaryCalls ⟂ Burglary, Earthquake | JohnCalls, Alarm)
(MaryCalls ⟂ JohnCalls, Earthquake | Burglary, Alarm)
(MaryCalls ⟂ JohnCalls, Burglary | Alarm, Earthquake)
(MaryCalls ⟂ Earthquake | JohnCalls, Burglary, Alarm)
(MaryCalls ⟂ Burglary | JohnCalls, Alarm, Earthquake)
(MaryCalls ⟂ JohnCalls | Burglary, Earthquake, Alarm)
(Earthquake ⟂ Burglary)
(Earthquake ⟂ JohnCalls, MaryCalls | Alarm)
(Earthquake ⟂ MaryCalls | JohnCalls, Alarm)
(Earthquake ⟂ JohnCalls, MaryCalls | Burglary, Alarm)
(Earthquake ⟂ JohnCalls | Alarm, MaryCalls)
(Earthquake ⟂ MaryCalls | JohnCalls, Burglary, Alarm)
(Earthquake ⟂ JohnCalls | Burglary, MaryCalls, Alarm)
(Burglary ⟂ Earthquake)
(Burglary ⟂ JohnCalls, MaryCalls | Alarm)
(Burglary ⟂ MaryCalls | JohnCalls, Alarm)
(Burglary ⟂ JohnCalls | Alarm, MaryCalls)
(Burglary ⟂ JohnCalls, MaryCalls | Alarm, Earthquake)
(Burglary ⟂ MaryCalls | JohnCalls, Alarm, Earthquake)
(Burglary ⟂ JohnCalls | Alarm, MaryCalls, Earthquake)