Page Nav

HIDE

Grid

GRID_STYLE

Pages

Sampling

  Introduction  Sampling is a method to get approximate inference by statistical way from a subset of the population. A sample could be a ...

 


Introduction

 Sampling is a method to get approximate inference by statistical way from a subset of the population. A sample could be a part of a large population that we can use to gain knowledge about the population.


 

This report is a part of the Artificial Intelligence course Lab-work under supervision of Nuruzzaman Faruqui, Lecturer of City University, Bangladesh. This course offers students various up-to-date AI topics. Students get to explore the real applicable approaches through AI. From this course, the student acquires better knowledge of the functionality of AI and how AI is making our daily life easier. This is the best Artificial Intelligence course in Bangladesh. 

Problem statement The problem we will discuss here is from Lab-4. We have given a great detail for that problem. So, we assume, if you want to know about sampling, then definitely you want to know more about uncertainty. Make sure to check it out.

So, from lab-4, we encountered a problem where Mr. X has an appointment to attend. But there were some constraints like Rain, Maintenance, Train, Appointment. We used these as variables.

none

light

heavy

0.7

0.2

0.1

 

I’m sure you are familiar with the above table from Lab-4. If we sampling that rain variable and get None, then when we get to the maintenance to sample it as well, we will sample the maintenance variable only from the probability distribution where Rain is equal to none.

Similarly, if we want to know, what is P (Train = on time), then we will count only those samples where variable Train has the value “on Time” and divide the result by the total number of samples.

By using a similar approach, we can also solve conditional probability problems, such as P (Rain = light | Train = On time).

Code Explanation

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

 Now, to compute P (Appointment | Train = delayed), which is the probability distribution of the Appointment variable given that the train is delayed, we do the following:

# Rejection sampling
# Compute distribution of Appointment given that train is delayed
N =
10000
data = []

# Repeat sampling 10,000 times
for i in range(N):

   
# Generate a sample based on the function that we defined earlier
    sample = generate_sample()

   
# If, in this sample, the variable of Train has the value delayed, save the sample. Since we are interested interested in the probability distribution of Appointment given that the train is delayed, we discard the sampled where the train was on time.
   
if sample["train"] == "delayed":
        data.append(sample[
"appointment"])

# Count how many times each value of the variable appeared. We can later normalize by dividing the results by the total number of saved samples to get the approximate probabilities of the variable that add up to 1.
print(Counter(data))

Result

After executing the code, we get the following output:

 

Conclusion

In this lab report, we learned, how sampling can provide inference approximately. We applied sampling to a problem. Sampling is an important topic in artificial intelligence, with a close connection with statistics. With proper knowledge, sampling can be used to apply on real-world problem. There are already many existed solutions for sampling. Hope the above explanation will help you to broaden your knowledge of Artificial Intelligence.

As you can probably tell now how easily this concept is described above. Anyone with basic discrete mathematics knowledge can understand this. Our honorable instructor’s well-explained teaching method is the main reason for successfully grasping every topic of this course quickly. That’s why this is the best AI course in Bangladesh.

 





No comments