Search in sources :

Example 46 with SemIm

use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.

the class TimeSeriesSemSimulation method createData.

@Override
public void createData(Parameters parameters) {
    dataSets = new ArrayList<>();
    graphs = new ArrayList<>();
    Graph graph = randomGraph.createGraph(parameters);
    graph = TimeSeriesUtils.graphToLagGraph(graph, parameters.getInt("numLags"));
    topToBottomLayout((TimeLagGraph) graph);
    this.knowledge = TimeSeriesUtils.getKnowledge(graph);
    for (int i = 0; i < parameters.getInt("numRuns"); i++) {
        if (parameters.getBoolean("differentGraphs") && i > 0) {
            graph = randomGraph.createGraph(parameters);
            graph = TimeSeriesUtils.graphToLagGraph(graph, 2);
            topToBottomLayout((TimeLagGraph) graph);
        }
        graphs.add(graph);
        SemPm pm = new SemPm(graph);
        SemIm im = new SemIm(pm, parameters);
        final int sampleSize = parameters.getInt("sampleSize");
        boolean saveLatentVars = parameters.getBoolean("saveLatentVars");
        DataSet dataSet = im.simulateData(sampleSize, saveLatentVars);
        int numLags = ((TimeLagGraph) graph).getMaxLag();
        dataSet = TimeSeriesUtils.createLagData(dataSet, numLags);
        dataSet.setName("" + (i + 1));
        dataSet.setKnowledge(knowledge.copy());
        dataSets.add(dataSet);
    // LargeScaleSimulation sim = new LargeScaleSimulation(graph);
    // if (parameters.getDouble("coefHigh") > 0.80) {
    // System.out.println("Coefficients have been set (perhaps by default) too " +
    // "high for stationary time series.");
    // System.out.println("Setting coefficient range to [0.20,0.60].");
    // sim.setCoefRange(0.20, 0.60);
    // } else sim.setCoefRange(parameters.getDouble("coefLow"), parameters.getDouble("coefHigh"));
    // boolean isStableTetradMatrix;
    // int attempt = 1;
    // int tierSize = parameters.getInt("numMeasures") + parameters.getInt("numLatents"); //params.getNumVars();
    // int[] sub = new int[tierSize];
    // int[] sub2 = new int[tierSize];
    // for (int j = 0; j < tierSize; j++) {
    // sub[j] = j;
    // sub2[j] = tierSize + j;
    // }
    // DataSet dataSet;
    // do {
    // dataSet = sim.simulateDataFisher(parameters.getInt("sampleSize")); //params.getSampleSize());
    // 
    // TetradMatrix coefMat = new TetradMatrix(sim.getCoefficientMatrix());
    // TetradMatrix B = coefMat.getSelection(sub, sub);
    // TetradMatrix Gamma1 = coefMat.getSelection(sub2, sub);
    // TetradMatrix Gamma0 = TetradMatrix.identity(tierSize).minus(B);
    // TetradMatrix A1 = Gamma0.inverse().times(Gamma1);
    // 
    // isStableTetradMatrix = TimeSeriesUtils.allEigenvaluesAreSmallerThanOneInModulus(A1);
    // attempt++;
    // } while ((!isStableTetradMatrix) && attempt <= 5);
    // if (!isStableTetradMatrix) {
    // System.out.println("%%%%%%%%%% WARNING %%%%%%%% not a stable coefficient matrix, forcing coefs to [0.15,0.3]");
    // System.out.println("Made " + (attempt - 1) + " attempts to get stable matrix.");
    // sim.setCoefRange(0.15, 0.3);
    // dataSet = sim.simulateDataFisher(parameters.getInt("sampleSize"));//params.getSampleSize());
    // } //else System.out.println("Coefficient matrix is stable.");
    // dataSet.setName("" + (i + 1));
    // dataSet.setKnowledge(knowledge.copy());
    // dataSets.add(dataSet);
    }
}
Also used : TimeLagGraph(edu.cmu.tetrad.graph.TimeLagGraph) Graph(edu.cmu.tetrad.graph.Graph) RandomGraph(edu.cmu.tetrad.algcomparison.graph.RandomGraph) SingleGraph(edu.cmu.tetrad.algcomparison.graph.SingleGraph) SemPm(edu.cmu.tetrad.sem.SemPm) TimeLagGraph(edu.cmu.tetrad.graph.TimeLagGraph) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 47 with SemIm

use of edu.cmu.tetrad.sem.SemIm 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();
    }
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) DecimalFormat(java.text.DecimalFormat) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Pc(edu.cmu.tetrad.search.Pc) SemPm(edu.cmu.tetrad.sem.SemPm) PrintStream(java.io.PrintStream) IndTestFisherZ(edu.cmu.tetrad.search.IndTestFisherZ) GFci(edu.cmu.tetrad.search.GFci) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) DagToPag(edu.cmu.tetrad.search.DagToPag) File(java.io.File) SemIm(edu.cmu.tetrad.sem.SemIm) NumberFormat(java.text.NumberFormat)

Example 48 with SemIm

use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.

the class EdgeWeightComparison method getDisplayString.

public String getDisplayString() {
    String displayString = "";
    SemIm ref = reference;
    TetradMatrix referenceMatrix = ref.getEdgeCoef();
    TetradMatrix targetMatrix = target.getEdgeCoef();
    if (targetMatrix.columns() != referenceMatrix.columns() || targetMatrix.rows() != referenceMatrix.rows())
        return "The SEM IM's you selected don't have the same number of variables!  No comparison is possible here.";
    double score = 0;
    for (int i = 0; i < ref.getEdgeCoef().rows(); i++) {
        for (int j = 0; j < ref.getEdgeCoef().columns(); j++) {
            score += (targetMatrix.get(i, j) - referenceMatrix.get(i, j)) * (targetMatrix.get(i, j) - referenceMatrix.get(i, j));
        }
    }
    displayString += "Scheines Score: " + score + "\n\n";
    displayString += "(Calculated by summing the squared differences\n of each corresponding edge weight.)";
    return displayString;
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 49 with SemIm

use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.

the class SemEstimatorEditor method compileReport.

private String compileReport() {
    StringBuilder builder = new StringBuilder();
    builder.append("Datset\tFrom\tTo\tType\tValue\tSE\tT\tP");
    java.util.List<SemEstimator> estimators = wrapper.getMultipleResultList();
    for (int i = 0; i < estimators.size(); i++) {
        SemEstimator estimator = estimators.get(i);
        SemIm estSem = estimator.getEstimatedSem();
        String dataName = estimator.getDataSet().getName();
        estSem.getFreeParameters().forEach(parameter -> {
            builder.append("\n");
            builder.append(dataName).append("\t");
            builder.append(parameter.getNodeA()).append("\t");
            builder.append(parameter.getNodeB()).append("\t");
            builder.append(typeString(parameter)).append("\t");
            builder.append(asString(paramValue(estSem, parameter))).append("\t");
            /**
             * Maximum number of free parameters for which statistics will
             * be calculated. (Calculating standard errors is high
             * complexity.) Set this to zero to turn off statistics
             * calculations (which can be problematic sometimes).
             */
            int maxFreeParamsForStatistics = 200;
            builder.append(asString(estSem.getStandardError(parameter, maxFreeParamsForStatistics))).append("\t");
            builder.append(asString(estSem.getTValue(parameter, maxFreeParamsForStatistics))).append("\t");
            builder.append(asString(estSem.getPValue(parameter, maxFreeParamsForStatistics))).append("\t");
        });
        List<Node> nodes = estSem.getVariableNodes();
        for (int j = 0; j < nodes.size(); j++) {
            Node node = nodes.get(j);
            int n = estSem.getSampleSize();
            int df = n - 1;
            double mean = estSem.getMean(node);
            double stdDev = estSem.getMeanStdDev(node);
            double stdErr = stdDev / Math.sqrt(n);
            double tValue = mean / stdErr;
            double p = 2.0 * (1.0 - ProbUtils.tCdf(Math.abs(tValue), df));
            builder.append("\n");
            builder.append(dataName).append("\t");
            builder.append(nodes.get(j)).append("\t");
            builder.append(nodes.get(j)).append("\t");
            builder.append("Mean").append("\t");
            builder.append(asString(mean)).append("\t");
            builder.append(asString(stdErr)).append("\t");
            builder.append(asString(tValue)).append("\t");
            builder.append(asString(p)).append("\t");
        }
    }
    return builder.toString();
}
Also used : Node(edu.cmu.tetrad.graph.Node) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 50 with SemIm

use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.

the class Washdown method gof.

/**
 * @return the p value of the given model.
 */
private double gof(List<List<Node>> clusters) {
    clusters = removeEmpty(clusters);
    Graph graph = pureMeasurementModel(clusters);
    SemPm pm = new SemPm(graph);
    SemEstimator estimator;
    if (cov != null) {
        estimator = new SemEstimator(cov, pm);
    } else {
        estimator = new SemEstimator(dataSet, pm);
    }
    SemIm est = estimator.estimate();
    return est.getBicScore();
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm)

Aggregations

SemIm (edu.cmu.tetrad.sem.SemIm)81 SemPm (edu.cmu.tetrad.sem.SemPm)71 Test (org.junit.Test)46 DataSet (edu.cmu.tetrad.data.DataSet)28 ArrayList (java.util.ArrayList)28 Graph (edu.cmu.tetrad.graph.Graph)26 Node (edu.cmu.tetrad.graph.Node)19 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)16 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)16 SemEstimator (edu.cmu.tetrad.sem.SemEstimator)15 Dag (edu.cmu.tetrad.graph.Dag)10 DMSearch (edu.cmu.tetrad.search.DMSearch)9 StandardizedSemIm (edu.cmu.tetrad.sem.StandardizedSemIm)9 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)7 NumberFormat (java.text.NumberFormat)7 GraphNode (edu.cmu.tetrad.graph.GraphNode)5 IndependenceTest (edu.cmu.tetrad.search.IndependenceTest)4 DecimalFormat (java.text.DecimalFormat)4 List (java.util.List)4 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)3