You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2014/06/17 18:30:35 UTC
svn commit: r1603217 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/stat/descriptive/rank/Percentile.java
test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java
Author: erans
Date: Tue Jun 17 16:30:35 2014
New Revision: 1603217
URL: http://svn.apache.org/r1603217
Log:
MATH-1129
Fix wrong sorting in the presence of NaN.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/descriptive/rank/Percentile.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/descriptive/rank/Percentile.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/descriptive/rank/Percentile.java?rev=1603217&r1=1603216&r2=1603217&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/descriptive/rank/Percentile.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/descriptive/rank/Percentile.java Tue Jun 17 16:30:35 2014
@@ -440,12 +440,18 @@ public class Percentile extends Abstract
* @param end index after the last element of the slice to sort
*/
private void insertionSort(final double[] work, final int begin, final int end) {
+ // Arrays.sort(work, begin, end); // Would also fix MATH-1129
for (int j = begin + 1; j < end; j++) {
final double saved = work[j];
int i = j - 1;
- while ((i >= begin) && (saved < work[i])) {
- work[i + 1] = work[i];
- i--;
+ while (i >= begin) {
+ final double wi = work[i];
+ if (saved < wi || Double.isNaN(wi)) {
+ work[i + 1] = wi;
+ i--;
+ } else {
+ break;
+ }
}
work[i + 1] = saved;
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java?rev=1603217&r1=1603216&r2=1603217&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java Tue Jun 17 16:30:35 2014
@@ -246,6 +246,36 @@ public class DescriptiveStatisticsTest {
}
}
+ @Test
+ public void testMath1129(){
+ final double[] data = new double[] {
+ -0.012086732064244697,
+ -0.24975668704012527,
+ 0.5706168483164684,
+ -0.322111769955327,
+ 0.24166759508327315,
+ Double.NaN,
+ 0.16698443218942854,
+ -0.10427763937565114,
+ -0.15595963093172435,
+ -0.028075857595882995,
+ -0.24137994506058857,
+ 0.47543170476574426,
+ -0.07495595384947631,
+ 0.37445697625436497,
+ -0.09944199541668033
+ };
+
+ final DescriptiveStatistics ds = new DescriptiveStatistics(data);
+
+ final double t = ds.getPercentile(75);
+ final double o = ds.getPercentile(25);
+
+ final double iqr = t - o;
+ // System.out.println(String.format("25th percentile %s 75th percentile %s", o, t));
+ Assert.assertTrue(iqr >= 0);
+ }
+
public void checkremoval(DescriptiveStatistics dstat, int wsize,
double mean1, double mean2, double mean3) {