Search in sources :

Example 56 with SemIm

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

the class TestDeltaTetradTest method test4aIteratedPositives.

@Test
public void test4aIteratedPositives() {
    RandomUtil.getInstance().setSeed(482834823L);
    int numTrials = 10;
    double alpha = 0.2;
    SemIm sem = getFigure4aSem();
    int[] sampleSizes = new int[] { 100, 500, 1000, 5000 };
    double[][] answers = { { .1, .5, .2, .1, .1, .1 }, { 0.1, 0.6, 0.0, 0.0, 0.0, 0.0 }, { 0.1, 0.7, 0.2, 0.2, 0.2, 0.2 }, { 0.3, 0.7, 0.2, 0.1, 0.1, 0.1 } };
    for (int i = 0; i < 4; i++) {
        System.out.println("i = " + i);
        int sampleSize = sampleSizes[i];
        int[] count = new int[6];
        for (int k = 0; k < numTrials; k++) {
            DataSet data = sem.simulateData(sampleSize, false);
            Node x1 = data.getVariable("x1");
            Node x2 = data.getVariable("x2");
            Node x3 = data.getVariable("x3");
            Node x4 = data.getVariable("x4");
            Tetrad t1234 = new Tetrad(x1, x2, x3, x4);
            Tetrad t1342 = new Tetrad(x1, x3, x4, x2);
            Tetrad t1423 = new Tetrad(x1, x4, x2, x3);
            DeltaTetradTest test = new DeltaTetradTest(new CorrelationMatrix(data));
            double p1 = test.getPValue(t1234);
            double p2 = test.getPValue(t1342);
            double p3 = test.getPValue(t1423);
            double p4 = test.getPValue(t1234, t1342);
            double p5 = test.getPValue(t1234, t1423);
            double p6 = test.getPValue(t1342, t1423);
            if (p1 < alpha)
                count[0]++;
            if (p2 < alpha)
                count[1]++;
            if (p3 < alpha)
                count[2]++;
            if (p4 < alpha)
                count[3]++;
            if (p5 < alpha)
                count[4]++;
            if (p6 < alpha)
                count[5]++;
        }
        double[] _answer = new double[6];
        for (int j = 0; j < 6; j++) {
            double v = count[j] / (double) numTrials;
            _answer[j] = v;
        }
    // System.out.println(MatrixUtils.toString(_answer));
    // System.out.println(MatrixUtils.toString(answers[i]));
    // assertTrue(Arrays.equals(_answer, answers[i]));
    }
}
Also used : DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) CorrelationMatrix(edu.cmu.tetrad.data.CorrelationMatrix) DataSet(edu.cmu.tetrad.data.DataSet) SemIm(edu.cmu.tetrad.sem.SemIm) Tetrad(edu.cmu.tetrad.search.Tetrad) DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) Test(org.junit.Test)

Example 57 with SemIm

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

the class TestDeltaTetradTest method getFigure4bSem.

private SemIm getFigure4bSem() {
    Graph graph = new EdgeListGraph();
    Node xi1 = new GraphNode("xi1");
    Node xi2 = new GraphNode("xi2");
    Node x1 = new GraphNode("x1");
    Node x2 = new GraphNode("x2");
    Node x3 = new GraphNode("x3");
    Node x4 = new GraphNode("x4");
    graph.addNode(xi1);
    graph.addNode(xi2);
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.addNode(x4);
    graph.addDirectedEdge(xi1, x1);
    graph.addDirectedEdge(xi1, x2);
    graph.addDirectedEdge(xi2, x3);
    graph.addDirectedEdge(xi2, x4);
    graph.addDirectedEdge(xi1, xi2);
    SemPm pm = new SemPm(graph);
    return new SemIm(pm);
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 58 with SemIm

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

the class TestStandardizedSem method test1.

// Test the code that standardizes a data set.
@Test
public void test1() {
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    SemGraph graph = new SemGraph(new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false)));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet dataSet = im.simulateData(1000, false);
    TetradMatrix _dataSet = dataSet.getDoubleData();
    _dataSet = DataUtils.standardizeData(_dataSet);
    DataSet dataSetStandardized = ColtDataSet.makeData(dataSet.getVariables(), _dataSet);
    DataUtils.cov(_dataSet);
    DataUtils.mean(_dataSet);
    SemEstimator estimator = new SemEstimator(dataSetStandardized, pm);
    SemIm imStandardized = estimator.estimate();
    imStandardized.getEdgeCoef();
    imStandardized.getErrCovar();
    new TetradVector(imStandardized.getMeans());
    imStandardized.getEdgeCoef();
    imStandardized.getErrCovar();
    StandardizedSemIm sem = new StandardizedSemIm(im);
    imStandardized.getEdgeCoef();
    imStandardized.getErrCovar();
    assertTrue(isStandardized(sem));
}
Also used : ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) TetradVector(edu.cmu.tetrad.util.TetradVector) SemPm(edu.cmu.tetrad.sem.SemPm) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

Example 59 with SemIm

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

the class TestStandardizedSem method test4.

// This tests what the user is going to try to do in the GUI.
@Test
public void test4() {
    List<Node> nodes = new ArrayList<>();
    for (int i1 = 0; i1 < 10; i1++) {
        nodes.add(new ContinuousVariable("X" + (i1 + 1)));
    }
    SemGraph graph = new SemGraph(new Dag(GraphUtils.randomGraph(nodes, 0, 10, 30, 15, 15, false)));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    StandardizedSemIm sem = new StandardizedSemIm(im);
    for (int i = 0; i < 20; i++) {
        List<Edge> edges = new ArrayList<>(graph.getEdges());
        RandomUtil random = RandomUtil.getInstance();
        int index = random.nextInt(edges.size());
        Edge edge = edges.get(index);
        Node a = edge.getNode1();
        Node b = edge.getNode2();
        StandardizedSemIm.ParameterRange range = sem.getCoefficientRange(a, b);
        double high = range.getHigh();
        double low = range.getLow();
        double coef = low + random.nextDouble() * (high - low);
        assertTrue(sem.setEdgeCoefficient(a, b, coef));
        coef = high + random.nextDouble() * (high - low);
        assertFalse(sem.setEdgeCoefficient(a, b, coef));
        coef = low - random.nextDouble() * (high - low);
        assertFalse(sem.setEdgeCoefficient(a, b, coef));
    }
}
Also used : ArrayList(java.util.ArrayList) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) RandomUtil(edu.cmu.tetrad.util.RandomUtil) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

Example 60 with SemIm

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

the class TestStandardizedSem method test3.

@Test
public void test3() {
    RandomUtil.getInstance().setSeed(582374923L);
    SemGraph graph = new SemGraph();
    Node x1 = new ContinuousVariable("X1");
    Node x2 = new ContinuousVariable("X2");
    Node x3 = new ContinuousVariable("X3");
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.setShowErrorTerms(true);
    graph.addDirectedEdge(x1, x3);
    graph.addDirectedEdge(x2, x3);
    graph.addDirectedEdge(x1, x2);
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    StandardizedSemIm sem = new StandardizedSemIm(im);
    DataSet data = sem.simulateData(5000, false);
    assertFalse(sem.setEdgeCoefficient(x1, x2, 1.2));
    assertFalse(sem.setEdgeCoefficient(x1, x2, 1.5));
    assertTrue(sem.setEdgeCoefficient(x1, x2, .5));
    assertTrue(sem.setEdgeCoefficient(x1, x3, -.1));
    assertTrue(isStandardized(sem));
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

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