100% found this document useful (1 vote)
747 views5 pages

04 - Burglary Alarm Example Using Bayesian Network

1. The document defines a Bayesian network model to analyze the probabilities in a burglary alarm example. It imports libraries and defines the network structure and conditional probability tables. 2. It checks the model, queries the network to obtain conditional probabilities like the probability of the alarm ringing under different evidence scenarios such as burglary and earthquakes occurring or not. 3. The model calculates the conditional probabilities for different scenarios involving burglary, earthquakes and whether John and Mary call Harry under those situations.

Uploaded by

John Wick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
747 views5 pages

04 - Burglary Alarm Example Using Bayesian Network

1. The document defines a Bayesian network model to analyze the probabilities in a burglary alarm example. It imports libraries and defines the network structure and conditional probability tables. 2. It checks the model, queries the network to obtain conditional probabilities like the probability of the alarm ringing under different evidence scenarios such as burglary and earthquakes occurring or not. 3. The model calculates the conditional probabilities for different scenarios involving burglary, earthquakes and whether John and Mary call Harry under those situations.

Uploaded by

John Wick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical - 4

AIM :- Burglary Alarm Example Using Bayesian


Network

Importing Library

In [29]:

1 from pgmpy.models import BayesianModel


2 from pgmpy.inference import VariableElimination
3 import bnlearn

Defining network structure

In [30]:

1 from pgmpy.factors.discrete import TabularCPD


2
3 alarm_model = BayesianModel([('Burglary', 'Alarm'), ('Earthquake', 'Alarm'),
4 ('Alarm', 'JohnCalls'), ('Alarm', 'MaryCalls')])
5
6 # Defining the parameters using CPT
7
8 cpd_burglary = TabularCPD(variable='Burglary',
9 variable_card=2,
10 values=[[.999], [0.001]])
11 cpd_earthquake = TabularCPD(variable='Earthquake',
12 variable_card=2,
13 values=[[0.998], [0.002]])
14 cpd_alarm = TabularCPD(variable='Alarm',
15 variable_card=2,
16 values=[[0.999, 0.71, 0.06, 0.05],
17 [0.001, 0.29, 0.94, 0.95]],
18 evidence=['Burglary', 'Earthquake'],
19 evidence_card=[2, 2])
20 cpd_johncalls = TabularCPD(variable='JohnCalls',
21 variable_card=2,
22 values=[[0.95, 0.1], [0.05, 0.9]],
23 evidence=['Alarm'],
24 evidence_card=[2])
25 cpd_marycalls = TabularCPD(variable='MaryCalls',
26 variable_card=2,
27 values=[[0.1, 0.7], [0.9, 0.3]],
28 evidence=['Alarm'],
29 evidence_card=[2])
30
31 # Associating the parameters with the model structure
32 alarm_model.add_cpds(cpd_burglary, cpd_earthquake, cpd_alarm, cpd_johncalls,
33 cpd_marycalls)


Checking if the cpds are valid for the model

In [31]:

1 alarm_model.check_model()

Out[31]:

True

Viewing nodes of the model

In [32]:

1 alarm_model.nodes()

Out[32]:

NodeView(('Burglary', 'Alarm', 'Earthquake', 'JohnCalls', 'MaryCalls'))

Viewing edges of the model

In [33]:

1 alarm_model.edges()

Out[33]:

OutEdgeView([('Burglary', 'Alarm'), ('Alarm', 'JohnCalls'), ('Alarm', 'MaryC


alls'), ('Earthquake', 'Alarm')])

Checking independcies of a node

In [34]:

1 alarm_model.local_independencies('Burglary')

Out[34]:

(Burglary ⟂ Earthquake)

Listing all Independencies


In [35]:

1 alarm_model.get_independencies()

Out[35]:

(MaryCalls ⟂ Earthquake, JohnCalls, Burglary | Alarm)

(MaryCalls ⟂ JohnCalls, Burglary | Earthquake, Alarm)

(MaryCalls ⟂ Earthquake, Burglary | JohnCalls, Alarm)

(MaryCalls ⟂ Earthquake, JohnCalls | Burglary, Alarm)

(MaryCalls ⟂ Burglary | Earthquake, JohnCalls, Alarm)

(MaryCalls ⟂ JohnCalls | Earthquake, Burglary, Alarm)

(MaryCalls ⟂ Earthquake | Burglary, JohnCalls, Alarm)

(Earthquake ⟂ Burglary)

(Earthquake ⟂ MaryCalls, JohnCalls | Alarm)

(Earthquake ⟂ JohnCalls | MaryCalls, Alarm)

(Earthquake ⟂ MaryCalls | JohnCalls, Alarm)

(Earthquake ⟂ MaryCalls, JohnCalls | Burglary, Alarm)

(Earthquake ⟂ JohnCalls | MaryCalls, Burglary, Alarm)

(Earthquake ⟂ MaryCalls | Burglary, JohnCalls, Alarm)

(JohnCalls ⟂ Earthquake, MaryCalls, Burglary | Alarm)

(JohnCalls ⟂ MaryCalls, Burglary | Earthquake, Alarm)

(JohnCalls ⟂ Earthquake, Burglary | MaryCalls, Alarm)

(JohnCalls ⟂ Earthquake, MaryCalls | Burglary, Alarm)

In [36]:

1 infer = VariableElimination(alarm_model)

Probability Distribution that are Explicitly Spelled out in our Graphs

In [37]:

1 alarm_prob = infer.query(['Alarm'])
2 print(alarm_prob)

0%| | 0/2 [00:00<?, ?it/s]

0%| | 0/2 [00:00<?, ?it/s]

+----------+--------------+

| Alarm | phi(Alarm) |

+==========+==============+

| Alarm(0) | 0.9975 |

+----------+--------------+

| Alarm(1) | 0.0025 |

+----------+--------------+

Conditional Probability of Ringing of Alarm When Burglary and Earthquake


is not occuring
In [38]:

1 print(infer.query(['Alarm'], evidence={'Burglary': 0, 'Earthquake': 0}))

0it [00:00, ?it/s]

0it [00:00, ?it/s]

+----------+--------------+

| Alarm | phi(Alarm) |

+==========+==============+

| Alarm(0) | 0.9990 |

+----------+--------------+

| Alarm(1) | 0.0010 |

+----------+--------------+

Conditional Probability Of Ringing Of Alarm When There Is No Burglary And


Earthquake But Still John And Merry Call Harry

In [39]:

1 print(
2 infer.query(['Alarm'],
3 evidence={
4 'Burglary': 0,
5 'Earthquake': 0,
6 'JohnCalls': 1,
7 'MaryCalls': 1
8 }))

0it [00:00, ?it/s]

0it [00:00, ?it/s]

+----------+--------------+

| Alarm | phi(Alarm) |

+==========+==============+

| Alarm(0) | 0.9940 |

+----------+--------------+

| Alarm(1) | 0.0060 |

+----------+--------------+

Conditional Probability of Ringing of Alarm When There is Burglary


Happens and Only Marry Calls Harry
In [40]:

1 print(
2 infer.query(['Alarm'],
3 evidence={
4 'Burglary': 1,
5 'Earthquake': 0,
6 'JohnCalls': 0,
7 'MaryCalls': 1
8 }))

0it [00:00, ?it/s]

0it [00:00, ?it/s]

+----------+--------------+

| Alarm | phi(Alarm) |

+==========+==============+

| Alarm(0) | 0.6453 |

+----------+--------------+

| Alarm(1) | 0.3547 |

+----------+--------------+

Conditional Probability of Ringing of Alarm When There is Burglary,


Earthquake and Both Call Harry

In [41]:

1 print(
2 infer.query(['Alarm'],
3 evidence={
4 'Burglary': 1,
5 'Earthquake': 1,
6 'JohnCalls': 1,
7 'MaryCalls': 1
8 }))

0it [00:00, ?it/s]

0it [00:00, ?it/s]

+----------+--------------+

| Alarm | phi(Alarm) |

+==========+==============+

| Alarm(0) | 0.0087 |

+----------+--------------+

| Alarm(1) | 0.9913 |

+----------+--------------+

You might also like