use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class TestFges method explore2.
@Test
public void explore2() {
RandomUtil.getInstance().setSeed(1457220623122L);
int numVars = 20;
double edgeFactor = 1.0;
int numCases = 1000;
double structurePrior = 1;
double samplePrior = 1;
List<Node> vars = new ArrayList<>();
for (int i = 0; i < numVars; i++) {
vars.add(new ContinuousVariable("X" + i));
}
Graph dag = GraphUtils.randomGraphRandomForwardEdges(vars, 0, (int) (numVars * edgeFactor), 30, 15, 15, false, true);
// printDegreeDistribution(dag, out);
BayesPm pm = new BayesPm(dag, 2, 3);
BayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(numCases, false);
// out.println("Finishing simulation");
BDeScore score = new BDeScore(data);
score.setSamplePrior(samplePrior);
score.setStructurePrior(structurePrior);
Fges ges = new Fges(score);
ges.setVerbose(false);
ges.setNumPatternsToStore(0);
ges.setFaithfulnessAssumed(false);
Graph estPattern = ges.search();
final Graph truePattern = SearchGraphUtils.patternForDag(dag);
int[][] counts = SearchGraphUtils.graphComparison(estPattern, truePattern, null);
int[][] expectedCounts = { { 2, 0, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 2, 0, 0, 13, 0, 3 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } };
// for (int i = 0; i < counts.length; i++) {
// assertTrue(Arrays.equals(counts[i], expectedCounts[i]));
// }
// System.out.println(MatrixUtils.toString(expectedCounts));
// System.out.println(MatrixUtils.toString(counts));
// System.out.println(RandomUtil.getInstance().getSeed());
}
use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method testFCId.
@Test
public void testFCId() {
double structurePrior = 1, samplePrior = 1;
int depth = -1;
int maxPathLength = -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("depth", depth);
parameters.set("maxPathLength", maxPathLength);
parameters.set("numPatternsToStore", 0);
parameters.set("verbose", verbose);
IndependenceWrapper test = new ChiSquare();
Algorithm algorithm = new Fci(test);
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.BayesIm 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.BayesIm 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.BayesIm 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));
}
Aggregations