Function Nodes Tutorial
In this tutorial we will construct the example network Functions, included with Bayes Server, from scratch. Although not required for this tutorial, for reference the Functions network can be opened from the Start Page or from File/Open.
New Network
- From the File main tab, click New to create a new empty network.
Add discrete node D
From the Build main tab, click Node, then Discrete.
Name the node D.
Rename the 2 discrete states D1 and D2.
Click OK.
Remember to save your network periodically
Add continuous node C
From the Build main tab, click Node then Continuous.
Name the node C.
Click OK.
Add function nodes
From the Build main tab, click Node, then Function.
Name the node F1.
Click OK.
Repeat the steps above to add 2 further function nodes, naming them F2 and F3.
Layout nodes
- Layout the nodes as in the image below.
Add link D -> C
From the Build main tab, click Link, then Add Link(s).
Set From to D.
Set To to C.
Click OK.
Define P(D)
Select node D.
From the Build main tab, click Distributions, then Edit distribution(s).
Set
P(D1) = 0.6961106280502056
, andP(D2) = 0.3038893719497943
.Click OK.
Define P(C | D)
- From the Build main tab, click Distributions, then Edit distribution(s).
P(C | D) consists of two Gaussian distributions, one for D1 and another for D2.
- Click on the cell containing
1
under D=D1 to editP(C|D=D1)
. Set the mean to100
and the variance to20
.
- Click on the cell containing
1
under D=D2 to editP(C|D=D2)
. Set the mean to-50
and the variance to45
.
- Click Ok.
Expression Aliases
Although not required, we can simplify expressions by giving each variable that is used in an expression, an alias.
From the Build main tab, click Expression then Edit variable aliases.
Set the alias for C to c.
Set the alias for D to d.
Set the alias for F1 to f1.
Define function F1
Click on the node F1.
From the Build main tab, click Expression then Function Node expression.
Set the Return Type to Double.
Set the Expression Text to the following:
var d1 = d.States.Get("D1", true);
var probD1 = ctx.TableValue(d1);
var meanC1 = ctx.Mean(c);
return probD1 * meanC1;
On line 1, we are using the variable alias d to refer to variable D, and referencing its first state D1 by name.
On line 2, we are using a built in variable called ctx which stands for context. This provides functions such as TableValue used here, which allows us to access the results from a query such as P(D=D1 | any evidence)
.
On line 3, we similarly use ctx to access the mean i.e. P(C | any evidence)
.
On line 4 we use the advanced expression language to multiply and return the two values.
- Click OK.
Note that even though the expression for F1 references nodes D and C we do not need to add links from them to F1. This is because function nodes are calculated after inference on the probability nodes has completed.
Define function F2
For F2 we are going to create an expression that involves F1.
Because F2 requires F1 to have already been computed, we need to first add a link from F1 to F2.
From the Build main tab, click Link then Add Link(s).
Set From to F1.
Set To to F2.
Click OK.
Click on the node F2.
From the Build main tab, click Expression then Function Node expression.
Set the Return Type to Double.
Set the Expression Text to the following:
var f1Val = ctx.FunctionValue(f1);
return f1Val * 2.0;
- Click Ok.
Define function F3
Click on the node F3.
From the Build main tab, click Expression then Function Node expression.
Set the Return Type to String.
Unlike F1 and F2 the return type here is String.
- Set the Expression Text to the following:
return "PI = " + Math.PI;
Click Ok.
Save the network.
Query the network
- From the Query main tab, click Add then All nodes.
- Try clicking on evidence (e.g.
D=D1
) and see how the function nodes automatically update.