use of edu.cmu.tetrad.bayes.MlBayesIm in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method testFGESd.
@Test
public void testFGESd() {
double structurePrior = 1, samplePrior = 1;
boolean faithfulnessAssumed = false;
int maxDegree = -1;
int numVars = 20;
int edgesPerNode = 2;
int numLatentConfounders = 0;
int numCases = 50;
int numBootstrapSamples = 5;
boolean verbose = true;
long seed = 123;
Graph dag = makeDiscreteDAG(numVars, numLatentConfounders, edgesPerNode);
System.out.println("Truth Graph:");
System.out.println(dag.toString());
BayesPm pm = new BayesPm(dag, 2, 3);
BayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(numCases, seed, false);
Parameters parameters = new Parameters();
parameters.set("structurePrior", structurePrior);
parameters.set("samplePrior", samplePrior);
parameters.set("faithfulnessAssumed", faithfulnessAssumed);
parameters.set("maxDegree", maxDegree);
parameters.set("numPatternsToStore", 0);
parameters.set("verbose", verbose);
ScoreWrapper score = new BdeuScore();
Algorithm algorithm = new Fges(score);
GeneralBootstrapTest bootstrapTest = new GeneralBootstrapTest(data, algorithm, numBootstrapSamples);
bootstrapTest.setVerbose(verbose);
bootstrapTest.setParameters(parameters);
bootstrapTest.setEdgeEnsemble(BootstrapEdgeEnsemble.Highest);
Graph resultGraph = bootstrapTest.search();
System.out.println("Estimated Graph:");
System.out.println(resultGraph.toString());
// Adjacency Confusion Matrix
int[][] adjAr = GeneralBootstrapTest.getAdjConfusionMatrix(dag, resultGraph);
printAdjConfusionMatrix(adjAr);
// Edge Type Confusion Matrix
int[][] edgeAr = GeneralBootstrapTest.getEdgeTypeConfusionMatrix(dag, resultGraph);
printEdgeTypeConfusionMatrix(edgeAr);
}
use of edu.cmu.tetrad.bayes.MlBayesIm in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method testGFCId.
@Test
public void testGFCId() {
double structurePrior = 1, samplePrior = 1;
boolean faithfulnessAssumed = false;
int maxDegree = -1;
int numVars = 20;
int edgesPerNode = 2;
int numLatentConfounders = 4;
int numCases = 50;
int numBootstrapSamples = 5;
boolean verbose = true;
long seed = 123;
Graph dag = makeDiscreteDAG(numVars, numLatentConfounders, edgesPerNode);
DagToPag dagToPag = new DagToPag(dag);
Graph truePag = dagToPag.convert();
System.out.println("Truth PAG_of_the_true_DAG Graph:");
System.out.println(truePag.toString());
BayesPm pm = new BayesPm(dag, 2, 3);
BayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(numCases, seed, false);
Parameters parameters = new Parameters();
parameters.set("structurePrior", structurePrior);
parameters.set("samplePrior", samplePrior);
parameters.set("faithfulnessAssumed", faithfulnessAssumed);
parameters.set("maxDegree", maxDegree);
parameters.set("numPatternsToStore", 0);
parameters.set("verbose", verbose);
ScoreWrapper score = new BdeuScore();
IndependenceWrapper test = new ChiSquare();
Algorithm algorithm = new Gfci(test, score);
GeneralBootstrapTest bootstrapTest = new GeneralBootstrapTest(data, algorithm, numBootstrapSamples);
bootstrapTest.setVerbose(verbose);
bootstrapTest.setParameters(parameters);
bootstrapTest.setEdgeEnsemble(BootstrapEdgeEnsemble.Highest);
Graph resultGraph = bootstrapTest.search();
System.out.println("Estimated Bootstrapped PAG_of_the_true_DAG Graph:");
System.out.println(resultGraph.toString());
// Adjacency Confusion Matrix
int[][] adjAr = GeneralBootstrapTest.getAdjConfusionMatrix(truePag, resultGraph);
printAdjConfusionMatrix(adjAr);
// Edge Type Confusion Matrix
int[][] edgeAr = GeneralBootstrapTest.getEdgeTypeConfusionMatrix(truePag, resultGraph);
printEdgeTypeConfusionMatrix(edgeAr);
}
use of edu.cmu.tetrad.bayes.MlBayesIm in project tetrad by cmu-phil.
the class TestProposition method testUpdate1.
/**
* Richard's 2-variable example worked by hand.
*/
@Test
public void testUpdate1() {
BayesIm bayesIm = sampleBayesIm2();
Proposition prop1 = Proposition.tautology(bayesIm);
prop1.removeCategory(0, 1);
prop1.setVariable(1, false);
Proposition prop2 = new Proposition(bayesIm, prop1);
assertEquals(prop1, prop2);
BayesIm bayesIm2 = new MlBayesIm(bayesIm);
Proposition prop3 = new Proposition(bayesIm2, prop1);
assertTrue(!prop3.equals(prop1));
}
use of edu.cmu.tetrad.bayes.MlBayesIm in project tetrad by cmu-phil.
the class TestHistogram method testHistogram.
@Test
public void testHistogram() {
RandomUtil.getInstance().setSeed(4829384L);
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
Dag trueGraph = new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false));
int sampleSize = 1000;
// Continuous
SemPm semPm = new SemPm(trueGraph);
SemIm semIm = new SemIm(semPm);
DataSet data = semIm.simulateData(sampleSize, false);
Histogram histogram = new Histogram(data);
histogram.setTarget("X1");
histogram.setNumBins(20);
assertEquals(3.76, histogram.getMax(), 0.01);
assertEquals(-3.83, histogram.getMin(), 0.01);
assertEquals(1000, histogram.getN());
histogram.setTarget("X1");
histogram.setNumBins(10);
histogram.addConditioningVariable("X3", 0, 1);
histogram.addConditioningVariable("X4", 0, 1);
histogram.removeConditioningVariable("X3");
assertEquals(3.76, histogram.getMax(), 0.01);
assertEquals(-3.83, histogram.getMin(), 0.01);
assertEquals(188, histogram.getN());
double[] arr = histogram.getContinuousData("X2");
histogram.addConditioningVariable("X2", StatUtils.min(arr), StatUtils.mean(arr));
// Discrete
BayesPm bayesPm = new BayesPm(trueGraph);
BayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
DataSet data2 = bayesIm.simulateData(sampleSize, false);
// For some reason these are giving different
// values when all of the unit tests are run are
// once. TODO They produce stable values when
// this particular test is run repeatedly.
Histogram histogram2 = new Histogram(data2);
histogram2.setTarget("X1");
int[] frequencies1 = histogram2.getFrequencies();
// assertEquals(928, frequencies1[0]);
// assertEquals(72, frequencies1[1]);
histogram2.setTarget("X1");
histogram2.addConditioningVariable("X2", 0);
histogram2.addConditioningVariable("X3", 1);
int[] frequencies = histogram2.getFrequencies();
// assertEquals(377, frequencies[0]);
// assertEquals(28, frequencies[1]);
}
use of edu.cmu.tetrad.bayes.MlBayesIm in project tetrad by cmu-phil.
the class TestBayesIm method testAddRemoveValues.
/**
* Tests whether the BayesIm does the right thing in a very simple case
* where values of a nodes are added or removed from the BayesPm. Start with
* graph a -> b <- c, construct and fill in probability tables in BayesIm.
* Then add edge c -> b "manually." This should create a table of values for
* c that is unspecified, and it should double up the rows from b. Then
* remove the node c. Now the table for b should be completely unspecified.
*/
@Test
public void testAddRemoveValues() {
Node a = new GraphNode("a");
Node b = new GraphNode("b");
Node c = new GraphNode("c");
Dag dag = new Dag();
dag.addNode(a);
dag.addNode(b);
dag.addNode(c);
dag.addDirectedEdge(a, b);
dag.addDirectedEdge(c, b);
assertTrue(Edges.isDirectedEdge(dag.getEdge(a, b)));
BayesPm bayesPm = new BayesPm(dag, 3, 3);
BayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
bayesPm.setNumCategories(a, 4);
bayesPm.setNumCategories(c, 4);
BayesIm bayesIm2 = new MlBayesIm(bayesPm, bayesIm, MlBayesIm.MANUAL);
bayesPm.setNumCategories(a, 2);
BayesIm bayesIm3 = new MlBayesIm(bayesPm, bayesIm2, MlBayesIm.MANUAL);
bayesPm.setNumCategories(b, 2);
BayesIm bayesIm4 = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
for (int node = 0; node < bayesIm4.getNumNodes(); node++) {
for (int row = 0; row < bayesIm4.getNumRows(node); row++) {
for (int col = 0; col < bayesIm4.getNumColumns(node); col++) {
bayesIm4.setProbability(node, row, col, Double.NaN);
}
}
}
double[][] aTable = { { .2, .8 } };
double[][] bTable = { { .1, .9 }, { .7, .3 }, { .3, .7 }, { .5, .5 }, { .09, .91 }, { .6, .4 }, { .2, .8 }, { .8, .2 } };
double[][] cTable = { { .1, .2, .3, .4 } };
int _a = bayesIm.getNodeIndex(a);
for (int row = 0; row < bayesIm4.getNumRows(_a); row++) {
for (int col = 0; col < bayesIm4.getNumColumns(_a); col++) {
bayesIm4.setProbability(_a, row, col, aTable[row][col]);
}
}
int _b = bayesIm.getNodeIndex(b);
for (int row = 0; row < bayesIm4.getNumRows(_b); row++) {
for (int col = 0; col < bayesIm4.getNumColumns(_b); col++) {
bayesIm4.setProbability(_b, row, col, bTable[row][col]);
}
}
int _c = bayesIm.getNodeIndex(c);
for (int row = 0; row < bayesIm4.getNumRows(_c); row++) {
for (int col = 0; col < bayesIm4.getNumColumns(_c); col++) {
bayesIm4.setProbability(_c, row, col, cTable[row][col]);
}
}
}
Aggregations