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)


In [9]:
#Importing Library
from pgmpy.models import BayesianModel
from pgmpy.inference import VariableElimination

In [10]:
#Defining network structure

alarm_model = BayesianModel([('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)

In [11]:
# Checking if the cpds are valid for the model
alarm_model.check_model()


Out[11]:
True

In [12]:
# Viewing nodes of the model
alarm_model.nodes()


Out[12]:
NodeView(('Burglary', 'Alarm', 'Earthquake', 'JohnCalls', 'MaryCalls'))

In [13]:
# Viewing edges of the model
alarm_model.edges()


Out[13]:
OutEdgeView([('Burglary', 'Alarm'), ('Alarm', 'JohnCalls'), ('Alarm', 'MaryCalls'), ('Earthquake', 'Alarm')])

In [15]:
#Checking independcies of a node
alarm_model.local_independencies('Burglary')


Out[15]:
(Burglary _|_ Earthquake)

In [16]:
#Listing all Independencies
alarm_model.get_independencies()


Out[16]:
(Burglary _|_ Earthquake)
(Burglary _|_ MaryCalls, JohnCalls | Alarm)
(Burglary _|_ JohnCalls | MaryCalls, Alarm)
(Burglary _|_ MaryCalls, JohnCalls | Alarm, Earthquake)
(Burglary _|_ MaryCalls | JohnCalls, Alarm)
(Burglary _|_ JohnCalls | MaryCalls, Alarm, Earthquake)
(Burglary _|_ MaryCalls | Alarm, JohnCalls, Earthquake)
(Earthquake _|_ Burglary)
(Earthquake _|_ MaryCalls, JohnCalls | Alarm)
(Earthquake _|_ MaryCalls, JohnCalls | Burglary, Alarm)
(Earthquake _|_ JohnCalls | MaryCalls, Alarm)
(Earthquake _|_ MaryCalls | JohnCalls, Alarm)
(Earthquake _|_ JohnCalls | Burglary, MaryCalls, Alarm)
(Earthquake _|_ MaryCalls | Burglary, JohnCalls, Alarm)
(JohnCalls _|_ Burglary, MaryCalls, Earthquake | Alarm)
(JohnCalls _|_ MaryCalls, Earthquake | Burglary, Alarm)
(JohnCalls _|_ Burglary, Earthquake | MaryCalls, Alarm)
(JohnCalls _|_ Burglary, MaryCalls | Alarm, Earthquake)
(JohnCalls _|_ Earthquake | Burglary, MaryCalls, Alarm)
(JohnCalls _|_ MaryCalls | Burglary, Alarm, Earthquake)
(JohnCalls _|_ Burglary | MaryCalls, Alarm, Earthquake)
(MaryCalls _|_ Burglary, JohnCalls, Earthquake | Alarm)
(MaryCalls _|_ JohnCalls, Earthquake | Burglary, Alarm)
(MaryCalls _|_ Burglary, JohnCalls | Alarm, Earthquake)
(MaryCalls _|_ Burglary, Earthquake | JohnCalls, Alarm)
(MaryCalls _|_ JohnCalls | Burglary, Alarm, Earthquake)
(MaryCalls _|_ Earthquake | Burglary, JohnCalls, Alarm)
(MaryCalls _|_ Burglary | Alarm, JohnCalls, Earthquake)