Search in sources :

Example 6 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.

the class TestColtDataSet method testPermuteRows.

@Test
public void testPermuteRows() {
    ContinuousVariable x1 = new ContinuousVariable("X1");
    ContinuousVariable x2 = new ContinuousVariable("X2");
    List<Node> nodes = new ArrayList<>();
    nodes.add(x1);
    nodes.add(x2);
    DataSet dataSet = new ColtDataSet(3, nodes);
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            dataSet.setDouble(i, j, randomUtil.nextDouble());
        }
    }
    ColtDataSet _dataSet = new ColtDataSet((ColtDataSet) dataSet);
    dataSet.permuteRows();
    I: for (int i = 0; i < dataSet.getNumRows(); i++) {
        TetradVector v = _dataSet.getDoubleData().getRow(i);
        for (int j = 0; j < dataSet.getNumRows(); j++) {
            TetradVector w = dataSet.getDoubleData().getRow(j);
            if (v.equals(w)) {
                continue I;
            }
        }
        fail("Missing row in permutation.");
    }
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) TetradVector(edu.cmu.tetrad.util.TetradVector) RandomUtil(edu.cmu.tetrad.util.RandomUtil) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.

the class TestColtDataSet method testContinuous.

@Test
public final void testContinuous() {
    int rows = 10;
    int cols = 5;
    List<Node> _variables = new LinkedList<>();
    for (int i = 0; i < cols; i++) {
        _variables.add(new ContinuousVariable("X" + i));
    }
    DataSet dataSet = new ColtDataSet(rows, _variables);
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            dataSet.setDouble(i, j, randomUtil.nextDouble());
        }
    }
    List<Node> variables = dataSet.getVariables();
    List<Node> newVars = new LinkedList<>();
    newVars.add(variables.get(2));
    newVars.add(variables.get(4));
    DataSet _dataSet = dataSet.subsetColumns(newVars);
    assertEquals(dataSet.getDoubleData().getColumn(2).get(0), _dataSet.getDoubleData().getColumn(0).get(0), .001);
    assertEquals(dataSet.getDoubleData().getColumn(4).get(0), _dataSet.getDoubleData().getColumn(1).get(0), .001);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) RandomUtil(edu.cmu.tetrad.util.RandomUtil) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 8 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.

the class TestColtDataSet method testRemoveRows.

@Test
public void testRemoveRows() {
    int rows = 10;
    int cols = 5;
    List<Node> variables = new LinkedList<>();
    for (int i = 0; i < cols; i++) {
        variables.add(new ContinuousVariable("X" + i));
    }
    DataSet dataSet = new ColtDataSet(rows, variables);
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            dataSet.setDouble(i, j, randomUtil.nextDouble());
        }
    }
    int numRows = dataSet.getNumRows();
    double d = dataSet.getDouble(3, 0);
    int[] _rows = new int[2];
    _rows[0] = 1;
    _rows[1] = 2;
    dataSet.removeRows(_rows);
    assertEquals(numRows - 2, dataSet.getNumRows());
    assertEquals(d, dataSet.getDouble(1, 0), 0.001);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) RandomUtil(edu.cmu.tetrad.util.RandomUtil) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 9 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.

the class TestCpc method test7.

@Test
public void test7() {
    int numVars = 6;
    int numEdges = 6;
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < numVars; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    Dag trueGraph = new Dag(GraphUtils.randomGraph(nodes, 0, numEdges, 7, 5, 5, false));
    SemPm semPm = new SemPm(trueGraph);
    SemIm semIm = new SemIm(semPm);
    DataSet _dataSet = semIm.simulateData(1000, false);
    IndependenceTest test = new IndTestFisherZ(_dataSet, 0.05);
    Cpc search = new Cpc(test);
    Graph resultGraph = search.search();
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) ArrayList(java.util.ArrayList) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Example 10 with ContinuousVariable

use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.

the class TestGeneralizedSem method test2.

@Test
public void test2() {
    RandomUtil.getInstance().setSeed(2999983L);
    int sampleSize = 1000;
    List<Node> variableNodes = new ArrayList<>();
    ContinuousVariable x1 = new ContinuousVariable("X1");
    ContinuousVariable x2 = new ContinuousVariable("X2");
    ContinuousVariable x3 = new ContinuousVariable("X3");
    ContinuousVariable x4 = new ContinuousVariable("X4");
    ContinuousVariable x5 = new ContinuousVariable("X5");
    variableNodes.add(x1);
    variableNodes.add(x2);
    variableNodes.add(x3);
    variableNodes.add(x4);
    variableNodes.add(x5);
    Graph _graph = new EdgeListGraph(variableNodes);
    SemGraph graph = new SemGraph(_graph);
    graph.addDirectedEdge(x1, x3);
    graph.addDirectedEdge(x2, x3);
    graph.addDirectedEdge(x3, x4);
    graph.addDirectedEdge(x2, x4);
    graph.addDirectedEdge(x4, x5);
    graph.addDirectedEdge(x2, x5);
    SemPm semPm = new SemPm(graph);
    SemIm semIm = new SemIm(semPm);
    DataSet dataSet = semIm.simulateData(sampleSize, false);
    print(semPm);
    GeneralizedSemPm _semPm = new GeneralizedSemPm(semPm);
    GeneralizedSemIm _semIm = new GeneralizedSemIm(_semPm, semIm);
    DataSet _dataSet = _semIm.simulateDataMinimizeSurface(sampleSize, false);
    print(_semPm);
    for (int j = 0; j < dataSet.getNumColumns(); j++) {
        double[] col = dataSet.getDoubleData().getColumn(j).toArray();
        double[] _col = _dataSet.getDoubleData().getColumn(j).toArray();
        double mean = StatUtils.mean(col);
        double _mean = StatUtils.mean(_col);
        double variance = StatUtils.variance(col);
        double _variance = StatUtils.variance(_col);
        assertEquals(mean, _mean, 0.3);
        assertEquals(1.0, variance / _variance, .2);
    }
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Test(org.junit.Test)

Aggregations

ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)91 DataSet (edu.cmu.tetrad.data.DataSet)48 Node (edu.cmu.tetrad.graph.Node)46 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)28 Graph (edu.cmu.tetrad.graph.Graph)22 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)19 SemPm (edu.cmu.tetrad.sem.SemPm)18 SemIm (edu.cmu.tetrad.sem.SemIm)16 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)15 LinkedList (java.util.LinkedList)13 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)12 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)8 DMSearch (edu.cmu.tetrad.search.DMSearch)7 Dag (edu.cmu.tetrad.graph.Dag)6 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)5 RandomUtil (edu.cmu.tetrad.util.RandomUtil)5 ParseException (java.text.ParseException)4 CovarianceMatrixOnTheFly (edu.cmu.tetrad.data.CovarianceMatrixOnTheFly)3 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)3