Node value & evidence

<< Click to Display Table of Contents >>

Navigation:  Using SMILE > Anatomy of a node >

Node value & evidence

The value of a node contains values (typically, marginal probability distribution or expected utilities) calculated for the node by the inference algorithm. Unlike the definition, the value is not written as part of the network by DSL_network::WriteFile and WriteString. Like the definition, the value object is managed by the network. The actual value object, which you can access through DSL_node::Val method, is an instance of the class derived from DSL_nodeVal. SMILE chooses the class appropriate for the node type during node creation.

In addition to the numeric output of the inference algorithm, the node value object may contain the evidence for the node, which is (along with the node definition) part of the input to the inference algorithm. To set and remove the evidence, use DSL_nodeVal::SetEvidence and ClearEvidence methods. As with the definition part of node, most of the time there is no need to cast the pointer returned by DSL_node::Val to a specific class, because the base class provides the set of general purpose virtual functions overridden by derived value classes.

int evidenceNodeHandle = …

DSL_nodeVal *evVal = net.GetNode(evidenceNodeHandle)->Val();

evVal.SetEvidence(1); 1 is the 0-based outcome index

net.UpdateBeliefs();

int beliefNodeHandle = …;

DSL_nodeVal *beVal = net.GetNode(beliefNodeHandle)->Val();

if (beVal->IsValueValid())

{

 const DSL_Dmatrix *m = beVal->GetMatrix();

 // use the matrix

}

Note that before accessing the actual numeric value of the node with GetMatrix, we need to check if the value is valid by calling IsValueValid. The value will not be valid, for example, if inference algorithm was not called yet, or some definition or evidence has changed after the last inference call.

It is also possible to specify virtual evidence using DSL_nodeVal::SetVirtualEvidence method. Virtual evidence allows for entering uncertain observation (in form of probability distribution over the possible states of the observation) directly into a normally unobservable variable. SetVirtualEvidence requires an array, a vector, or an initializer list with size equal to the number of node outcomes. The following snippet shows how to set virtual evidence for a node with three outcomes:

int evidenceNodeHandle = …

DSL_nodeVal *evVal = net.GetNode(evidenceNodeHandle)->Val();

evVal->SetVirtualEvidence({ 0.2, 0.7, 0.1});

Similarly to the node definition class hierarchy, some functionality (like equation node values) is available only when the DSL_nodeVal pointer is cast to an appropriate type. The DSL_node::Val<T> template member function performs the inline static_cast. The example below assumes that eqNodeHandle is the handle to the DSL_EQUATION node, which uses DSL_equationEvaluation class derived from DSL_nodeVal to represent its value:

DSL_node *node = net.GetNode(eqNodeHandle);

auto veq = node->Val<DSL_equationEvaluation>();

veq->GetHistogram(-1, 1, 100, histogram);