use of edu.cmu.tetrad.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class PerformanceTests method testFges.
public void testFges(int numVars, double edgeFactor, int numCases, double penaltyDiscount) {
init(new File("long.fges." + numVars + "." + edgeFactor + "." + penaltyDiscount + ".txt"), "Tests performance of the FGES algorithm");
long time1 = System.currentTimeMillis();
Graph dag = makeDag(numVars, edgeFactor);
System.out.println("Graph done");
out.println("Graph done");
System.out.println("Starting simulation");
LargeScaleSimulation simulator = new LargeScaleSimulation(dag);
simulator.setOut(out);
DataSet data = simulator.simulateDataFisher(numCases);
System.out.println("Finishing simulation");
long time2 = System.currentTimeMillis();
out.println("Elapsed (simulating the data): " + (time2 - time1) + " ms");
System.out.println("Making covariance matrix");
ICovarianceMatrix cov = new CovarianceMatrixOnTheFly(data);
System.out.println("Covariance matrix done");
long time3 = System.currentTimeMillis();
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms");
SemBicScore semBicScore = new SemBicScore(cov);
semBicScore.setPenaltyDiscount(penaltyDiscount);
Fges pcStable = new Fges(semBicScore);
Graph estPattern = pcStable.search();
// out.println(estPattern);
long time4 = System.currentTimeMillis();
// out.println("# Vars = " + numVars);
// out.println("# Edges = " + (int) (numVars * edgeFactor));
out.println("# Cases = " + numCases);
out.println("penalty discount = " + penaltyDiscount);
out.println("Elapsed (simulating the data): " + (time2 - time1) + " ms");
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms");
out.println("Elapsed (running FGES) " + (time4 - time3) + " ms");
out.println("Total elapsed (cov + FGES) " + (time4 - time2) + " ms");
final Graph truePattern = SearchGraphUtils.patternForDag(dag);
System.out.println("# edges in true pattern = " + truePattern.getNumEdges());
System.out.println("# edges in est pattern = " + estPattern.getNumEdges());
SearchGraphUtils.graphComparison(estPattern, truePattern, out);
out.println("seed = " + RandomUtil.getInstance().getSeed() + "L");
out.close();
}
use of edu.cmu.tetrad.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class PerformanceTests method testFges.
private void testFges(int numVars, double edgeFactor, int numCases, int numRuns, boolean continuous) {
out.println(new Date());
// RandomUtil.getInstance().setSeed(4828384343999L);
double penaltyDiscount = 4.0;
int maxIndegree = 5;
boolean faithfulness = true;
// RandomUtil.getInstance().setSeed(50304050454L);
List<int[][]> allCounts = new ArrayList<>();
List<double[]> comparisons = new ArrayList<>();
List<Double> degrees = new ArrayList<>();
List<Long> elapsedTimes = new ArrayList<>();
if (continuous) {
init(new File("fges.comparison.continuous" + numVars + "." + (int) (edgeFactor * numVars) + "." + numCases + "." + numRuns + ".txt"), "Num runs = " + numRuns);
out.println("Num vars = " + numVars);
out.println("Num edges = " + (int) (numVars * edgeFactor));
out.println("Num cases = " + numCases);
out.println("Penalty discount = " + penaltyDiscount);
out.println("Depth = " + maxIndegree);
out.println();
} else {
init(new File("fges.comparison.discrete" + numVars + "." + (int) (edgeFactor * numVars) + "." + numCases + "." + numRuns + ".txt"), "Num runs = " + numRuns);
out.println("Num vars = " + numVars);
out.println("Num edges = " + (int) (numVars * edgeFactor));
out.println("Num cases = " + numCases);
out.println("Sample prior = " + 1);
out.println("Structure prior = " + 1);
out.println("Depth = " + 1);
out.println();
}
for (int run = 0; run < numRuns; run++) {
out.println("\n\n\n******************************** RUN " + (run + 1) + " ********************************\n\n");
System.out.println("Making dag");
out.println(new Date());
Graph dag = makeDag(numVars, edgeFactor);
System.out.println(new Date());
System.out.println("Calculating pattern for DAG");
Graph pattern = SearchGraphUtils.patternForDag(dag);
List<Node> vars = dag.getNodes();
int[] tiers = new int[vars.size()];
for (int i = 0; i < vars.size(); i++) {
tiers[i] = i;
}
System.out.println("Graph done");
long time1 = System.currentTimeMillis();
out.println("Graph done");
System.out.println(new Date());
System.out.println("Starting simulation");
Graph estPattern;
long elapsed;
if (continuous) {
LargeScaleSimulation simulator = new LargeScaleSimulation(dag, vars, tiers);
simulator.setVerbose(false);
simulator.setOut(out);
DataSet data = simulator.simulateDataFisher(numCases);
System.out.println("Finishing simulation");
System.out.println(new Date());
long time2 = System.currentTimeMillis();
out.println("Elapsed (simulating the data): " + (time2 - time1) + " ms");
System.out.println(new Date());
System.out.println("Making covariance matrix");
long time3 = System.currentTimeMillis();
ICovarianceMatrix cov = new CovarianceMatrixOnTheFly(data, true);
System.out.println("Covariance matrix done");
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms\n");
SemBicScore score = new SemBicScore(cov);
score.setPenaltyDiscount(penaltyDiscount);
System.out.println(new Date());
System.out.println("\nStarting FGES");
long timea = System.currentTimeMillis();
Fges fges = new Fges(score);
// fges.setVerbose(false);
fges.setNumPatternsToStore(0);
fges.setOut(System.out);
fges.setFaithfulnessAssumed(faithfulness);
fges.setCycleBound(-1);
long timeb = System.currentTimeMillis();
estPattern = fges.search();
long timec = System.currentTimeMillis();
out.println("Time for FGES constructor " + (timeb - timea) + " ms");
out.println("Time for FGES search " + (timec - timea) + " ms");
out.println();
out.flush();
elapsed = timec - timea;
} else {
BayesPm pm = new BayesPm(dag, 3, 3);
MlBayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(numCases, false, tiers);
System.out.println("Finishing simulation");
long time2 = System.currentTimeMillis();
out.println("Elapsed (simulating the data): " + (time2 - time1) + " ms");
long time3 = System.currentTimeMillis();
BDeuScore score = new BDeuScore(data);
score.setStructurePrior(1);
score.setSamplePrior(1);
System.out.println(new Date());
System.out.println("\nStarting FGES");
long timea = System.currentTimeMillis();
Fges fges = new Fges(score);
// fges.setVerbose(false);
fges.setNumPatternsToStore(0);
fges.setOut(System.out);
fges.setFaithfulnessAssumed(faithfulness);
fges.setCycleBound(-1);
long timeb = System.currentTimeMillis();
estPattern = fges.search();
long timec = System.currentTimeMillis();
out.println("Time consructing BDeu score " + (timea - time3) + " ms");
out.println("Time for FGES constructor " + (timeb - timea) + " ms");
out.println("Time for FGES search " + (timec - timea) + " ms");
out.println();
elapsed = timec - timea;
}
System.out.println("Done with FGES");
System.out.println(new Date());
// System.out.println("Replacing nodes");d
//
// estPattern = GraphUtils.replaceNodes(estPattern, dag.getNodes());
// System.out.println("Calculating degree");
//
// double degree = GraphUtils.degree(estPattern);
// degrees.add(degree);
//
// out.println("Degree out output graph = " + degree);
double[] comparison = new double[4];
// int adjFn = GraphUtils.countAdjErrors(pattern, estPattern);
// int adjFp = GraphUtils.countAdjErrors(estPattern, pattern);
// int trueAdj = pattern.getNumEdges();
//
// comparison[0] = trueAdj / (double) (trueAdj + adjFp);
// comparison[1] = trueAdj / (double) (trueAdj + adjFn);
System.out.println("Counting misclassifications.");
estPattern = GraphUtils.replaceNodes(estPattern, pattern.getNodes());
int[][] counts = GraphUtils.edgeMisclassificationCounts(pattern, estPattern, false);
allCounts.add(counts);
System.out.println(new Date());
int sumRow = counts[4][0] + counts[4][3] + counts[4][5];
int sumCol = counts[0][3] + counts[4][3] + counts[5][3] + counts[7][3];
int trueArrow = counts[4][3];
int sumTrueAdjacencies = 0;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 5; j++) {
sumTrueAdjacencies += counts[i][j];
}
}
int falsePositiveAdjacencies = 0;
for (int j = 0; j < 5; j++) {
falsePositiveAdjacencies += counts[7][j];
}
int falseNegativeAdjacencies = 0;
for (int i = 0; i < 5; i++) {
falseNegativeAdjacencies += counts[i][5];
}
comparison[0] = sumTrueAdjacencies / (double) (sumTrueAdjacencies + falsePositiveAdjacencies);
comparison[1] = sumTrueAdjacencies / (double) (sumTrueAdjacencies + falseNegativeAdjacencies);
comparison[2] = trueArrow / (double) sumCol;
comparison[3] = trueArrow / (double) sumRow;
comparisons.add(comparison);
out.println(GraphUtils.edgeMisclassifications(counts));
out.println(precisionRecall(comparison));
elapsedTimes.add(elapsed);
out.println("\nElapsed: " + elapsed + " ms");
}
printAverageConfusion("Average", allCounts);
printAveragePrecisionRecall(comparisons);
printAverageStatistics(elapsedTimes, degrees);
out.close();
}
use of edu.cmu.tetrad.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method testFGESc.
@Test
public void testFGESc() {
int penaltyDiscount = 2;
boolean faithfulnessAssumed = false;
int maxDegree = -1;
int numVars = 20;
int edgesPerNode = 2;
int numLatentConfounders = 0;
int numCases = 50;
int numBootstrapSamples = 5;
boolean verbose = true;
Graph dag = makeContinuousDAG(numVars, numLatentConfounders, edgesPerNode);
System.out.println("Truth Graph:");
System.out.println(dag.toString());
int[] causalOrdering = new int[numVars];
for (int i = 0; i < numVars; i++) {
causalOrdering[i] = i;
}
LargeScaleSimulation simulator = new LargeScaleSimulation(dag, dag.getNodes(), causalOrdering);
DataSet data = simulator.simulateDataFisher(numCases);
Parameters parameters = new Parameters();
parameters.set("penaltyDiscount", penaltyDiscount);
parameters.set("faithfulnessAssumed", faithfulnessAssumed);
parameters.set("maxDegree", maxDegree);
parameters.set("numPatternsToStore", 0);
parameters.set("verbose", verbose);
ScoreWrapper score = new SemBicScore();
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.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method testFCIc.
@Test
public void testFCIc() {
int penaltyDiscount = 2;
int depth = 3;
int maxPathLength = -1;
int numVars = 20;
int edgesPerNode = 2;
int numLatentConfounders = 2;
int numCases = 50;
int numBootstrapSamples = 5;
boolean verbose = true;
Graph dag = makeContinuousDAG(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());
int[] causalOrdering = new int[numVars];
for (int i = 0; i < numVars; i++) {
causalOrdering[i] = i;
}
LargeScaleSimulation simulator = new LargeScaleSimulation(dag, dag.getNodes(), causalOrdering);
DataSet data = simulator.simulateDataFisher(numCases);
Parameters parameters = new Parameters();
parameters.set("penaltyDiscount", penaltyDiscount);
parameters.set("depth", depth);
parameters.set("maxPathLength", maxPathLength);
parameters.set("numPatternsToStore", 0);
parameters.set("verbose", verbose);
IndependenceWrapper test = new FisherZ();
Fci algorithm = new Fci(test);
GeneralBootstrapTest bootstrapTest = new GeneralBootstrapTest(data, algorithm, numBootstrapSamples);
bootstrapTest.setVerbose(verbose);
bootstrapTest.setParameters(parameters);
bootstrapTest.setEdgeEnsemble(BootstrapEdgeEnsemble.Preserved);
// bootstrapTest.setParallelMode(false);
Graph resultGraph = bootstrapTest.search();
System.out.println("Estimated 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.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method testGFCIc.
@Test
public void testGFCIc() {
int penaltyDiscount = 2;
boolean faithfulnessAssumed = false;
int maxDegree = -1;
int numVars = 20;
int edgesPerNode = 2;
int numLatentConfounders = 2;
int numCases = 50;
int numBootstrapSamples = 5;
boolean verbose = true;
Graph dag = makeContinuousDAG(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());
int[] causalOrdering = new int[numVars];
for (int i = 0; i < numVars; i++) {
causalOrdering[i] = i;
}
LargeScaleSimulation simulator = new LargeScaleSimulation(dag, dag.getNodes(), causalOrdering);
DataSet data = simulator.simulateDataFisher(numCases);
Parameters parameters = new Parameters();
parameters.set("penaltyDiscount", penaltyDiscount);
parameters.set("faithfulnessAssumed", faithfulnessAssumed);
parameters.set("maxDegree", maxDegree);
parameters.set("numPatternsToStore", 0);
parameters.set("verbose", verbose);
ScoreWrapper score = new SemBicScore();
IndependenceWrapper test = new FisherZ();
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 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);
}
Aggregations