Search in sources :

Example 6 with StandardizedSemIm

use of edu.cmu.tetrad.sem.StandardizedSemIm 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 7 with StandardizedSemIm

use of edu.cmu.tetrad.sem.StandardizedSemIm 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 8 with StandardizedSemIm

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

Example 9 with StandardizedSemIm

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

the class TestStandardizedSem method test7.

// This tests what the user is going to try to do in the GUI.
@Test
public void test7() {
    RandomUtil random = RandomUtil.getInstance();
    random.setSeed(9394929393L);
    List<Node> nodes1 = new ArrayList<>();
    for (int i1 = 0; i1 < 5; i1++) {
        nodes1.add(new ContinuousVariable("X" + (i1 + 1)));
    }
    SemGraph graph = new SemGraph(new Dag(GraphUtils.randomGraph(nodes1, 0, 5, 30, 15, 15, false)));
    List<Node> nodes = graph.getNodes();
    int n1 = RandomUtil.getInstance().nextInt(nodes.size());
    int n2 = RandomUtil.getInstance().nextInt(nodes.size());
    while (n1 == n2) {
        n2 = RandomUtil.getInstance().nextInt(nodes.size());
    }
    Node node1 = nodes.get(n1);
    Node node2 = nodes.get(n2);
    Edge _edge = Edges.bidirectedEdge(node1, node2);
    graph.addEdge(_edge);
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    StandardizedSemIm sem = new StandardizedSemIm(im);
    DataSet data3 = sem.simulateDataReducedForm(1000, false);
    graph.setShowErrorTerms(false);
    for (int i = 0; i < 1; i++) {
        for (Edge edge : graph.getEdges()) {
            Node a = edge.getNode1();
            Node b = edge.getNode2();
            if (Edges.isDirectedEdge(edge)) {
                double initial = sem.getEdgeCoef(a, b);
                StandardizedSemIm.ParameterRange range = sem.getCoefficientRange(a, b);
                assertEquals(initial, sem.getEdgeCoef(a, b), 0.1);
                double low = range.getLow();
                double high = range.getHigh();
                double _coef = sem.getEdgeCoef(a, b);
                double coef = low + random.nextDouble() * (high - low);
                assertTrue(sem.setEdgeCoefficient(a, b, coef));
                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));
            } else if (Edges.isBidirectedEdge(edge)) {
                sem.setErrorCovariance(node1, node2, .15);
                assertTrue(isStandardized(sem));
                StandardizedSemIm.ParameterRange range2 = sem.getCovarianceRange(a, b);
                double low = range2.getLow();
                double high = range2.getHigh();
                if (low == Double.NEGATIVE_INFINITY)
                    low = -10000;
                if (high == Double.POSITIVE_INFINITY)
                    high = 10000;
                double _coef = sem.getErrorCovariance(a, b);
                double coef = low + random.nextDouble() * (high - low);
                assertTrue(sem.setErrorCovariance(a, b, coef));
                sem.setErrorCovariance(a, b, _coef);
                if (high != 10000) {
                    coef = high + random.nextDouble() * (high - low);
                    assertFalse(sem.setErrorCovariance(a, b, coef));
                }
                if (low != -10000) {
                    coef = low - random.nextDouble() * (high - low);
                    assertFalse(sem.setErrorCovariance(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)

Aggregations

SemIm (edu.cmu.tetrad.sem.SemIm)9 SemPm (edu.cmu.tetrad.sem.SemPm)9 StandardizedSemIm (edu.cmu.tetrad.sem.StandardizedSemIm)9 Test (org.junit.Test)7 SemEstimator (edu.cmu.tetrad.sem.SemEstimator)4 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)3 ArrayList (java.util.ArrayList)3 RandomUtil (edu.cmu.tetrad.util.RandomUtil)2 TetradVector (edu.cmu.tetrad.util.TetradVector)1