From 0073facaf9f32999a63482acc602d65ea711ee92 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Tue, 20 Nov 2012 21:13:54 +0000 Subject: [PATCH] [MATH-906] Use NaNStrategy.FAILED as default in NaturalRanking. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1411880 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 3 ++ .../math3/stat/ranking/NaturalRanking.java | 4 +-- .../stat/ranking/NaturalRankingTest.java | 31 +++++++++++++------ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 92f4747e2..7bbd7768a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -52,6 +52,9 @@ If the output is not quite correct, check for invisible trailing spaces! + + Use "NaNStrategy#FAILED" as default strategy in "NaturalRanking". + Added a new "SynchronizedRandomGenerator" that wraps another "RandomGenerator" with all methods being synchronized, thus diff --git a/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java b/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java index 3f669d6f5..83c61cdfa 100644 --- a/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java +++ b/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java @@ -35,7 +35,7 @@ import org.apache.commons.math3.util.FastMath; *

NaNs are treated according to the configured {@link NaNStrategy} and ties * are handled using the selected {@link TiesStrategy}. * Configuration settings are supplied in optional constructor arguments. - * Defaults are {@link NaNStrategy#MAXIMAL} and {@link TiesStrategy#AVERAGE}, + * Defaults are {@link NaNStrategy#FAILED} and {@link TiesStrategy#AVERAGE}, * respectively. When using {@link TiesStrategy#RANDOM}, a * {@link RandomGenerator} may be supplied as a constructor argument.

*

Examples: @@ -72,7 +72,7 @@ import org.apache.commons.math3.util.FastMath; public class NaturalRanking implements RankingAlgorithm { /** default NaN strategy */ - public static final NaNStrategy DEFAULT_NAN_STRATEGY = NaNStrategy.MAXIMAL; + public static final NaNStrategy DEFAULT_NAN_STRATEGY = NaNStrategy.FAILED; /** default ties strategy */ public static final TiesStrategy DEFAULT_TIES_STRATEGY = TiesStrategy.AVERAGE; diff --git a/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.java b/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.java index 4fc51cc45..c63e1227f 100644 --- a/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.java +++ b/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.math3.stat.ranking; +import junit.framework.Assert; + import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.random.JDKRandomGenerator; @@ -40,20 +42,31 @@ public class NaturalRankingTest { private final double[] allSame = { 0, 0, 0, 0 }; @Test - public void testDefault() { // Ties averaged, NaNs maximal + public void testDefault() { // Ties averaged, NaNs failed NaturalRanking ranking = new NaturalRanking(); - double[] ranks = ranking.rank(exampleData); - double[] correctRanks = { 5, 3, 6, 7, 3, 8, 9, 1, 3 }; - TestUtils.assertEquals(correctRanks, ranks, 0d); + double[] ranks; + + try { + ranks = ranking.rank(exampleData); + Assert.fail("expected NotANumberException due to NaNStrategy.FAILED"); + } catch (NotANumberException e) { + // expected + } + ranks = ranking.rank(tiesFirst); - correctRanks = new double[] { 1.5, 1.5, 4, 3, 5 }; + double[] correctRanks = new double[] { 1.5, 1.5, 4, 3, 5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesLast); correctRanks = new double[] { 3.5, 3.5, 2, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); - ranks = ranking.rank(multipleNaNs); - correctRanks = new double[] { 1, 2, 3.5, 3.5 }; - TestUtils.assertEquals(correctRanks, ranks, 0d); + + try { + ranks = ranking.rank(multipleNaNs); + Assert.fail("expected NotANumberException due to NaNStrategy.FAILED"); + } catch (NotANumberException e) { + // expected + } + ranks = ranking.rank(multipleTies); correctRanks = new double[] { 3, 2, 4.5, 4.5, 6.5, 6.5, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); @@ -64,7 +77,7 @@ public class NaturalRankingTest { @Test public void testNaNsMaximalTiesMinimum() { - NaturalRanking ranking = new NaturalRanking(TiesStrategy.MINIMUM); + NaturalRanking ranking = new NaturalRanking(NaNStrategy.MAXIMAL, TiesStrategy.MINIMUM); double[] ranks = ranking.rank(exampleData); double[] correctRanks = { 5, 2, 6, 7, 2, 8, 9, 1, 2 }; TestUtils.assertEquals(correctRanks, ranks, 0d);