Search in sources :

Example 16 with Algorithm

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);
}
Also used : Simulations(edu.cmu.tetrad.algcomparison.simulation.Simulations) ExternalAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.ExternalAlgorithm) Algorithm(edu.cmu.tetrad.algcomparison.algorithm.Algorithm) MultiDataSetAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.MultiDataSetAlgorithm) ExternalAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.ExternalAlgorithm) LoadDataAndGraphs(edu.cmu.tetrad.algcomparison.simulation.LoadDataAndGraphs)

Example 17 with Algorithm

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);
}
Also used : Simulations(edu.cmu.tetrad.algcomparison.simulation.Simulations) ExternalAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.ExternalAlgorithm) Algorithm(edu.cmu.tetrad.algcomparison.algorithm.Algorithm) MultiDataSetAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.MultiDataSetAlgorithm) ExternalAlgorithm(edu.cmu.tetrad.algcomparison.algorithm.ExternalAlgorithm) LoadDataAndGraphs(edu.cmu.tetrad.algcomparison.simulation.LoadDataAndGraphs)

Example 18 with Algorithm

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;
}
Also used : SingleGraphAlg(edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.SingleGraphAlg) TakesInitialGraph(edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph) Algorithm(edu.cmu.tetrad.algcomparison.algorithm.Algorithm)

Example 19 with 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);
}
Also used : LinearFisherModel(edu.cmu.tetrad.algcomparison.simulation.LinearFisherModel) ScoreWrapper(edu.cmu.tetrad.algcomparison.score.ScoreWrapper) RandomForward(edu.cmu.tetrad.algcomparison.graph.RandomForward) Algorithm(edu.cmu.tetrad.algcomparison.algorithm.Algorithm) Fges(edu.cmu.tetrad.search.Fges) RandomGraph(edu.cmu.tetrad.algcomparison.graph.RandomGraph) IndependenceWrapper(edu.cmu.tetrad.algcomparison.independence.IndependenceWrapper) RandomGraph(edu.cmu.tetrad.algcomparison.graph.RandomGraph) FisherZ(edu.cmu.tetrad.algcomparison.independence.FisherZ) SemSimulation(edu.cmu.tetrad.algcomparison.simulation.SemSimulation) Simulation(edu.cmu.tetrad.algcomparison.simulation.Simulation) SemBicScore(edu.cmu.tetrad.search.SemBicScore) SemBicDTest(edu.cmu.tetrad.algcomparison.independence.SemBicDTest) SemBicTest(edu.cmu.tetrad.algcomparison.independence.SemBicTest) Test(org.junit.Test)

Example 20 with Algorithm

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));
}
Also used : SemBicTest(edu.cmu.tetrad.algcomparison.independence.SemBicTest) LinearFisherModel(edu.cmu.tetrad.algcomparison.simulation.LinearFisherModel) RandomForward(edu.cmu.tetrad.algcomparison.graph.RandomForward) Algorithm(edu.cmu.tetrad.algcomparison.algorithm.Algorithm) RandomGraph(edu.cmu.tetrad.algcomparison.graph.RandomGraph) IndependenceWrapper(edu.cmu.tetrad.algcomparison.independence.IndependenceWrapper) RandomGraph(edu.cmu.tetrad.algcomparison.graph.RandomGraph)

Aggregations

Algorithm (edu.cmu.tetrad.algcomparison.algorithm.Algorithm)25 MultiDataSetAlgorithm (edu.cmu.tetrad.algcomparison.algorithm.MultiDataSetAlgorithm)13 ExternalAlgorithm (edu.cmu.tetrad.algcomparison.algorithm.ExternalAlgorithm)12 Parameters (edu.cmu.tetrad.util.Parameters)10 Simulation (edu.cmu.tetrad.algcomparison.simulation.Simulation)9 ScoreWrapper (edu.cmu.tetrad.algcomparison.score.ScoreWrapper)8 Statistic (edu.cmu.tetrad.algcomparison.statistic.Statistic)8 Graph (edu.cmu.tetrad.graph.Graph)8 IndependenceWrapper (edu.cmu.tetrad.algcomparison.independence.IndependenceWrapper)7 DataSet (edu.cmu.tetrad.data.DataSet)7 Test (org.junit.Test)6 Simulations (edu.cmu.tetrad.algcomparison.simulation.Simulations)5 DagToPag (edu.cmu.tetrad.search.DagToPag)5 GeneralBootstrapTest (edu.pitt.dbmi.algo.bootstrap.GeneralBootstrapTest)5 Fges (edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.Fges)4 RandomForward (edu.cmu.tetrad.algcomparison.graph.RandomForward)4 BdeuScore (edu.cmu.tetrad.algcomparison.score.BdeuScore)4 SemBicScore (edu.cmu.tetrad.algcomparison.score.SemBicScore)4 LinearFisherModel (edu.cmu.tetrad.algcomparison.simulation.LinearFisherModel)4 LoadDataAndGraphs (edu.cmu.tetrad.algcomparison.simulation.LoadDataAndGraphs)4