/** Returns a random number from the distribution. */ @Override public double nextDouble() { return nextDouble(alpha, rate); }
/** * Returns a sample from this distribution. The value returned will * be the number of negative samples required before achieving r * positive samples. Each successive sample is taken independently * from a Bernouli process with probability p of success. * * The algorithm used is taken from J.H. Ahrens, U. Dieter (1974): * Computer methods for sampling from gamma, beta, Poisson and * binomial distributions, Computing 12, 223--246. * * This algorithm is essentially the same as described at * http://en.wikipedia.org/wiki/Negative_binomial_distribution#Gamma.E2.80.93Poisson_mixture * except that the notion of positive and negative outcomes is uniformly * inverted. Because the inversion is complete and consistent, this * definition is effectively identical to that defined on wikipedia. */ public int nextInt(int r, double p) { return this.poisson.nextInt(gamma.nextDouble(r, p / (1.0 - p))); }
int[] values = new int[1000]; for (int k = 0; k < 1000; k++) { int j = (int) Math.min(1000, gen.nextDouble()); values[j]++; int[] values = new int[1000]; for (int k = 0; k < 1000; k++) { int j = (int) Math.min(1000, gen.nextDouble()); values[j]++;
@Test public void testNextDouble() { double[] z = new double[100000]; Random gen = RandomUtils.getRandom(); for (double alpha : new double[]{1, 2, 10, 0.1, 0.01, 100}) { Gamma g = new Gamma(alpha, 1, gen); for (int i = 0; i < z.length; i++) { z[i] = g.nextDouble(); } Arrays.sort(z); // verify that empirical CDF matches theoretical one pretty closely for (double q : seq(0.01, 1, 0.01)) { double p = z[(int) (q * z.length)]; assertEquals(q, g.cdf(p), 0.01); } } }
int[] values = new int[1000]; for (int k = 0; k < 1000; k++) { int j = (int) Math.min(1000, gen.nextDouble()); values[j]++;
@Test(timeout=50000) public void testTimesOtherSparseEfficiency() { Random raw = RandomUtils.getRandom(); Gamma gen = new Gamma(0.1, 0.1, raw); // build a sequential sparse matrix and a diagonal matrix and multiply them Matrix x = new SparseRowMatrix(1000, 2000, false); for (int i = 0; i < 1000; i++) { int[] values = new int[1000]; for (int k = 0; k < 1000; k++) { int j = (int) Math.min(1000, gen.nextDouble()); values[j]++; } for (int j = 0; j < 1000; j++) { if (values[j] > 0) { x.set(i, j, values[j]); } } } Vector d = new DenseVector(2000).assign(Functions.random()); Matrix y = new DiagonalMatrix(d); long t0 = System.nanoTime(); Matrix z = x.times(y); double elapsedTime = (System.nanoTime() - t0) * 1e-6; System.out.printf("done in %.1f ms\n", elapsedTime); for (MatrixSlice row : z) { for (Vector.Element element : row.nonZeroes()) { assertEquals(x.get(row.index(), element.index()) * d.get(element.index()), element.get(), 1e-12); } } }
public double nextDouble(double alpha, double beta) { double x = gAlpha.nextDouble(alpha, 1); double y = gBeta.nextDouble(beta, 1); return x / (x + y); }
/** Returns a random number from the distribution. */ @Override public double nextDouble() { return nextDouble(alpha, rate); }
/** Returns a random number from the distribution. */ @Override public double nextDouble() { return nextDouble(alpha, rate); }
/** * Returns a random number from the distribution. * * @return A new sample from this distribution. */ @Override public double nextDouble() { double x = gAlpha.nextDouble(alpha, 1); double y = gBeta.nextDouble(beta, 1); return x / (x + y); }
private double nextVariance() { return 1 / gd.nextDouble(n / 2, ss / 2); } }
private int pickField() { double xValue = x.nextDouble(); double yValue = y.nextDouble(); double beta = xValue / (xValue + yValue); return (int) Math.floor(beta * fieldNames.size()); } }
@Override public JsonNode sample() { return new DoubleNode(rand.nextDouble()); }
/** * Returns a sample from this distribution. The value returned will * be the number of negative samples required before achieving r * positive samples. Each successive sample is taken independently * from a Bernouli process with probability p of success. * * The algorithm used is taken from J.H. Ahrens, U. Dieter (1974): * Computer methods for sampling from gamma, beta, Poisson and * binomial distributions, Computing 12, 223--246. * * This algorithm is essentially the same as described at * http://en.wikipedia.org/wiki/Negative_binomial_distribution#Gamma.E2.80.93Poisson_mixture * except that the notion of positive and negative outcomes is uniformly * inverted. Because the inversion is complete and consistent, this * definition is effectively identical to that defined on wikipedia. */ public int nextInt(int r, double p) { return this.poisson.nextInt(gamma.nextDouble(r, p / (1.0 - p))); }
/** * Returns a sample from this distribution. The value returned will * be the number of negative samples required before achieving r * positive samples. Each successive sample is taken independently * from a Bernouli process with probability p of success. * * The algorithm used is taken from J.H. Ahrens, U. Dieter (1974): * Computer methods for sampling from gamma, beta, Poisson and * binomial distributions, Computing 12, 223--246. * * This algorithm is essentially the same as described at * http://en.wikipedia.org/wiki/Negative_binomial_distribution#Gamma.E2.80.93Poisson_mixture * except that the notion of positive and negative outcomes is uniformly * inverted. Because the inversion is complete and consistent, this * definition is effectively identical to that defined on wikipedia. */ public int nextInt(int r, double p) { return this.poisson.nextInt(gamma.nextDouble(r, p / (1.0 - p))); }
double averageInterval = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS) / transactionsPerDay.nextDouble(); Exponential interval = new Exponential(1 / averageInterval, gen);
@Test public void testNextDouble() { double[] z = new double[100000]; Random gen = RandomUtils.getRandom(); for (double alpha : new double[]{1, 2, 10, 0.1, 0.01, 100}) { Gamma g = new Gamma(alpha, 1, gen); for (int i = 0; i < z.length; i++) { z[i] = g.nextDouble(); } Arrays.sort(z); // verify that empirical CDF matches theoretical one pretty closely for (double q : seq(0.01, 1, 0.01)) { double p = z[(int) (q * z.length)]; assertEquals(q, g.cdf(p), 0.01); } } }