PartialMomentCalculator extends Function1D<double[], Double> { @Override public Double evaluate(final double[] x) { Validate.notNull(x, "x"); Validate.isTrue(x.length > 0, "x cannot be empty"); final int n = x.length; final double[] copyX = Arrays.copyOf(x, n); Arrays.sort(copyX); double sum = 0; if (_useDownSide) { int i = 0; if (copyX[i] > _threshold) { return 0.; } while (i < n && copyX[i] < _threshold) { sum += (copyX[i] - _threshold) * (copyX[i] - _threshold); i++; } return Math.sqrt(sum / i); } int i = n - 1; int count = 0; if (copyX[i] < _threshold) { return 0.; } while (i >= 0 && copyX[i] > _threshold) { sum += (copyX[i] - _threshold) * (copyX[i] - _threshold); count++; i--; } return Math.sqrt(sum / count); } PartialMomentCalculator();  PartialMomentCalculator(final double threshold, final boolean useDownSide); @Override Double evaluate(final double[] x);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullArray() { UPSIDE1.evaluate((double[]) null); }
@Test public void test() { final double eps = 1e-15; assertEquals(UPSIDE1.evaluate(POSITIVE_X), 0.5, eps); assertEquals(DOWNSIDE1.evaluate(POSITIVE_X), 0, eps); assertEquals(UPSIDE1.evaluate(NEGATIVE_X), 0, eps); assertEquals(DOWNSIDE1.evaluate(NEGATIVE_X), 0.5, eps); assertEquals(UPSIDE2.evaluate(X1), 0.14, eps); assertEquals(DOWNSIDE2.evaluate(X2), 0.1, eps); }