Percentile extends AbstractUnivariateStatistic implements Serializable { public double evaluate(final double p) throws MathIllegalArgumentException { return evaluate(getDataRef(), p); } Percentile();  Percentile(final double quantile);  Percentile(final Percentile original); protected  Percentile(final double quantile,
                         final EstimationType estimationType,
                         final NaNStrategy nanStrategy,
                         final KthSelector kthSelector); @Override void setData(final double[] values); @Override void setData(final double[] values, final int begin, final int length); double evaluate(final double p); double evaluate(final double[] values, final double p); @Override double evaluate(final double[] values, final int start, final int length); double evaluate(final double[] values, final int begin,
                           final int length, final double p); double getQuantile(); void setQuantile(final double p); @Override Percentile copy(); @Deprecated static void copy(final Percentile source, final Percentile dest); EstimationType getEstimationType(); Percentile withEstimationType(final EstimationType newEstimationType); NaNStrategy getNaNStrategy(); Percentile withNaNStrategy(final NaNStrategy newNaNStrategy); KthSelector getKthSelector(); PivotingStrategyInterface getPivotingStrategy(); Percentile withKthSelector(final KthSelector newKthSelector);  }
@Test public void testHighPercentile(){ final double[] d = new double[]{1, 2, 3}; final Percentile p = new Percentile(75); Assert.assertEquals(3.0, p.evaluate(d), 1.0e-5); }
@Test public void testLowPercentile() { final double[] d = new double[] {0, 1}; final Percentile p = new Percentile(25); Assert.assertEquals(0d, p.evaluate(d), Double.MIN_VALUE); }
@Test public void testNISTExample() { final double[] d = new double[] {95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.0610, 95.1591, 95.1195, 95.1772, 95.0925, 95.1990, 95.1682 }; final Percentile p = new Percentile(90); Assert.assertEquals(95.1981, p.evaluate(d), 1.0e-4); Assert.assertEquals(95.1990, p.evaluate(d,0,d.length, 100d), 0); }
@Test public void test5() { final Percentile percentile = new Percentile(5); Assert.assertEquals(this.percentile5, percentile.evaluate(testArray), getTolerance()); }
@Test public void testNullEmpty() { final Percentile percentile = new Percentile(50); final double[] nullArray = null; final double[] emptyArray = new double[] {}; try { percentile.evaluate(nullArray); Assert.fail("Expecting MathIllegalArgumentException for null array"); } catch (final MathIllegalArgumentException ex) { } Assert.assertTrue(Double.isNaN(percentile.evaluate(emptyArray))); }
@Test public void testSingleton() { final Percentile percentile = new Percentile(50); final double[] singletonArray = new double[] {1d}; Assert.assertEquals(1d, percentile.evaluate(singletonArray), 0); Assert.assertEquals(1d, percentile.evaluate(singletonArray, 0, 1), 0); Assert.assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 5), 0); Assert.assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 100), 0); Assert.assertTrue(Double.isNaN(percentile.evaluate(singletonArray, 0, 0))); }
@Test public void testSpecialValues() { final Percentile percentile = new Percentile(50); double[] specialValues = new double[] {0d, 1d, 2d, 3d, 4d, Double.NaN}; Assert.assertEquals(2d, percentile.evaluate(specialValues), 0); specialValues = new double[] {Double.NEGATIVE_INFINITY, 1d, 2d, 3d, Double.NaN, Double.POSITIVE_INFINITY}; Assert.assertEquals(2d, percentile.evaluate(specialValues), 0); specialValues = new double[] {1d, 1d, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; Assert.assertTrue(Double.isInfinite(percentile.evaluate(specialValues))); specialValues = new double[] {1d, 1d, Double.NaN, Double.NaN}; Assert.assertTrue(!Double.isNaN(percentile.evaluate(specialValues))); Assert.assertTrue(1d==percentile.evaluate(specialValues)); specialValues = new double[] {1d, 1d, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY}; Assert.assertTrue(Double.isNaN(percentile.evaluate(specialValues))); }
@Test public void testAllTechniquesNISTExample() { final double[] d = new double[] { 95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.0610, 95.1591, 95.1195, 95.1772, 95.0925, 95.1990, 95.1682 }; testAssertMappedValues(d, new Object[][] { { Percentile.EstimationType.LEGACY, 95.1981 }, { Percentile.EstimationType.R_1, 95.19590 }, { Percentile.EstimationType.R_2, 95.19590 }, { Percentile.EstimationType.R_3, 95.19590 }, { Percentile.EstimationType.R_4, 95.19546 }, { Percentile.EstimationType.R_5, 95.19683 }, { Percentile.EstimationType.R_6, 95.19807 }, { Percentile.EstimationType.R_7, 95.19568 }, { Percentile.EstimationType.R_8, 95.19724 }, { Percentile.EstimationType.R_9, 95.19714 } }, 90d, 1.0e-04); for (final Percentile.EstimationType e : Percentile.EstimationType.values()) { reset(100.0, e); Assert.assertEquals(95.1990, getUnivariateStatistic().evaluate(d), 1.0e-4); } }
@Test public void testAllTechniques5() { reset(5, Percentile.EstimationType.LEGACY); final UnivariateStatistic percentile = getUnivariateStatistic(); Assert.assertEquals(this.percentile5, percentile.evaluate(testArray), getTolerance()); testAssertMappedValues(testArray, new Object[][] { { Percentile.EstimationType.LEGACY, percentile5 }, { Percentile.EstimationType.R_1, 8.8000 }, { Percentile.EstimationType.R_2, 8.8000 }, { Percentile.EstimationType.R_3, 8.2000 }, { Percentile.EstimationType.R_4, 8.2600 }, { Percentile.EstimationType.R_5, 8.5600 }, { Percentile.EstimationType.R_6, 8.2900 }, { Percentile.EstimationType.R_7, 8.8100 }, { Percentile.EstimationType.R_8, 8.4700 }, { Percentile.EstimationType.R_9, 8.4925 }}, 5d, getTolerance()); }
@Test public void testAllTechniquesNullEmpty() { final double[] nullArray = null; final double[] emptyArray = new double[] {}; for (final Percentile.EstimationType e : Percentile.EstimationType.values()) { reset (50, e); final UnivariateStatistic percentile = getUnivariateStatistic(); try { percentile.evaluate(nullArray); Assert.fail("Expecting MathIllegalArgumentException " + "for null array"); } catch (final MathIllegalArgumentException ex) { } Assert.assertTrue(Double.isNaN(percentile.evaluate(emptyArray))); } }
@Test public void testAllTechniquesSingleton() { final double[] singletonArray = new double[] { 1d }; for (final Percentile.EstimationType e : Percentile.EstimationType.values()) { reset (50, e); final UnivariateStatistic percentile = getUnivariateStatistic(); Assert.assertEquals(1d, percentile.evaluate(singletonArray), 0); Assert.assertEquals(1d, percentile.evaluate(singletonArray, 0, 1), 0); Assert.assertEquals(1d, new Percentile().evaluate(singletonArray, 0, 1, 5), 0); Assert.assertEquals(1d, new Percentile().evaluate(singletonArray, 0, 1, 100), 0); Assert.assertTrue(Double.isNaN(percentile.evaluate(singletonArray, 0, 0))); } }
@Test public void testAllTechniquesEmpty() { final double[] singletonArray = new double[] { }; for (final Percentile.EstimationType e : Percentile.EstimationType.values()) { reset (50, e); final UnivariateStatistic percentile = getUnivariateStatistic(); Assert.assertEquals(Double.NaN, percentile.evaluate(singletonArray), 0); Assert.assertEquals(Double.NaN, percentile.evaluate(singletonArray, 0, 0), 0); Assert.assertEquals(Double.NaN, new Percentile().evaluate(singletonArray, 0, 0, 5), 0); Assert.assertEquals(Double.NaN, new Percentile().evaluate(singletonArray, 0, 0, 100), 0); Assert.assertTrue(Double.isNaN(percentile.evaluate(singletonArray, 0, 0))); } }
@Test public void testRemoveNan() { final double[] specialValues = new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN }; final double[] expectedValues = new double[] { 0d, 1d, 2d, 3d, 4d }; reset (50, Percentile.EstimationType.R_1); Assert.assertEquals(2.0, getUnivariateStatistic().evaluate(specialValues), 0d); Assert.assertEquals(2.0, getUnivariateStatistic().evaluate(expectedValues),0d); Assert.assertTrue(Double.isNaN(getUnivariateStatistic().evaluate(specialValues,5,1))); Assert.assertEquals(4d, getUnivariateStatistic().evaluate(specialValues, 4, 2), 0d); Assert.assertEquals(3d, getUnivariateStatistic().evaluate(specialValues,3,3),0d); reset(50, Percentile.EstimationType.R_2); Assert.assertEquals(3.5d, getUnivariateStatistic().evaluate(specialValues,3,3),0d); }
@Test public void testAllTechniquesSpecialValues() { reset(50d, Percentile.EstimationType.LEGACY); final UnivariateStatistic percentile = getUnivariateStatistic(); double[] specialValues = new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN }; Assert.assertEquals(2.5d, percentile.evaluate(specialValues), 0); testAssertMappedValues(specialValues, new Object[][] { { Percentile.EstimationType.LEGACY, 2.5d }, { Percentile.EstimationType.R_1, 2.0 }, { Percentile.EstimationType.R_2, 2.0 }, { Percentile.EstimationType.R_3, 1.0 }, { Percentile.EstimationType.R_4, 1.5 }, { Percentile.EstimationType.R_5, 2.0 }, { Percentile.EstimationType.R_6, 2.0 }, { Percentile.EstimationType.R_7, 2.0 }, { Percentile.EstimationType.R_8, 2.0 }, { Percentile.EstimationType.R_9, 2.0 }}, 50d, 0d); specialValues = new double[] { Double.NEGATIVE_INFINITY, 1d, 2d, 3d, Double.NaN, Double.POSITIVE_INFINITY }; Assert.assertEquals(2.5d, percentile.evaluate(specialValues), 0); testAssertMappedValues(specialValues, new Object[][] { { Percentile.EstimationType.LEGACY, 2.5d }, { Percentile.EstimationType.R_1, 2.0 }, { Percentile.EstimationType.R_2, 2.0 }, { Percentile.EstimationType.R_3, 1.0 }, { Percentile.EstimationType.R_4, 1.5 }, { Percentile.EstimationType.R_5, 2.0 }, { Percentile.EstimationType.R_7, 2.0 }, { Percentile.EstimationType.R_7, 2.0 }, { Percentile.EstimationType.R_8, 2.0 }, { Percentile.EstimationType.R_9, 2.0 } }, 50d, 0d); specialValues = new double[] { 1d, 1d, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }; Assert.assertTrue(Double.isInfinite(percentile.evaluate(specialValues))); testAssertMappedValues(specialValues, new Object[][] { { Percentile.EstimationType.LEGACY, Double.POSITIVE_INFINITY }, { Percentile.EstimationType.R_1,Double.NaN }, { Percentile.EstimationType.R_2, Double.NaN }, { Percentile.EstimationType.R_3, Double.NaN }, { Percentile.EstimationType.R_4, Double.NaN }, { Percentile.EstimationType.R_5, Double.POSITIVE_INFINITY }, { Percentile.EstimationType.R_6, Double.POSITIVE_INFINITY }, { Percentile.EstimationType.R_7, Double.POSITIVE_INFINITY }, { Percentile.EstimationType.R_8, Double.POSITIVE_INFINITY }, { Percentile.EstimationType.R_9, Double.POSITIVE_INFINITY }, }, 50d, 0d); specialValues = new double[] { 1d, 1d, Double.NaN, Double.NaN }; Assert.assertTrue(Double.isNaN(percentile.evaluate(specialValues))); testAssertMappedValues(specialValues, new Object[][] { { Percentile.EstimationType.LEGACY, Double.NaN }, { Percentile.EstimationType.R_1, 1.0 }, { Percentile.EstimationType.R_2, 1.0 }, { Percentile.EstimationType.R_3, 1.0 }, { Percentile.EstimationType.R_4, 1.0 }, { Percentile.EstimationType.R_5, 1.0 },{ Percentile.EstimationType.R_6, 1.0 },{ Percentile.EstimationType.R_7, 1.0 }, { Percentile.EstimationType.R_8, 1.0 }, { Percentile.EstimationType.R_9, 1.0 },}, 50d, 0d); specialValues = new double[] { 1d, 1d, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY }; testAssertMappedValues(specialValues, new Object[][] { { Percentile.EstimationType.LEGACY, Double.NaN }, { Percentile.EstimationType.R_1, Double.NaN }, { Percentile.EstimationType.R_2, Double.NaN }, { Percentile.EstimationType.R_3, Double.NaN }, { Percentile.EstimationType.R_4, Double.NaN }, { Percentile.EstimationType.R_5, Double.NaN }, { Percentile.EstimationType.R_6, Double.NaN }, { Percentile.EstimationType.R_7, Double.NaN }, { Percentile.EstimationType.R_8, Double.NaN }, { Percentile.EstimationType.R_9, Double.NaN } }, 50d, 0d); }
@Test public void testAllEstimationTechniquesOnly() { Assert.assertEquals("Legacy Apache Commons Math",Percentile.EstimationType.LEGACY.getName()); final Object[][] map = new Object[][] { { Percentile.EstimationType.LEGACY, 20.82 }, { Percentile.EstimationType.R_1, 19.8 }, { Percentile.EstimationType.R_2, 19.8 }, { Percentile.EstimationType.R_3, 19.8 }, { Percentile.EstimationType.R_4, 19.310 }, { Percentile.EstimationType.R_5, 20.280}, { Percentile.EstimationType.R_6, 20.820}, { Percentile.EstimationType.R_7, 19.555 }, { Percentile.EstimationType.R_8, 20.460 },{Percentile.EstimationType.R_9,20.415} }; try { Percentile.EstimationType.LEGACY.evaluate(testArray, -1d, new KthSelector(new MedianOf3PivotingStrategy())); } catch (final OutOfRangeException oore) { } try { Percentile.EstimationType.LEGACY.evaluate(testArray, 101d, new KthSelector()); } catch (final OutOfRangeException oore) { } try { Percentile.EstimationType.LEGACY.evaluate(testArray, 50d, new KthSelector()); } catch(final OutOfRangeException oore) { } for (final Object[] o : map) { final Percentile.EstimationType e = (Percentile.EstimationType) o[0]; final double expected = (Double) o[1]; final double result = e.evaluate(testArray, DEFAULT_PERCENTILE, new KthSelector()); Assert.assertEquals("expected[" + e + "] = " + expected + " but was = " + result, expected, result, tolerance); } }
@Test public void testAllEstimationTechniquesOnlyForNullsAndOOR() { final Object[][] map = new Object[][] { { Percentile.EstimationType.LEGACY, 20.82 }, { Percentile.EstimationType.R_1, 19.8 }, { Percentile.EstimationType.R_2, 19.8 }, { Percentile.EstimationType.R_3, 19.8 }, { Percentile.EstimationType.R_4, 19.310 }, { Percentile.EstimationType.R_5, 20.280}, { Percentile.EstimationType.R_6, 20.820}, { Percentile.EstimationType.R_7, 19.555 }, { Percentile.EstimationType.R_8, 20.460 },{ Percentile.EstimationType.R_9, 20.415 } }; for (final Object[] o : map) { final Percentile.EstimationType e = (Percentile.EstimationType) o[0]; try { e.evaluate(null, DEFAULT_PERCENTILE, new KthSelector()); Assert.fail("Expecting NullArgumentException"); } catch (final NullArgumentException nae) { } try { e.evaluate(testArray, 120, new KthSelector()); Assert.fail("Expecting OutOfRangeException"); } catch (final OutOfRangeException oore) { } } }