From 8eba63863c102b144fe43d008baf719997599fc9 Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Mon, 11 Feb 2008 01:04:48 +0000 Subject: [PATCH] Fixed AbstractIntegerDistribution cumulativeProbablility(-,-) to correctly handle double arguments. JIRA: MATH-184 Reported by Yegor Bryukhov git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@620368 13f79535-47bb-0310-9956-ffa450edef68 --- .../AbstractIntegerDistribution.java | 28 +++++++++++ src/site/xdoc/changes.xml | 4 ++ .../IntegerDistributionAbstractTest.java | 50 +++++++++++++++++++ 3 files changed, 82 insertions(+) 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. +