in commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/HypergeometricDistribution.java [320:352]
private int computeInverseProbability(double p, double q, boolean complement) {
if (p == 0) {
return lowerBound;
}
if (q == 0) {
return upperBound;
}
// Sum the PDF(x) until the appropriate p-value is obtained
// CDF: require smallest x where P(X<=x) >= p
// SF: require smallest x where P(X>x) <= q
// The choice of summation uses the mid-point.
// The test on the CDF or SF is based on the appropriate input p-value.
final double[] mid = getMidPoint();
final int m = (int) mid[0];
final double mp = mid[1];
final int midPointComparison = complement ?
Double.compare(1 - mp, q) :
Double.compare(p, mp);
if (midPointComparison < 0) {
return inverseLower(p, q, complement);
} else if (midPointComparison > 0) {
// Avoid floating-point summation error when the mid-point computed using the
// lower sum is different to the midpoint computed using the upper sum.
// Here we know the result must be above the midpoint so we can clip the result.
return Math.max(m + 1, inverseUpper(p, q, complement));
}
// Exact mid-point
return m;
}