Search in sources :

Example 51 with SemPm

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

the class TestDiscretizer method testManualDiscretize3.

@Test
public void testManualDiscretize3() {
    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 data = im.simulateData(100, false);
    List<Node> nodes = data.getVariables();
    Discretizer discretizer = new Discretizer(data);
    discretizer.setVariablesCopied(true);
    discretizer.setVariablesCopied(true);
    discretizer.equalCounts(nodes.get(0), 3);
    DataSet discretized = discretizer.discretize();
    assertTrue(discretized.getVariable(0) instanceof DiscreteVariable);
    assertTrue(discretized.getVariable(1) instanceof ContinuousVariable);
    assertTrue(discretized.getVariable(2) instanceof ContinuousVariable);
    assertTrue(discretized.getVariable(3) instanceof ContinuousVariable);
    assertTrue(discretized.getVariable(4) instanceof ContinuousVariable);
}
Also used : 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) Test(org.junit.Test)

Example 52 with SemPm

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

the class TestIndTestFisherZ method testDirections.

@Test
public void testDirections() {
    RandomUtil.getInstance().setSeed(48285934L);
    Graph graph1 = new EdgeListGraph();
    Graph graph2 = new EdgeListGraph();
    Node x = new GraphNode("X");
    Node y = new GraphNode("Y");
    Node z = new GraphNode("Z");
    graph1.addNode(x);
    graph1.addNode(y);
    graph1.addNode(z);
    graph2.addNode(x);
    graph2.addNode(y);
    graph2.addNode(z);
    graph1.addEdge(Edges.directedEdge(x, y));
    graph1.addEdge(Edges.directedEdge(y, z));
    graph2.addEdge(Edges.directedEdge(x, y));
    graph2.addEdge(Edges.directedEdge(z, y));
    SemPm pm1 = new SemPm(graph1);
    SemPm pm2 = new SemPm(graph2);
    SemIm im1 = new SemIm(pm1);
    SemIm im2 = new SemIm(pm2);
    im2.setEdgeCoef(x, y, im1.getEdgeCoef(x, y));
    im2.setEdgeCoef(z, y, im1.getEdgeCoef(y, z));
    DataSet data1 = im1.simulateData(500, false);
    DataSet data2 = im2.simulateData(500, false);
    IndependenceTest test1 = new IndTestFisherZ(data1, 0.05);
    IndependenceTest test2 = new IndTestFisherZ(data2, 0.05);
    test1.isIndependent(data1.getVariable(x.getName()), data1.getVariable(y.getName()));
    double p1 = test1.getPValue();
    test2.isIndependent(data2.getVariable(x.getName()), data2.getVariable(z.getName()), data2.getVariable(y.getName()));
    double p2 = test2.getPValue();
    test2.isIndependent(data2.getVariable(x.getName()), data2.getVariable(z.getName()));
    double p3 = test2.getPValue();
    assertEquals(0, p1, 0.01);
    assertEquals(0, p2, 0.01);
    assertEquals(0.38, p3, 0.01);
}
Also used : IndependenceTest(edu.cmu.tetrad.search.IndependenceTest) IndTestFisherZ(edu.cmu.tetrad.search.IndTestFisherZ) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test) IndependenceTest(edu.cmu.tetrad.search.IndependenceTest)

Example 53 with SemPm

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

the class TestDeltaTetradTest method test3.

@Test
public void test3() {
    SemPm pm = makePm();
    DataSet data = new SemIm(pm).simulateData(1000, false);
    CovarianceMatrix cov = new CovarianceMatrix(data);
    List<Node> variables = data.getVariables();
    Node x1 = variables.get(0);
    Node x2 = variables.get(1);
    Node x3 = variables.get(2);
    Node x4 = variables.get(3);
    Node x5 = variables.get(4);
    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 test1 = new DeltaTetradTest(data);
    // DeltaTetradTest test = new DeltaTetradTest(new CorrelationMatrix(cov));
    double chiSq1 = test1.calcChiSquare(t1234, t1342);
    DeltaTetradTest test2 = new DeltaTetradTest(cov);
    // DeltaTetradTest test = new DeltaTetradTest(new CorrelationMatrix(cov));
    double chiSq2 = test2.calcChiSquare(t1234, t1342);
}
Also used : DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) DataSet(edu.cmu.tetrad.data.DataSet) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Tetrad(edu.cmu.tetrad.search.Tetrad) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) Test(org.junit.Test)

Example 54 with SemPm

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

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

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