Bayesian Networks

 

Expt No: 4                                           Bayesian Networks

Date:

 

Aim: To write a program to demonstrate Bayesian Network

 

Program

 

from pgmpy.models import BayesianNetwork

from pgmpy.factors.discrete import TabularCPD

from pgmpy.inference import VariableElimination

import networkx as nx

import matplotlib.pyplot as plt

 

# Define the structure of the Bayesian Network

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

 

# Define Conditional Probability Distributions (CPDs)

cpd_burglary = TabularCPD(variable='Burglary', variable_card=2, values=[[0.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.99, 0.3], [0.01, 0.7]], evidence=['Alarm'], evidence_card=[2])

 

# Add CPDs to the model

model.add_cpds(cpd_burglary, cpd_earthquake, cpd_alarm, cpd_johncalls, cpd_marycalls)

 

# Check if the model is correctly defined

model.check_model()

 

# Perform some inference queries

infer = VariableElimination(model)

 


 

# Query: P(Alarm | Burglary=True, Earthquake=False)

query_result_alarm = infer.query(variables=['Alarm'], evidence={'Burglary': True, 'Earthquake': False})

 

# Query: P(JohnCalls | Burglary=True, Earthquake=False)

query_result_johncalls = infer.query(variables=['JohnCalls'], evidence={'Burglary': True, 'Earthquake': False})

 

# Query: P(MaryCalls | Burglary=True, Earthquake=False)

query_result_marycalls = infer.query(variables=['MaryCalls'], evidence={'Burglary': True, 'Earthquake': False})

 

# Print the CPDs

print("\nConditional Probability Distributions (CPDs):")

for cpd in model.get_cpds():

    print(cpd.variable)

    print(cpd.values)

    print()

 

# Print the query results

print("\nInference Results:")

print("P(Alarm | Burglary=True, Earthquake=False):")

print(query_result_alarm.values)

print("\nP(JohnCalls | Burglary=True, Earthquake=False):")

print(query_result_johncalls.values)

print("\nP(MaryCalls | Burglary=True, Earthquake=False):")

print(query_result_marycalls.values)

 

# Create a directed graph

graph = nx.DiGraph(model.edges())

 

# Plot the graph

plt.figure(figsize=(10, 6))

pos = nx.spring_layout(graph)  # Positions for all nodes

nx.draw(graph, pos, with_labels=False, node_size=2000, node_color="skyblue", font_size=16, font_weight="bold", arrowsize=20)

labels = {node: node for node in model.nodes()}  # Node labels

nx.draw_networkx_labels(graph, pos, labels, font_color="black")

plt.title("Bayesian Network")

plt.show()





Result: Thus the program to demonstrate Bayesian Network was written and executed.

 

Sample Output

Conditional Probability Distributions (CPDs):

Burglary

[0.999 0.001]

 

Earthquake

[0.998 0.002]

 

Alarm

[[[0.999 0.71 ]

  [0.06  0.05 ]]

 

 [[0.001 0.29 ]

  [0.94  0.95 ]]]

 

JohnCalls

[[0.95 0.1 ]

 [0.05 0.9 ]]

 

MaryCalls

[[0.99 0.3 ]

 [0.01 0.7 ]]

 

 

Inference Results:

P(Alarm | Burglary=True, Earthquake=False):

[0.06 0.94]

 

P(JohnCalls | Burglary=True, Earthquake=False):

[0.151 0.849]

 

P(MaryCalls | Burglary=True, Earthquake=False):

[0.3414 0.6586]

 

 



No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu