C Sharp Tutorial 2: Performing Inference with a Bayesian Network
In this tutorial we will get some results from the network we created in Tutorial 1. What is the probability of the expert forecasting a moderate success? What is the probability of failure if the forecast is good? What is the probability of success if the forecast is poor? We will show how to compute these values and how to read the results.
Instead of creating the network again from scratch, we load the file we stored at the end of the tutorial 1.
Network net = new Network();
net.ReadFile("tutorial_a.xdsl");
Now let us try to answer the first question, i.e. what is the probability of the expert forecasting a moderate success? In other words, how often does the expert forecast a moderate success for these type of companies? We can answer this question by updating the network. Default algorithm will be used (Lauritzen).
net.UpdateBeliefs();
We will get the result by asking for the probability of the outcome Moderate in the node Forecast. We also need to know what is the index of the state Moderate in this node, i.e. we need the position of the state in the array of states of the node. We do this by looking for the name Moderate in the list of names returned by the GetOutcomeIds() method.
String[] aForecastOutcomeIds = net.GetOutcomeIds("Forecast");
int outcomeIndex;
for (outcomeIndex = 0; outcomeIndex < aForecastOutcomeIds.Length; outcomeIndex++)
if ("Moderate".Equals(aForecastOutcomeIds[outcomeIndex]))
break;
Next, we retrieve the values of the interesting node (i.e. Forecast), select the one corresponding with the found outcome index, and print it.
double[] aValues = net.GetNodeValue("Forecast");
double P_ForecastIsModerate = aValues[outcomeIndex];
Console.WriteLine("P(\"Forecast\" = Moderate) = " + P_ForecastIsModerate);
Let us answer the second question, i.e. what is the probability of failure if the forecast is good? The question also gives us some information: the forecast is good. This means that before doing any calculations, we have to enter this evidence into the network.
net.SetEvidence("Forecast", "Good");
Note that evidence deals with the value of the node. The definition remains unchanged. Note that we can use the Id of an outcome index would be Ok too). Now we just need to update the network again.
net.UpdateBeliefs();
Next, we get the required information from the node Success, repeating the steps already described above.
// Getting the index of the "Failure" outcome:
String[] aSuccessOutcomeIds = net.GetOutcomeIds("Success");
for (outcomeIndex = 0; outcomeIndex < aSuccessOutcomeIds.Length; outcomeIndex++)
if ("Failure".Equals(aSuccessOutcomeIds[outcomeIndex]))
break;
// Getting the value of the probability:
aValues = net.GetNodeValue("Success");
double P_SuccIsFailGivenForeIsGood = aValues[outcomeIndex];
Console.WriteLine("P(\"Success\" = Failure | \"Forecast\" = Good) = " + P_SuccIsFailGivenForeIsGood);
Now the last question, i.e. what is the probability of success if the forecast is poor? As before, we can extract a piece of information from the question: the forecast is poor. So just introduce this new evidence in the network and update it again.
net.ClearEvidence("Forecast");
net.SetEvidence("Forecast", "Good");
net.UpdateBeliefs();
Note that we call ClearEvidence() method before setting the evidence. In this particular case it is not necessary, as we introduce a new evidence for the same node. However, if we chose a different node we cannot forget about clearing the evidence.
Now we are ready to get the interesting value, again, repeating the steps described above.
// Getting the index of the "Failure" outcome:
aSuccessOutcomeIds = net.GetOutcomeIds("Success");
for (outcomeIndex = 0; outcomeIndex < aSuccessOutcomeIds.Length; outcomeIndex++)
if ("Failure".Equals(aSuccessOutcomeIds[outcomeIndex]))
break;
// Getting the value of the probability:
aValues = net.GetNodeValue("Success");
double P_SuccIsSuccGivenForeIsPoor = aValues[outcomeIndex];
Console.WriteLine("P(\"Success\" = Success | \"Forecast\" = Poor) = " + P_SuccIsSuccGivenForeIsPoor);
We have answered all three questions.