Hello Bayes

Startpage
Tutorial

This document instructs you how to create a small Visual Basic Project called "HelloBayes" which uses the Hugin API ActiveX Server. The program reads a belief network designed in the Hugin tool, allows the application-user to enter evidence, propagates it and displays the resulting probabilities.

A simple network

The simple network in Figure 1 displays a murder investigation: A person has been killed and Colombo has to identify the murderer. His only lead is a stain of the killers blood found at the scene of the crime. This biological stain has been used to extract a DNA-fingerprint of the killer. Colombo is certain that the culprit belongs to a particular group of people, e.g. the Danish population. To satisfy the publics demand of a scapegoat he lapses into the somewhat arbitrary strategy of picking a random individual from the group and comparing this "suspects" DNA profile with the one from the evidence. Depending on a match he wants to reason about the possibility that the suspect really is the murderer.
Figure 1: The Bayesian belief network modelling the above murder investigation. [Details]

Colombo is definitely a Bayesian belief network illiterate and knows equally little about using the Hugin tool. Forcing him to understand the network, enter evidence, propagate, and so on is not suitable. Hence your job as knowledge-engineer is to create an easy to use application tailormade for his needs and hiding all the complexity of Bayesian belief networks. The "HelloBayes" application created in the next section accomplishes this task.

The HelloBayes Visual Basic Project

In Microsoft Visual Basic (version 5.0 or higher) create a new project named "HelloBayes" and with type "Standard EXE". Start by adding the Hugin API ActiveX Server to your project. This is done as illustrated in
Figure 2, by selecting it in Project | References Dialog Box.
Figure 2:Selecting the Hugin API ActiveX Server in the References Dialog box.

Create a form and add GUI components similar to Figure 3. Two components deserve special attention.

DNAState
An array of option controls used to indicate the state of the DNA-match? node.
GuiltyProb
A label displaying the probability that Murderer? is in the state yes.
Figure 3: The Visual Basic Form used in the HelloBayes application.

After the above steps it is time to add code turning the form into a useful application. The code is divided into four different sections: the declaration section of the module, initial operations performed on loading the form, operations performed when closing the form and operations performed when one of the option buttons selects a DNA-match? state.

Declaration section of the module

A global variable d is declared which will refer to the murder network. To hold a network it is necessary to allocate a domain structure of type HAPI.Domain which keeps track of the nodes belonging to the network, the compilation status of the network etc. Furthermore, the section declares constants in order to be able to symbolically identify the states of the two nodes in the network.
Option Explicit
Dim d As HAPI.Domain
Private Const MurderYes = 0
Private Const MurderNo = 1
Private Const DNAMatch = 0
Private Const DNANoMatch = 1

On the Load of the Form

The Form_Load() event is triggered when the form is loaded. We use it to initialize the global variables and display the guilty probability in the initial no state of the DNA-match? node.
Private Sub Form_Load()
    ' A collection to hold the found parseErrors
     Dim parseErrors As Collection

    ' Load the domain from the murder.net file and compile it.
    Set d = HAPI.LoadDomainFromNet("D:\VB Projects\HelloBayes\murder.net", parseErrors, 10)
    d.Compile
    
     ' Initially the DNANoMatch state is selected - display the probability 
    Call DNAState_Click(DNANoMatch)
    Exit Sub
End Sub

First the domain variable is initialized by calling the API function LoadDomainFromNet with the path of the murder network and a collection object in case of parse errors as arguments. Depending on the location of your murder.net file you might have to use a different path. Hereafter the domain is compiled which triangulates the graph and constructs the junction tree. The domain is now ready for propagation.

Initially the DNANoMatch option button is selected. We call the sub procedure described in the next section to display the guilty probability.

When closing the Form

A Domain object holds a reference to all the Nodes contained in it, and these nodes again hold a reference to the Domain object. Therefore the garbage collector in Visual Basic cannot determine if a Domain objects has been released by an application. This is a domain should be explicitly deleted once you are done using it.
Private Sub Form_Terminate()
    ' We are done using the domain - explicitly delete it.
    d.Delete
End Sub

Clicking on one of the option buttons

The DNAState_Click event is triggered whenever the selected DNAState is changed.
Private Sub DNAState_Click(Index As Integer)
    Dim n As HAPI.Node
    
    ' Fetch the node with label "DNA-match?" and name "DNA" from the domain
    Set n = d.GetNodeByName("DNA")
    
    ' Instantiate the Node with state "Index" as evidence and sum propagate it
    n.SelectState (Index) 
    Call d.Propagate(hEquilibriumSum, hModeNormal)
    
    ' Fetch the "Murderer?" node and display the probability for MurderYes
    Set n = d.GetNodeByName("M")
    GuiltyProb.Caption = Format(n.Belief(MurderYes) * 100, "##0.00000000") & "%"
End Sub

To instantiate the DNA-match? node we first fetch it from the domain by calling GetNodeByName with the DNA-match? node's name ("DNA") as parameter. Hereafter we enter the state selected by the DNAState options as a finding and sum-propagate the information in the network. Finally the probability for the suspect to be guilty is displayed by calling GetBelief for the Murderer? node with MurderYes as the desired state.

You should now be able to to compile and run the application. The low probability in case of a DNA match indicates that such a case needs substantial strengthening before going to trial. Hopefully the probabilities will persuade Colombo to rely on more sophisticated investigation methods!

HelloBayes files

The following are the main Murder Trial form file and the Murder Trial net file.
murder.frm
murder.net
To get up and running very fast with this demo application, follow these instructions:
  1. Create a new VB project (Standard Exe).
  2. Add "Hugin API ActiveX Server" by selecting it in Project | References Dialog Box.
  3. Add a new existing form and select the murder.frm file in the hellobayes folder.
  4. Make this form the "Start Up Object" of the project (somewhere in Project Properties).

HUGIN Expert A/S, 2007 - comments to activex@hugin.com