Percentiles { public static void calcPercentileValues(final double[] values, final double[] percentileValues) { if(values == null || percentileValues == null) return; if(values.length == 1) { for(int i = 0; i < percentileValues.length; ++i) { percentileValues[i] = values[0]; } return; } int valueCount = values.length; int slotCount = percentileValues.length; double valuesPerSlot = (valueCount - 1) / (double) (slotCount - 1); percentileValues[0] = values[0]; for(int i = 1; i < percentileValues.length; ++i) { double valueIndex = i * valuesPerSlot; int valueLowerIndex = (int) floor(valueIndex); int valueUpperIndex = min((int) ceil(valueIndex), valueCount - 1); double valueLower = values[valueLowerIndex]; double valueUpper = values[valueUpperIndex]; double lowerFraction = capValue(1 - (valueIndex - valueLowerIndex), 0, 1); percentileValues[i] = lowerFraction * valueLower + (1 - lowerFraction) * valueUpper; } }  static double getPercentile(final double[] percentileValues, double value); static double getPercentile(final double[] percentileValues, double value, boolean useMaxMultiple); static double[] buildPercentiles(final double[] values); static void calcPercentileValues(final double[] values, final double[] percentileValues); static final double INVALID_VALUE; static final int PERCENTILE_COUNT; }
@Test public void testPercentileCreation() { int slots = 11; double[] percentileValues = new double[slots]; double[] values = { 1.0, 2.0, 3.0, 4.0, 5.0 }; calcPercentileValues(values, percentileValues); assertEquals(1.0, percentileValues[0], 0.001); assertEquals(1.4, percentileValues[1], 0.001); assertEquals(1.8, percentileValues[2], 0.001); assertEquals(2.2, percentileValues[3], 0.001); assertEquals(4.6, percentileValues[9], 0.001); assertEquals(5.0, percentileValues[10], 0.001); values = new double[51]; for(int i = 0; i < values.length; ++i) { values[i] = i; } calcPercentileValues(values, percentileValues); assertEquals(0, percentileValues[0], 0.001); assertEquals(5, percentileValues[1], 0.001); assertEquals(45, percentileValues[9], 0.001); assertEquals(50, percentileValues[10], 0.001); }