StatisticsUtilities { public static double percentile(final double[] values, final double p) { if ((p > 100) || (p <= 0)) { throw new IllegalArgumentException("invalid quantile value: " + p); } double n = (double) values.length; if (n == 0) { return Double.NaN; } if (n == 1) { return values[0]; } double pos = p * (n + 1) / 100; double fpos = Math.floor(pos); int intPos = (int) fpos; double dif = pos - fpos; double[] sorted = new double[values.length]; System.arraycopy(values, 0, sorted, 0, values.length); Arrays.sort(sorted); if (pos < 1) { return sorted[0]; } if (pos >= n) { return sorted[values.length - 1]; } double lower = sorted[intPos - 1]; double upper = sorted[intPos]; return lower + dif * (upper - lower); }  static double percentile(final double[] values, final double p); static double median(double[] xs); static int argmax(double[] xs); static int argmax(int[] xs); @SuppressWarnings("unchecked") static int argmax(int[] xs, int rank); static int argmin(double[] xs); static int argmin(int[] xs); static int softmax(double[] ps, double temperature); static int probabilistic(double[] ps); static double[] distribution(double[] raw); static double[] distribution(int[] raw); static double mode(double[] xs); static double mode(double[] xs, double epsilon); static double maximum(double[] xs); static int maximum(Collection<Integer> xs); static long maximum(long[] xs); static double minimum(double[] xs); static double average(double[] xs); static int sum(int[] vals); static double sum(double[] vals); static int sum(Collection<Integer> xs); static double sumDouble(Collection<Double> xs); static double populationStandardDeviation(double[] xs); static double sampleStandardDeviation(double[] xs); static double populationVariance(double[] xs); static double sampleVariance(double[] xs); static double sumOfSquares(double[] xs); static double instantaneousErrorEnergy(double desired, double output); static double instantaneousTotalErrorEnergy(ArrayList<Pair<Double, Double>> pairs); static double averageSquaredErrorEnergy(ArrayList<ArrayList<Pair<Double, Double>>> samples); static double tValue(int runs);  }
@Test public void testPercentile() { final double[] values = { 1, 2, 3, 4, 5 }; final double p = 1.0; double answer = StatisticsUtilities.percentile(values, p); assertEquals(answer, 1.0, ERROR); }