Search in sources :

Example 16 with RandomUtil

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

the class MlBayesIm method simulateData.

/**
 * Simulates a sample with the given sample size.
 *
 * @param sampleSize the sample size.
 * @param seed       the random number generator seed allows you
 *                   recreate the simulated data by passing in the same
 *                   seed (so you don't have to store the sample data
 * @return the simulated sample as a DataSet.
 */
public DataSet simulateData(int sampleSize, long seed, boolean latentDataSaved) {
    RandomUtil random = RandomUtil.getInstance();
    long _seed = random.getSeed();
    random.setSeed(seed);
    DataSet dataSet = simulateData(sampleSize, latentDataSaved);
    random.revertSeed(_seed);
    return dataSet;
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil)

Example 17 with RandomUtil

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

the class DirichletBayesIm method getRandomWeights.

/**
 * This method chooses random probabilities for a row which add up to 1.0.
 * Random doubles are drawn from a random distribution, and the final row is
 * then normalized.
 *
 * @param size the length of the row.
 * @return an array with randomly distributed probabilities of this length.
 * @see #randomizeRow
 */
private static double[] getRandomWeights(int size) {
    assert size >= 0;
    double[] weights = new double[size];
    double sum = 0.0;
    for (int i = 0; i < size; i++) {
        RandomUtil randomUtil = RandomUtil.getInstance();
        weights[i] = randomUtil.nextDouble();
        sum += weights[i];
    }
    for (int i = 0; i < size; i++) {
        weights[i] /= sum;
    }
    return weights;
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil)

Example 18 with RandomUtil

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

the class MlBayesImObs method simulateData.

public DataSet simulateData(DataSet dataSet, long seed, boolean latentDataSaved) {
    RandomUtil random = RandomUtil.getInstance();
    random.setSeed(seed);
    return simulateDataHelper(dataSet, latentDataSaved);
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil)

Example 19 with RandomUtil

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

the class ApproximateUpdater method getSinglePoint.

private static int[] getSinglePoint(BayesIm bayesIm, int[] tiers) {
    int[] point = new int[bayesIm.getNumNodes()];
    int[] combination = new int[bayesIm.getNumNodes()];
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int nodeIndex : tiers) {
        double cutoff = randomUtil.nextDouble();
        for (int k = 0; k < bayesIm.getNumParents(nodeIndex); k++) {
            combination[k] = point[bayesIm.getParent(nodeIndex, k)];
        }
        int rowIndex = bayesIm.getRowIndex(nodeIndex, combination);
        double sum = 0.0;
        for (int k = 0; k < bayesIm.getNumColumns(nodeIndex); k++) {
            double probability = bayesIm.getProbability(nodeIndex, rowIndex, k);
            if (Double.isNaN(probability)) {
                throw new IllegalStateException("Some probability " + "values in the BayesIm are not filled in; " + "cannot simulate data to do approximate updating.");
            }
            sum += probability;
            if (sum >= cutoff) {
                point[nodeIndex] = k;
                break;
            }
        }
    }
    return point;
}
Also used : RandomUtil(edu.cmu.tetrad.util.RandomUtil)

Example 20 with RandomUtil

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

the class TestColtDataSet method testRemoveColumn.

@Test
public void testRemoveColumn() {
    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[] _cols = new int[2];
    _cols[0] = 1;
    _cols[1] = 2;
    dataSet.removeCols(_cols);
    List<Node> _variables = new LinkedList<>(variables);
    _variables.remove(2);
    _variables.remove(1);
    assertEquals(dataSet.getVariables(), _variables);
}
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)

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