[MATH-893] Add new NaNStrategy FAILED, thanks to Patrick Meyer.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1407852 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
def7381851
commit
16acabd53f
|
@ -52,6 +52,11 @@ If the output is not quite correct, check for invisible trailing spaces!
|
|||
<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.
|
||||
|
|
|
@ -28,6 +28,8 @@ package org.apache.commons.math3.stat.ranking;
|
|||
* <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
|
||||
}
|
||||
|
|
|
@ -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 RankingAlgorithm {
|
|||
*
|
||||
* @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 RankingAlgorithm {
|
|||
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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue