Search in sources :

Example 41 with SemPm

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

the class PcStableLocalSearchEditor method reportIfContinuous.

private String reportIfContinuous(Graph dag, DataSet dataSet) {
    SemPm semPm = new SemPm(dag);
    SemEstimator estimator = new SemEstimator(dataSet, semPm);
    estimator.estimate();
    SemIm semIm = estimator.getEstimatedSem();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    StringBuilder buf = new StringBuilder();
    buf.append("\nDegrees of Freedom = ").append(semPm.getDof()).append("Chi-Square = ").append(nf.format(semIm.getChiSquare())).append("\nP Value = ").append(nf.format(semIm.getPValue())).append("\nBIC Score = ").append(nf.format(semIm.getBicScore()));
    buf.append("\n\nThe above chi square test assumes that the maximum " + "likelihood function over the measured variables has been " + "maximized. Under that assumption, the null hypothesis for " + "the test is that the population covariance matrix over all " + "of the measured variables is equal to the estimated covariance " + "matrix over all of the measured variables written as a function " + "of the free model parameters--that is, the unfixed parameters " + "for each directed edge (the linear coefficient for that edge), " + "each exogenous variable (the variance for the error term for " + "that variable), and each bidirected edge (the covariance for " + "the exogenous variables it connects).  The model is explained " + "in Bollen, Structural Equations with Latent Variable, 110. ");
    return buf.toString();
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) NumberFormat(java.text.NumberFormat)

Example 42 with SemPm

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

the class PcGesSearchEditor method reportIfCovMatrix.

private String reportIfCovMatrix(Graph dag, ICovarianceMatrix dataSet) {
    SemPm semPm = new SemPm(dag);
    SemEstimator estimator = new SemEstimator(dataSet, semPm);
    estimator.estimate();
    SemIm semIm = estimator.getEstimatedSem();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    StringBuilder buf = new StringBuilder();
    buf.append("\nDegrees of Freedom = ").append(semPm.getDof()).append(" Chi-Square = ").append(nf.format(semIm.getChiSquare())).append("\nP Value = ").append(nf.format(semIm.getPValue())).append("\nBIC Score = ").append(nf.format(semIm.getBicScore()));
    buf.append("\n\nThe above chi square test assumes that the maximum " + "likelihood function over the measured variables has been " + "maximized. Under that assumption, the null hypothesis for " + "the test is that the population covariance matrix over all " + "of the measured variables is equal to the estimated covariance " + "matrix over all of the measured variables written as a function " + "of the free model parameters--that is, the unfixed parameters " + "for each directed edge (the linear coefficient for that edge), " + "each exogenous variable (the variance for the error term for " + "that variable), and each bidirected edge (the covariance for " + "the exogenous variables it connects).  The model is explained " + "in Bollen, Structural Equations with Latent Variable, 110. ");
    return buf.toString();
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) NumberFormat(java.text.NumberFormat)

Example 43 with SemPm

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

the class PcGesSearchEditor method reportIfContinuous.

private String reportIfContinuous(Graph dag, DataSet dataSet) {
    SemPm semPm = new SemPm(dag);
    SemEstimator estimator = new SemEstimator(dataSet, semPm);
    estimator.estimate();
    SemIm semIm = estimator.getEstimatedSem();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    StringBuilder buf = new StringBuilder();
    buf.append("\nDegrees of Freedom = ").append(semPm.getDof()).append(" Chi-Square = ").append(nf.format(semIm.getChiSquare())).append("\nP Value = ").append(nf.format(semIm.getPValue())).append("\nBIC Score = ").append(nf.format(semIm.getBicScore()));
    buf.append("\n\nThe above chi square test assumes that the maximum " + "likelihood function over the measured variables has been " + "maximized. Under that assumption, the null hypothesis for " + "the test is that the population covariance matrix over all " + "of the measured variables is equal to the estimated covariance " + "matrix over all of the measured variables written as a function " + "of the free model parameters--that is, the unfixed parameters " + "for each directed edge (the linear coefficient for that edge), " + "each exogenous variable (the variance for the error term for " + "that variable), and each bidirected edge (the covariance for " + "the exogenous variables it connects).  The model is explained " + "in Bollen, Structural Equations with Latent Variable, 110. ");
    return buf.toString();
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) NumberFormat(java.text.NumberFormat)

Example 44 with SemPm

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

the class SemThenDiscretize method simulate.

private DataSet simulate(Graph graph, Parameters parameters) {
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet continuousData = im.simulateData(parameters.getInt("sampleSize"), false);
    if (this.shuffledOrder == null) {
        List<Node> shuffledNodes = new ArrayList<>(continuousData.getVariables());
        Collections.shuffle(shuffledNodes);
        this.shuffledOrder = shuffledNodes;
    }
    Discretizer discretizer = new Discretizer(continuousData);
    for (int i = 0; i < shuffledOrder.size() * parameters.getDouble("percentDiscrete") * 0.01; i++) {
        discretizer.equalIntervals(continuousData.getVariable(shuffledOrder.get(i).getName()), parameters.getInt("numCategories"));
    }
    return discretizer.discretize();
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Discretizer(edu.cmu.tetrad.data.Discretizer)

Example 45 with SemPm

use of edu.cmu.tetrad.sem.SemPm 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)

Aggregations

SemPm (edu.cmu.tetrad.sem.SemPm)77 SemIm (edu.cmu.tetrad.sem.SemIm)71 Test (org.junit.Test)44 ArrayList (java.util.ArrayList)29 DataSet (edu.cmu.tetrad.data.DataSet)28 Graph (edu.cmu.tetrad.graph.Graph)25 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)18 Node (edu.cmu.tetrad.graph.Node)18 SemEstimator (edu.cmu.tetrad.sem.SemEstimator)16 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)15 Dag (edu.cmu.tetrad.graph.Dag)10 DMSearch (edu.cmu.tetrad.search.DMSearch)9 StandardizedSemIm (edu.cmu.tetrad.sem.StandardizedSemIm)9 NumberFormat (java.text.NumberFormat)7 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)6 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)5 GraphNode (edu.cmu.tetrad.graph.GraphNode)5 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)4 IndependenceTest (edu.cmu.tetrad.search.IndependenceTest)4 Parameters (edu.cmu.tetrad.util.Parameters)4