Dynamic Bayesian Networks: Code Examples¶
Data Streams¶
In this example we show how to use the main features of a DataStream object. More precisely, we show how to load a dynamic data stream and how to iterate over the DynamicDataInstance objects.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.datastream;
import eu.amidst.core.datastream.Attribute;
import eu.amidst.core.datastream.DataStream;
import eu.amidst.dynamic.datastream.DynamicDataInstance;
import eu.amidst.dynamic.io.DynamicDataStreamLoader;
import eu.amidst.dynamic.utils.DataSetGenerator;
/**
* An example showing how to load an use a DataStream object. For more options refer to class
* eu.amidst.core.examples.datastream and simply change DataInstance by DynamicDataInstance
*
* Created by ana@cs.aau.dk on 02/12/15.
*/
public class DataStreamsExample {
public static void main(String[] args) throws Exception {
//Open the data stream using the class DynamicDataStreamLoader
DataStream<DynamicDataInstance> data = DynamicDataStreamLoader.loadFromFile("datasets/simulated/exampleDS_d2_c3.arff");
//Access the attributes defining the data stream
System.out.println("Attributes defining the data set");
for (Attribute attribute : data.getAttributes()) {
System.out.println(attribute.getName());
}
Attribute discreteVar0 = data.getAttributes().getAttributeByName("DiscreteVar0");
//Iterate over dynamic data instances
System.out.println("1. Iterating over samples using a for loop");
for (DynamicDataInstance dataInstance : data) {
System.out.println("SequenceID = "+dataInstance.getSequenceID()+", TimeID = "+dataInstance.getTimeID());
System.out.println("The value of attribute discreteVar0 for the current data instance is: " +
dataInstance.getValue(discreteVar0));
}
}
}
Dynamic Random Variables¶
This example show the basic functionalities related to dynamic variables.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.variables;
import eu.amidst.core.variables.Variable;
import eu.amidst.dynamic.variables.DynamicVariables;
import java.util.Arrays;
/**
* This example show the basic functionalities related to dynamic variables.
*/
public class DynamicVariablesExample {
public static void main(String[] args) throws Exception {
//Create an empty DynamicVariables object
DynamicVariables variables = new DynamicVariables();
//Invoke the "new" methods of the object DynamicVariables to create new dynamic variables.
//Create a Gaussian dynamic variables
Variable gaussianVar = variables.newGaussianDynamicVariable("GaussianVar");
//Create a Multinomial dynamic variable with two states
Variable multinomialVar = variables.newMultinomialDynamicVariable("MultinomialVar", 2);
//Create a Multinomial dynamic variable with two states: TRUE and FALSE
Variable multinomialVar2 = variables.newMultinomialDynamicVariable("MultinomialVar2", Arrays.asList("TRUE, FALSE"));
//We must block the object before we start to query.
variables.block();
//All dynamic Variables have an interface variable
Variable gaussianVarInt = gaussianVar.getInterfaceVariable();
Variable multinomialVarInt = multinomialVar.getInterfaceVariable();
//Get the "main" Variable associated with each interface variable through the DynamicVariable object
Variable mainMultinomialVar = variables.getVariableFromInterface(multinomialVarInt);
//Check whether a variable is an interface variable
System.out.println("Is Variable "+gaussianVar.getName()+" an interface variable? "
+gaussianVar.isInterfaceVariable());
System.out.println("Is Variable "+gaussianVarInt.getName()+" an interface variable? "
+gaussianVarInt.isInterfaceVariable());
//Check whether a variable is a dynamic variable
System.out.println("Is Variable "+multinomialVar.getName()+" a dynamic variable? "
+gaussianVar.isDynamicVariable());
}
}
Dynamic Bayesian networks¶
Creating Dynamic Bayesian networks¶
This example creates a dynamic BN, from a dynamic data stream, with randomly generated probability distributions, then saves it to a file.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.models;
import eu.amidst.core.datastream.DataStream;
import eu.amidst.core.variables.Variable;
import eu.amidst.dynamic.datastream.DynamicDataInstance;
import eu.amidst.dynamic.io.DynamicBayesianNetworkWriter;
import eu.amidst.dynamic.io.DynamicDataStreamLoader;
import eu.amidst.dynamic.models.DynamicBayesianNetwork;
import eu.amidst.dynamic.models.DynamicDAG;
import eu.amidst.dynamic.variables.DynamicVariables;
/**
* This example creates a dynamic BN from a dynamic data stream, with randomly generated probability distributions, then saves it to a file.
*/
public class CreatingDBNs {
public static void main(String[] args) throws Exception{
//Open the data stream using the static class DynamicDataStreamLoader
DataStream<DynamicDataInstance> data = DynamicDataStreamLoader.loadFromFile(
"datasets/simulated/syntheticDataDiscrete.arff");
/**
* 1. Once the data is loaded, we create a random variable for each of the attributes (i.e. data columns)
* in our data.
*
* 2. {@link DynamicVariables} is the class for doing that. It takes a list of Attributes and internally creates
* all the variables. We create the variables using DynamicVariables class to guarantee that each variable
* has a different ID number and make it transparent for the user. Each random variable has an associated
* interface variable.
*
* 3. We can extract the Variable objects by using the method getVariableByName();
*/
DynamicVariables dynamicVariables = new DynamicVariables(data.getAttributes());
DynamicDAG dynamicDAG = new DynamicDAG(dynamicVariables);
Variable A = dynamicVariables.getVariableByName("A");
Variable B = dynamicVariables.getVariableByName("B");
Variable C = dynamicVariables.getVariableByName("C");
Variable D = dynamicVariables.getVariableByName("D");
Variable E = dynamicVariables.getVariableByName("E");
Variable G = dynamicVariables.getVariableByName("G");
Variable A_Interface = dynamicVariables.getInterfaceVariable(A);
Variable B_Interface = dynamicVariables.getInterfaceVariable(B);
//Note that C_Interface and D_Interface are also created although they will not be used
//(we will not add temporal dependencies)
Variable E_Interface = dynamicVariables.getInterfaceVariable(E);
Variable G_Interface = dynamicVariables.getInterfaceVariable(G);
// Example of the dynamic DAG structure
// Time 0: Parents at time 0 are automatically created when adding parents at time T
dynamicDAG.getParentSetTimeT(B).addParent(A);
dynamicDAG.getParentSetTimeT(C).addParent(A);
dynamicDAG.getParentSetTimeT(D).addParent(A);
dynamicDAG.getParentSetTimeT(E).addParent(A);
dynamicDAG.getParentSetTimeT(G).addParent(A);
dynamicDAG.getParentSetTimeT(A).addParent(A_Interface);
dynamicDAG.getParentSetTimeT(B).addParent(B_Interface);
dynamicDAG.getParentSetTimeT(E).addParent(E_Interface);
dynamicDAG.getParentSetTimeT(G).addParent(G_Interface);
System.out.println(dynamicDAG.toString());
/**
* 1. We now create the Dynamic Bayesian network from the previous Dynamic DAG.
*
* 2. The DBN object is created from the DynamicDAG. It automatically looks at the distribution type
* of each variable and their parents to initialize the Distributions objects that are stored
* inside (i.e. Multinomial, Normal, CLG, etc). The parameters defining these distributions are
* properly initialized.
*
* 3. The network is printed and we can have a look at the kind of distributions stored in the DBN object.
*/
DynamicBayesianNetwork dbn = new DynamicBayesianNetwork(dynamicDAG);
System.out.printf(dbn.toString());
/**
* Finally teh Bayesian network is saved to a file.
*/
DynamicBayesianNetworkWriter.save(dbn, "networks/simulated/DBNExample.dbn");
}
}
Creating Dynamic Bayesian Networks with Latent Variables¶
This example shows how to create a BN model with hidden variables. We simply create a BN for clustering, i.e., a naive Bayes like structure with a single hidden variable acting as parant of all the remaining observable variables.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.models;
import eu.amidst.core.datastream.DataStream;
import eu.amidst.core.variables.Variable;
import eu.amidst.dynamic.datastream.DynamicDataInstance;
import eu.amidst.dynamic.io.DynamicBayesianNetworkWriter;
import eu.amidst.dynamic.io.DynamicDataStreamLoader;
import eu.amidst.dynamic.models.DynamicBayesianNetwork;
import eu.amidst.dynamic.models.DynamicDAG;
import eu.amidst.dynamic.variables.DynamicVariables;
/**
*
* In this example, we show how to create a DBN model with latent variables. We create a DBN a class and a hidden
* variable acting as parent of all the observable variables.
*
* Created by ana@cs.aau.dk on 02/12/15.
*/
public class CreatingDBNsWithLatentVariables {
public static void main(String[] args) throws Exception{
//We can open the data stream using the static class DynamicDataStreamLoader
DataStream<DynamicDataInstance> data = DynamicDataStreamLoader.loadFromFile(
"datasets/simulated/syntheticDataDiscrete.arff");
/**
* 1. Once the data is loaded, we create a random variable for each of the attributes (i.e. data columns)
* in our data.
*
* 2. {@link DynamicVariables} is the class for doing that. It takes a list of Attributes and internally creates
* all the variables. We create the variables using DynamicVariables class to guarantee that each variable
* has a different ID number and make it transparent for the user. Each random variable has an associated
* interface variable.
*
* 3. We can extract the Variable objects by using the method getVariableByName();
*/
DynamicVariables dynamicVariables = new DynamicVariables(data.getAttributes());
Variable A = dynamicVariables.getVariableByName("A");
Variable B = dynamicVariables.getVariableByName("B");
Variable C = dynamicVariables.getVariableByName("C");
Variable D = dynamicVariables.getVariableByName("D");
Variable E = dynamicVariables.getVariableByName("E");
Variable G = dynamicVariables.getVariableByName("G");
Variable A_Interface = dynamicVariables.getInterfaceVariable(A);
Variable B_Interface = dynamicVariables.getInterfaceVariable(B);
//Note that C_Interface and D_Interface are also created although they will not be used
//(we will not add temporal dependencies)
Variable E_Interface = dynamicVariables.getInterfaceVariable(E);
Variable G_Interface = dynamicVariables.getInterfaceVariable(G);
/*
* We add a hidden multinomial variable (with 2 states) as parent of all variables except A
*/
Variable H = dynamicVariables.newMultinomialDynamicVariable("H", 2);
Variable H_Interface = dynamicVariables.getInterfaceVariable(H);
/**
* 1. Once we have defined your {@link DynamicVariables} object, including the latent variable,
* the next step is to create a DynamicDAG structure over this set of variables.
*
*/
DynamicDAG dynamicDAG = new DynamicDAG(dynamicVariables);
// EXAMPLE OF THE DAG STRUCTURE
/*
DAG Time 0
H : { }
A : { }
B : { A, H }
C : { A, H }
D : { A, H }
E : { A, H }
G : { A, H }
DAG Time T
H : { H_Interface }
A : { A_Interface }
B : { H, A, B_Interface }
C : { H, A }
D : { H, A }
E : { H, A, E_Interface }
G : { H, A, G_Interface }
*/
/*
* 1. To add parents to each variable, we first recover the ParentSet object by the method
* getParentSet(Variable var) and then call the method addParent(Variable var).
*
* 2. We just put the hidden variable as parent of all the other variables (except A), and
* link it temporally.
*
*/
// Time 0: Parents at time 0 are automatically created when adding parents at time T
// Time t
dynamicDAG.getParentSetsTimeT().stream()
.filter(pset -> pset.getMainVar().getVarID() != A.getVarID())
.filter(pset -> pset.getMainVar().getVarID() != H.getVarID())
.forEach(pset -> {
pset.addParent(A);
pset.addParent(H);
});
dynamicDAG.getParentSetTimeT(A).addParent(A_Interface);
dynamicDAG.getParentSetTimeT(B).addParent(B_Interface);
dynamicDAG.getParentSetTimeT(E).addParent(E_Interface);
dynamicDAG.getParentSetTimeT(G).addParent(G_Interface);
dynamicDAG.getParentSetTimeT(H).addParent(H_Interface);
System.out.println(dynamicDAG.toString());
/**
* 1. We now create the Dynamic Bayesian network from the previous Dynamic DAG.
*
* 2. The DBN object is created from the DynamicDAG. It automatically looks at the distribution type
* of each variable and their parents to initialize the Distributions objects that are stored
* inside (i.e. Multinomial, Normal, CLG, etc). The parameters defining these distributions are
* properly initialized.
*
* 3. The network is printed and we can have a look at the kind of distributions stored in the DBN object.
*/
DynamicBayesianNetwork dbn = new DynamicBayesianNetwork(dynamicDAG);
System.out.printf(dbn.toString());
/**
* Finally teh Bayesian network is saved to a file.
*/
DynamicBayesianNetworkWriter.save(dbn, "networks/simulated/DBNExample.dbn");
}
}
Modifying Dynamic Bayesian Networks¶
This example shows how to create a BN model with hidden variables. We This example shows how to access and modify the conditional probabilities of a Dynamic Bayesian network model.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.models;
import eu.amidst.core.datastream.DataStream;
import eu.amidst.core.distribution.Multinomial;
import eu.amidst.core.distribution.Multinomial_MultinomialParents;
import eu.amidst.core.variables.Variable;
import eu.amidst.dynamic.datastream.DynamicDataInstance;
import eu.amidst.dynamic.io.DynamicDataStreamLoader;
import eu.amidst.dynamic.models.DynamicBayesianNetwork;
import eu.amidst.dynamic.models.DynamicDAG;
import eu.amidst.dynamic.variables.DynamicVariables;
/**
* In this example we show how to access and modify the conditional probabilities of a Dynamic Bayesian network model.
*
* Created by ana@cs.aau.dk on 02/12/15.
*/
public class ModifyingDBNs {
public static void main(String[] args) throws Exception {
//We can open the data stream using the static class DynamicDataStreamLoader
DataStream<DynamicDataInstance> data = DynamicDataStreamLoader.loadFromFile(
"datasets/simulated/syntheticDataDiscrete.arff");
/**
* 1. Once the data is loaded, we create a random variable for each of the attributes (i.e. data columns)
* in our data.
*
* 2. {@link DynamicVariables} is the class for doing that. It takes a list of Attributes and internally creates
* all the variables. We create the variables using DynamicVariables class to guarantee that each variable
* has a different ID number and make it transparent for the user. Each random variable has an associated
* interface variable.
*
* 3. We can extract the Variable objects by using the method getVariableByName();
*/
DynamicVariables dynamicVariables = new DynamicVariables(data.getAttributes());
DynamicDAG dynamicDAG = new DynamicDAG(dynamicVariables);
Variable A = dynamicVariables.getVariableByName("A");
Variable B = dynamicVariables.getVariableByName("B");
Variable C = dynamicVariables.getVariableByName("C");
Variable D = dynamicVariables.getVariableByName("D");
Variable E = dynamicVariables.getVariableByName("E");
Variable G = dynamicVariables.getVariableByName("G");
Variable A_Interface = dynamicVariables.getInterfaceVariable(A);
Variable B_Interface = dynamicVariables.getInterfaceVariable(B);
//Note that C_Interface and D_Interface are also created although they will not be used
//(we will not add temporal dependencies)
Variable E_Interface = dynamicVariables.getInterfaceVariable(E);
Variable G_Interface = dynamicVariables.getInterfaceVariable(G);
// EXAMPLE OF THE DAG STRUCTURE
/*
DAG Time 0
A : { }
B : { A }
C : { A }
D : { A }
E : { A }
G : { A }
DAG Time T
A : { A_Interface }
B : { A, B_Interface }
C : { A }
D : { A }
E : { A, E_Interface }
G : { A, G_Interface }
*/
// Time 0: Parents at time 0 are automatically created when adding parents at time T
// Time t
dynamicDAG.getParentSetTimeT(B).addParent(A);
dynamicDAG.getParentSetTimeT(C).addParent(A);
dynamicDAG.getParentSetTimeT(D).addParent(A);
dynamicDAG.getParentSetTimeT(E).addParent(A);
dynamicDAG.getParentSetTimeT(G).addParent(A);
dynamicDAG.getParentSetTimeT(A).addParent(A_Interface);
dynamicDAG.getParentSetTimeT(B).addParent(B_Interface);
dynamicDAG.getParentSetTimeT(E).addParent(E_Interface);
dynamicDAG.getParentSetTimeT(G).addParent(G_Interface);
/**
* 1. We now create the Dynamic Bayesian network from the previous Dynamic DAG.
*
* 2. The DBN object is created from the DynamicDAG. It automatically looks at the distribution type
* of each variable and their parents to initialize the Distributions objects that are stored
* inside (i.e. Multinomial, Normal, CLG, etc). The parameters defining these distributions are
* properly initialized.
*
* 3. The network is printed and we can have a look at the kind of distributions stored in the DBN object.
*/
DynamicBayesianNetwork dbn = new DynamicBayesianNetwork(dynamicDAG);
System.out.printf(dbn.toString());
/*
* We now modify the conditional probability distributions to assigned the values we want
*/
/* IMPORTANT: The parents are indexed according to Koller (Chapter 10. Pag. 358). Example:
Parents: A = {A0,A1} and B = {B0,B1,B2}.
NumberOfPossibleAssignments = 6
Index A B
0 A0 B0
1 A1 B1
2 A0 B2
3 A1 B0
4 A0 B1
5 A1 B2
*/
// ********************************************************************************************
// ************************************** TIME 0 **********************************************
// ********************************************************************************************
// Variable A
Multinomial distA_Time0 = dbn.getConditionalDistributionTime0(A);
distA_Time0.setProbabilities(new double[]{0.3, 0.7});
// Variable B
Multinomial_MultinomialParents distB_Time0 = dbn.getConditionalDistributionTime0(B);
distB_Time0.getMultinomial(0).setProbabilities(new double[]{0.4, 0.1, 0.5});
distB_Time0.getMultinomial(1).setProbabilities(new double[]{0.2, 0.5, 0.3});
// Variable C
Multinomial_MultinomialParents distC_Time0 = dbn.getConditionalDistributionTime0(C);
distC_Time0.getMultinomial(0).setProbabilities(new double[]{0.4, 0.6});
distC_Time0.getMultinomial(1).setProbabilities(new double[]{0.2, 0.8});
// Variable D
Multinomial_MultinomialParents distD_Time0 = dbn.getConditionalDistributionTime0(D);
distD_Time0.getMultinomial(0).setProbabilities(new double[]{0.7, 0.3});
distD_Time0.getMultinomial(1).setProbabilities(new double[]{0.1, 0.9});
// Variable E
Multinomial_MultinomialParents distE_Time0 = dbn.getConditionalDistributionTime0(E);
distE_Time0.getMultinomial(0).setProbabilities(new double[]{0.8, 0.2});
distE_Time0.getMultinomial(1).setProbabilities(new double[]{0.1, 0.9});
// Variable G
Multinomial_MultinomialParents distG_Time0 = dbn.getConditionalDistributionTime0(G);
distG_Time0.getMultinomial(0).setProbabilities(new double[]{0.6, 0.4});
distG_Time0.getMultinomial(1).setProbabilities(new double[]{0.7, 0.3});
// ********************************************************************************************
// ************************************** TIME T **********************************************
// ********************************************************************************************
// Variable A
Multinomial_MultinomialParents distA_TimeT = dbn.getConditionalDistributionTimeT(A);
distA_TimeT.getMultinomial(0).setProbabilities(new double[]{0.15, 0.85});
distA_TimeT.getMultinomial(1).setProbabilities(new double[]{0.1, 0.9});
// Variable B
Multinomial_MultinomialParents distB_TimeT = dbn.getConditionalDistributionTimeT(B);
distB_TimeT.getMultinomial(0).setProbabilities(new double[]{0.1, 0.2, 0.7});
distB_TimeT.getMultinomial(1).setProbabilities(new double[]{0.6, 0.1, 0.3});
distB_TimeT.getMultinomial(2).setProbabilities(new double[]{0.3, 0.4, 0.3});
distB_TimeT.getMultinomial(3).setProbabilities(new double[]{0.2, 0.1, 0.7});
distB_TimeT.getMultinomial(4).setProbabilities(new double[]{0.5, 0.1, 0.4});
distB_TimeT.getMultinomial(5).setProbabilities(new double[]{0.1, 0.1, 0.8});
// Variable C: equals to the distribution at time 0 (C does not have temporal clone)
Multinomial_MultinomialParents distC_TimeT = dbn.getConditionalDistributionTimeT(C);
distC_TimeT.getMultinomial(0).setProbabilities(new double[]{0.4, 0.6});
distC_TimeT.getMultinomial(1).setProbabilities(new double[]{0.2, 0.8});
// Variable D: equals to the distribution at time 0 (D does not have temporal clone)
Multinomial_MultinomialParents distD_TimeT = dbn.getConditionalDistributionTimeT(D);
distD_TimeT.getMultinomial(0).setProbabilities(new double[]{0.7, 0.3});
distD_TimeT.getMultinomial(1).setProbabilities(new double[]{0.1, 0.9});
// Variable E
Multinomial_MultinomialParents distE_TimeT = dbn.getConditionalDistributionTimeT(E);
distE_TimeT.getMultinomial(0).setProbabilities(new double[]{0.3, 0.7});
distE_TimeT.getMultinomial(1).setProbabilities(new double[]{0.6, 0.4});
distE_TimeT.getMultinomial(2).setProbabilities(new double[]{0.7, 0.3});
distE_TimeT.getMultinomial(3).setProbabilities(new double[]{0.9, 0.1});
// Variable G
Multinomial_MultinomialParents distG_TimeT = dbn.getConditionalDistributionTimeT(G);
distG_TimeT.getMultinomial(0).setProbabilities(new double[]{0.2, 0.8});
distG_TimeT.getMultinomial(1).setProbabilities(new double[]{0.5, 0.5});
distG_TimeT.getMultinomial(2).setProbabilities(new double[]{0.3, 0.7});
distG_TimeT.getMultinomial(3).setProbabilities(new double[]{0.8, 0.2});
/*
* We print the new DBN
*/
System.out.println(dbn.toString());
}
}
Sampling from Dynamic Bayesian Networks¶
This example shows how to use the DynamicBayesianNetworkSampler class to randomly generate a dynamic data stream from a given Dynamic Bayesian network.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.utils;
import eu.amidst.core.datastream.DataStream;
import eu.amidst.core.io.DataStreamWriter;
import eu.amidst.dynamic.datastream.DynamicDataInstance;
import eu.amidst.dynamic.models.DynamicBayesianNetwork;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkGenerator;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkSampler;
import java.util.Random;
/**
* This example shows how to use the DynamicBayesianNetworkSampler class to randomly generate a data sample
* for a given Dynamic Bayesian network.
*
* Created by ana@cs.aau.dk on 02/12/15.
*/
public class DynamicBayesianNetworkSamplerExample {
public static void main(String[] args) throws Exception{
//We first generate a DBN with 3 continuous and 3 discrete variables with 2 states
DynamicBayesianNetworkGenerator dbnGenerator = new DynamicBayesianNetworkGenerator();
dbnGenerator.setNumberOfContinuousVars(3);
dbnGenerator.setNumberOfDiscreteVars(3);
dbnGenerator.setNumberOfStates(2);
//Create a NB-like structure with temporal links in the children (leaves) and 2 states for
//the class variable
DynamicBayesianNetwork network = DynamicBayesianNetworkGenerator.generateDynamicNaiveBayes(
new Random(0), 2, true);
//Create the sampler from this network
DynamicBayesianNetworkSampler sampler = new DynamicBayesianNetworkSampler(network);
sampler.setSeed(0);
//Sample a dataStream of 3 sequences of 1000 samples each
DataStream<DynamicDataInstance> dataStream = sampler.sampleToDataBase(3,1000);
//Save the created data sample in a file
DataStreamWriter.writeDataToFile(dataStream, "./datasets/simulated/dnb-samples.arff");
}
}
Inference Algorithms for Dynamic Bayesian Networks¶
The Dynamic MAP Inference¶
This example shows how to use the Dynamic MAP Inference algorithm.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.inference;
import eu.amidst.core.variables.Assignment;
import eu.amidst.core.variables.Variable;
import eu.amidst.dynamic.models.DynamicBayesianNetwork;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkGenerator;
import eu.amidst.dynamic.variables.DynamicAssignment;
import eu.amidst.dynamic.variables.HashMapDynamicAssignment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
/**
* This example shows how to use the Dynamic MAP Inference algorithm described in Deliverable 3.4 (Section 6).
* Created by dario on 11/11/15.
*/
public class DynamicMAPInference {
public static void main(String[] arguments) throws IOException, ClassNotFoundException {
/*
* LOADS THE DYNAMIC NETWORK AND PRINTS IT
*/
DynamicBayesianNetworkGenerator.setNumberOfContinuousVars(3);
DynamicBayesianNetworkGenerator.setNumberOfDiscreteVars(5);
DynamicBayesianNetworkGenerator.setNumberOfStates(2);
DynamicBayesianNetworkGenerator.setNumberOfLinks(5);
DynamicBayesianNetwork dynamicBayesianNetwork = DynamicBayesianNetworkGenerator.generateDynamicNaiveBayes(new Random(0), 2, true);
/*
* INITIALIZE THE DYNAMIC MAP OBJECT
*/
int nTimeSteps = 6;
eu.amidst.dynamic.inference.DynamicMAPInference dynMAP = new eu.amidst.dynamic.inference.DynamicMAPInference();
dynMAP.setModel(dynamicBayesianNetwork);
dynMAP.setNumberOfTimeSteps(nTimeSteps);
Variable mapVariable = dynamicBayesianNetwork.getDynamicVariables().getVariableByName("ClassVar");
dynMAP.setMAPvariable(mapVariable);
/*
* GENERATE AN EVIDENCE FOR T=0,...,nTimeSteps-1
*/
List<Variable> varsDynamicModel = dynamicBayesianNetwork.getDynamicVariables().getListOfDynamicVariables();
System.out.println("DYNAMIC VARIABLES:");
varsDynamicModel.forEach(var -> System.out.println("Var ID " + var.getVarID() + ": " + var.getName()));
System.out.println();
int indexVarEvidence1 = 2;
int indexVarEvidence2 = 3;
int indexVarEvidence3 = 4;
Variable varEvidence1 = varsDynamicModel.get(indexVarEvidence1);
Variable varEvidence2 = varsDynamicModel.get(indexVarEvidence2);
Variable varEvidence3 = varsDynamicModel.get(indexVarEvidence3);
List<Variable> varsEvidence = new ArrayList<>(3);
varsEvidence.add(0,varEvidence1);
varsEvidence.add(1,varEvidence2);
varsEvidence.add(2,varEvidence3);
double varEvidenceValue;
Random random = new Random(4634);
List<DynamicAssignment> evidence = new ArrayList<>(nTimeSteps);
for (int t = 0; t < nTimeSteps; t++) {
HashMapDynamicAssignment dynAssignment = new HashMapDynamicAssignment(varsEvidence.size());
for (int i = 0; i < varsEvidence.size(); i++) {
dynAssignment.setSequenceID(12302253);
dynAssignment.setTimeID(t);
Variable varEvidence = varsEvidence.get(i);
if (varEvidence.isMultinomial()) {
varEvidenceValue = random.nextInt(varEvidence1.getNumberOfStates());
} else {
varEvidenceValue = -5 + 10 * random.nextDouble();
}
dynAssignment.setValue(varEvidence, varEvidenceValue);
}
evidence.add(dynAssignment);
}
System.out.println("EVIDENCE:");
evidence.forEach(evid -> {
System.out.println("Evidence at time " + evid.getTimeID());
evid.getVariables().forEach(variable -> System.out.println(variable.getName() + ": " + Integer.toString((int) evid.getValue(variable))));
System.out.println();
});
/*
* SET THE EVIDENCE AND MAKE INFERENCE
*/
dynMAP.setEvidence(evidence);
dynMAP.runInference(eu.amidst.dynamic.inference.DynamicMAPInference.SearchAlgorithm.IS);
/*
* SHOW RESULTS
*/
Assignment MAPestimate = dynMAP.getMAPestimate();
double MAPestimateProbability = dynMAP.getMAPestimateProbability();
System.out.println("MAP sequence over " + mapVariable.getName() + ":");
List<Variable> MAPvarReplications = MAPestimate.getVariables().stream().sorted((var1,var2) -> (var1.getVarID()>var2.getVarID()? 1 : -1)).collect(Collectors.toList());
StringBuilder sequence = new StringBuilder();
MAPvarReplications.stream().forEachOrdered(var -> sequence.append(Integer.toString((int) MAPestimate.getValue(var)) + ", "));
//System.out.println(MAPestimate.outputString(MAPvarReplications));
System.out.println(sequence.toString());
System.out.println("with probability prop. to: " + MAPestimateProbability);
}
}
The Dynamic Variational Message Passing¶
This example shows how to use the Factored Frontier algorithm with Variational Message Passing for running inference on dynamic Bayesian networks.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.inference;
import eu.amidst.core.datastream.DataStream;
import eu.amidst.core.distribution.UnivariateDistribution;
import eu.amidst.core.inference.messagepassing.VMP;
import eu.amidst.core.variables.Variable;
import eu.amidst.dynamic.datastream.DynamicDataInstance;
import eu.amidst.dynamic.inference.FactoredFrontierForDBN;
import eu.amidst.dynamic.inference.InferenceEngineForDBN;
import eu.amidst.dynamic.models.DynamicBayesianNetwork;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkGenerator;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkSampler;
import java.io.IOException;
import java.util.Random;
/**
* This example shows how to use the Factored Frontier algorithm with Variational Message Passing described in
* Deliverable 3.4 (Section 6).
* Created by ana@cs.aau.dk on 16/11/15.
*/
public class DynamicVMP_FactoredFrontier {
public static void main(String[] args) throws IOException {
Random random = new Random(1);
//We first generate a dynamic Bayesian network (NB structure with class and attributes temporally linked)
DynamicBayesianNetworkGenerator.setNumberOfContinuousVars(2);
DynamicBayesianNetworkGenerator.setNumberOfDiscreteVars(5);
DynamicBayesianNetworkGenerator.setNumberOfStates(3);
DynamicBayesianNetwork extendedDBN = DynamicBayesianNetworkGenerator.generateDynamicNaiveBayes(random,2,true);
System.out.println(extendedDBN.toString());
//We select the target variable for inference, in this case the class variable
Variable classVar = extendedDBN.getDynamicVariables().getVariableByName("ClassVar");
//We create a dynamic dataset with 3 sequences for prediction. The class var is made hidden.
DynamicBayesianNetworkSampler dynamicSampler = new DynamicBayesianNetworkSampler(extendedDBN);
dynamicSampler.setHiddenVar(classVar);
DataStream<DynamicDataInstance> dataPredict = dynamicSampler.sampleToDataBase(1,10);
//We select VMP with the factored frontier algorithm as the Inference Algorithm
FactoredFrontierForDBN FFalgorithm = new FactoredFrontierForDBN(new VMP());
InferenceEngineForDBN.setInferenceAlgorithmForDBN(FFalgorithm);
//Then, we set the DBN model
InferenceEngineForDBN.setModel(extendedDBN);
int time = 0 ;
UnivariateDistribution posterior = null;
for (DynamicDataInstance instance : dataPredict) {
//The InferenceEngineForDBN must be reset at the begining of each Sequence.
if (instance.getTimeID()==0 && posterior != null) {
InferenceEngineForDBN.reset();
time=0;
}
//We also set the evidence.
InferenceEngineForDBN.addDynamicEvidence(instance);
//Then we run inference
InferenceEngineForDBN.runInference();
//Then we query the posterior of the target variable
posterior = InferenceEngineForDBN.getFilteredPosterior(classVar);
//We show the output
System.out.println("P(ClassVar|e[0:"+(time++)+"]) = "+posterior);
}
}
}
The Dynamic Importance Sampling¶
This example shows how to use the Factored Frontier algorithm with Importance Sampling for running inference in dynamic Bayesian networks.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.inference;
import eu.amidst.core.datastream.DataStream;
import eu.amidst.core.distribution.UnivariateDistribution;
import eu.amidst.core.inference.ImportanceSampling;
import eu.amidst.core.variables.Variable;
import eu.amidst.dynamic.datastream.DynamicDataInstance;
import eu.amidst.dynamic.inference.FactoredFrontierForDBN;
import eu.amidst.dynamic.inference.InferenceEngineForDBN;
import eu.amidst.dynamic.models.DynamicBayesianNetwork;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkGenerator;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkSampler;
import java.io.IOException;
import java.util.Random;
/**
* This example shows how to use the Factored Frontier algorithm with Importance Sampling described in
* Deliverable 3.4 (Section 6).
* Created by ana@cs.aau.dk on 16/11/15.
*/
public class DynamicIS_FactoredFrontier {
public static void main(String[] args) throws IOException {
Random random = new Random(1);
//We first generate a dynamic Bayesian network (NB structure with class and attributes temporally linked)
DynamicBayesianNetworkGenerator.setNumberOfContinuousVars(2);
DynamicBayesianNetworkGenerator.setNumberOfDiscreteVars(5);
DynamicBayesianNetworkGenerator.setNumberOfStates(3);
DynamicBayesianNetwork extendedDBN = DynamicBayesianNetworkGenerator.generateDynamicNaiveBayes(random, 2, true);
System.out.println(extendedDBN.toString());
//We select the target variable for inference, in this case the class variable
Variable classVar = extendedDBN.getDynamicVariables().getVariableByName("ClassVar");
//We create a dynamic dataset with 3 sequences for prediction. The class var is made hidden.
DynamicBayesianNetworkSampler dynamicSampler = new DynamicBayesianNetworkSampler(extendedDBN);
dynamicSampler.setHiddenVar(classVar);
DataStream<DynamicDataInstance> dataPredict = dynamicSampler.sampleToDataBase(1, 10);
//We select IS with the factored frontier algorithm as the Inference Algorithm
ImportanceSampling importanceSampling = new ImportanceSampling();
importanceSampling.setKeepDataOnMemory(true);
FactoredFrontierForDBN FFalgorithm = new FactoredFrontierForDBN(importanceSampling);
InferenceEngineForDBN.setInferenceAlgorithmForDBN(FFalgorithm);
//Then, we set the DBN model
InferenceEngineForDBN.setModel(extendedDBN);
int time = 0;
UnivariateDistribution posterior = null;
for (DynamicDataInstance instance : dataPredict) {
//The InferenceEngineForDBN must be reset at the begining of each Sequence.
if (instance.getTimeID() == 0 && posterior != null) {
InferenceEngineForDBN.reset();
time = 0;
}
//We also set the evidence.
InferenceEngineForDBN.addDynamicEvidence(instance);
//Then we run inference
InferenceEngineForDBN.runInference();
//Then we query the posterior of the target variable
posterior = InferenceEngineForDBN.getFilteredPosterior(classVar);
//We show the output
System.out.println("P(ClassVar|e[0:" + (time++) + "]) = " + posterior);
}
}
}
Dynamic Learning Algorithms¶
Maximum Likelihood for DBNs¶
This example shows how to learn the parameters of a dynamic Bayesian network using maximum likelihood from a randomly sampled data stream.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.learning;
import eu.amidst.core.datastream.DataStream;
import eu.amidst.dynamic.datastream.DynamicDataInstance;
import eu.amidst.dynamic.learning.parametric.ParallelMaximumLikelihood;
import eu.amidst.dynamic.learning.parametric.ParameterLearningAlgorithm;
import eu.amidst.dynamic.models.DynamicBayesianNetwork;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkGenerator;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkSampler;
import java.io.IOException;
import java.util.Random;
/**
*
* This example shows how to learn the parameters of a dynamic Bayesian network using maximum likelihood
* from a sample data.
*
* Created by ana@cs.aau.dk on 01/12/15.
*/
public class MLforDBNsampling {
public static void main(String[] args) throws IOException {
Random random = new Random(1);
//We first generate a dynamic Bayesian network (NB structure with class and attributes temporally linked)
DynamicBayesianNetworkGenerator.setNumberOfContinuousVars(2);
DynamicBayesianNetworkGenerator.setNumberOfDiscreteVars(5);
DynamicBayesianNetworkGenerator.setNumberOfStates(3);
DynamicBayesianNetwork dbnRandom = DynamicBayesianNetworkGenerator.generateDynamicNaiveBayes(random,2,true);
//Sample dynamic data from the created dbn with random parameters
DynamicBayesianNetworkSampler sampler = new DynamicBayesianNetworkSampler(dbnRandom);
sampler.setSeed(0);
//Sample 3 sequences of 100K instances
DataStream<DynamicDataInstance> data = sampler.sampleToDataBase(3,10000);
/*Parameter Learning with ML*/
//We fix the DAG structure, the data and learn the DBN
ParameterLearningAlgorithm parallelMaximumLikelihood = new ParallelMaximumLikelihood();
parallelMaximumLikelihood.setWindowsSize(1000);
parallelMaximumLikelihood.setDynamicDAG(dbnRandom.getDynamicDAG());
parallelMaximumLikelihood.initLearning();
parallelMaximumLikelihood.updateModel(data);
DynamicBayesianNetwork dbnLearnt = parallelMaximumLikelihood.getLearntDBN();
//We print the model
System.out.println(dbnLearnt.toString());
}
}
Streaming Variational Bayes for DBNs¶
This example shows how to learn the parameters of a dynamic Bayesian network using streaming variational Bayes from a randomly sampled data stream.
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*
*/
package eu.amidst.dynamic.examples.learning;
import eu.amidst.core.datastream.DataStream;
import eu.amidst.core.inference.messagepassing.VMP;
import eu.amidst.dynamic.datastream.DynamicDataInstance;
import eu.amidst.dynamic.learning.parametric.bayesian.SVB;
import eu.amidst.dynamic.models.DynamicBayesianNetwork;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkGenerator;
import eu.amidst.dynamic.utils.DynamicBayesianNetworkSampler;
import java.io.IOException;
import java.util.Random;
/**
* Created by ana@cs.aau.dk on 01/12/15.
*/
public class SVBforDBN {
public static void main(String[] args) throws IOException {
Random random = new Random(1);
//We first generate a dynamic Bayesian network (NB structure with class and attributes temporally linked)
DynamicBayesianNetworkGenerator.setNumberOfContinuousVars(2);
DynamicBayesianNetworkGenerator.setNumberOfDiscreteVars(5);
DynamicBayesianNetworkGenerator.setNumberOfStates(3);
DynamicBayesianNetwork dbnRandom = DynamicBayesianNetworkGenerator.generateDynamicNaiveBayes(random,2,true);
//Sample dynamic data from the created dbn with random parameters
DynamicBayesianNetworkSampler sampler = new DynamicBayesianNetworkSampler(dbnRandom);
sampler.setSeed(0);
//Sample 3 sequences of 100K instances
DataStream<DynamicDataInstance> data = sampler.sampleToDataBase(3,10000);
/*Parameter Learning with Streaming variational Bayes VMP*/
SVB svb = new SVB();
//We set the desired options for the svb
svb.setWindowsSize(100);
svb.setSeed(0);
//If desired, we also set some options for the VMP
VMP vmp = svb.getPlateauStructure().getVMPTimeT();
vmp.setOutput(false);
vmp.setTestELBO(true);
vmp.setMaxIter(1000);
vmp.setThreshold(0.0001);
//We set the dynamicDAG, the data and start learning
svb.setDynamicDAG(dbnRandom.getDynamicDAG());
svb.setDataStream(data);
svb.runLearning();
//We get the learnt DBN
DynamicBayesianNetwork dbnLearnt = svb.getLearntDBN();
//We print the model
System.out.println(dbnLearnt.toString());
}
}