commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1566274 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java test/java/org/apache/commons/math3/distribution/EnumeratedRealDistributionTest.java
Date Sun, 09 Feb 2014 11:21:28 GMT
Author: tn
Date: Sun Feb  9 11:21:28 2014
New Revision: 1566274

URL: http://svn.apache.org/r1566274
Log:
[MATH-1065] Fix EnumeratedRealDistribution.inverseCumulativeProbability. Thanks to matteodg
and Phil.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/EnumeratedRealDistributionTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1566274&r1=1566273&r2=1566274&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Sun Feb  9 11:21:28 2014
@@ -51,6 +51,11 @@ If the output is not quite correct, chec
   </properties>
   <body>
     <release version="3.3" date="TBD" description="TBD">
+      <action dev="tn" type="fix" issue="MATH-1065" due-to="matteodg">
+        Calculating the inverse cumulative probability of an "EnumeratedRealDistribution"
+        will now return the correct result according to the selected enumerated probability
+        mass function.
+      </action>
       <action dev="erans" type="update" issue="MATH-1050">
         Deprecated "ArithmeticUtils#pow(int, long)" and "ArithmeticUtils#pow(long, long)"
         in favor of corresponding methods "ArithmeticUtils#pow(..., int)".

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java?rev=1566274&r1=1566273&r2=1566274&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.java
Sun Feb  9 11:21:28 2014
@@ -18,11 +18,13 @@ package org.apache.commons.math3.distrib
 
 import java.util.ArrayList;
 import java.util.List;
+
 import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.MathArithmeticException;
 import org.apache.commons.math3.exception.NotANumberException;
 import org.apache.commons.math3.exception.NotFiniteNumberException;
 import org.apache.commons.math3.exception.NotPositiveException;
+import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.random.RandomGenerator;
 import org.apache.commons.math3.random.Well19937c;
 import org.apache.commons.math3.util.Pair;
@@ -138,6 +140,33 @@ public class EnumeratedRealDistribution 
 
     /**
      * {@inheritDoc}
+     */
+    @Override
+    public double inverseCumulativeProbability(final double p) throws OutOfRangeException
{
+        if (p < 0.0 || p > 1.0) {
+            throw new OutOfRangeException(p, 0, 1);
+        }
+
+        double probability = 0;
+        double x = getSupportLowerBound();
+        for (final Pair<Double, Double> sample : innerDistribution.getPmf()) {
+            if (sample.getValue() == 0.0) {
+                continue;
+            }
+
+            probability += sample.getValue();
+            x = sample.getKey();
+
+            if (probability >= p) {
+                break;
+            }
+        }
+
+        return x;
+    }
+
+    /**
+     * {@inheritDoc}
      *
      * @return {@code sum(singletons[i] * probabilities[i])}
      */

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/EnumeratedRealDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/EnumeratedRealDistributionTest.java?rev=1566274&r1=1566273&r2=1566274&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/EnumeratedRealDistributionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/EnumeratedRealDistributionTest.java
Sun Feb  9 11:21:28 2014
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.math3.distribution;
 
+import static org.junit.Assert.assertEquals;
+
 import java.util.ArrayList;
 import java.util.List;
 
@@ -213,4 +215,46 @@ public class EnumeratedRealDistributionT
         Assert.assertEquals(1, new EnumeratedDistribution<Object>(list).sample(1).length);
     }
 
+    @Test
+    public void testIssue1065() {
+        // Test Distribution for inverseCumulativeProbability
+        //
+        //         ^
+        //         |
+        // 1.000   +--------------------------------o===============
+        //         |                               3|
+        //         |                                |
+        //         |                             1o=
+        // 0.750   +-------------------------> o==  .
+        //         |                          3|  . .
+        //         |                   0       |  . .
+        // 0.5625  +---------------> o==o======   . .
+        //         |                 |  .      .  . .
+        //         |                 |  .      .  . .
+        //         |                5|  .      .  . .
+        //         |                 |  .      .  . .
+        //         |             o===   .      .  . .
+        //         |             |   .  .      .  . .
+        //         |            4|   .  .      .  . .
+        //         |             |   .  .      .  . .
+        // 0.000   +=============----+--+------+--+-+--------------->
+        //                      14  18 21     28 31 33
+        //
+        // sum  = 4+5+0+3+1+3 = 16
+        
+        EnumeratedRealDistribution distribution = new EnumeratedRealDistribution(
+                new double[] { 14.0, 18.0, 21.0, 28.0, 31.0, 33.0 },
+                new double[] { 4.0 / 16.0, 5.0 / 16.0, 0.0 / 16.0, 3.0 / 16.0, 1.0 / 16.0,
3.0 / 16.0 });
+        
+        assertEquals(14.0, distribution.inverseCumulativeProbability(0.0000), 0.0);
+        assertEquals(14.0, distribution.inverseCumulativeProbability(0.2500), 0.0);
+        assertEquals(33.0, distribution.inverseCumulativeProbability(1.0000), 0.0);
+
+        assertEquals(18.0, distribution.inverseCumulativeProbability(0.5000), 0.0);
+        assertEquals(18.0, distribution.inverseCumulativeProbability(0.5624), 0.0);
+        assertEquals(28.0, distribution.inverseCumulativeProbability(0.5626), 0.0);
+        assertEquals(31.0, distribution.inverseCumulativeProbability(0.7600), 0.0);
+        assertEquals(18.0, distribution.inverseCumulativeProbability(0.5625), 0.0);
+        assertEquals(28.0, distribution.inverseCumulativeProbability(0.7500), 0.0);
+    }
 }



Mime
View raw message