Search in sources :

Example 71 with SemIm

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

the class TestHistogram method testHistogram.

@Test
public void testHistogram() {
    RandomUtil.getInstance().setSeed(4829384L);
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    Dag trueGraph = new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false));
    int sampleSize = 1000;
    // Continuous
    SemPm semPm = new SemPm(trueGraph);
    SemIm semIm = new SemIm(semPm);
    DataSet data = semIm.simulateData(sampleSize, false);
    Histogram histogram = new Histogram(data);
    histogram.setTarget("X1");
    histogram.setNumBins(20);
    assertEquals(3.76, histogram.getMax(), 0.01);
    assertEquals(-3.83, histogram.getMin(), 0.01);
    assertEquals(1000, histogram.getN());
    histogram.setTarget("X1");
    histogram.setNumBins(10);
    histogram.addConditioningVariable("X3", 0, 1);
    histogram.addConditioningVariable("X4", 0, 1);
    histogram.removeConditioningVariable("X3");
    assertEquals(3.76, histogram.getMax(), 0.01);
    assertEquals(-3.83, histogram.getMin(), 0.01);
    assertEquals(188, histogram.getN());
    double[] arr = histogram.getContinuousData("X2");
    histogram.addConditioningVariable("X2", StatUtils.min(arr), StatUtils.mean(arr));
    // Discrete
    BayesPm bayesPm = new BayesPm(trueGraph);
    BayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
    DataSet data2 = bayesIm.simulateData(sampleSize, false);
    // For some reason these are giving different
    // values when all of the unit tests are run are
    // once. TODO They produce stable values when
    // this particular test is run repeatedly.
    Histogram histogram2 = new Histogram(data2);
    histogram2.setTarget("X1");
    int[] frequencies1 = histogram2.getFrequencies();
    // assertEquals(928, frequencies1[0]);
    // assertEquals(72, frequencies1[1]);
    histogram2.setTarget("X1");
    histogram2.addConditioningVariable("X2", 0);
    histogram2.addConditioningVariable("X3", 1);
    int[] frequencies = histogram2.getFrequencies();
// assertEquals(377, frequencies[0]);
// assertEquals(28, frequencies[1]);
}
Also used : Histogram(edu.cmu.tetrad.data.Histogram) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Dag(edu.cmu.tetrad.graph.Dag) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm) Test(org.junit.Test)

Example 72 with SemIm

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

the class TestIndTestFisherGeneralizedInverse 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 IndTestFisherZGeneralizedInverse(data1, 0.05);
    IndependenceTest test2 = new IndTestFisherZGeneralizedInverse(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) DataSet(edu.cmu.tetrad.data.DataSet) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) IndTestFisherZGeneralizedInverse(edu.cmu.tetrad.search.IndTestFisherZGeneralizedInverse) Test(org.junit.Test) IndependenceTest(edu.cmu.tetrad.search.IndependenceTest)

Example 73 with SemIm

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

the class TestRegression method setUp.

public void setUp() {
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        nodes.add(new GraphNode("X" + (i + 1)));
    }
    RandomUtil.getInstance().setSeed(342233L);
    Graph graph = new Dag(GraphUtils.randomGraphRandomForwardEdges(nodes, 0, 5, 3, 3, 3, false, true));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    data = im.simulateDataReducedForm(1000, false);
}
Also used : ArrayList(java.util.ArrayList) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 74 with SemIm

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

the class TestSearchGraph method rtestDSeparation4.

public void rtestDSeparation4() {
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    Graph graph = new Dag(GraphUtils.randomGraph(nodes, 20, 100, 5, 5, 5, false));
    long start, stop;
    int depth = -1;
    IndependenceTest test = new IndTestDSep(graph);
    Rfci fci = new Rfci(test);
    Fas fas = new Fas(test);
    start = System.currentTimeMillis();
    fci.setDepth(depth);
    fci.setVerbose(false);
    fci.search(fas, fas.getNodes());
    stop = System.currentTimeMillis();
    System.out.println("DSEP RFCI");
    System.out.println("# dsep checks = " + fas.getNumIndependenceTests());
    System.out.println("Elapsed " + (stop - start));
    System.out.println("Per " + fas.getNumIndependenceTests() / (double) (stop - start));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(1000, false);
    IndependenceTest test2 = new IndTestFisherZ(data, 0.001);
    Rfci fci3 = new Rfci(test2);
    Fas fas2 = new Fas(test2);
    start = System.currentTimeMillis();
    fci3.setDepth(depth);
    fci3.search(fas2, fas2.getNodes());
    stop = System.currentTimeMillis();
    System.out.println("FISHER Z RFCI");
    System.out.println("# indep checks = " + fas.getNumIndependenceTests());
    System.out.println("Elapsed " + (stop - start));
    System.out.println("Per " + fas.getNumIndependenceTests() / (double) (stop - start));
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 75 with SemIm

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

the class TestPcStableMax method testPcStable2.

@Test
public void testPcStable2() {
    RandomUtil.getInstance().setSeed(1450030184196L);
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    Graph graph = GraphUtils.randomGraph(nodes, 0, 10, 30, 15, 15, false);
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(200, false);
    TetradLogger.getInstance().setForceLog(false);
    IndependenceTest test = new IndTestFisherZ(data, 0.05);
    PcStableMax pc = new PcStableMax(test);
    pc.setVerbose(false);
    Graph pattern = pc.search();
    for (int i = 0; i < 1; i++) {
        DataSet data2 = DataUtils.reorderColumns(data);
        IndependenceTest test2 = new IndTestFisherZ(data2, 0.05);
        PcStableMax pc2 = new PcStableMax(test2);
        pc2.setVerbose(false);
        Graph pattern2 = pc2.search();
        assertTrue(pattern.equals(pattern2));
    }
}
Also used : ArrayList(java.util.ArrayList) 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