commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1407852 - in /commons/proper/math/trunk/src: changes/ main/java/org/apache/commons/math3/stat/ranking/ test/java/org/apache/commons/math3/stat/ranking/
Date Sat, 10 Nov 2012 17:14:52 GMT
Author: tn
Date: Sat Nov 10 17:14:51 2012
New Revision: 1407852

URL: http://svn.apache.org/viewvc?rev=1407852&view=rev
Log:
[MATH-893] Add new NaNStrategy FAILED, thanks to Patrick Meyer.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaNStrategy.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.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=1407852&r1=1407851&r2=1407852&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Sat Nov 10 17:14:51 2012
@@ -52,6 +52,11 @@ If the output is not quite correct, chec
   <body>
     <release version="3.1" date="TBD" description="
 ">
+      <action dev="tn" type="add" issue="MATH-893" due-to="Patrick Meyer">
+        Add new "NaNStrategy": FAILED, used in "RankingAlgorithm" implementations.
+        Any encountered input value that succeeds a "Double#isNaN" check, results in a
+        "NotANumberException".
+      </action>
       <action dev="tn" type="add" issue="MATH-892">
         Add new constructor to "SpearmansCorrelation" class which allows to specify the
         "RankingAlgorithm" to be used.

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaNStrategy.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaNStrategy.java?rev=1407852&r1=1407851&r2=1407852&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaNStrategy.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaNStrategy.java
Sat Nov 10 17:14:51 2012
@@ -28,6 +28,8 @@ package org.apache.commons.math3.stat.ra
  * <li>FIXED - NaNs are left "in place," that is the rank transformation is
  * applied to the other elements in the input array, but the NaN elements
  * are returned unchanged.</li>
+ * <li>FAILED - If any NaN is encountered in the input array, an appropriate
+ * exception is thrown</li>
  * </ul>
  *
  * @since 2.0
@@ -45,5 +47,8 @@ public enum NaNStrategy {
     REMOVED,
 
     /** NaNs are left in place */
-    FIXED
+    FIXED,
+
+    /** NaNs result in an exception */
+    FAILED
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java?rev=1407852&r1=1407851&r2=1407852&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java
Sat Nov 10 17:14:51 2012
@@ -23,6 +23,7 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.apache.commons.math3.exception.MathInternalError;
+import org.apache.commons.math3.exception.NotANumberException;
 import org.apache.commons.math3.random.RandomData;
 import org.apache.commons.math3.random.RandomDataImpl;
 import org.apache.commons.math3.random.RandomGenerator;
@@ -186,6 +187,8 @@ public class NaturalRanking implements R
      *
      * @param data array to be ranked
      * @return array of ranks
+     * @throws NotANumberException if the selected {@link NaNStrategy} is {@code FAILED}
+     * and a {@link Double#NaN} is encountered in the input data
      */
     public double[] rank(double[] data) {
 
@@ -210,6 +213,12 @@ public class NaturalRanking implements R
             case FIXED:   // Record positions of NaNs
                 nanPositions = getNanPositions(ranks);
                 break;
+            case FAILED:
+                nanPositions = getNanPositions(ranks);
+                if (nanPositions.size() > 0) {
+                    throw new NotANumberException();
+                }
+                break;
             default: // this should not happen unless NaNStrategy enum is changed
                 throw new MathInternalError();
         }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.java?rev=1407852&r1=1407851&r2=1407852&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.java
Sat Nov 10 17:14:51 2012
@@ -17,6 +17,7 @@
 package org.apache.commons.math3.stat.ranking;
 
 import org.apache.commons.math3.TestUtils;
+import org.apache.commons.math3.exception.NotANumberException;
 import org.apache.commons.math3.random.JDKRandomGenerator;
 import org.apache.commons.math3.random.RandomGenerator;
 import org.junit.Test;
@@ -194,4 +195,20 @@ public class NaturalRankingTest {
         correctRanks = new double[] { 3, 4, 1.5, 1.5 };
         TestUtils.assertEquals(correctRanks, ranks, 0d);
     }
+    
+    @Test(expected=NotANumberException.class)
+    public void testNaNsFailed() {
+        double[] data = { 0, Double.POSITIVE_INFINITY, Double.NaN, Double.NEGATIVE_INFINITY
};
+        NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED);
+        ranking.rank(data);
+    }
+    
+    @Test
+    public void testNoNaNsFailed() {
+        double[] data = { 1, 2, 3, 4 };
+        NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED);
+        double[] ranks = ranking.rank(data);
+        TestUtils.assertEquals(data, ranks, 0d);
+    }
+    
 }



Mime
View raw message