use of edu.cmu.tetrad.algcomparison.algorithm.Algorithm in project tetrad by cmu-phil.
the class Comparison method generateReportFromExternalAlgorithms.
public void generateReportFromExternalAlgorithms(String dataPath, String resultsPath, String outputFileName, Algorithms algorithms, Statistics statistics, Parameters parameters) {
this.saveGraphs = false;
this.dataPath = dataPath;
this.resultsPath = resultsPath;
for (Algorithm algorithm : algorithms.getAlgorithms()) {
if (!(algorithm instanceof ExternalAlgorithm))
throw new IllegalArgumentException("Expecting all algorithms to implement ExternalAlgorithm.");
}
Simulations simulations = new Simulations();
File file = new File(this.dataPath, "save");
File[] dirs = file.listFiles();
if (dirs == null) {
throw new NullPointerException("No files in " + file.getAbsolutePath());
}
this.dirs = new ArrayList<String>();
int count = 0;
for (File dir : dirs) {
if (dir.getName().contains("DS_Store"))
continue;
count++;
}
for (int i = 1; i <= count; i++) {
File _dir = new File(dataPath, "save/" + i);
simulations.add(new LoadDataAndGraphs(_dir.getAbsolutePath()));
this.dirs.add(_dir.getAbsolutePath());
}
compareFromSimulations(this.resultsPath, simulations, outputFileName, algorithms, statistics, parameters);
}
use of edu.cmu.tetrad.algcomparison.algorithm.Algorithm in project tetrad by cmu-phil.
the class Comparison method compareFromFiles.
/**
* Compares algorithms.
*
* @param dataPath Path to the directory where data and graph files have been saved.
* @param resultsPath Path to the file where the results should be stored.
* @param algorithms The list of algorithms to be compared.
* @param statistics The list of statistics on which to compare the algorithm, and their utility weights.
* @param parameters The list of parameters and their values.
*/
public void compareFromFiles(String dataPath, String resultsPath, Algorithms algorithms, Statistics statistics, Parameters parameters) {
for (Algorithm algorithm : algorithms.getAlgorithms()) {
if (algorithm instanceof ExternalAlgorithm) {
throw new IllegalArgumentException("Not expecting any implementations of ExternalAlgorithm here.");
}
}
this.dataPath = dataPath;
this.resultsPath = resultsPath;
Simulations simulations = new Simulations();
File file = new File(this.dataPath, "save");
File[] dirs = file.listFiles();
if (dirs == null) {
throw new NullPointerException("No files in " + file.getAbsolutePath());
}
this.dirs = new ArrayList<String>();
int count = 0;
for (File dir : dirs) {
if (dir.getName().contains("DS_Store"))
continue;
count++;
}
for (int i = 1; i <= count; i++) {
File _dir = new File(dataPath, "save/" + i);
simulations.add(new LoadDataAndGraphs(_dir.getAbsolutePath()));
this.dirs.add(_dir.getAbsolutePath());
}
compareFromSimulations(this.resultsPath, simulations, algorithms, statistics, parameters);
}
use of edu.cmu.tetrad.algcomparison.algorithm.Algorithm in project tetrad by cmu-phil.
the class GeneralAlgorithmEditor method getAlgorithmFromInterface.
/**
* Initialize algorithm
*
* @param algoModel
* @param indTestModel
* @param scoreModel
* @return Algorithm
*/
public Algorithm getAlgorithmFromInterface(AlgorithmModel algoModel, IndependenceTestModel indTestModel, ScoreModel scoreModel) {
Class algoClass = algoModel.getAlgorithm().getClazz();
Class indTestClass = (indTestModel == null) ? null : indTestModel.getIndependenceTest().getClazz();
Class scoreClass = (scoreModel == null) ? null : scoreModel.getScore().getClazz();
Algorithm algorithm = null;
try {
algorithm = AlgorithmFactory.create(algoClass, indTestClass, scoreClass);
} catch (IllegalAccessException | InstantiationException exception) {
LOGGER.error("", exception);
}
// Those pairwise algos (R1, R2,..) require source graph to initialize - Zhou
if (algorithm != null && algorithm instanceof TakesInitialGraph && runner.getSourceGraph() != null && !runner.getDataModelList().isEmpty()) {
Algorithm initialGraph = new SingleGraphAlg(runner.getSourceGraph());
((TakesInitialGraph) algorithm).setInitialGraph(initialGraph);
}
return algorithm;
}
use of edu.cmu.tetrad.algcomparison.algorithm.Algorithm in project tetrad by cmu-phil.
the class TestFges method clarkTest.
@Test
public void clarkTest() {
RandomGraph randomGraph = new RandomForward();
Simulation simulation = new LinearFisherModel(randomGraph);
Parameters parameters = new Parameters();
parameters.set("numMeasures", 100);
parameters.set("numLatents", 0);
parameters.set("coefLow", 0.2);
parameters.set("coefHigh", 0.8);
parameters.set("avgDegree", 2);
parameters.set("maxDegree", 100);
parameters.set("maxIndegree", 100);
parameters.set("maxOutdegree", 100);
parameters.set("connected", false);
parameters.set("numRuns", 1);
parameters.set("differentGraphs", false);
parameters.set("sampleSize", 1000);
parameters.set("faithfulnessAssumed", false);
parameters.set("maxDegree", -1);
parameters.set("verbose", false);
parameters.set("alpha", 0.01);
simulation.createData(parameters);
DataSet dataSet = (DataSet) simulation.getDataModel(0);
Graph trueGraph = simulation.getTrueGraph(0);
// trueGraph = SearchGraphUtils.patternForDag(trueGraph);
ScoreWrapper score = new edu.cmu.tetrad.algcomparison.score.SemBicScore();
IndependenceWrapper test = new FisherZ();
Algorithm fges = new edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.Fges(score, false);
Graph fgesGraph = fges.search(dataSet, parameters);
clarkTestForAlpha(0.05, parameters, dataSet, trueGraph, fgesGraph, test);
clarkTestForAlpha(0.01, parameters, dataSet, trueGraph, fgesGraph, test);
}
use of edu.cmu.tetrad.algcomparison.algorithm.Algorithm in project tetrad by cmu-phil.
the class TestFges method test9.
public void test9() {
Parameters parameters = new Parameters();
parameters.set("numMeasures", 50);
parameters.set("numLatents", 0);
parameters.set("avgDegree", 2);
parameters.set("maxDegree", 20);
parameters.set("maxIndegree", 20);
parameters.set("maxOutdegree", 20);
parameters.set("connected", false);
parameters.set("coefLow", 0.2);
parameters.set("coefHigh", 0.9);
parameters.set("varLow", 1);
parameters.set("varHigh", 3);
parameters.set("verbose", false);
parameters.set("coefSymmetric", true);
parameters.set("numRuns", 1);
parameters.set("percentDiscrete", 0);
parameters.set("numCategories", 3);
parameters.set("differentGraphs", true);
parameters.set("sampleSize", 500);
parameters.set("intervalBetweenShocks", 10);
parameters.set("intervalBetweenRecordings", 10);
parameters.set("fisherEpsilon", 0.001);
parameters.set("randomizeColumns", true);
RandomGraph graph = new RandomForward();
LinearFisherModel sim = new LinearFisherModel(graph);
sim.createData(parameters);
Graph previous = null;
int prevDiff = Integer.MAX_VALUE;
// for (int l = 7; l >= 1; l--) {
for (int i = 2; i <= 20; i++) {
parameters.set("penaltyDiscount", i / (double) 10);
// parameters.set("alpha", Double.parseDouble("1E-" + l));
// ScoreWrapper score = new edu.cmu.tetrad.algcomparison.score.SemBicScore();
// Algorithm alg = new edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.Fges(score);
IndependenceWrapper test = new SemBicTest();
// IndependenceWrapper test = new FisherZ();
Algorithm alg = new edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.Cpc(test);
Graph out = alg.search(sim.getDataModel(0), parameters);
// Graph out = GraphUtils.undirectedGraph(alg.search(sim.getDataModel(0), parameters));
Set<Edge> edges1 = out.getEdges();
int numEdges = edges1.size();
if (previous != null) {
Set<Edge> edges2 = previous.getEdges();
edges2.removeAll(edges1);
int diff = edges2.size();
//
System.out.println("Penalty discount =" + parameters.getDouble("penaltyDiscount") + " # edges = " + numEdges + " # additional = " + diff);
previous = out;
if (diff > prevDiff)
break;
prevDiff = diff;
} else {
previous = out;
}
}
Graph estGraph = previous;
Graph trueGraph = sim.getTrueGraph(0);
estGraph = GraphUtils.replaceNodes(estGraph, trueGraph.getNodes());
Statistic ap = new AdjacencyPrecision();
Statistic ar = new AdjacencyRecall();
Statistic ahp = new ArrowheadPrecision();
Statistic ahr = new ArrowheadRecall();
System.out.println("AP = " + ap.getValue(trueGraph, estGraph));
System.out.println("AR = " + ar.getValue(trueGraph, estGraph));
System.out.println("AHP = " + ahp.getValue(trueGraph, estGraph));
System.out.println("AHR = " + ahr.getValue(trueGraph, estGraph));
}
Aggregations