Uncertainity - Implement a Bayesian Network and Calculate the probability for a observation.
Introduction:
Today we explain something about Bayesian Network. Bayesian Network is a data structure that represents the dependencies among random variables. Bayesian networks are a type of probabilistic graphical model that uses Bayesian inference for probability computations. Bayesian networks aim to model conditional dependence, and therefore causation, by representing conditional dependence by edges in a directed graph. Through these relationships, one can efficiently conduct inference on the random variables in the graph through the use of factors.
Bayesian Network is dependent on random variables. Suppose, The probability of today's weather clouds is 0.7 and the probability of rain is 0.3. And there another probability of train reached in time is 0.5 and the probability of train can't reached in time is 0.5. Now by using this probability we can get a final probability from computers by using Bayesian Network. You can say Prophecy.
How we can implement. First, we need to do insert all of the random variables and mentions their dependencies. Now create a bayesian network and add all of the states. After that add edges connecting with the nodes. Then need to computing joint probabilities.
This is an Artificial Intelligence course conducted in City Universit by Nuruzzaman Faruqui. This is the best course in Bangladesh. Because Sir explained all the necessary things to us well. The problem we are dealing with in the lab. First, sir gives us a very good idea of the problem by using many examples. After that sir explains about the solutions like how we can solve the problem, how they work. For that, we understand everything very easily.
Problem :
First, we have to know about the problem. The problem is :
In this picture, we can see that there are some states or node which name is rain, Maintenance, Train, Appointment. In those states we can see that every state has some random values. First, we need to implement this Bayesian Network. After that add all the states and connect the nodes. Now need to computing joint probabilities.
The python code to implement Bayesian Network
First, we have to import pomegranate. Pomegranate is a library includes for the bayesian network.
from pomegranate import *
# Rain node has no parent for that we use
DiscreteDistribution
rain = Node(DiscreteDistribution({
"none": 0.7,
"light": 0.2,
"heavy": 0.1
}),
name="rain")
# Maintenance node is conditional on rain for
that we use
Conditional ProbabilityTable.
maintenance = Node(ConditionalProbabilityTable([
["none", "yes", 0.4],
["none", "no", 0.6],
["light",
"yes", 0.2],
["light",
"no", 0.8],
["heavy",
"yes", 0.1],
["heavy",
"no", 0.9]
], [rain.distribution]), name="maintenance")
# Train node is conditional on rain and
maintenance
train = Node(ConditionalProbabilityTable([
["none", "yes", "on time", 0.8],
["none", "yes", "delayed",
0.2],
["none", "no", "on time",
0.9],
["none", "no", "delayed",
0.1],
["light",
"yes", "on time", 0.6],
["light",
"yes", "delayed", 0.4],
["light",
"no", "on time", 0.7],
["light",
"no", "delayed", 0.3],
["heavy",
"yes", "on time", 0.4],
["heavy",
"yes", "delayed", 0.6],
["heavy",
"no", "on time", 0.5],
["heavy",
"no", "delayed", 0.5],
],
[rain.distribution, maintenance.distribution]), name="train")
# Appointment node is conditional on train
appointment =
Node(ConditionalProbabilityTable([
["on
time", "attend",
0.9],
["on time",
"miss", 0.1],
["delayed",
"attend", 0.6],
["delayed",
"miss", 0.4]
], [train.distribution]), name="appointment")
# Now create a Bayesian Network and add states
model = BayesianNetwork()
model.add_states(rain, maintenance, train, appointment)
# Add all the edges to connecting nodes
model.add_edge(rain, maintenance)
model.add_edge(rain, train)
model.add_edge(maintenance, train)
model.add_edge(train, appointment)
# Finalize model
model.bake()
Now we need to calculate the probability for a given
observation .
from model import model
# Calculate the probability for a given observation
reds_probability = model.probability([["heavy", "no", "delayed", "attend"]])
print(reds_probability)
Result :
In the pseudo-code, we crate a Bayesian network and connect all the edges. If everything fine then we will get this type of result
No comments