Cache the domain support
diff --git a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/HypergeometricDistribution.java b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/HypergeometricDistribution.java
index 8d4596c..4b3e16c 100644
--- a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/HypergeometricDistribution.java
+++ b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/HypergeometricDistribution.java
@@ -27,6 +27,10 @@
private final int populationSize;
/** The sample size. */
private final int sampleSize;
+ /** The lower bound of the support (inclusive). */
+ private final int lowerBound;
+ /** The upper bound of the support (inclusive). */
+ private final int upperBound;
/**
* Creates a new hypergeometric distribution.
@@ -66,6 +70,8 @@
this.numberOfSuccesses = numberOfSuccesses;
this.populationSize = populationSize;
this.sampleSize = sampleSize;
+ lowerBound = getLowerDomain(populationSize, numberOfSuccesses, sampleSize);
+ upperBound = getUpperDomain(numberOfSuccesses, sampleSize);
}
/** {@inheritDoc} */
@@ -73,13 +79,12 @@
public double cumulativeProbability(int x) {
double ret;
- final int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize);
- if (x < domain[0]) {
+ if (x < lowerBound) {
ret = 0.0;
- } else if (x >= domain[1]) {
+ } else if (x >= upperBound) {
ret = 1.0;
} else {
- ret = innerCumulativeProbability(domain[0], x);
+ ret = innerCumulativeProbability(lowerBound, x);
}
return ret;
@@ -90,32 +95,18 @@
public double survivalProbability(int x) {
double ret;
- final int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize);
- if (x < domain[0]) {
+ if (x < lowerBound) {
ret = 1.0;
- } else if (x >= domain[1]) {
+ } else if (x >= upperBound) {
ret = 0.0;
} else {
- ret = innerCumulativeProbability(domain[1], x + 1);
+ ret = innerCumulativeProbability(upperBound, x + 1);
}
return ret;
}
/**
- * Return the domain for the given hypergeometric distribution parameters.
- *
- * @param n Population size.
- * @param m Number of successes in the population.
- * @param k Sample size.
- * @return a two element array containing the lower and upper bounds of the
- * hypergeometric distribution.
- */
- private static int[] getDomain(int n, int m, int k) {
- return new int[] {getLowerDomain(n, m, k), getUpperDomain(m, k)};
- }
-
- /**
* Return the lowest domain value for the given hypergeometric distribution
* parameters.
*
@@ -179,8 +170,7 @@
public double logProbability(int x) {
double ret;
- final int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize);
- if (x < domain[0] || x > domain[1]) {
+ if (x < lowerBound || x > upperBound) {
ret = Double.NEGATIVE_INFINITY;
} else {
final double p = (double) sampleSize / (double) populationSize;
@@ -221,13 +211,12 @@
public double upperCumulativeProbability(int x) {
double ret;
- final int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize);
- if (x <= domain[0]) {
+ if (x <= lowerBound) {
ret = 1.0;
- } else if (x > domain[1]) {
+ } else if (x > upperBound) {
ret = 0.0;
} else {
- ret = innerCumulativeProbability(domain[1], x);
+ ret = innerCumulativeProbability(upperBound, x);
}
return ret;
@@ -304,8 +293,7 @@
*/
@Override
public int getSupportLowerBound() {
- return Math.max(0,
- getSampleSize() + getNumberOfSuccesses() - getPopulationSize());
+ return lowerBound;
}
/**
@@ -318,7 +306,7 @@
*/
@Override
public int getSupportUpperBound() {
- return Math.min(getNumberOfSuccesses(), getSampleSize());
+ return upperBound;
}
/**