-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from IDSIA/dev
Release 0.2.0
- Loading branch information
Showing
241 changed files
with
9,054 additions
and
5,724 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,51 @@ | ||
package examples; | ||
|
||
import ch.idsia.crema.factor.bayesian.BayesianDefaultFactor; | ||
import ch.idsia.crema.factor.bayesian.BayesianFactor; | ||
import ch.idsia.crema.model.graphical.BayesianNetwork; | ||
|
||
|
||
public class BNdefinition { | ||
public static void main(String[] args) { | ||
|
||
BayesianNetwork model = new BayesianNetwork(); | ||
BayesianFactor[] f = new BayesianFactor[5]; | ||
|
||
// Winter? | ||
int A = model.addVariable(2); | ||
f[A] = new BayesianFactor(model.getDomain(A), new double[]{.6, .4}, false); | ||
|
||
// Sprinkler? | ||
int B = model.addVariable(2); | ||
model.addParent(B, A); | ||
f[B] = new BayesianFactor(model.getDomain(A, B), false); | ||
f[B].setData(new int[]{B, A}, new double[]{.2, .8, .75, .25}); | ||
|
||
// Rain? | ||
int C = model.addVariable(2); | ||
model.addParent(C, A); | ||
f[C] = new BayesianFactor(model.getDomain(A, C), false); | ||
f[C].setData(new int[]{C, A}, new double[]{.8, .2, .1, .9}); | ||
|
||
// Wet Grass? | ||
int D = model.addVariable(2); | ||
model.addParent(D, B); | ||
model.addParent(D, C); | ||
f[D] = new BayesianFactor(model.getDomain(B, C, D), false); | ||
f[D].setData(new int[]{D, B, C}, new double[]{.95, .05, .9, .1, .8, .2, 0, 1}); | ||
|
||
// Slippery Road? | ||
int E = model.addVariable(2); | ||
model.addParent(E, C); | ||
f[E] = new BayesianFactor(model.getDomain(C, E), false); | ||
f[E].setData(new int[]{E, C}, new double[]{.7, .3, 0, 1}); | ||
|
||
model.setFactors(f); | ||
|
||
System.out.println(model); | ||
|
||
for(int x: model.getVariables()){ | ||
for(int y: model.getChildren(x)){ | ||
System.out.print("("+x+"-->"+y+")"); | ||
} | ||
} | ||
|
||
} | ||
public static void main(String[] args) { | ||
|
||
BayesianNetwork model = new BayesianNetwork(); | ||
BayesianFactor[] f = new BayesianFactor[5]; | ||
|
||
// Winter? | ||
int A = model.addVariable(2); | ||
f[A] = new BayesianDefaultFactor(model.getDomain(A), new double[]{.6, .4}); | ||
|
||
// Sprinkler? | ||
int B = model.addVariable(2); | ||
model.addParent(B, A); | ||
f[B] = new BayesianDefaultFactor(model.getDomain(A, B), new int[]{B, A}, new double[]{.2, .8, .75, .25}); | ||
|
||
// Rain? | ||
int C = model.addVariable(2); | ||
model.addParent(C, A); | ||
f[C] = new BayesianDefaultFactor(model.getDomain(A, C), new int[]{C, A}, new double[]{.8, .2, .1, .9}); | ||
|
||
// Wet Grass? | ||
int D = model.addVariable(2); | ||
model.addParent(D, B); | ||
model.addParent(D, C); | ||
f[D] = new BayesianDefaultFactor(model.getDomain(B, C, D), new int[]{D, B, C}, new double[]{.95, .05, .9, .1, .8, .2, 0, 1}); | ||
|
||
// Slippery Road? | ||
int E = model.addVariable(2); | ||
model.addParent(E, C); | ||
f[E] = new BayesianDefaultFactor(model.getDomain(C, E), new int[]{E, C}, new double[]{.7, .3, 0, 1}); | ||
|
||
model.setFactors(f); | ||
|
||
System.out.println(model); | ||
|
||
for (int x : model.getVariables()) { | ||
for (int y : model.getChildren(x)) { | ||
System.out.print("(" + x + "-->" + y + ")"); | ||
} | ||
} | ||
|
||
} | ||
} | ||
//54 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,61 @@ | ||
package examples; | ||
|
||
import ch.idsia.crema.core.Domain; | ||
import ch.idsia.crema.core.DomainBuilder; | ||
import ch.idsia.crema.factor.bayesian.BayesianDefaultFactor; | ||
import ch.idsia.crema.factor.bayesian.BayesianFactor; | ||
import ch.idsia.crema.factor.bayesian.BayesianFactorFactory; | ||
import ch.idsia.crema.model.graphical.BayesianNetwork; | ||
|
||
|
||
public class BayesianFactors { | ||
public static void main(String[] args) { | ||
|
||
// Create domain of 2 variables of sizes 2,3 | ||
Domain d1 = DomainBuilder.var(0, 1).size(2, 3); | ||
public static void main(String[] args) { | ||
|
||
// Some operations over a domain | ||
d1.getVariables(); // vector of variables (int[]) | ||
d1.getSize(); // number of variables | ||
d1.getCardinality(0); // size of a specific variable | ||
d1.getSizeAt(0); | ||
// Create domain of 2 variables of sizes 2,3 | ||
Domain d1 = DomainBuilder.var(0, 1).size(2, 3); | ||
|
||
// Some operations over a domain | ||
d1.getVariables(); // vector of variables (int[]) | ||
d1.getSize(); // number of variables | ||
d1.getCardinality(0); // size of a specific variable | ||
d1.getSizeAt(0); | ||
|
||
// Crate a factor over the domain | ||
BayesianFactor f = new BayesianFactor(d1); // P([0, 1]) -> P([X|Y]) | ||
|
||
f.getDomain(); | ||
// Crate a factor over the domain and set the data (i.e., values) of the factor | ||
BayesianFactor f = new BayesianDefaultFactor(d1, // P([0, 1]) -> P([X|Y]) | ||
new double[]{ | ||
// x1 x2 | ||
0.2, 0.8, // y1 | ||
0.5, 0.5, // y2 | ||
0.1, 0.9 // y3 | ||
}); | ||
f.getDomain(); | ||
|
||
// Set the data (i.e., values) of the factor | ||
// x1 x2 | ||
f.setData(new double[]{0.2, 0.8, //y1 | ||
0.5, 0.5, //y2 | ||
0.1, 0.9 //y3 | ||
}); | ||
// or using the dedicated Factory class: | ||
f = BayesianFactorFactory.factory() | ||
.domain(d1) | ||
.data(new double[]{0.2, 0.8, 0.5, 0.5, 0.1, 0.9}) | ||
.get(); | ||
|
||
f.marginalize(0).getData(); // = double[3] { 1.0, 1.0, 1.0 } | ||
f.marginalize(0).getData(); // = double[3] { 1.0, 1.0, 1.0 } | ||
|
||
|
||
////// A factor can also be defined in the context of a specific model | ||
////// A factor can also be defined in the context of a specific model | ||
|
||
|
||
// Define the BN | ||
BayesianNetwork model = new BayesianNetwork(); | ||
model.addVariables(2, 3); | ||
model.addParent(0, 1); | ||
// Define the BN | ||
BayesianNetwork model = new BayesianNetwork(); | ||
model.addVariables(2, 3); | ||
model.addParent(0, 1); | ||
|
||
// extract a domain with varaiables 0 and 1 in the BN | ||
Domain d2 = model.getDomain(0, 1); | ||
// extract a domain with varaiables 0 and 1 in the BN | ||
Domain d2 = model.getDomain(0, 1); | ||
|
||
// Create an empty factor with that domain | ||
f = new BayesianFactor(model.getDomain(0, 1)); | ||
// Create an empty factor with that domain | ||
f = new BayesianDefaultFactor(model.getDomain(0, 1), null); | ||
|
||
|
||
} | ||
} | ||
} | ||
//53 | ||
|
Oops, something went wrong.