class QuartileSkewnessCalculator extends Function1D<double[], Double> {

  @Override
  public Double evaluate(final double[] x) {
    Validate.notNull(x, "x");
    final int n = x.length;
    Validate.isTrue(n >= 3, "Need at least three points to calculate interquartile range");
    if (n == 3) {
      return (x[2] - 2 * x[1] + x[0]) / 2.;
    }
    final double[] copy = Arrays.copyOf(x, n);
    Arrays.sort(copy);
    double[] lower, upper;
    if (n % 2 == 0) {
      lower = Arrays.copyOfRange(copy, 0, n / 2);
      upper = Arrays.copyOfRange(copy, n / 2, n);
    } else {
      lower = Arrays.copyOfRange(copy, 0, n / 2 + 1);
      upper = Arrays.copyOfRange(copy, n / 2, n);
    }
    final double q1 = MEDIAN.evaluate(lower);
    final double q2 = MEDIAN.evaluate(x);
    final double q3 = MEDIAN.evaluate(upper);
    return (q1 - 2 * q2 + q3) / (q3 - q1);
  }

}