in commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/GammaDistribution.java [139:161]
public double logDensity(double x) {
if (x <= SUPPORT_LO ||
x >= SUPPORT_HI) {
// Special case x=0
if (x == SUPPORT_LO && shape <= 1) {
return shape == 1 ?
-Math.log(scale) :
Double.POSITIVE_INFINITY;
}
return Double.NEGATIVE_INFINITY;
}
final double y = x / scale;
// More accurate to log the density when it is finite.
// See NUMBERS-174: 'Log of the Gamma P Derivative'
final double p = RegularizedGamma.P.derivative(shape, y) / scale;
if (p <= Double.MAX_VALUE && p >= Double.MIN_NORMAL) {
return Math.log(p);
}
// Use the log computation
return minusLogGammaShapeMinusLogScale - y + Math.log(y) * (shape - 1);
}