private MultiNormal(Matrix scale, Vector mean, int dimension) { gen = RandomUtils.getRandom(); this.dimension = dimension; this.scale = scale; this.mean = mean; }
/** * Constructs a uniform distribution with the given minimum and maximum, using a {@link * org.apache.mahout.math.jet.random.engine.MersenneTwister} seeded with the given seed. */ public Uniform(double min, double max, int seed) { this(min, max, RandomUtils.getRandom(seed)); }
public Missing(int seed, double p, Sampler<T> delegate, T missingMarker) { this.p = p; this.delegate = delegate; this.missingMarker = missingMarker; gen = RandomUtils.getRandom(seed); }
public IndianBuffet(double alpha, WordFunction<T> converter) { this.alpha = alpha; this.converter = converter; gen = RandomUtils.getRandom(); }
private static double[] normal(int n) { double[] r = new double[n]; Random gen = RandomUtils.getRandom(1L); for (int i = 0; i < n; i++) { r[i] = gen.nextGaussian(); } return r; }
public PoissonSampler(double lambda) { limit = 1; gen = RandomUtils.getRandom(); pd = new PoissonDistribution(gen.getRandomGenerator(), lambda, PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS); }
private static double[] exp(int n) { double[] r = new double[n]; Random gen = RandomUtils.getRandom(1L); for (int i = 0; i < n; i++) { r[i] = -Math.log1p(-gen.nextDouble()); } return r; }
public void testHashLong() { List<Long> original = Lists.newArrayList(); for (int k = 0; k < 10; k++) { Random gen = RandomUtils.getRandom(); for (int i = 0; i < 10000; i++) { long x = gen.nextLong(); original.add(x); } checkCounts(original); } }
public Multinomial(Multiset<T> counts) { this(); Preconditions.checkArgument(!counts.isEmpty(), "Need some data to build sampler"); rand = RandomUtils.getRandom(); for (T t : counts.elementSet()) { add(t, counts.count(t)); } }
private static Vector randomVector() { Vector v = new DenseVector(20); v.assign(new DoubleFunction() { private final Random gen = RandomUtils.getRandom(); @Override public double apply(double arg1) { return gen.nextDouble(); } }); return v; } }
@Override public RandomAccessSparseVector vectorToTest(int size) { RandomAccessSparseVector r = new RandomAccessSparseVector(size); Random gen = RandomUtils.getRandom(); for (int i = 0; i < 3; i++) { r.set(gen.nextInt(r.size()), gen.nextGaussian()); } return r; }
@Override public SequentialAccessSparseVector vectorToTest(int size) { SequentialAccessSparseVector r = new SequentialAccessSparseVector(size); Random gen = RandomUtils.getRandom(); for (int i = 0; i < 3; i++) { r.set(gen.nextInt(r.size()), gen.nextGaussian()); } return r; }
private static void checkGammaCdf(double alpha, double beta, double... values) { Gamma g = new Gamma(alpha, beta, RandomUtils.getRandom()); int i = 0; for (double x : seq(0, 2 * alpha, 2 * alpha / 10)) { assertEquals(String.format(Locale.ENGLISH, "alpha=%.2f, i=%d, x=%.2f", alpha, i, x), values[i], g.cdf(x), 1.0e-7); i++; } }
@Test public void testCdf() { Exponential dist = new Exponential(5.0, RandomUtils.getRandom()); for (int i = 0; i < 1000; i++) { double x = i / 50.0; assertEquals(1 - Math.exp(-x * 5.0), dist.cdf(x), 1.0e-9); } }
@Test public void beta() { Random r = RandomUtils.getRandom(); for (int i = 0; i < 200; i++) { double alpha = -50 * Math.log1p(-r.nextDouble()); double beta = -50 * Math.log1p(-r.nextDouble()); double ref = Math.exp(Gamma.logGamma(alpha) + Gamma.logGamma(beta) - Gamma.logGamma(alpha + beta)); double actual = Gamma.beta(alpha, beta); double err = (ref - actual) / ref; assertEquals("beta at (" + alpha + ", " + beta + ") relative error = " + err, 0, err, 1.0e-10); } }
@Test public void testUniform() { Random gen = RandomUtils.getRandom(); for (int i = 0; i < repeats(); i++) { runTest(new Uniform(0, 1, gen), 100, new double[]{0.001, 0.01, 0.1, 0.5, 0.9, 0.99, 0.999}, "uniform", true, gen); } }
@Test public void testSetState() throws Exception { Normal dist = new Normal(0, 1, RandomUtils.getRandom()); dist.setState(1.3, 5.9); DistributionChecks.checkDistribution(dist, breaks, 1.3, 5.9, 10000); }
@Test public void consistency() { Exponential dist = new Exponential(1, RandomUtils.getRandom()); // deciles computed using R double[] breaks = {0.1053605, 0.2231436, 0.3566749, 0.5108256, 0.6931472, 0.9162907, 1.2039728, 1.6094379, 2.3025851}; for (double lambda : new double[]{0.01, 0.1, 1, 2, 5, 100}) { dist.setState(lambda); DistributionChecks.checkDistribution(dist, breaks, 0, 1 / lambda, 10000); } } @Test
@Test public void testNextDouble() throws Exception { double[] x = { -0.01, 0.1053605, 0.2231436, 0.3566749, 0.5108256, 0.6931472, 0.9162907, 1.2039728, 1.6094379, 2.3025851 }; Exponential dist = new Exponential(1, RandomUtils.getRandom()); for (double lambda : new double[]{13.0, 0.02, 1.6}) { dist.setState(lambda); checkEmpiricalDistribution(dist, 10000, lambda); DistributionChecks.checkDistribution(dist, x, 0, 1 / lambda, 10000); } }
@Test public void compareToQDigest() { Random rand = RandomUtils.getRandom(); for (int i = 0; i < repeats(); i++) { compare(new Gamma(0.1, 0.1, rand), "gamma", 1L << 48, rand); compare(new Uniform(0, 1, rand), "uniform", 1L << 48, rand); } }