Gamma extends AbstractContinousDistribution { @Override public double pdf(double x) { if (x < 0) { throw new IllegalArgumentException(); } if (x == 0) { if (alpha == 1.0) { return rate; } else if (alpha < 1) { return Double.POSITIVE_INFINITY; } else { return 0; } } if (alpha == 1.0) { return rate * Math.exp(-x * rate); } return rate * Math.exp((alpha - 1.0) * Math.log(x * rate) - x * rate - logGamma(alpha)); } Gamma(double alpha, double rate, Random randomGenerator); @Override double cdf(double x); @Override double nextDouble(); double nextDouble(double alpha, double rate); @Override double pdf(double x); @Override String toString(); static double logGamma(double x);  }
@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); } } } }