Search in sources :

Example 21 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class TestColtDataSet method testDiscrete.

@Test
public void testDiscrete() {
    int rows = 10;
    int cols = 5;
    List<Node> variables = new LinkedList<>();
    for (int i = 0; i < cols; i++) {
        DiscreteVariable variable = new DiscreteVariable("X" + (i + 1), 3);
        variables.add(variable);
    }
    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.setInt(i, j, randomUtil.nextInt(3));
        }
    }
    ColtDataSet _dataSet = new ColtDataSet((ColtDataSet) dataSet);
    assertEquals(dataSet, _dataSet);
}
Also used : DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) 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 22 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class TestColtDataSet method testRowSubset.

@Test
public void testRowSubset() {
    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());
        }
    }
    double d = dataSet.getDouble(2, 0);
    DataSet _dataSet = dataSet.subsetRows(new int[] { 2, 3, 4 });
    assertEquals(3, _dataSet.getNumRows());
    assertEquals(d, _dataSet.getDouble(0, 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 23 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class TestBoxDataSet 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 BoxDataSet(new DoubleDataBox(rows, variables.size()), 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 : RandomUtil(edu.cmu.tetrad.util.RandomUtil) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 24 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class TestBoxDataSet 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 BoxDataSet(new DoubleDataBox(rows, _variables.size()), _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 : RandomUtil(edu.cmu.tetrad.util.RandomUtil) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 25 with RandomUtil

use of edu.cmu.tetrad.util.RandomUtil in project tetrad by cmu-phil.

the class TestBoxDataSet 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 BoxDataSet(new DoubleDataBox(3, nodes.size()), 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());
        }
    }
    BoxDataSet _dataSet = new BoxDataSet((BoxDataSet) 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 : TetradVector(edu.cmu.tetrad.util.TetradVector) RandomUtil(edu.cmu.tetrad.util.RandomUtil) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

RandomUtil (edu.cmu.tetrad.util.RandomUtil)25 Test (org.junit.Test)14 Node (edu.cmu.tetrad.graph.Node)13 LinkedList (java.util.LinkedList)10 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)8 DataSet (edu.cmu.tetrad.data.DataSet)8 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)5 ArrayList (java.util.ArrayList)5 SemIm (edu.cmu.tetrad.sem.SemIm)2 SemPm (edu.cmu.tetrad.sem.SemPm)2 StandardizedSemIm (edu.cmu.tetrad.sem.StandardizedSemIm)2 TetradVector (edu.cmu.tetrad.util.TetradVector)2 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)1 Graph (edu.cmu.tetrad.graph.Graph)1 Dimension (java.awt.Dimension)1 Point (java.awt.Point)1 HashSet (java.util.HashSet)1