org.apache.commons.math3.random.RandomDataGenerator Java Examples

The following examples show how to use org.apache.commons.math3.random.RandomDataGenerator. 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: RandomUtil.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Get random data from a Poisson distribution
 *
 * @param mean Poisson mean
 * @param shape Shape
 * @return Array Result array
 */
public static Array poisson(double mean, List<Integer> shape) {
    int[] ashape = new int[shape.size()];
    for (int i = 0; i < shape.size(); i++) {
        ashape[i] = shape.get(i);
    }
    Array a = Array.factory(DataType.INT, ashape);
    RandomDataGenerator rd = new RandomDataGenerator();
    if (useSeed)
        rd.reSeed(seed);
    for (int i = 0; i < a.getSize(); i++) {
        a.setDouble(i, rd.nextPoisson(mean));
    }

    return a;
}
 
Example #2
Source File: CalculateFingerprintMetrics.java    From picard with MIT License 6 votes vote down vote up
/** Creates a new fingerprint from the current one by randomizing the probabilities within each haplotype
 *
 */
private static Fingerprint randomizeFingerprint(final Fingerprint fingerprint, final RandomGenerator rg) {
    final Fingerprint retVal = new Fingerprint(null, null, null);

    final RandomDataGenerator rng = new RandomDataGenerator(rg);

    fingerprint.forEach((key, hp) -> {
        final HaplotypeProbabilitiesFromGenotypeLikelihoods permutedHaplotypeProbabilities = new HaplotypeProbabilitiesFromGenotypeLikelihoods(key);
        permutedHaplotypeProbabilities.addToLogLikelihoods(
                hp.getRepresentativeSnp(),
                hp.getRepresentativeSnp().getAlleles(),
                MathUtil.permute(hp.getLogLikelihoods(), rng));
        retVal.add(permutedHaplotypeProbabilities);
    });

    return retVal;
}
 
Example #3
Source File: ArithmeticUtilsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGcdConsistency() {
    int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
    ArrayList<Integer> primes = new ArrayList<Integer>();
    for (int i = 0; i < primeList.length; i++) {
        primes.add(Integer.valueOf(primeList[i]));
    }
    RandomDataGenerator randomData = new RandomDataGenerator();
    for (int i = 0; i < 20; i++) {
        Object[] sample = randomData.nextSample(primes, 4);
        int p1 = ((Integer) sample[0]).intValue();
        int p2 = ((Integer) sample[1]).intValue();
        int p3 = ((Integer) sample[2]).intValue();
        int p4 = ((Integer) sample[3]).intValue();
        int i1 = p1 * p2 * p3;
        int i2 = p1 * p2 * p4;
        int gcd = p1 * p2;
        Assert.assertEquals(gcd, ArithmeticUtils.gcd(i1, i2));
        long l1 = i1;
        long l2 = i2;
        Assert.assertEquals(gcd, ArithmeticUtils.gcd(l1, l2));
    }
}
 
Example #4
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 #5
Source File: ArithmeticUtilsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGcdConsistency() {
    int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
    ArrayList<Integer> primes = new ArrayList<Integer>();
    for (int i = 0; i < primeList.length; i++) {
        primes.add(Integer.valueOf(primeList[i]));
    }
    RandomDataGenerator randomData = new RandomDataGenerator();
    for (int i = 0; i < 20; i++) {
        Object[] sample = randomData.nextSample(primes, 4);
        int p1 = ((Integer) sample[0]).intValue();
        int p2 = ((Integer) sample[1]).intValue();
        int p3 = ((Integer) sample[2]).intValue();
        int p4 = ((Integer) sample[3]).intValue();
        int i1 = p1 * p2 * p3;
        int i2 = p1 * p2 * p4;
        int gcd = p1 * p2;
        Assert.assertEquals(gcd, ArithmeticUtils.gcd(i1, i2));
        long l1 = i1;
        long l2 = i2;
        Assert.assertEquals(gcd, ArithmeticUtils.gcd(l1, l2));
    }
}
 
Example #6
Source File: ArithmeticUtilsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGcdConsistency() {
    int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
    ArrayList<Integer> primes = new ArrayList<Integer>();
    for (int i = 0; i < primeList.length; i++) {
        primes.add(Integer.valueOf(primeList[i]));
    }
    RandomDataGenerator randomData = new RandomDataGenerator();
    for (int i = 0; i < 20; i++) {
        Object[] sample = randomData.nextSample(primes, 4);
        int p1 = ((Integer) sample[0]).intValue();
        int p2 = ((Integer) sample[1]).intValue();
        int p3 = ((Integer) sample[2]).intValue();
        int p4 = ((Integer) sample[3]).intValue();
        int i1 = p1 * p2 * p3;
        int i2 = p1 * p2 * p4;
        int gcd = p1 * p2;
        Assert.assertEquals(gcd, ArithmeticUtils.gcd(i1, i2));
        long l1 = i1;
        long l2 = i2;
        Assert.assertEquals(gcd, ArithmeticUtils.gcd(l1, l2));
    }
}
 
Example #7
Source File: RandomSearch.java    From oryx with Apache License 2.0 6 votes vote down vote up
/**
 * @param ranges ranges of hyperparameters to try, one per hyperparameters
 * @param howMany how many combinations of hyperparameters to return
 * @return combinations of concrete hyperparameter values
 */
static List<List<?>> chooseHyperParameterCombos(Collection<? extends HyperParamValues<?>> ranges, int howMany) {
  Preconditions.checkArgument(howMany > 0);

  int numParams = ranges.size();
  if (numParams == 0) {
    return Collections.singletonList(Collections.emptyList());
  }
  
  RandomDataGenerator rdg = new RandomDataGenerator(RandomManager.getRandom());
  List<List<?>> allCombinations = new ArrayList<>(howMany);
  for (int i = 0; i < howMany; i++) {
    List<Object> combination = new ArrayList<>(numParams);
    for (HyperParamValues<?> range : ranges) {
      combination.add(range.getRandomValue(rdg));
    }
    allCombinations.add(combination);
  }

  return allCombinations;
}
 
Example #8
Source File: JavaRandomUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenUsingApache_whenGeneratingRandomIntegerUnbounded_thenCorrect() {
    final Integer generatedInteger = new RandomDataGenerator().getRandomGenerator()
        .nextInt();

    LOG.debug("{}", generatedInteger);
}
 
Example #9
Source File: JavaRandomUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenUsingApache_whenGeneratingRandomDoubleUnbounded_thenCorrect() {
    final double generatedDouble = new RandomDataGenerator().getRandomGenerator()
        .nextDouble();

    LOG.debug("{}", generatedDouble);
}
 
Example #10
Source File: JavaRandomUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenUsingApache_whenGeneratingRandomFloatBounded_thenCorrect() {
    final float leftLimit = 1F;
    final float rightLimit = 10F;
    final float randomFloat = new RandomDataGenerator().getRandomGenerator()
        .nextFloat();
    final float generatedFloat = leftLimit + randomFloat * (rightLimit - leftLimit);

    LOG.debug("{}", generatedFloat);
}
 
Example #11
Source File: JavaRandomUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenUsingApache_whenGeneratingRandomFloatUnbounded_thenCorrect() {
    final float generatedFloat = new RandomDataGenerator().getRandomGenerator()
        .nextFloat();

    LOG.debug("{}", generatedFloat);
}
 
Example #12
Source File: JavaRandomUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenUsingApache_whenGeneratingRandomIntegerBounded_thenCorrect() {
    final int leftLimit = 1;
    final int rightLimit = 10;
    final int generatedInteger = new RandomDataGenerator().nextInt(leftLimit, rightLimit);

    LOG.debug("{}", generatedInteger);
}
 
Example #13
Source File: RulesApplier.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public RulesApplier(XMLConfigParser parser, long seed) {
    this.parser = parser;
    this.modelList = new ArrayList<Map>();
    this.columnMap = new HashMap<String, Column>();
    this.rndNull = new Random(seed);
    this.rndVal = new Random(seed);
    this.randomDataGenerator = new RandomDataGenerator();
    this.cachedScenarioOverrideName = null;
    populateModelList();
}
 
Example #14
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 #15
Source File: HDF5LibraryUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private RealMatrix createMatrixOfGaussianValues(int numRows, int numCols, final double mean, final double sigma) {
    final RealMatrix bigCounts = new Array2DRowRealMatrix(numRows, numCols);
    final RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    randomDataGenerator.reSeed(337337337);
    bigCounts.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            return randomDataGenerator.nextGaussian(mean, sigma);
        }
    });
    return bigCounts;
}
 
Example #16
Source File: RandomUtil.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get random data from a Poisson distribution
 *
 * @param mean Poisson mean
 * @param n Array length
 * @return Array Result array
 */
public static Array poisson(double mean, int n) {
    Array a = Array.factory(DataType.INT, new int[]{n});
    RandomDataGenerator rd = new RandomDataGenerator();
    if (useSeed)
        rd.reSeed(seed);
    for (int i = 0; i < a.getSize(); i++) {
        a.setDouble(i, rd.nextPoisson(mean));
    }

    return a;
}
 
Example #17
Source File: DiscreteRange.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 Integer getRandomValue(RandomDataGenerator rdg) {
  if (max == min) {
    return min;
  }
  return rdg.nextInt(min, max);
}
 
Example #18
Source File: MathUtil.java    From picard with MIT License 5 votes vote down vote up
/**
 * permute the input array randomly (using a RandomDataGenerator)
 *
 * @param array input array
 * @param rdg   a RandomDataGenerator for drawing a permutation from
 * @return a newly allocated array with a permuted version of the original data.
 */
public static double[] permute(double[] array, RandomDataGenerator rdg) {

    final int n = array.length;
    final double[] retVal = new double[n];
    final int[] randomPermutation = rdg.nextPermutation(n, n);
    for (int i = 0; i < n; i++) {
        retVal[i] = array[randomPermutation[i]];
    }
    return retVal;
}
 
Example #19
Source File: HDF5UtilsUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static RealMatrix createMatrixOfGaussianValues(final int numRows,
                                                       final int numColumns,
                                                       final double mean,
                                                       final double sigma) {
    final RealMatrix largeMatrix = new Array2DRowRealMatrix(numRows, numColumns);
    final RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    randomDataGenerator.reSeed(337337337);
    largeMatrix.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            return randomDataGenerator.nextGaussian(mean, sigma);
        }
    });
    return largeMatrix;
}
 
Example #20
Source File: JavaRandomUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenUsingApache_whenGeneratingRandomDoubleBounded_thenCorrect() {
    final double leftLimit = 1D;
    final double rightLimit = 100D;
    final double generatedDouble = new RandomDataGenerator().nextUniform(leftLimit, rightLimit);

    LOG.debug("{}", generatedDouble);
}
 
Example #21
Source File: JavaRandomUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenUsingApacheCommons_whenGeneratingRandomLongBounded_thenCorrect() {
    final long leftLimit = 10L;
    final long rightLimit = 100L;
    final long generatedLong = new RandomDataGenerator().nextLong(leftLimit, rightLimit);

    LOG.debug("{}", generatedLong);
}
 
Example #22
Source File: RandomGenerator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open(
		String name,
		FunctionInitializationContext context,
		RuntimeContext runtimeContext) throws Exception {
	this.random = new RandomDataGenerator();
}
 
Example #23
Source File: MathUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make sure that permuted arrays do not hash to the same value.
 */
@Test
public void testPermutedArrayHash() {
    double[] original = new double[10];
    double[] permuted = new double[10];
    RandomDataGenerator random = new RandomDataGenerator();

    // Generate 10 distinct random values
    for (int i = 0; i < 10; i++) {
        final RealDistribution u = new UniformRealDistribution(i + 0.5, i + 0.75);
        original[i] = u.sample();
    }

    // Generate a random permutation, making sure it is not the identity
    boolean isIdentity = true;
    do {
        int[] permutation = random.nextPermutation(10, 10);
        for (int i = 0; i < 10; i++) {
            if (i != permutation[i]) {
                isIdentity = false;
            }
            permuted[i] = original[permutation[i]];
        }
    } while (isIdentity);

    // Verify that permuted array has different hash
    Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
 
Example #24
Source File: NaturalRanking.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a NaturalRanking with the given NaNStrategy, TiesStrategy.RANDOM
 * and the given source of random data.
 *
 * @param nanStrategy NaNStrategy to use
 * @param randomGenerator source of random data
 */
public NaturalRanking(NaNStrategy nanStrategy,
        RandomGenerator randomGenerator) {
    super();
    this.nanStrategy = nanStrategy;
    this.tiesStrategy = TiesStrategy.RANDOM;
    randomData = new RandomDataGenerator(randomGenerator);
}
 
Example #25
Source File: NaturalRanking.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a NaturalRanking with TiesStrategy.RANDOM and the given
 * RandomGenerator as the source of random data.
 *
 * @param randomGenerator source of random data
 */
public NaturalRanking(RandomGenerator randomGenerator) {
    super();
    this.tiesStrategy = TiesStrategy.RANDOM;
    nanStrategy = DEFAULT_NAN_STRATEGY;
    randomData = new RandomDataGenerator(randomGenerator);
}
 
Example #26
Source File: NaturalRanking.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a NaturalRanking with the given NaNStrategy and TiesStrategy.
 *
 * @param nanStrategy NaNStrategy to use
 * @param tiesStrategy TiesStrategy to use
 */
public NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy) {
    super();
    this.nanStrategy = nanStrategy;
    this.tiesStrategy = tiesStrategy;
    randomData = new RandomDataGenerator();
}
 
Example #27
Source File: NaturalRanking.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a NaturalRanking with the given TiesStrategy.
 *
 * @param tiesStrategy the TiesStrategy to use
 */
public NaturalRanking(TiesStrategy tiesStrategy) {
    super();
    this.tiesStrategy = tiesStrategy;
    nanStrategy = DEFAULT_NAN_STRATEGY;
    randomData = new RandomDataGenerator();
}
 
Example #28
Source File: NaturalRanking.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a NaturalRanking with the given NaNStrategy, TiesStrategy.RANDOM
 * and the given source of random data.
 *
 * @param nanStrategy NaNStrategy to use
 * @param randomGenerator source of random data
 */
public NaturalRanking(NaNStrategy nanStrategy,
        RandomGenerator randomGenerator) {
    super();
    this.nanStrategy = nanStrategy;
    this.tiesStrategy = TiesStrategy.RANDOM;
    randomData = new RandomDataGenerator(randomGenerator);
}
 
Example #29
Source File: NaturalRanking.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a NaturalRanking with TiesStrategy.RANDOM and the given
 * RandomGenerator as the source of random data.
 *
 * @param randomGenerator source of random data
 */
public NaturalRanking(RandomGenerator randomGenerator) {
    super();
    this.tiesStrategy = TiesStrategy.RANDOM;
    nanStrategy = DEFAULT_NAN_STRATEGY;
    randomData = new RandomDataGenerator(randomGenerator);
}
 
Example #30
Source File: NaturalRanking.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a NaturalRanking with the given NaNStrategy and TiesStrategy.
 *
 * @param nanStrategy NaNStrategy to use
 * @param tiesStrategy TiesStrategy to use
 */
public NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy) {
    super();
    this.nanStrategy = nanStrategy;
    this.tiesStrategy = tiesStrategy;
    randomData = new RandomDataGenerator();
}