Fixed BinomialDistribution to deal with degenerate cases correctly.

JIRA: MATH-1136
Reported and patched by Aleksei Dievskii.



git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1609775 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2014-07-11 19:22:18 +00:00
parent a7363a2ae6
commit cc4ab51ee9
3 changed files with 24 additions and 0 deletions

View File

@ -73,6 +73,9 @@ Users are encouraged to upgrade to this version as this release not
2. A few methods in the FastMath class are in fact slower that their
counterpart in either Math or StrictMath (cf. MATH-740 and MATH-901).
">
<ation dev="psteitz" type="fix" issue="MATH-1136" due-to="Aleksei Dievskii">
Fixed BinomialDistribution to deal with degenerate cases correctly.
</action>
<action dev="tn" type="fix" issue="MATH-1135" due-to="Guillaume Marceau">
"MonotoneChain" failed to generate a convex hull if only a minimal hull
shall be created (includeCollinearPoints=false) and collinear hull points

View File

@ -106,6 +106,9 @@ public class BinomialDistribution extends AbstractIntegerDistribution {
/** {@inheritDoc} **/
@Override
public double logProbability(int x) {
if (numberOfTrials == 0) {
return (x == 0) ? 0. : Double.NEGATIVE_INFINITY;
}
double ret;
if (x < 0 || x > numberOfTrials) {
ret = Double.NEGATIVE_INFINITY;

View File

@ -127,6 +127,24 @@ public class BinomialDistributionTest extends IntegerDistributionAbstractTest {
Assert.assertEquals(dist.getSupportUpperBound(), 5);
}
/** Test degenerate case n = 0 */
@Test
public void testDegenerate2() {
BinomialDistribution dist = new BinomialDistribution(0, 0.01d);
setDistribution(dist);
setCumulativeTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
setCumulativeTestValues(new double[] { 0d, 1d, 1d, 1d, 1d, 1d });
setDensityTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
setDensityTestValues(new double[] { 0d, 1d, 0d, 0d, 0d, 0d });
setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
setInverseCumulativeTestValues(new int[] { 0, 0 });
verifyDensities();
verifyCumulativeProbabilities();
verifyInverseCumulativeProbabilities();
Assert.assertEquals(dist.getSupportLowerBound(), 0);
Assert.assertEquals(dist.getSupportUpperBound(), 0);
}
@Test
public void testMoments() {
final double tol = 1e-9;