@Test public void testMedian() { double expResult = 5.5d; double result = SampleStats.median(values, true); assertEquals(expResult, result, TOL); }
@SuppressWarnings("empty-statement") public static double median(Double[] values, boolean ignoreNaN) { if (values == null) { return Double.NaN; } List<Double> nonNaNValues = CollectionFactory.list(); nonNaNValues.addAll(Arrays.asList(values)); if (ignoreNaN) { while (nonNaNValues.remove(Double.NaN)) ; } if (nonNaNValues.isEmpty()) { return Double.NaN; } else if (nonNaNValues.size() == 1) { return nonNaNValues.get(0); } else if (nonNaNValues.size() == 2) { return (nonNaNValues.get(0) + nonNaNValues.get(1)) / 2; } Collections.sort(nonNaNValues); int midHi = nonNaNValues.size() / 2; int midLo = midHi - 1; boolean even = nonNaNValues.size() % 2 == 0; Double result = 0.0d; int k = 0; for (Double val : nonNaNValues) { if (k == midHi) { if (!even) { return val; } else { result += val; return result / 2; } } else if (even && k == midLo) { result += val; } k++ ; } return 0; }
SampleStats { @SuppressWarnings("empty-statement") public static double median(Double[] values, boolean ignoreNaN) { if (values == null) { return Double.NaN; } List<Double> nonNaNValues = CollectionFactory.list(); nonNaNValues.addAll(Arrays.asList(values)); if (ignoreNaN) { while (nonNaNValues.remove(Double.NaN)) ; } if (nonNaNValues.isEmpty()) { return Double.NaN; } else if (nonNaNValues.size() == 1) { return nonNaNValues.get(0); } else if (nonNaNValues.size() == 2) { return (nonNaNValues.get(0) + nonNaNValues.get(1)) / 2; } Collections.sort(nonNaNValues); int midHi = nonNaNValues.size() / 2; int midLo = midHi - 1; boolean even = nonNaNValues.size() % 2 == 0; Double result = 0.0d; int k = 0; for (Double val : nonNaNValues) { if (k == midHi) { if (!even) { return val; } else { result += val; return result / 2; } } else if (even && k == midLo) { result += val; } k++ ; } return 0; } }
SampleStats { @SuppressWarnings("empty-statement") public static double median(Double[] values, boolean ignoreNaN) { if (values == null) { return Double.NaN; } List<Double> nonNaNValues = CollectionFactory.list(); nonNaNValues.addAll(Arrays.asList(values)); if (ignoreNaN) { while (nonNaNValues.remove(Double.NaN)) ; } if (nonNaNValues.isEmpty()) { return Double.NaN; } else if (nonNaNValues.size() == 1) { return nonNaNValues.get(0); } else if (nonNaNValues.size() == 2) { return (nonNaNValues.get(0) + nonNaNValues.get(1)) / 2; } Collections.sort(nonNaNValues); int midHi = nonNaNValues.size() / 2; int midLo = midHi - 1; boolean even = nonNaNValues.size() % 2 == 0; Double result = 0.0d; int k = 0; for (Double val : nonNaNValues) { if (k == midHi) { if (!even) { return val; } else { result += val; return result / 2; } } else if (even && k == midLo) { result += val; } k++ ; } return 0; }  }
SampleStats { @SuppressWarnings("empty-statement") public static double median(Double[] values, boolean ignoreNaN) { if (values == null) { return Double.NaN; } List<Double> nonNaNValues = CollectionFactory.list(); nonNaNValues.addAll(Arrays.asList(values)); if (ignoreNaN) { while (nonNaNValues.remove(Double.NaN)) ; } if (nonNaNValues.isEmpty()) { return Double.NaN; } else if (nonNaNValues.size() == 1) { return nonNaNValues.get(0); } else if (nonNaNValues.size() == 2) { return (nonNaNValues.get(0) + nonNaNValues.get(1)) / 2; } Collections.sort(nonNaNValues); int midHi = nonNaNValues.size() / 2; int midLo = midHi - 1; boolean even = nonNaNValues.size() % 2 == 0; Double result = 0.0d; int k = 0; for (Double val : nonNaNValues) { if (k == midHi) { if (!even) { return val; } else { result += val; return result / 2; } } else if (even && k == midLo) { result += val; } k++ ; } return 0; }  static double max(Double[] values, boolean ignoreNaN); static double mean(Double[] values, boolean ignoreNaN); static double min(Double[] values, boolean ignoreNaN); @SuppressWarnings("empty-statement") static double median(Double[] values, boolean ignoreNaN); @SuppressWarnings("empty-statement") static double mode(Double[] values, boolean ignoreNaN); static double range(Double[] values, boolean ignoreNaN); static double variance(Double[] values, boolean ignoreNaN); static double sdev(Double[] values, boolean ignoreNaN); static double sum(Double[] values, boolean ignoreNaN); }
SampleStats { @SuppressWarnings("empty-statement") public static double median(Double[] values, boolean ignoreNaN) { if (values == null) { return Double.NaN; } List<Double> nonNaNValues = CollectionFactory.list(); nonNaNValues.addAll(Arrays.asList(values)); if (ignoreNaN) { while (nonNaNValues.remove(Double.NaN)) ; } if (nonNaNValues.isEmpty()) { return Double.NaN; } else if (nonNaNValues.size() == 1) { return nonNaNValues.get(0); } else if (nonNaNValues.size() == 2) { return (nonNaNValues.get(0) + nonNaNValues.get(1)) / 2; } Collections.sort(nonNaNValues); int midHi = nonNaNValues.size() / 2; int midLo = midHi - 1; boolean even = nonNaNValues.size() % 2 == 0; Double result = 0.0d; int k = 0; for (Double val : nonNaNValues) { if (k == midHi) { if (!even) { return val; } else { result += val; return result / 2; } } else if (even && k == midLo) { result += val; } k++ ; } return 0; }  static double max(Double[] values, boolean ignoreNaN); static double mean(Double[] values, boolean ignoreNaN); static double min(Double[] values, boolean ignoreNaN); @SuppressWarnings("empty-statement") static double median(Double[] values, boolean ignoreNaN); @SuppressWarnings("empty-statement") static double mode(Double[] values, boolean ignoreNaN); static double range(Double[] values, boolean ignoreNaN); static double variance(Double[] values, boolean ignoreNaN); static double sdev(Double[] values, boolean ignoreNaN); static double sum(Double[] values, boolean ignoreNaN);  }