Search in sources :

Example 61 with SemIm

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

Example 62 with SemIm

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

the class TestSemEvidence method testSemEvidence.

@Test
public void testSemEvidence() {
    Graph graph = constructGraph1();
    SemPm semPm = new SemPm(graph);
    SemIm semIm = new SemIm(semPm);
    SemEvidence evidence = new SemEvidence(semIm);
    evidence.setManipulated(1, true);
    assertTrue(evidence.isManipulated(1));
}
Also used : SemEvidence(edu.cmu.tetrad.sem.SemEvidence) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Example 63 with SemIm

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

the class TestSemUpdater method testUpdate.

@Test
public void testUpdate() {
    Graph graph = constructGraph1();
    SemPm semPm = new SemPm(graph);
    SemIm semIm = new SemIm(semPm);
    List<Node> nodes = semIm.getVariableNodes();
    SemUpdater semUpdater = new SemUpdater(semIm);
    SemEvidence evidence = new SemEvidence(semIm);
    evidence.getProposition().setValue(nodes.get(4), 10.0);
    evidence.getProposition().setValue(nodes.get(2), 1.5);
    semUpdater.setEvidence(evidence);
    evidence.setManipulated(1, true);
    semUpdater.getManipulatedSemIm();
    semUpdater.getUpdatedSemIm();
}
Also used : SemUpdater(edu.cmu.tetrad.sem.SemUpdater) SemEvidence(edu.cmu.tetrad.sem.SemEvidence) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Example 64 with SemIm

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

the class TestSemXml method sampleSemIm1.

private static SemIm sampleSemIm1() {
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    Graph graph = new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, true));
    SemPm pm = new SemPm(graph);
    return new SemIm(pm);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) SemPm(edu.cmu.tetrad.sem.SemPm) Dag(edu.cmu.tetrad.graph.Dag) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 65 with SemIm

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

the class TestStatUtils method testConditionalCorrelation.

/**
 * Tests that the unconditional correlations and covariances are correct,
 * at least for the unconditional tests.
 */
@Test
public void testConditionalCorrelation() {
    RandomUtil.getInstance().setSeed(30299533L);
    // Make sure the unconditional correlations and covariances are OK.
    List<Node> nodes1 = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        nodes1.add(new ContinuousVariable("X" + (i + 1)));
    }
    Graph graph = new Dag(GraphUtils.randomGraph(nodes1, 0, 5, 3, 3, 3, false));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet dataSet = im.simulateData(1000, false);
    double[] x = dataSet.getDoubleData().getColumn(0).toArray();
    double[] y = dataSet.getDoubleData().getColumn(1).toArray();
    double r1 = StatUtils.correlation(x, y);
    double s1 = StatUtils.covariance(x, y);
    double v1 = StatUtils.variance(x);
    double sd1 = StatUtils.sd(x);
    ICovarianceMatrix cov = new CovarianceMatrix(dataSet);
    TetradMatrix _cov = cov.getMatrix();
    double r2 = StatUtils.partialCorrelation(_cov, 0, 1);
    double s2 = StatUtils.partialCovariance(_cov, 0, 1);
    double v2 = StatUtils.partialVariance(_cov, 0);
    double sd2 = StatUtils.partialStandardDeviation(_cov, 0);
    assertEquals(r1, r2, .1);
    assertEquals(s1, s2, .1);
    assertEquals(v1, v2, .1);
    assertEquals(sd1, sd2, 0.1);
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) Dag(edu.cmu.tetrad.graph.Dag) Graph(edu.cmu.tetrad.graph.Graph) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) 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