Implement forward sampling for inference in python.
Your task for this project is to implement forward sampling and use it on an example network. The following lines are an example code representing a Bayesian Network that your implementation should be able to use:
Implement forward sampling for Bayesian networks specified using numpy like in the example above. The example specifies the conditional probability distributions (CPDs) in v0 – v4, and stores them in a list cpds, in the order that they should be processed during forward sampling. There are also variables that list all parents of all nodes (parents), and the cardinality of each variable (cards), which means how many different values a variable can take.
Take the following steps:
1. Implement a function that returns a single sample, given a CPD and possibly some
evidence. Follow the template in Listing 2 below. An example function call will then
look like this:
In [22]: sample one(v2, [1, 0], [2, 2])
Out[22]: 1
In this example, we are drawing a sample from the CPD in v2, with evidence v0 = 1,
v1 = 0. The cardinality of v0 and v1 is both 2, and the returned sample is 1.
2. Implement a function that forward-samples a whole Bayesian Network. Follow the template in Listing 3 below. An example function call will then look like this:
In [44]: forward sample (cpds, parents, cards)
Out[44]: [0, 1, 2, 1, 0]
In this example, we get a list of values, with one value for each node in the Bayesian
Network.
3. Implement a function that draws N samples from the Bayesian Network, and returns
them in a list. Follow the example in Listing 4. An example function call to draw 3
samples will look like this:
In [47]: sample(3,cpds,parents,cards)
Out[47]: [[1, 0, 2, 0, 0], [0, 0, 2, 0, 0], [0, 0, 1, 0, 0]]
4. Implement rejection sampling using your methods from above, so that you can compute (estimate) p(v4 | v3 = 1). Use your approach multiple times, with increasing number of samples (start with N=20, increase in steps of 20 up to N=1000). Plot a graph from your results, with N on the x-axis, and the estimated probability on the y. Describe
your results in 2-3 sentences.
What to submit
(1) Create a single python file that contains all your code. Include your
name /student id in your file.
(2) Create a PDF document that, for each task, contains results of an example run of your code. For task 4, include the plot and short description. The PDF file should contain your name and student id at the beginning. If you could not solve a task, you can also use the file to describe what you wanted to do. zip both files together (use zip, or tar/gzip on linux, do not use rar), and submit via vUWS.
Codersarts is a top-rated website for students which is looking for online Programming Assignment Help, Homework Help, Coursework Help in Machine Learning. Hire us and Get your projects done by a Machine Learning expert.
Contact Codersarts for any such project/assignment help
Comments