use of edu.cmu.tetrad.search.DagToPag in project tetrad by cmu-phil.
the class PerformanceTestsDan method testIdaOutputForDan.
private void testIdaOutputForDan() {
int numRuns = 100;
for (int run = 0; run < numRuns; run++) {
double alphaGFci = 0.01;
double alphaPc = 0.01;
int penaltyDiscount = 1;
int depth = 3;
int maxPathLength = 3;
final int numVars = 15;
final double edgesPerNode = 1.0;
final int numCases = 1000;
// final int numLatents = RandomUtil.getInstance().nextInt(3) + 1;
final int numLatents = 6;
// writeToFile = false;
PrintStream out1;
PrintStream out2;
PrintStream out3;
PrintStream out4;
PrintStream out5;
PrintStream out6;
PrintStream out7;
PrintStream out8;
PrintStream out9;
PrintStream out10;
PrintStream out11;
PrintStream out12;
File dir0 = new File("gfci.output");
dir0.mkdirs();
File dir = new File(dir0, "" + (run + 1));
dir.mkdir();
try {
out1 = new PrintStream(new File(dir, "hyperparameters.txt"));
out2 = new PrintStream(new File(dir, "variables.txt"));
out3 = new PrintStream(new File(dir, "dag.long.txt"));
out4 = new PrintStream(new File(dir, "dag.matrix.txt"));
out5 = new PrintStream(new File(dir, "coef.matrix.txt"));
out6 = new PrintStream(new File(dir, "pag.long.txt"));
out7 = new PrintStream(new File(dir, "pag.matrix.txt"));
out8 = new PrintStream(new File(dir, "pattern.long.txt"));
out9 = new PrintStream(new File(dir, "pattern.matrix.txt"));
out10 = new PrintStream(new File(dir, "data.txt"));
out11 = new PrintStream(new File(dir, "true.pag.long.txt"));
out12 = new PrintStream(new File(dir, "true.pag.matrix.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
out1.println("Num _vars = " + numVars);
out1.println("Num edges = " + (int) (numVars * edgesPerNode));
out1.println("Num cases = " + numCases);
out1.println("Alpha for PC = " + alphaPc);
out1.println("Alpha for FFCI = " + alphaGFci);
out1.println("Penalty discount = " + penaltyDiscount);
out1.println("Depth = " + depth);
out1.println("Maximum reachable path length for dsep search and discriminating undirectedPaths = " + maxPathLength);
List<Node> vars = new ArrayList<>();
for (int i = 0; i < numVars; i++) vars.add(new GraphNode("X" + (i + 1)));
// Graph dag = DataGraphUtils.randomDagQuick2(varsWithLatents, 0, (int) (varsWithLatents.size() * edgesPerNode));
Graph dag = GraphUtils.randomGraph(vars, 0, (int) (vars.size() * edgesPerNode), 5, 5, 5, false);
GraphUtils.fixLatents1(numLatents, dag);
// List<Node> varsWithLatents = new ArrayList<Node>();
//
// Graph dag = getLatentGraph(_vars, varsWithLatents, edgesPerNode, numLatents);
out3.println(dag);
printDanMatrix(vars, dag, out4);
SemPm pm = new SemPm(dag);
SemIm im = new SemIm(pm);
NumberFormat nf = new DecimalFormat("0.0000");
for (int i = 0; i < vars.size(); i++) {
for (Node var : vars) {
if (im.existsEdgeCoef(var, vars.get(i))) {
double coef = im.getEdgeCoef(var, vars.get(i));
out5.print(nf.format(coef) + "\t");
} else {
out5.print(nf.format(0) + "\t");
}
}
out5.println();
}
out5.println();
String vars_temp = vars.toString();
vars_temp = vars_temp.replace("[", "");
vars_temp = vars_temp.replace("]", "");
vars_temp = vars_temp.replace("X", "");
out2.println(vars_temp);
List<Node> _vars = new ArrayList<>();
for (Node node : vars) {
if (node.getNodeType() == NodeType.MEASURED) {
_vars.add(node);
}
}
String _vars_temp = _vars.toString();
_vars_temp = _vars_temp.replace("[", "");
_vars_temp = _vars_temp.replace("]", "");
_vars_temp = _vars_temp.replace("X", "");
out2.println(_vars_temp);
DataSet fullData = im.simulateData(numCases, false);
DataSet data = DataUtils.restrictToMeasured(fullData);
ICovarianceMatrix cov = new CovarianceMatrix(data);
final IndTestFisherZ independenceTestGFci = new IndTestFisherZ(cov, alphaGFci);
final edu.cmu.tetrad.search.SemBicScore scoreGfci = new edu.cmu.tetrad.search.SemBicScore(cov);
out6.println("GFCI.PAG_of_the_true_DAG");
GFci gFci = new GFci(independenceTestGFci, scoreGfci);
gFci.setVerbose(false);
gFci.setMaxDegree(depth);
gFci.setMaxPathLength(maxPathLength);
// gFci.setPossibleDsepSearchDone(true);
gFci.setCompleteRuleSetUsed(true);
Graph pag = gFci.search();
out6.println(pag);
printDanMatrix(_vars, pag, out7);
out8.println("Pattern_of_the_true_DAG OVER MEASURED VARIABLES");
final IndTestFisherZ independencePc = new IndTestFisherZ(cov, alphaPc);
Pc pc = new Pc(independencePc);
pc.setVerbose(false);
pc.setDepth(depth);
Graph pattern = pc.search();
out8.println(pattern);
printDanMatrix(_vars, pattern, out9);
out10.println(data);
out11.println("True PAG_of_the_true_DAG");
final Graph truePag = new DagToPag(dag).convert();
out11.println(truePag);
printDanMatrix(_vars, truePag, out12);
out1.close();
out2.close();
out3.close();
out4.close();
out5.close();
out6.close();
out7.close();
out8.close();
out9.close();
out10.close();
out11.close();
out12.close();
}
}
use of edu.cmu.tetrad.search.DagToPag in project tetrad by cmu-phil.
the class TimeoutComparison method doRun.
private void doRun(List<AlgorithmSimulationWrapper> algorithmSimulationWrappers, List<AlgorithmWrapper> algorithmWrappers, List<SimulationWrapper> simulationWrappers, Statistics statistics, int numGraphTypes, double[][][][] allStats, Run run) {
System.out.println();
System.out.println("Run " + (run.getRunIndex() + 1));
System.out.println();
AlgorithmSimulationWrapper algorithmSimulationWrapper = algorithmSimulationWrappers.get(run.getAlgSimIndex());
AlgorithmWrapper algorithmWrapper = algorithmSimulationWrapper.getAlgorithmWrapper();
SimulationWrapper simulationWrapper = algorithmSimulationWrapper.getSimulationWrapper();
DataModel data = simulationWrapper.getDataModel(run.getRunIndex());
Graph trueGraph = simulationWrapper.getTrueGraph(run.getRunIndex());
System.out.println((run.getAlgSimIndex() + 1) + ". " + algorithmWrapper.getDescription() + " simulationWrapper: " + simulationWrapper.getDescription());
long start = System.currentTimeMillis();
Graph out;
try {
Algorithm algorithm = algorithmWrapper.getAlgorithm();
Simulation simulation = simulationWrapper.getSimulation();
if (algorithm instanceof HasKnowledge && simulation instanceof HasKnowledge) {
((HasKnowledge) algorithm).setKnowledge(((HasKnowledge) simulation).getKnowledge());
}
if (algorithmWrapper.getAlgorithm() instanceof ExternalAlgorithm) {
ExternalAlgorithm external = (ExternalAlgorithm) algorithmWrapper.getAlgorithm();
external.setSimulation(simulationWrapper.getSimulation());
external.setPath(resultsPath);
external.setSimIndex(simulationWrappers.indexOf(simulationWrapper));
}
if (algorithm instanceof MultiDataSetAlgorithm) {
List<Integer> indices = new ArrayList<>();
int numDataModels = simulationWrapper.getSimulation().getNumDataModels();
for (int i = 0; i < numDataModels; i++) {
indices.add(i);
}
Collections.shuffle(indices);
List<DataModel> dataModels = new ArrayList<>();
int randomSelectionSize = algorithmWrapper.getAlgorithmSpecificParameters().getInt("randomSelectionSize");
for (int i = 0; i < Math.min(numDataModels, randomSelectionSize); i++) {
dataModels.add(simulationWrapper.getSimulation().getDataModel(indices.get(i)));
}
Parameters _params = algorithmWrapper.getAlgorithmSpecificParameters();
out = ((MultiDataSetAlgorithm) algorithm).search(dataModels, _params);
} else {
DataModel dataModel = copyData ? data.copy() : data;
Parameters _params = algorithmWrapper.getAlgorithmSpecificParameters();
out = algorithm.search(dataModel, _params);
}
} catch (Exception e) {
System.out.println("Could not run " + algorithmWrapper.getDescription());
e.printStackTrace();
return;
}
int simIndex = simulationWrappers.indexOf(simulationWrapper) + 1;
int algIndex = algorithmWrappers.indexOf(algorithmWrapper) + 1;
long stop = System.currentTimeMillis();
long elapsed = stop - start;
saveGraph(resultsPath, out, run.getRunIndex(), simIndex, algIndex, algorithmWrapper, elapsed);
if (trueGraph != null) {
out = GraphUtils.replaceNodes(out, trueGraph.getNodes());
}
if (algorithmWrapper.getAlgorithm() instanceof ExternalAlgorithm) {
ExternalAlgorithm extAlg = (ExternalAlgorithm) algorithmWrapper.getAlgorithm();
extAlg.setSimIndex(simulationWrappers.indexOf(simulationWrapper));
extAlg.setSimulation(simulationWrapper.getSimulation());
extAlg.setPath(resultsPath);
elapsed = extAlg.getElapsedTime(data, simulationWrapper.getSimulationSpecificParameters());
}
Graph[] est = new Graph[numGraphTypes];
Graph comparisonGraph;
if (this.comparisonGraph == ComparisonGraph.true_DAG) {
comparisonGraph = new EdgeListGraph(trueGraph);
} else if (this.comparisonGraph == ComparisonGraph.Pattern_of_the_true_DAG) {
comparisonGraph = SearchGraphUtils.patternForDag(new EdgeListGraph(trueGraph));
} else if (this.comparisonGraph == ComparisonGraph.PAG_of_the_true_DAG) {
comparisonGraph = new DagToPag(new EdgeListGraph(trueGraph)).convert();
} else {
throw new IllegalArgumentException("Unrecognized graph type.");
}
// Graph comparisonGraph = trueGraph == null ? null : algorithmSimulationWrapper.getComparisonGraph(trueGraph);
est[0] = out;
graphTypeUsed[0] = true;
if (data.isMixed()) {
est[1] = getSubgraph(out, true, true, data);
est[2] = getSubgraph(out, true, false, data);
est[3] = getSubgraph(out, false, false, data);
graphTypeUsed[1] = true;
graphTypeUsed[2] = true;
graphTypeUsed[3] = true;
}
Graph[] truth = new Graph[numGraphTypes];
truth[0] = comparisonGraph;
if (data.isMixed() && comparisonGraph != null) {
truth[1] = getSubgraph(comparisonGraph, true, true, data);
truth[2] = getSubgraph(comparisonGraph, true, false, data);
truth[3] = getSubgraph(comparisonGraph, false, false, data);
}
if (comparisonGraph != null) {
for (int u = 0; u < numGraphTypes; u++) {
if (!graphTypeUsed[u]) {
continue;
}
int statIndex = -1;
for (Statistic _stat : statistics.getStatistics()) {
statIndex++;
if (_stat instanceof ParameterColumn) {
continue;
}
double stat;
if (_stat instanceof ElapsedTime) {
stat = elapsed / 1000.0;
} else {
stat = _stat.getValue(truth[u], est[u]);
}
allStats[u][run.getAlgSimIndex()][statIndex][run.getRunIndex()] = stat;
}
}
}
}
use of edu.cmu.tetrad.search.DagToPag in project tetrad by cmu-phil.
the class Comparison method doRun.
private void doRun(List<AlgorithmSimulationWrapper> algorithmSimulationWrappers, List<AlgorithmWrapper> algorithmWrappers, List<SimulationWrapper> simulationWrappers, Statistics statistics, int numGraphTypes, double[][][][] allStats, Run run) {
System.out.println();
System.out.println("Run " + (run.getRunIndex() + 1));
System.out.println();
AlgorithmSimulationWrapper algorithmSimulationWrapper = algorithmSimulationWrappers.get(run.getAlgSimIndex());
AlgorithmWrapper algorithmWrapper = algorithmSimulationWrapper.getAlgorithmWrapper();
SimulationWrapper simulationWrapper = algorithmSimulationWrapper.getSimulationWrapper();
DataModel data = simulationWrapper.getDataModel(run.getRunIndex());
Graph trueGraph = simulationWrapper.getTrueGraph(run.getRunIndex());
System.out.println((run.getAlgSimIndex() + 1) + ". " + algorithmWrapper.getDescription() + " simulationWrapper: " + simulationWrapper.getDescription());
long start = System.currentTimeMillis();
Graph out;
try {
Algorithm algorithm = algorithmWrapper.getAlgorithm();
Simulation simulation = simulationWrapper.getSimulation();
if (algorithm instanceof HasKnowledge && simulation instanceof HasKnowledge) {
((HasKnowledge) algorithm).setKnowledge(((HasKnowledge) simulation).getKnowledge());
}
if (algorithmWrapper.getAlgorithm() instanceof ExternalAlgorithm) {
ExternalAlgorithm external = (ExternalAlgorithm) algorithmWrapper.getAlgorithm();
external.setSimulation(simulationWrapper.getSimulation());
external.setPath(resultsPath);
external.setSimIndex(simulationWrappers.indexOf(simulationWrapper));
}
if (algorithm instanceof MultiDataSetAlgorithm) {
List<Integer> indices = new ArrayList<>();
int numDataModels = simulationWrapper.getSimulation().getNumDataModels();
for (int i = 0; i < numDataModels; i++) indices.add(i);
Collections.shuffle(indices);
List<DataModel> dataModels = new ArrayList<>();
int randomSelectionSize = algorithmWrapper.getAlgorithmSpecificParameters().getInt("randomSelectionSize");
for (int i = 0; i < Math.min(numDataModels, randomSelectionSize); i++) {
dataModels.add(simulationWrapper.getSimulation().getDataModel(indices.get(i)));
}
Parameters _params = algorithmWrapper.getAlgorithmSpecificParameters();
out = ((MultiDataSetAlgorithm) algorithm).search(dataModels, _params);
} else {
DataModel dataModel = copyData ? data.copy() : data;
Parameters _params = algorithmWrapper.getAlgorithmSpecificParameters();
out = algorithm.search(dataModel, _params);
}
} catch (Exception e) {
System.out.println("Could not run " + algorithmWrapper.getDescription());
e.printStackTrace();
return;
}
int simIndex = simulationWrappers.indexOf(simulationWrapper) + 1;
int algIndex = algorithmWrappers.indexOf(algorithmWrapper) + 1;
long stop = System.currentTimeMillis();
long elapsed = stop - start;
saveGraph(resultsPath, out, run.getRunIndex(), simIndex, algIndex, algorithmWrapper, elapsed);
if (trueGraph != null) {
out = GraphUtils.replaceNodes(out, trueGraph.getNodes());
}
if (algorithmWrapper.getAlgorithm() instanceof ExternalAlgorithm) {
ExternalAlgorithm extAlg = (ExternalAlgorithm) algorithmWrapper.getAlgorithm();
extAlg.setSimIndex(simulationWrappers.indexOf(simulationWrapper));
extAlg.setSimulation(simulationWrapper.getSimulation());
extAlg.setPath(resultsPath);
elapsed = extAlg.getElapsedTime(data, simulationWrapper.getSimulationSpecificParameters());
}
Graph[] est = new Graph[numGraphTypes];
Graph comparisonGraph;
if (this.comparisonGraph == ComparisonGraph.true_DAG) {
comparisonGraph = new EdgeListGraph(trueGraph);
} else if (this.comparisonGraph == ComparisonGraph.Pattern_of_the_true_DAG) {
comparisonGraph = SearchGraphUtils.patternForDag(new EdgeListGraph(trueGraph));
} else if (this.comparisonGraph == ComparisonGraph.PAG_of_the_true_DAG) {
comparisonGraph = new DagToPag(new EdgeListGraph(trueGraph)).convert();
} else {
throw new IllegalArgumentException("Unrecognized graph type.");
}
// Graph comparisonGraph = trueGraph == null ? null : algorithmSimulationWrapper.getComparisonGraph(trueGraph);
est[0] = out;
graphTypeUsed[0] = true;
if (data.isMixed()) {
est[1] = getSubgraph(out, true, true, data);
est[2] = getSubgraph(out, true, false, data);
est[3] = getSubgraph(out, false, false, data);
graphTypeUsed[1] = true;
graphTypeUsed[2] = true;
graphTypeUsed[3] = true;
}
Graph[] truth = new Graph[numGraphTypes];
truth[0] = comparisonGraph;
if (data.isMixed() && comparisonGraph != null) {
truth[1] = getSubgraph(comparisonGraph, true, true, data);
truth[2] = getSubgraph(comparisonGraph, true, false, data);
truth[3] = getSubgraph(comparisonGraph, false, false, data);
}
if (comparisonGraph != null) {
for (int u = 0; u < numGraphTypes; u++) {
if (!graphTypeUsed[u])
continue;
int statIndex = -1;
for (Statistic _stat : statistics.getStatistics()) {
statIndex++;
if (_stat instanceof ParameterColumn)
continue;
double stat;
if (_stat instanceof ElapsedTime) {
stat = elapsed / 1000.0;
} else {
stat = _stat.getValue(truth[u], est[u]);
}
allStats[u][run.getAlgSimIndex()][statIndex][run.getRunIndex()] = stat;
}
}
}
}
use of edu.cmu.tetrad.search.DagToPag 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.search.DagToPag 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);
}
Aggregations