Skip to main content

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

  1. From the File main tab, click New to create a new empty network.

Add discrete node D

  1. From the Build main tab, click Node, then Discrete.

  2. Name the node D.

  3. Rename the 2 discrete states D1 and D2.

  4. Click OK.

New discrete node

info

Remember to save your network periodically

Add continuous node C

  1. From the Build main tab, click Node then Continuous.

  2. Name the node C.

  3. Click OK.

Add function nodes

  1. From the Build main tab, click Node, then Function.

  2. Name the node F1.

  3. Click OK.

  4. Repeat the steps above to add 2 further function nodes, naming them F2 and F3.

Layout nodes

  1. Layout the nodes as in the image below.

Node layout

  1. From the Build main tab, click Link, then Add Link(s).

  2. Set From to D.

  3. Set To to C.

  4. Click OK.

Link D -> C

Define P(D)

  1. Select node D.

  2. From the Build main tab, click Distributions, then Edit distribution(s).

  3. Set P(D1) = 0.6961106280502056, and P(D2) = 0.3038893719497943.

  4. Click OK.

P(D)

Define P(C | D)

  1. From the Build main tab, click Distributions, then Edit distribution(s).
info

P(C | D) consists of two Gaussian distributions, one for D1 and another for D2.

  1. Click on the cell containing 1 under D=D1 to edit P(C|D=D1). Set the mean to 100 and the variance to 20.

P(C|D1)

  1. Click on the cell containing 1 under D=D2 to edit P(C|D=D2). Set the mean to -50 and the variance to 45.

P(C|D1)

  1. Click Ok.

Expression Aliases

Although not required, we can simplify expressions by giving each variable that is used in an expression, an alias.

  1. From the Build main tab, click Expression then Edit variable aliases.

  2. Set the alias for C to c.

  3. Set the alias for D to d.

  4. Set the alias for F1 to f1.

Expression aliases

Define function F1

  1. Click on the node F1.

  2. From the Build main tab, click Expression then Function Node expression.

  3. Set the Return Type to Double.

  4. 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;
info

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.

  1. Click OK.

Function F1

info

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.

info

Because F2 requires F1 to have already been computed, we need to first add a link from F1 to F2.

  1. From the Build main tab, click Link then Add Link(s).

  2. Set From to F1.

  3. Set To to F2.

  4. Click OK.

  5. Click on the node F2.

  6. From the Build main tab, click Expression then Function Node expression.

  7. Set the Return Type to Double.

  8. Set the Expression Text to the following:

var f1Val = ctx.FunctionValue(f1);

return f1Val * 2.0;
  1. Click Ok.

Define function F3

  1. Click on the node F3.

  2. From the Build main tab, click Expression then Function Node expression.

  3. Set the Return Type to String.

info

Unlike F1 and F2 the return type here is String.

  1. Set the Expression Text to the following:
return "PI = " + Math.PI;
  1. Click Ok.

  2. Save the network.

Query the network

  1. From the Query main tab, click Add then All nodes.

Query all nodes

  1. Try clicking on evidence (e.g. D=D1) and see how the function nodes automatically update.