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
This commit is contained in:
Phil Steitz 2008-02-11 01:04:48 +00:00
parent 3c9e28e533
commit 8eba63863c
3 changed files with 82 additions and 0 deletions

View File

@ -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 <code>x0</code> and <code>x1</code>,
* including the endpoints.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws IllegalArgumentException if <code>x0 > x1</code>
*/
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 &le; x). In other words,

View File

@ -168,6 +168,10 @@ Commons Math Release Notes</title>
Added getSumOfLogs method to SummaryStatistics and made SumOfLogs
instance used by GeometricMean configurable.
</action>
<action dev="psteitz" type="fix" issue="MATH-184" due-to="Yegor Bryukhov">
Fixed AbstractIntegerDistribution cumulativeProbablility(-,-)
to correctly handle double arguments.
</action>
</release>
<release version="1.1" date="2005-12-17"
description="This is a maintenance release containing bug fixes and enhancements.

View File

@ -156,6 +156,7 @@ public abstract class IntegerDistributionAbstractTest extends TestCase {
}
}
/**
* Verifies that inverse cumulative probability density calculations match expected values
* using current test instance data
@ -186,6 +187,55 @@ public abstract class IntegerDistributionAbstractTest extends TestCase {
verifyCumulativeProbabilities();
}
/**
* Verifies that floating point arguments are correctly handled by
* cumulativeProbablility(-,-)
* JIRA: MATH-184
*/
public void testFloatingPointArguments() throws Exception {
for (int i = 0; i < cumulativeTestPoints.length; i++) {
double arg = (double) cumulativeTestPoints[i];
assertEquals(
"Incorrect cumulative probability value returned for " +
cumulativeTestPoints[i],
cumulativeTestValues[i],
distribution.cumulativeProbability(arg), tolerance);
if (i < cumulativeTestPoints.length - 1) {
double arg2 = (double) cumulativeTestPoints[i + 1];
assertEquals("Inconsistent probability for discrete range " +
"[ " + arg + "," + arg2 + " ]",
distribution.cumulativeProbability(
cumulativeTestPoints[i],
cumulativeTestPoints[i + 1]),
distribution.cumulativeProbability(arg, arg2), tolerance);
arg = arg - Math.random();
arg2 = arg2 + Math.random();
assertEquals("Inconsistent probability for discrete range " +
"[ " + arg + "," + arg2 + " ]",
distribution.cumulativeProbability(
cumulativeTestPoints[i],
cumulativeTestPoints[i + 1]),
distribution.cumulativeProbability(arg, arg2), tolerance);
}
}
int one = 1;
int ten = 10;
int two = 2;
double oned = (double) one;
double twod = (double) two;
double tend = (double) ten;
assertEquals(distribution.cumulativeProbability(one, two),
distribution.cumulativeProbability(oned, twod), tolerance);
assertEquals(distribution.cumulativeProbability(one, two),
distribution.cumulativeProbability(oned - tolerance,
twod + 0.9), tolerance);
assertEquals(distribution.cumulativeProbability(two, ten),
distribution.cumulativeProbability(twod, tend), tolerance);
assertEquals(distribution.cumulativeProbability(two, ten),
distribution.cumulativeProbability(twod - tolerance,
tend + 0.9), tolerance);
}
/**
* Verifies that inverse cumulative probability density calculations match expected values
* using default test instance data