Java Code Examples for org.apache.commons.math3.random.RandomDataGenerator#nextUniform()

The following examples show how to use org.apache.commons.math3.random.RandomDataGenerator#nextUniform() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StrandArtifactFilterUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSymmetry() {
    Utils.resetRandomGenerator();
    final RandomDataGenerator rdg = Utils.getRandomDataGenerator();

    for (int n = 0; n < 10; n++) {
        final double prior = rdg.nextUniform(0,1);
        final int forwardCount = rdg.nextInt(0, 100);
        final int reverseCount = rdg.nextInt(0, 100);
        final int forwardAltCount = rdg.nextInt(0, forwardCount);
        final int reverseAltCount = rdg.nextInt(0, reverseCount);

        final double prob = new StrandArtifactFilter()
                .strandArtifactProbability(prior, forwardCount, reverseCount, forwardAltCount, reverseAltCount, 0)
                .getArtifactProbability();
        final double flipped = new StrandArtifactFilter()
                .strandArtifactProbability(prior, reverseCount, forwardCount, reverseAltCount, forwardAltCount, 0)
                .getArtifactProbability();

        Assert.assertEquals(prob, flipped, 1.0e-8);
    }
}
 
Example 2
Source File: ContinuousRange.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * @param rdg random number generator to use
 * @return a hyperparameter value chosen uniformly at random from the range
 */
@Override
public Double getRandomValue(RandomDataGenerator rdg) {
  if (max == min) {
    return min;
  }
  return rdg.nextUniform(min, max, true);
}
 
Example 3
Source File: OnlineCorpusStep.java    From Alink with Apache License 2.0 4 votes vote down vote up
private static Tuple4<DenseMatrix, DenseMatrix, Long, Long> onlineCorpusUpdate(
    List<Vector> data, DenseMatrix lambda, DenseMatrix alpha, DenseMatrix gammad,
    int vocabularySize, int numTopic, double subSamplingRate) {

    boolean isRandGamma = gammad == null;
    //wordTopicStat is the word topic probability information.
    DenseMatrix wordTopicStat = DenseMatrix.zeros(numTopic, vocabularySize);
    DenseMatrix logPhatPart = new DenseMatrix(numTopic, 1);
    DenseMatrix expELogBeta = LdaUtil.expDirichletExpectation(lambda).transpose();
    long nonEmptyWordCount = 0;
    long nonEmptyDocCount = 0;
    //the online corpus update stage can update the model in two way.
    //if the document order is determined, then it will update in the order.
    //or it will choose documents randomly.
    RandomDataGenerator random = new RandomDataGenerator();
    GammaDistribution distribution = new GammaDistribution(100, 100);
    int dataSize = data.size();
    boolean sampled = false;
    for (int j = 0; j < dataSize; ++j) {
        //if the subSamplingRate is too small and no doc is sampled in one iteration, then will randomly
        //choose one doc to update the model.
        double rate = random.nextUniform(0, 1);
        int index = -1;
        if (rate < subSamplingRate) {
            index = j;
            sampled = true;
        }
        if (j + 1 == dataSize && !sampled) {
            index = random.nextInt(0, dataSize - 1);
        }
        if (index != -1) {
            Vector vec = data.get(index);
            SparseVector sv = (SparseVector) vec;
            sv.setSize(vocabularySize);
            sv.removeZeroValues();
            for (int i = 0; i < sv.numberOfValues(); i++) {
                nonEmptyWordCount += sv.getValues()[i];
            }
            if (isRandGamma) {
                if (gammad == null) {
                    gammad = new DenseMatrix(numTopic, 1);
                }
                for (int i = 0; i < numTopic; i++) {
                    gammad.set(i, 0, distribution.inverseCumulativeProbability(random.nextUniform(0, 1)));
                }
            }
            Tuple2<DenseMatrix, DenseMatrix> topicDistributionTuple =
                LdaUtil.getTopicDistributionMethod(sv, expELogBeta, alpha, gammad, numTopic);

            for (int i = 0; i < sv.getIndices().length; i++) {
                for (int k = 0; k < numTopic; k++) {
                    wordTopicStat.add(k, sv.getIndices()[i], topicDistributionTuple.f1.get(k, i));
                }
            }
            gammad = topicDistributionTuple.f0;
            DenseMatrix deGammad = LdaUtil.dirichletExpectationVec(topicDistributionTuple.f0);
            for (int k = 0; k < numTopic; k++) {
                logPhatPart.add(k, 0, deGammad.get(k, 0));
            }
            nonEmptyDocCount++;
        }
    }
    return new Tuple4<>(wordTopicStat, logPhatPart, nonEmptyWordCount, nonEmptyDocCount);
}
 
Example 4
Source File: OutlierValueSelector.java    From BART with MIT License 4 votes vote down vote up
private double getRandomInRange(double min, double max) {
    RandomDataGenerator generator = new RandomDataGenerator();
    return generator.nextUniform(min, max);
}