in commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/BetaDistribution.java [126:159]
public double logDensity(double x) {
if (x < 0 || x > 1) {
return Double.NEGATIVE_INFINITY;
} else if (x == 0) {
if (alpha < 1) {
// Distribution is not valid when x=0, alpha<1
// due to a divide by zero error.
// Do not raise an exception and return the limit.
return Double.POSITIVE_INFINITY;
}
// Special case of cancellation: x^(a-1) (1-x)^(b-1) / B(a, b) = 1 / B(a, b)
if (alpha == 1) {
return -logBeta;
}
return Double.NEGATIVE_INFINITY;
} else if (x == 1) {
if (beta < 1) {
// Distribution is not valid when x=1, beta<1
// due to a divide by zero error.
// Do not raise an exception and return the limit.
return Double.POSITIVE_INFINITY;
}
// Special case of cancellation: x^(a-1) (1-x)^(b-1) / B(a, b) = 1 / B(a, b)
if (beta == 1) {
return -logBeta;
}
return Double.NEGATIVE_INFINITY;
}
// Log computation
final double logX = Math.log(x);
final double log1mX = Math.log1p(-x);
return (alpha - 1) * logX + (beta - 1) * log1mX - logBeta;
}