diff --git a/src/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java b/src/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java index 23d32f224..41210c5e6 100644 --- a/src/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java +++ b/src/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java @@ -60,6 +60,34 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution return cumulativeProbability((int) Math.floor(x)); } + /** + * For a random variable X whose values are distributed according + * to this distribution, this method returns P(x0 ≤ X ≤ x1). + * + * @param x0 the (inclusive) lower bound + * @param x1 the (inclusive) upper bound + * @return the probability that a random variable with this distribution + * will take a value between x0 and x1, + * including the endpoints. + * @throws MathException if the cumulative probability can not be + * computed due to convergence or other numerical errors. + * @throws IllegalArgumentException if x0 > x1 + */ + public double cumulativeProbability(double x0, double x1) + throws MathException { + if (x0 > x1) { + throw new IllegalArgumentException + ("lower endpoint must be less than or equal to upper endpoint"); + } + if (Math.floor(x0) < x0) { + return cumulativeProbability(((int) Math.floor(x0)) + 1, + (int) Math.floor(x1)); // don't want to count mass below x0 + } else { // x0 is mathematical integer, so use as is + return cumulativeProbability((int) Math.floor(x0), + (int) Math.floor(x1)); + } + } + /** * For a random variable X whose values are distributed according * to this distribution, this method returns P(X ≤ x). In other words, diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index e5860772a..1cc8a873c 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -168,6 +168,10 @@ Commons Math Release Notes Added getSumOfLogs method to SummaryStatistics and made SumOfLogs instance used by GeometricMean configurable. + + Fixed AbstractIntegerDistribution cumulativeProbablility(-,-) + to correctly handle double arguments. +