Search in sources :

Example 51 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class TestMimbuild2 method test1.

@Test
public void test1() {
    RandomUtil.getInstance().setSeed(49283494L);
    for (int r = 0; r < 1; r++) {
        Graph mim = DataGraphUtils.randomSingleFactorModel(5, 5, 6, 0, 0, 0);
        Graph mimStructure = structure(mim);
        Parameters params = new Parameters();
        params.set("coefLow", .5);
        params.set("coefHigh", 1.5);
        SemPm pm = new SemPm(mim);
        SemIm im = new SemIm(pm, params);
        DataSet data = im.simulateData(300, false);
        String algorithm = "FOFC";
        Graph searchGraph;
        List<List<Node>> partition;
        if (algorithm.equals("FOFC")) {
            FindOneFactorClusters fofc = new FindOneFactorClusters(data, TestType.TETRAD_WISHART, FindOneFactorClusters.Algorithm.GAP, 0.001);
            searchGraph = fofc.search();
            partition = fofc.getClusters();
        } else if (algorithm.equals("BPC")) {
            TestType testType = TestType.TETRAD_WISHART;
            TestType purifyType = TestType.TETRAD_BASED;
            BuildPureClusters bpc = new BuildPureClusters(data, 0.001, testType, purifyType);
            searchGraph = bpc.search();
            partition = MimUtils.convertToClusters2(searchGraph);
        } else {
            throw new IllegalStateException();
        }
        List<String> latentVarList = reidentifyVariables(mim, data, partition, 2);
        // System.out.println(partition);
        // System.out.println(latentVarList);
        // 
        // System.out.println("True\n" + mimStructure);
        Graph mimbuildStructure;
        for (int mimbuildMethod : new int[] { 2 }) {
            if (mimbuildMethod == 2) {
                Mimbuild2 mimbuild = new Mimbuild2();
                mimbuild.setAlpha(0.001);
                mimbuild.setMinClusterSize(3);
                mimbuildStructure = mimbuild.search(partition, latentVarList, new CovarianceMatrix(data));
                int shd = SearchGraphUtils.structuralHammingDistance(mimStructure, mimbuildStructure);
                assertEquals(7, shd);
            } else if (mimbuildMethod == 3) {
                // System.out.println("Mimbuild Trek\n");
                MimbuildTrek mimbuild = new MimbuildTrek();
                mimbuild.setAlpha(0.1);
                mimbuild.setMinClusterSize(3);
                mimbuildStructure = mimbuild.search(partition, latentVarList, new CovarianceMatrix(data));
                // ICovarianceMatrix latentcov = mimbuild.getLatentsCov();
                // System.out.println("\nCovariance over the latents");
                // System.out.println(latentcov);
                // System.out.println("Estimated\n" + mimbuildStructure);
                int shd = SearchGraphUtils.structuralHammingDistance(mimStructure, mimbuildStructure);
                // System.out.println("SHD = " + shd);
                // System.out.println();
                assertEquals(3, shd);
            } else {
                throw new IllegalStateException();
            }
        }
    }
}
Also used : Mimbuild2(edu.cmu.tetrad.search.Mimbuild2) Parameters(edu.cmu.tetrad.util.Parameters) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Example 52 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class TsFciRunner method getIndependenceTest.

public IndependenceTest getIndependenceTest() {
    Object dataModel = getDataModel();
    if (dataModel == null) {
        dataModel = getSourceGraph();
    }
    Parameters params = getParams();
    IndTestType testType;
    if (getParams() instanceof Parameters) {
        Parameters _params = params;
        testType = (IndTestType) _params.get("indTestType", IndTestType.FISHER_Z);
    } else {
        Parameters _params = params;
        testType = (IndTestType) _params.get("indTestType", IndTestType.FISHER_Z);
    }
    return new IndTestChooser().getTest(dataModel, params, testType);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) IndTestType(edu.cmu.tetrad.search.IndTestType)

Example 53 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class TsFgesRunner method execute.

// ============================PUBLIC METHODS==========================//
/**
 * Executes the algorithm, producing (at least) a result workbench. Must be
 * implemented in the extending class.
 */
public void execute() {
    System.out.println("A");
    Object model = getDataModel();
    if (model == null && getSourceGraph() != null) {
        model = getSourceGraph();
    }
    if (model == null) {
        throw new RuntimeException("Data source is unspecified. You may need to double click all your data boxes, \n" + "then click Save, and then right click on them and select Propagate Downstream. \n" + "The issue is that we use a seed to simulate from IM's, so your data is not saved to \n" + "file when you save the session. It can, however, be recreated from the saved seed.");
    }
    Parameters params = getParams();
    if (model instanceof Graph) {
        GraphScore gesScore = new GraphScore((Graph) model);
        fges = new TsFges2(gesScore);
        fges.setKnowledge((IKnowledge) getParams().get("knowledge", new Knowledge2()));
        fges.setVerbose(true);
    } else {
        double penaltyDiscount = params.getDouble("penaltyDiscount", 4);
        if (model instanceof DataSet) {
            DataSet dataSet = (DataSet) model;
            if (dataSet.isContinuous()) {
                SemBicScore gesScore = new SemBicScore(new CovarianceMatrixOnTheFly((DataSet) model));
                // SemBicScore2 gesScore = new SemBicScore2(new CovarianceMatrixOnTheFly((DataSet) model));
                // SemGpScore gesScore = new SemGpScore(new CovarianceMatrixOnTheFly((DataSet) model));
                // SvrScore gesScore = new SvrScore((DataSet) model);
                gesScore.setPenaltyDiscount(penaltyDiscount);
                System.out.println("Score done");
                fges = new TsFges2(gesScore);
            } else if (dataSet.isDiscrete()) {
                double samplePrior = getParams().getDouble("samplePrior", 1);
                double structurePrior = getParams().getDouble("structurePrior", 1);
                BDeuScore score = new BDeuScore(dataSet);
                score.setSamplePrior(samplePrior);
                score.setStructurePrior(structurePrior);
                fges = new TsFges2(score);
            } else {
                MixedBicScore gesScore = new MixedBicScore(dataSet);
                gesScore.setPenaltyDiscount(penaltyDiscount);
                fges = new TsFges2(gesScore);
            }
        } else if (model instanceof ICovarianceMatrix) {
            SemBicScore gesScore = new SemBicScore((ICovarianceMatrix) model);
            gesScore.setPenaltyDiscount(penaltyDiscount);
            gesScore.setPenaltyDiscount(penaltyDiscount);
            fges = new TsFges2(gesScore);
        } else if (model instanceof DataModelList) {
            DataModelList list = (DataModelList) model;
            for (DataModel dataModel : list) {
                if (!(dataModel instanceof DataSet || dataModel instanceof ICovarianceMatrix)) {
                    throw new IllegalArgumentException("Need a combination of all continuous data sets or " + "covariance matrices, or else all discrete data sets, or else a single initialGraph.");
                }
            }
            if (list.size() != 1) {
                throw new IllegalArgumentException("FGES takes exactly one data set, covariance matrix, or initialGraph " + "as input. For multiple data sets as input, use IMaGES.");
            }
            if (allContinuous(list)) {
                double penalty = getParams().getDouble("penaltyDiscount", 4);
                if (params.getBoolean("firstNontriangular", false)) {
                    SemBicScoreImages fgesScore = new SemBicScoreImages(list);
                    fgesScore.setPenaltyDiscount(penalty);
                    fges = new TsFges2(fgesScore);
                } else {
                    SemBicScoreImages fgesScore = new SemBicScoreImages(list);
                    fgesScore.setPenaltyDiscount(penalty);
                    fges = new TsFges2(fgesScore);
                }
            } else if (allDiscrete(list)) {
                double structurePrior = getParams().getDouble("structurePrior", 1);
                double samplePrior = getParams().getDouble("samplePrior", 1);
                BdeuScoreImages fgesScore = new BdeuScoreImages(list);
                fgesScore.setSamplePrior(samplePrior);
                fgesScore.setStructurePrior(structurePrior);
                if (params.getBoolean("firstNontriangular", false)) {
                    fges = new TsFges2(fgesScore);
                } else {
                    fges = new TsFges2(fgesScore);
                }
            } else {
                throw new IllegalArgumentException("Data must be either all discrete or all continuous.");
            }
        } else {
            System.out.println("No viable input.");
        }
    }
    fges.setInitialGraph(initialGraph);
    fges.setKnowledge((IKnowledge) getParams().get("knowledge", new Knowledge2()));
    fges.setNumPatternsToStore(params.getInt("numPatternsToSave", 1));
    fges.setVerbose(true);
    // fges.setHeuristicSpeedup(((Parameters) params.getIndTestParams()).isFaithfulnessAssumed());
    // * there is no setHeuristicSpeedup option in Fges2 and so likewise TsFges2. *
    Graph graph = fges.search();
    if (getSourceGraph() != null) {
        GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
    } else if (((IKnowledge) getParams().get("knowledge", new Knowledge2())).isDefaultToKnowledgeLayout()) {
        SearchGraphUtils.arrangeByKnowledgeTiers(graph, (IKnowledge) getParams().get("knowledge", new Knowledge2()));
    } else {
        GraphUtils.circleLayout(graph, 200, 200, 150);
    }
    setResultGraph(graph);
    this.topGraphs = new ArrayList<>(fges.getTopGraphs());
    if (topGraphs.isEmpty()) {
        topGraphs.add(new ScoredGraph(getResultGraph(), Double.NaN));
    }
    setIndex(topGraphs.size() - 1);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) Graph(edu.cmu.tetrad.graph.Graph)

Example 54 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class TsFgesRunner method getParamSettings.

@Override
public Map<String, String> getParamSettings() {
    super.getParamSettings();
    Parameters params = getParams();
    paramSettings.put("Penalty Discount", new DecimalFormat("0.0").format(params.getDouble("penaltyDiscount", 4)));
    return paramSettings;
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) DecimalFormat(java.text.DecimalFormat)

Example 55 with Parameters

use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.

the class LofsRunner method applyLofs.

private Graph applyLofs(DataModelList dataSets, Graph pattern) {
    final Parameters params = getParams();
    List<DataSet> _dataSets = new ArrayList<>();
    for (DataModel dataModel : dataSets) {
        _dataSets.add((DataSet) dataModel);
    }
    Lofs2 lofs = new Lofs2(pattern, _dataSets);
    lofs.setAlpha(getParams().getDouble("alpha", 0.001));
    lofs.setRule((Lofs2.Rule) params.get("rule", Lofs2.Rule.R3));
    lofs.setOrientStrongerDirection(params.getBoolean("orientStrongerDirection", true));
    lofs.setEdgeCorrected(params.getBoolean("meanCenterResiduals", false));
    lofs.setR2Orient2Cycles(params.getBoolean("r2Orient2Cycles", false));
    lofs.setScore((Lofs.Score) params.get("score", Lofs.Score.andersonDarling));
    lofs.setEpsilon(params.getDouble("epsilon", .1));
    lofs.setZeta(params.getDouble("zeta", 1));
    lofs.setSelfLoopStrength(params.getDouble("selfLoopStrength", 0.0));
    lofs.setKnowledge((IKnowledge) params.get("knowledge", new Knowledge2()));
    return lofs.orient();
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) ArrayList(java.util.ArrayList)

Aggregations

Parameters (edu.cmu.tetrad.util.Parameters)134 Comparison (edu.cmu.tetrad.algcomparison.Comparison)30 Graph (edu.cmu.tetrad.graph.Graph)26 Algorithms (edu.cmu.tetrad.algcomparison.algorithm.Algorithms)25 DataSet (edu.cmu.tetrad.data.DataSet)22 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)20 IKnowledge (edu.cmu.tetrad.data.IKnowledge)18 Simulations (edu.cmu.tetrad.algcomparison.simulation.Simulations)17 RandomForward (edu.cmu.tetrad.algcomparison.graph.RandomForward)14 ArrayList (java.util.ArrayList)14 SemBicScore (edu.cmu.tetrad.algcomparison.score.SemBicScore)13 Test (org.junit.Test)13 Node (edu.cmu.tetrad.graph.Node)11 ActionEvent (java.awt.event.ActionEvent)10 ActionListener (java.awt.event.ActionListener)10 Algorithm (edu.cmu.tetrad.algcomparison.algorithm.Algorithm)8 Fges (edu.cmu.tetrad.algcomparison.algorithm.oracle.pattern.Fges)8 DataModel (edu.cmu.tetrad.data.DataModel)8 TitledBorder (javax.swing.border.TitledBorder)8 FisherZ (edu.cmu.tetrad.algcomparison.independence.FisherZ)7