Create the DBN structure by JSMILE methods

The engine.
Post Reply
Yun
Posts: 20
Joined: Mon Oct 17, 2016 8:34 pm

Create the DBN structure by JSMILE methods

Post by Yun »

Hi, SMILE creators,

Thank you so much for your time! I tried to create a DBN structure file using JSMILE's methods rather than "manual" coding (after successfully running em.learn of the DBN with the given structure file from the tutorial). I tried to create it in a "roll" way in which I don't have to write down all the unrolled variables and arcs. However, I don't find a way to specify number of slices, or specify temporal arcs. Here is the code I constructed:

--------------------------------------------

...
import smile.Network;
import smile.SMILEException;
import smile.learning.DataMatch;
import smile.learning.DataSet;
import smile.learning.EM;

public class CallSmile {

public static void createDBN1(String outfile) {
Network net = new Network();

String[] nodes = { "A", "B", "C" };
for (String n : nodes) {
net.addNode(Network.NodeType.Cpt, n);
net.setOutcomeId(n, 0, "f");
net.setOutcomeId(n, 1, "t");
}

// Adding an arc from "A" to "C", from "B" to "C"
net.addArc("A", "C");
net.addArc("B", "C");
net.addTemporalArc("A", "A", 1); //<-- an error here

// Filling in the conditional distribution for node "A", "B". The probabilities are:
double[] aDef = { 0.5, 0.5 };
net.setNodeDefinition("A", aDef);
net.setNodeDefinition("B", aDef);

// Filling in the conditional distribution for node "C". The probabilities are:
double[] aDef2 = { 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 };
net.setNodeDefinition("C", aDef2);

double[] aDef3 = { 0.5, 0.5, 0.5, 0.5 };
net.setNodeTemporalDefinition("A", 1, aDef3);

// Writting the network to a file:
net.writeFile(outfile);
System.out.println("Saved network!");

}

public static void main(String[] args) throws IOException {
createDBN1("net_tut_6_bycode.xdsl");
}
}

---------------------------------------------------
However, it gives me an error as follows with respect to the line "net.addTemporalArc("A", "A", 1)" ("addTemporalArc" is the only method I could find related to adding temporal arcs; its arguments are parentId, childID, and order):

Exception in thread "main" smile.SMILEException: SMILE error -2 in function AddTemporalArc
at smile.Network.addTemporalArc(Native Method)
at CallSmile.createDBN1(CallSmile.java:489)
at CallSmile.main(CallSmile.java:911)


I wonder whether I should explicitly write down the unrolled variables and arcs as follows:
net.addNode(Network.NodeType.Cpt, "A");
net.addNode(Network.NodeType.Cpt, "A_1");
net.addNode(Network.NodeType.Cpt, "A_2");
net.addTemporalArc("A", "A_1", 1);
net.addTemporalArc("A_1", "A_2", 1);

I wonder whether you could give me some hints on resolving this? I really appreciate it!

Yun
PhD, University of Pittsburgh
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: Create the DBN structure by JSMILE methods

Post by shooltz[BayesFusion] »

Only nodes residing on a temporal plate can be linked with temporal arcs (this includes self-arcs). You'll need to ensure node A is a plate node:

Code: Select all

net.setNodeTemporalType("A", Network.NodeTemporalType.Plate);
Other temporal node types are: Contemporal, Anchor and Terminal. By default, a node is Contemporal.
Yun
Posts: 20
Joined: Mon Oct 17, 2016 8:34 pm

Re: Create the DBN structure by JSMILE methods

Post by Yun »

Thank you so much!!! Now I can use JSMILE methods to create a network same as the the one from the tutorial :)

In case it is useful to others, I am posting the code after fixing the problem:

Network net = new Network();

String[] nodes = { "A", "B", "C" };
int nbSlices = 3;

for (String n : nodes) {
net.addNode(Network.NodeType.Cpt, n);
net.setOutcomeId(n, 0, "f");
net.setOutcomeId(n, 1, "t");
net.setNodeTemporalType(n, Network.NodeTemporalType.Plate);
}
net.setSliceCount(nbSlices);
net.addArc("A", "C");
net.addArc("B", "C");
net.addTemporalArc("A", "A", 1);

double[] aDef = { 0.5, 0.5 };
net.setNodeDefinition("A", aDef);
net.setNodeDefinition("B", aDef);

double[] aDef2 = { 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 };
net.setNodeDefinition("C", aDef2);

double[] aDef3 = { 0.5, 0.5, 0.5, 0.5 };
net.setNodeTemporalDefinition("A", 1, aDef3);

net.writeFile("net_tut_6_bycode.xdsl");
Post Reply