Tutorial 3: Exploring the contents of a model

<< Click to Display Table of Contents >>

Navigation:  Tutorials >

Tutorial 3: Exploring the contents of a model

This tutorial will not perform any calculations. Instead, we will display the information about the network structure (nodes and arcs) and parameters (in this case, conditional probability tables).

We load the network created by Tutorial1 and for each node invoke the locally defined helper method printNodeInfo. This is where real work is done. The first displayed node attributes are identifier and name.

Java:

System.out.printf("Node id/name: %s/%s\n", 

    net.getNodeId(nodeHandle), 

    net.getNodeName(nodeHandle));

Python:

 print("Node id/name: " + net.get_node_id(node_handle) + "/" +

    net.get_node_name(node_handle))

C#:

Console.WriteLine("Node id/name: {0}/{1}",

    net.GetNodeId(nodeHandle),

    net.GetNodeName(nodeHandle));

The identfiers of node's outcomes are next.

Java:

System.out.print("  Outcomes:");

for (String outcomeId: net.getOutcomeIds(nodeHandle)) {

    System.out.print(" " + outcomeId);

}

Python:

print("  Outcomes: " + " ".join(net.get_outcome_ids(node_handle)))

C#:

foreach (String outcomeId in net.GetOutcomeIds(nodeHandle))

{

    Console.Write(" " + outcomeId);

}

The parents of the node follow.

Java:

String[] parentIds = net.getParentIds(nodeHandle);

if (parentIds.length > 0) {

    System.out.print("  Parents:");

    for (String parentId: parentIds) {

        System.out.print(" " + parentId);

    }

    System.out.println();

}

Python:

parent_ids = net.get_parent_ids(node_handle)

if len(parent_ids) > 0:

    print("  Parents: " + " ".join(parent_ids))

C#:

String[] parentIds = net.GetParentIds(nodeHandle);

if (parentIds.Length > 0)

{

    Console.Write("  Parents:");

    foreach (String parentId in parentIds)

    {

        Console.Write(" " + parentId);

    }

    Console.WriteLine();

}

We proceed to display information about node's children. The code fragment is virtually identical to the iteration over parents. Note that while tutorial uses Network.getParentIds and getChildIds to obtain identifiers of related nodes, we could alternatively use the Network.getParents and getChildren, which return the node handles (integer numbers) instead of identifiers (strings).

Finally, the node probabilities are displayed by the printCptMatrix helper. We retrieve the single-dimensional array containing the probabilities with Network.getNodeDefinition and iterate over its elements, translating each index into a multi-dimensional coordinates of the elements within node's CPT. This conversion is performed in the helper method indexToCoords, which requires the information about the outcome counts of the node and its parents in its dimSizes input parameter. The first part of printCptMatrix obtains the CPT and initializes dimSizes

Java:

double[] cpt = net.getNodeDefinition(nodeHandle);

int[] parents = net.getParents(nodeHandle);

int dimCount = 1 + parents.length;  

int[] dimSizes = new int[dimCount];

for (int i = 0; i < dimCount - 1; i ++) {

    dimSizes[i] = net.getOutcomeCount(parents[i]);

}

dimSizes[dimSizes.length - 1] = net.getOutcomeCount(nodeHandle);

Python:

cpt = net.get_node_definition(node_handle)

parents = net.get_parents(node_handle)

dim_count = 1 + len(parents)

dim_sizes = [0] * dim_count

for i in range(0, dim_count - 1):

    dim_sizes[i] = net.get_outcome_count(parents[i])

dim_sizes[len(dim_sizes) - 1] = net.get_outcome_count(node_handle)

C#:

double[] cpt = net.GetNodeDefinition(nodeHandle);

int[] parents = net.GetParents(nodeHandle);

int dimCount = 1 + parents.Length;

int[] dimSizes = new int[dimCount];

for (int i = 0; i < dimCount - 1; i++)

{

    dimSizes[i] = net.GetOutcomeCount(parents[i]);

}

dimSizes[dimSizes.Length - 1] = net.GetOutcomeCount(nodeHandle);

The main loop in the printCptMatrix uses performs the iteration over the elements of the CPT. The coords integer array is filled by indexToCoords and used to print the textual information about the outcome of the node and parents for each element. Note that the node outcome is the rightmost entry in the coords. The parents’ outcome indexes start from the left at index 0 in the coords. Part of the Tutorial3 output for the node Forecast is show below. All lines starting with “P”( were printed by printCptMatrix.

Node: Expert forecast

 Outcomes: Good Moderate Poor

 Parents: Success Economy

 Definition type: CPT

   P(Good | Success=Success,Economy=Up)=0.7

   P(Moderate | Success=Success,Economy=Up)=0.29

   P(Poor | Success=Success,Economy=Up)=0.01

   P(Good | Success=Success,Economy=Flat)=0.65

   P(Moderate | Success=Success,Economy=Flat)=0.3

   P(Poor | Success=Success,Economy=Flat)=0.05

   P(Good | Success=Success,Economy=Down)=0.6

   P(Moderate | Success=Success,Economy=Down)=0.3

   P(Poor | Success=Success,Economy=Down)=0.1

   P(Good | Success=Failure,Economy=Up)=0.15

   P(Moderate | Success=Failure,Economy=Up)=0.3

   P(Poor | Success=Failure,Economy=Up)=0.55

   P(Good | Success=Failure,Economy=Flat)=0.1

   P(Moderate | Success=Failure,Economy=Flat)=0.3

   P(Poor | Success=Failure,Economy=Flat)=0.6

   P(Good | Success=Failure,Economy=Down)=0.05

   P(Moderate | Success=Failure,Economy=Down)=0.25

   P(Poor | Success=Failure,Economy=Down)=0.7