You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2008/02/11 02:04:55 UTC

svn commit: r620368 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java site/xdoc/changes.xml test/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java

Author: psteitz
Date: Sun Feb 10 17:04:48 2008
New Revision: 620368

URL: http://svn.apache.org/viewvc?rev=620368&view=rev
Log:
Fixed AbstractIntegerDistribution cumulativeProbablility(-,-)
to correctly handle double arguments.
JIRA: MATH-184
Reported by Yegor Bryukhov

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java?rev=620368&r1=620367&r2=620368&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java Sun Feb 10 17:04:48 2008
@@ -62,6 +62,34 @@
     
     /**
      * 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,
      * this method represents the probability distribution function, or PDF,
      * for this distribution.

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=620368&r1=620367&r2=620368&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun Feb 10 17:04:48 2008
@@ -168,6 +168,10 @@
         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.

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java?rev=620368&r1=620367&r2=620368&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java Sun Feb 10 17:04:48 2008
@@ -156,6 +156,7 @@
         }           
     }
     
+    
     /**
      * Verifies that inverse cumulative probability density calculations match expected values
      * using current test instance data
@@ -184,6 +185,55 @@
      */
     public void testCumulativeProbabilities() throws Exception {
         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);
     }
     
     /**