use of edu.cmu.tetrad.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class PerformanceTests method testFci.
public void testFci(int numVars, double edgeFactor, int numCases) {
double alpha = 0.001;
int depth = 3;
init(new File("long.fci." + numVars + ".txt"), "Tests performance of the FCI algorithm");
long time1 = System.currentTimeMillis();
System.out.println("Making list of vars");
List<Node> vars = new ArrayList<>();
for (int i = 0; i < numVars; i++) {
vars.add(new ContinuousVariable("X" + i));
}
System.out.println("Finishing list of vars");
System.out.println("Making graph");
Graph graph = GraphUtils.randomGraphRandomForwardEdges(vars, 0, (int) (numVars * edgeFactor), 30, 15, 15, false, true);
System.out.println("Graph done");
out.println("Graph done");
System.out.println("Starting simulation");
LargeScaleSimulation simulator = new LargeScaleSimulation(graph);
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 CovarianceMatrix2(data);
ICovarianceMatrix cov = new CovarianceMatrixOnTheFly(data);
// ICovarianceMatrix cov = new CorreqlationMatrix(new CovarianceMatrix2(data));
// ICovarianceMatrix cov = new CovarianceMatrixOnTheFly(data, false);
// ICovarianceMatrix cov = DataUtils.covarianceParanormalDrton(data);
// ICovarianceMatrix cov = new CovarianceMatrix(DataUtils.covarianceParanormalWasserman(data));
// System.out.println(cov);
System.out.println("Covariance matrix done");
long time3 = System.currentTimeMillis();
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms");
// out.println(cov);
IndTestFisherZ independenceTest = new IndTestFisherZ(cov, alpha);
Fci fci = new Fci(independenceTest);
fci.setVerbose(false);
fci.setDepth(depth);
fci.setMaxPathLength(2);
// fci.setTrueDag(truePag);
Graph outGraph = fci.search();
out.println(outGraph);
long time4 = System.currentTimeMillis();
out.println("# Vars = " + numVars);
out.println("# Edges = " + (int) (numVars * edgeFactor));
out.println("# Cases = " + numCases);
out.println("Elapsed (simulating the data): " + (time2 - time1) + " ms");
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms");
out.println("Elapsed (running FCI) " + (time4 - time3) + " ms");
out.close();
}
use of edu.cmu.tetrad.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class PerformanceTests method testGfci.
public void testGfci(int numVars, double edgeFactor) {
System.out.println("Seed = " + RandomUtil.getInstance().getSeed());
// RandomUtil.getInstance().setSeed(1460491316813L);
double alpha = .1;
int depth = -1;
double penaltyDiscount = 4.0;
int maxPathLength = -1;
double coefLow = .3;
double coefHigh = 1.5;
int numLatentConfounders = 50;
int numCases = 1000;
init(new File("long.gfci." + numVars + ".txt"), "Tests performance of the FCI-GES algorithm");
long time1 = System.currentTimeMillis();
System.out.println("Making list of vars");
List<Node> vars = new ArrayList<>();
for (int i = 0; i < numVars; i++) {
vars.add(new ContinuousVariable("X" + i));
}
System.out.println("Finishing list of vars");
System.out.println("Making graph");
System.out.println("Finishing list of vars");
Graph dag = GraphUtils.randomGraphRandomForwardEdges(vars, numLatentConfounders, (int) (numVars * edgeFactor), 10, 10, 10, false, false);
System.out.println("Graph done");
out.println("Graph done");
System.out.println("Starting simulation");
LargeScaleSimulation simulator = new LargeScaleSimulation(dag);
simulator.setCoefRange(coefLow, coefHigh);
DataSet data = simulator.simulateDataFisher(numCases);
data = DataUtils.restrictToMeasured(data);
System.out.println("Finishing simulation");
System.out.println("Num measured vars = " + data.getNumColumns());
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");
IndependenceTest independenceTest = new IndTestFisherZ(cov, alpha);
// GFci fci = new GFci(independenceTest);
SemBicScore score = new SemBicScore(cov);
score.setPenaltyDiscount(penaltyDiscount);
GFci fci = new GFci(independenceTest, score);
fci.setVerbose(false);
fci.setMaxPathLength(maxPathLength);
fci.setMaxDegree(depth);
fci.setFaithfulnessAssumed(false);
fci.setCompleteRuleSetUsed(true);
Graph outGraph = fci.search();
out.println(outGraph);
System.out.println(MisclassificationUtils.edgeMisclassifications(outGraph, new DagToPag(dag).convert()));
long time4 = System.currentTimeMillis();
out.println("# Vars = " + numVars);
out.println("# Edges = " + (int) (numVars * edgeFactor));
out.println("# Cases = " + numCases);
out.println("Elapsed (simulating the data): " + (time2 - time1) + " ms");
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms");
out.println("Elapsed (running FCI) " + (time4 - time3) + " ms");
out.close();
}
use of edu.cmu.tetrad.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class PerformanceTests method testCpcStable.
public void testCpcStable(int numVars, double edgeFactor, int numCases, double alpha) {
int depth = 3;
init(new File("long.cpcstable." + numVars + ".txt"), "Tests performance of the CPC algorithm");
long time1 = System.currentTimeMillis();
System.out.println("Making list of vars");
System.out.println("Finishing list of vars");
System.out.println("Making graph");
// Graph graph = DataGraphUtils.randomGraphRandomForwardEdges(vars, 0, (int) (numVars * edgeFactor));
// Graph graph = DataGraphUtils.randomGraphUniform(vars, 0, (int) (numVars * edgeFactor), 5, 5, 5, false);
Graph graph = makeDag(numVars, edgeFactor);
System.out.println("Graph done");
out.println("Graph done");
System.out.println("Starting simulation");
LargeScaleSimulation simulator = new LargeScaleSimulation(graph);
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 CovarianceMatrix2(data);
ICovarianceMatrix cov = new CovarianceMatrixOnTheFly(data);
// ICovarianceMatrix cov = new CorrelationMatrix(new CovarianceMatrix2(data));
// ICovarianceMatrix cov = new CovarianceMatrixOnTheFly(data, false);
// ICovarianceMatrix cov = DataUtils.covarianceParanormalDrton(data);
// ICovarianceMatrix cov = new CovarianceMatrix(DataUtils.covarianceParanormalWasserman(data));
// System.out.println(cov);
System.out.println("Covariance matrix done");
long time3 = System.currentTimeMillis();
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms");
// out.println(cov);
IndTestFisherZ test = new IndTestFisherZ(cov, alpha);
CpcStable cpcStable = new CpcStable(test);
cpcStable.setVerbose(false);
cpcStable.setDepth(depth);
cpcStable.setOut(out);
Graph outGraph = cpcStable.search();
// out.println(outGraph);
long time4 = System.currentTimeMillis();
// out.println("# Vars = " + numVars);
// out.println("# Edges = " + (int) (numVars * edgeFactor));
out.println("# Cases = " + numCases);
out.println("Elapsed (simulating the data): " + (time2 - time1) + " ms");
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms");
out.println("Elapsed (running CPC-Stable) " + (time4 - time3) + " ms");
out.println("Total elapsed (cov + CPC-Stable) " + (time4 - time2) + " ms");
final Graph truePattern = SearchGraphUtils.patternForDag(graph);
SearchGraphUtils.graphComparison(outGraph, truePattern, out);
out.println("# ambiguous triples = " + outGraph.getAmbiguousTriples().size());
out.close();
}
use of edu.cmu.tetrad.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class PerformanceTests method testFgesMb.
private void testFgesMb(int numVars, double edgeFactor, int numCases, int numRuns, boolean continuous) {
double penaltyDiscount = 4.0;
int structurePrior = 10;
int samplePrior = 10;
int maxIndegree = -1;
// boolean faithfulness = false;
List<int[][]> allCounts = new ArrayList<>();
List<double[]> comparisons = new ArrayList<>();
List<Double> degrees = new ArrayList<>();
List<Long> elapsedTimes = new ArrayList<>();
System.out.println("Making dag");
Graph dag = makeDag(numVars, edgeFactor);
System.out.println(new Date());
System.out.println("Calculating pattern for DAG");
Graph pattern = SearchGraphUtils.patternForDag(dag);
int[] tiers = new int[dag.getNumNodes()];
for (int i = 0; i < dag.getNumNodes(); 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;
FgesMb fges;
List<Node> vars;
if (continuous) {
init(new File("FgesMb.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();
out.println(new Date());
vars = dag.getNodes();
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-MB");
fges = new FgesMb(score);
fges.setVerbose(false);
fges.setNumPatternsToStore(0);
fges.setOut(System.out);
// fges.setHeuristicSpeedup(faithfulness);
fges.setMaxIndegree(maxIndegree);
fges.setCycleBound(-1);
} else {
init(new File("FgesMb.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 = " + samplePrior);
out.println("Structure prior = " + structurePrior);
out.println("Depth = " + maxIndegree);
out.println();
out.println(new Date());
BayesPm pm = new BayesPm(dag, 3, 3);
MlBayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(numCases, false, tiers);
vars = data.getVariables();
pattern = GraphUtils.replaceNodes(pattern, vars);
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(structurePrior);
score.setSamplePrior(samplePrior);
System.out.println(new Date());
System.out.println("\nStarting FGES");
long time4 = System.currentTimeMillis();
fges = new FgesMb(score);
fges.setVerbose(false);
fges.setNumPatternsToStore(0);
fges.setOut(System.out);
// fges.setHeuristicSpeedup(faithfulness);
fges.setMaxIndegree(maxIndegree);
fges.setCycleBound(-1);
long timeb = System.currentTimeMillis();
out.println("Time consructing BDeu score " + (time4 - time3) + " ms");
out.println("Time for FGES-MB constructor " + (timeb - time4) + " ms");
out.println();
}
int numSkipped = 0;
for (int run = 0; run < numRuns; run++) {
out.println("\n\n\n******************************** RUN " + (run + 1) + " ********************************\n\n");
Node target = vars.get(RandomUtil.getInstance().nextInt(vars.size()));
System.out.println("Target = " + target);
long timea = System.currentTimeMillis();
estPattern = fges.search(target);
long timed = System.currentTimeMillis();
elapsed = timed - timea;
Set<Node> mb = new HashSet<>();
mb.add(target);
mb.addAll(pattern.getAdjacentNodes(target));
for (Node child : pattern.getChildren(target)) {
mb.addAll(pattern.getParents(child));
}
Graph trueMbGraph = pattern.subgraph(new ArrayList<>(mb));
long timec = System.currentTimeMillis();
out.println("Time for FGES-MB search " + (timec - timea) + " ms");
out.println();
System.out.println("Done with FGES");
System.out.println(new Date());
double[] comparison = new double[4];
System.out.println("Counting misclassifications.");
int[][] counts = GraphUtils.edgeMisclassificationCounts(trueMbGraph, 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;
// if (Double.isNaN(comparison[0]) || Double.isNaN(comparison[1]) || Double.isNaN(comparison[2]) ||
// Double.isNaN(comparison[3])) {
// run--;
// numSkipped++;
// continue;
// }
comparisons.add(comparison);
out.println(GraphUtils.edgeMisclassifications(counts));
out.println(precisionRecall(comparison));
// printAverageConfusion("Average", allCounts);
elapsedTimes.add(elapsed);
out.println("\nElapsed: " + elapsed + " ms");
}
printAverageConfusion("Average", allCounts, new DecimalFormat("0.0"));
printAveragePrecisionRecall(comparisons);
out.println("Number of runs skipped because of undefined accuracies: " + numSkipped);
printAverageStatistics(elapsedTimes, degrees);
out.close();
}
use of edu.cmu.tetrad.sem.LargeScaleSimulation in project tetrad by cmu-phil.
the class PerformanceTests method testPcStableMax.
public void testPcStableMax(int numVars, double edgeFactor, int numCases, double alpha) {
int depth = -1;
init(new File("long.pcstablemax." + numVars + "." + edgeFactor + "." + alpha + ".txt"), "Tests performance of the PC Max 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 CovarianceMatrix(data);
ICovarianceMatrix cov = new CovarianceMatrixOnTheFly(data);
// ICovarianceMatrix cov = new CorrelationMatrix(new CovarianceMatrix(data));
// ICovarianceMatrix cov = DataUtils.covarianceParanormalDrton(data);
// ICovarianceMatrix cov = new CovarianceMatrix(DataUtils.covarianceParanormalWasserman(data));
// System.out.println(cov);
System.out.println("Covariance matrix done");
long time3 = System.currentTimeMillis();
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms");
// out.println(cov);
IndTestFisherZ test = new IndTestFisherZ(cov, alpha);
PcStableMax pcStable = new PcStableMax(test);
// pcStable.setVerbose(false);
// pcStable.setDepth(depth);
// pcStable.setOut(out);
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("alpha = " + alpha);
out.println("depth = " + depth);
out.println("Elapsed (simulating the data): " + (time2 - time1) + " ms");
out.println("Elapsed (calculating cov): " + (time3 - time2) + " ms");
out.println("Elapsed (running PC-Max) " + (time4 - time3) + " ms");
out.println("Total elapsed (cov + PC-Max) " + (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();
}
Aggregations