Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

In this problem, you will write a program that implements two algorithms for performing exact inference given a Bayesian network, namely, enumeration and variable elimination. Your program should accept exactly three command-line arguments:

BayesNet

The first argument is a file specifying a Bayesian network (see the section "Specifying a Bayesian Network" below for the file format for a Bayes net). The second argument is either elim or enum, which indicates whether enumeration or variable elimination should be used as the underlying inference mechanism. The third argument is a query. The query should be formu- lated as a probability, such as "P(C|A=f,E=t)". This query would return the two probabilities for C given that A is false, and E is true. Do not include spaces in the query, and you should put the query in quotes. Given these three inputs, one of the things your program should print to stdout the result of the inference. Specifically, the result should be output by printing the conditional probabilities of the query variable given the observed variables, such as:

P(C = f | A = f, E = t) = #

P(C = t | A = f, E = t) = #

for both values of the query variable and the values for the observed variables.

In addition to outputting the result of the inference, your program also needs to output other information it computes during the inference process so that we can track the correctness of your implementation. The sections "Implementing the Variable Enumeration Algorithm" and "Implementing the Variable Elimination Algorithm" provide details on what to print during inference.

To simplify your implementation, your program only needs to handle queries with one query variable. Nevertheless, your program should work even if no observed variables are given. In other words, your program should accept a query containing an unconditional probability of a variable.

Specifying a Bayesian Network

Let us try to understand the file format for a Bayes net via the "alarm" example we saw in class. Recall that in the "alarm" Bayes net, there are five nodes, B, E, A, J, and M, each of which is associated with a conditional probability table. The file for this Bayes net would look like this:

P(B) = .001

P(E) = .002

B E | A

----|-----

t t | .95

t f | .94

f t | .29

f f | .001

A | J

--|-----

t | 0.90

f | 0.05

A | M

--|-----

t | 0.7

f | 0.01

Specifically, each node is sectioned off from the others using double newlines, and there are two types of nodes: nodes without parents and nodes with parents. The nodes without parents are just represented as P(X) = #. The nodes with parents are represented as a table. The first line for the node is a table header which lists the parent nodes separated by spaces, followed by a pipe symbol and finally the name of that node. The next line can be skipped. There are then 2(#parents) lines representing table entries. A table entry is a list of p 't' or 'f' symbols (where p = #parents), separated by spaces, followed by a pipe symbol and finally a decimal number between 0 and 1. The entry can be interpreted as the probability of the node being true given the truth values for its parents. Truth value i corresponds to parent i from the table header.

If you understand BNF notation, below is the specification of a Bayes net in BNF (with extensions):

::= ( )*

::= |

::= "P(" ") = "

::=

::= (" " )* " | "

::= "-"+ "|" "-"+

::= ( )+

::= (" " )* " | "

::= "t" | "f"

::= "0" | "0"? "." +

::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"

::=

"A"|"B"|"C"|"D"|"E"|"F"|"G"|"H"|"I"|"J"|"K"|"L"|"M"|"N"

| "O"|"P"|"Q"|"R"|"S"|"T"|"U"|"V"|"W"|"X"|"Y"|"Z"

::= "\n"

Implementing the Variable Enumeration Algorithm

The variables should be visited in this order:

1. Parents should be visited before their children.

2. To choose between two nodes whose parents have all been visited, choose alphabetically.

At the end of your implementation of the ENUMERATE-ALL function, you should print the following:

  • the variables passed in to ENUMERATE-ALL;
  • the assignment passed in to ENUMERATE-ALL;
  • the return value for ENUMERATE-ALL.

 

Below is the output you should get when running the program on the "alarm" example using variable enumeration and the query "P(B|J=t,M=t)".

M | A=t B=f E=t J=t M=t = 0.70000000

J M | A=t B=f E=t J=t M=t = 0.63000000

M | A=f B=f E=t J=t M=t = 0.01000000

J M | A=f B=f E=t J=t M=t = 0.00050000

A J M | B=f E=t J=t M=t = 0.18305500

M | A=t B=f E=f J=t M=t = 0.70000000

J M | A=t B=f E=f J=t M=t = 0.63000000

M | A=f B=f E=f J=t M=t = 0.01000000

J M | A=f B=f E=f J=t M=t = 0.00050000

A J M | B=f E=f J=t M=t = 0.00112950

E A J M | B=f J=t M=t = 0.00149335

B E A J M | B=f J=t M=t = 0.00149186

M | A=t B=t E=t J=t M=t = 0.70000000

J M | A=t B=t E=t J=t M=t = 0.63000000

M | A=f B=t E=t J=t M=t = 0.01000000

J M | A=f B=t E=t J=t M=t = 0.00050000

A J M | B=t E=t J=t M=t = 0.59852500

M | A=t B=t E=f J=t M=t = 0.70000000

J M | A=t B=t E=f J=t M=t = 0.63000000

M | A=f B=t E=f J=t M=t = 0.01000000

J M | A=f B=t E=f J=t M=t = 0.00050000

A J M | B=t E=f J=t M=t = 0.59223000

E A J M | B=t J=t M=t = 0.59224259

B E A J M | B=t J=t M=t = 0.00059224

RESULT:

P(B = f | J = t, M = t) = 0.7158281646356071

P(B = t | J = t, M = t) = 0.2841718353643929

Implementing the Variable Elimination Algorithm

Variables are visited according to a greedy heuristic: eliminate whichever variable minimizes the size of the next factor to be constructed. Ties are broken alphabetically. Importantly, a variable cannot be eliminated if all the factors that depend on that variable have not been created yet.

Print out when a variable is being eliminated. At the end of the main loop for ELIMINATION-ASK, print the factors that are active. These can be printed one line per element with the corresponding assignment and element value.

Below is the output you should get when running the program on the "alarm" example using variable elimination and the query "P (B|J=t,M=t)".

----- Variable: J -----

Factors:

B=f E=f: 0.0011295

B=t E=f: 0.5922299999999999

B=f E=t: 0.183055

B=t E=t: 0.598525

B=f E=f: 0.0011295

B=t E=f: 0.5922299999999999

B=f E=t: 0.183055

B=t E=t: 0.598525

A=f: 0.05

A=t: 0.9

----- Variable: M -----

Factors:

A=f: 0.05

A=t: 0.9

A=f: 0.01

A=t: 0.7

----- Variable: A -----

Factors:

----- Variable: B -----

Factors:

B=f: 0.999

B=t: 0.0010

----- Variable: E -----

Factors:

B=f: 0.999

B=t: 0.0010

B=f: 0.0014933510000000002

B=t: 0.5922425899999999

RESULT:

P(B = f | J = t, M = t) = 0.7158281646356071

P(B = t | J = t, M = t) = 0.2841718353643929

 

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9526163

Have any Question?


Related Questions in C/C++

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As