@Override AbstractDistribution create(Random random) { return new Gamma(0.1, 0.1, random); } },
/** * Constructs a Negative Binomial distribution which describes the probability of getting * a particular number of negative trials (k) before getting a fixed number of positive * trials (r) where each positive trial has probability (p) of being successful. * * @param r the required number of positive trials. * @param p the probability of success. * @param randomGenerator a uniform random number generator. */ public NegativeBinomial(int r, double p, Random randomGenerator) { setRandomGenerator(randomGenerator); this.r = r; this.p = p; this.gamma = new Gamma(r, 1, randomGenerator); this.poisson = new Poisson(0.0, randomGenerator); }
private static double[] gamma(int n, double shape) { double[] r = new double[n]; Random gen = RandomUtils.getRandom(); AbstractContinousDistribution gamma = new Gamma(shape, shape, gen); for (int i = 0; i < n; i++) { r[i] = gamma.nextDouble(); } 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 testPdf() { Random gen = RandomUtils.getRandom(); for (double alpha : new double[]{0.01, 0.1, 1, 2, 10, 100}) { for (double beta : new double[]{0.1, 1, 2, 100}) { Gamma g1 = new Gamma(alpha, beta, gen); for (double x : seq(0, 0.99, 0.1)) { double p = Math.pow(beta, alpha) * Math.pow(x, alpha - 1) * Math.exp(-beta * x - org.apache.mahout.math.jet.stat.Gamma.logGamma(alpha)); assertEquals(String.format(Locale.ENGLISH, "alpha=%.2f, beta=%.2f, x=%.2f\n", alpha, beta, x), p, g1.pdf(x), 1.0e-9); } } } } }
@Test public void testGamma() { // this Gamma distribution is very heavily skewed. The 0.1%-ile is 6.07e-30 while // the median is 0.006 and the 99.9th %-ile is 33.6 while the mean is 1. // this severe skew means that we have to have positional accuracy that // varies by over 11 orders of magnitude. Random gen = RandomUtils.getRandom(); for (int i = 0; i < repeats(); i++) { runTest(new Gamma(0.1, 0.1, gen), 100, // new double[]{6.0730483624079e-30, 6.0730483624079e-20, 6.0730483627432e-10, 5.9339110446023e-03, // 2.6615455373884e+00, 1.5884778179295e+01, 3.3636770117188e+01}, new double[]{0.001, 0.01, 0.1, 0.5, 0.9, 0.99, 0.999}, "gamma", true, 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); } } }
Gamma g1 = new Gamma(1, beta, gen); Gamma g2 = new Gamma(1, 1, gen); for (double x : seq(0, 0.99, 0.1)) { assertEquals(String.format(Locale.ENGLISH, "Rate invariance: x = %.4f, alpha = 1, beta = %.1f", x, beta), Gamma g = new Gamma(alpha, 1, gen); for (double beta : new double[]{0.1, 1, 2, 100}) { Gamma g1 = new Gamma(alpha, beta, gen); for (double x : seq(0, 0.9999, 0.001)) { assertEquals(
@Test(timeout=50000) public void testTimesSparseEfficiency() { Random raw = RandomUtils.getRandom(); Gamma gen = new Gamma(0.1, 0.1, raw);
@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); } }
@Test(timeout=50000) public void testTimesDenseEfficiency() { Random raw = RandomUtils.getRandom(); Gamma gen = new Gamma(0.1, 0.1, raw);
@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); } } }
/** * Constructs a Negative Binomial distribution which describes the probability of getting * a particular number of negative trials (k) before getting a fixed number of positive * trials (r) where each positive trial has probability (p) of being successful. * * @param r the required number of positive trials. * @param p the probability of success. * @param randomGenerator a uniform random number generator. */ public NegativeBinomial(int r, double p, Random randomGenerator) { setRandomGenerator(randomGenerator); this.r = r; this.p = p; this.gamma = new Gamma(r, 1, randomGenerator); this.poisson = new Poisson(0.0, randomGenerator); }
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 testPdf() { Random gen = RandomUtils.getRandom(); for (double alpha : new double[]{0.01, 0.1, 1, 2, 10, 100}) { for (double beta : new double[]{0.1, 1, 2, 100}) { Gamma g1 = new Gamma(alpha, beta, gen); for (double x : seq(0, 0.99, 0.1)) { double p = Math.pow(beta, alpha) * Math.pow(x, alpha - 1) * Math.exp(-beta * x - org.apache.mahout.math.jet.stat.Gamma.logGamma(alpha)); assertEquals(String.format(Locale.ENGLISH, "alpha=%.2f, beta=%.2f, x=%.2f\n", alpha, beta, x), p, g1.pdf(x), 1.0e-9); } } } } }
@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); } } }