Uncertainty - Sampling
Introduction:
Sample Means taking the sample the sample of something and then inferring how the actual thing is. Sampling is one technique of approximate inference. In Sampling, each variable is sampled for a value according to its probability distribution. Suppose, there are some people coming to give an interview for an IT company. And you are taking this interview. They have different different characteristics suppose the characteristics are sample. Now there are many people which age is 24-30. Some of them known as PHP, java-script, python. Some of them known as C, C++, Java. Some of them have experience of more than one year and someone have less then one year of experience. Now you give a sample like this you need this kind of person who is under 25 years old, more than one year experience, they must known python, javascript, PHP, With this sample we can easily reject this persons who have not this kind of qualities. So, sampling is something like that.
Now I'm going to talking about how you can solve the problem. First, we need a Bayesian network, After that, we need to import pomegranate and counter. Now we need to generate_sample functions in this function, In the function, we start a loop over all states, Assuming topological order. After that, if we have a non-root node. sample conditional on parents then "sample[state.name] = state.distribution.sample(parent_values=parents)" . Otherwise "sample[state.name] = state.distribution.sample()". Now return the sample. After that need to reject the sample. Now compute the sample.
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:
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, we need to generate a sample which we will show you in the pseudocode. Now It's time to reject the sample. Compute distribution of appointment if the train is delayed. we try it 1000 times and we want to count how many times it attend and how many times it misses.
The python code to implement sampling
This is the model file. We need this model file to conduct sample
from pomegranate import *
# Rain node has no parent
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 edges 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 have to import pomegranate, counter, and the model file where we create a Bayesian Network.
import pomegranate
from collections import Counter
from model import model
def generate_sample():
# Mapping of random variable name to sample generated
sample = {}
# Mapping of distribution to sample generated
parents = {}
# Loop over all states, assuming topological order
for state in model.states:
# If we have a non-root node, sample conditional on parents
if isinstance(state.distribution, pomegranate.ConditionalProbabilityTable):
sample[state.name] = state.distribution.sample(parent_values=parents)
# Otherwise, just sample from the distribution alone
else:
sample[state.name] = state.distribution.sample()
# Keep track of the sampled value in the parents mapping
parents[state.distribution] = sample[state.name]
# Return generated sample
return sample
# Rejection sampling
# Compute distribution of Appointment given that train is delayed
N = 10000
data = []
for i in range(N):
sample = generate_sample()
if sample["train"] == "delayed":
data.append(sample["appointment"])
print(Counter(data))
Result :
If everything is fine then we will get this type of result.
No comments