    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.math.stat.descriptive.rank;
    17
    18  import junit.framework.Test;
    19  import junit.framework.TestSuite;
    20
    21  import org.apache.commons.math.stat.descriptive.UnivariateStatistic;
    22  import org.apache.commons.math.stat.descriptive.UnivariateStatisticAbstractTest;
    23
    24  /***
    25   * Test cases for the {@link UnivariateStatistic} class.
    26   * @version $Revision: 1.1 $ $Date: 2004/10/08 05:08:20 $
    27   */
    28  public class PercentileTest extends UnivariateStatisticAbstractTest{
    29
    30      protected Percentile stat;
    31
    32      /***
    33       * @param name
    34       */
    35      public PercentileTest(String name) {
    36          super(name);
    37      }
    38
    39      public static Test suite() {
    40          TestSuite suite = new TestSuite(PercentileTest.class);
    41          suite.setName("Percentile Tests");
    42          return suite;
    43      }
    44
    45      /* (non-Javadoc)
    46       * @see org.apache.commons.math.stat.descriptive.UnivariateStatisticAbstractTest#getUnivariateStatistic()
    47       */
    48      public UnivariateStatistic getUnivariateStatistic() {
    49          return new Percentile(95.0);
    50      }
    51
    52      /* (non-Javadoc)
    53       * @see org.apache.commons.math.stat.descriptive.UnivariateStatisticAbstractTest#expectedValue()
    54       */
    55      public double expectedValue() {
    56          return this.percentile95;
    57      }
    58
    59      public void testHighPercentile(){
    60          double[] d = new double[]{1, 2, 3};
    61          Percentile p = new Percentile(75);
    62          assertEquals(3.0, p.evaluate(d), 1.0e-5);
    63      }
    64
    65      public void testPercentile() {
    66          double[] d = new double[] {1, 3, 2, 4};
    67          Percentile p = new Percentile(30);
    68          assertEquals(1.5, p.evaluate(d), 1.0e-5);
    69          p.setQuantile(25);
    70          assertEquals(1.25, p.evaluate(d), 1.0e-5);
    71          p.setQuantile(75);
    72          assertEquals(3.75, p.evaluate(d), 1.0e-5);
    73          p.setQuantile(50);
    74          assertEquals(2.5, p.evaluate(d), 1.0e-5);
    75      }
    76
    77      public void testNISTExample() {
    78          double[] d = new double[] {95.1772, 95.1567, 95.1937, 95.1959,
    79                  95.1442, 95.0610,  95.1591, 95.1195, 95.1772, 95.0925, 95.1990, 95.1682
    80          };
    81          Percentile p = new Percentile(90);
    82          assertEquals(95.1981, p.evaluate(d), 1.0e-4);
    83          assertEquals(95.1990, p.evaluate(d,0,d.length, 100d), 0);
    84      }
    85
    86      public void test5() {
    87          Percentile percentile = new Percentile(5);
    88          assertEquals(this.percentile5, percentile.evaluate(testArray), getTolerance());
    89      }
    90
    91      public void testNullEmpty() {
    92          Percentile percentile = new Percentile(50);
    93          double[] nullArray = null;
    94          double[] emptyArray = new double[] {};
    95          try {
    96              percentile.evaluate(nullArray);
    97              fail("Expecting IllegalArgumentException for null array");
    98          } catch (IllegalArgumentException ex) {
    99              // expected
    100         }
    101         assertTrue(Double.isNaN(percentile.evaluate(emptyArray)));
    102     }
    103
    104     public void testSingleton() {
    105         Percentile percentile = new Percentile(50);
    106         double[] singletonArray = new double[] {1d};
    107         assertEquals(1d, percentile.evaluate(singletonArray), 0);
    108         assertEquals(1d, percentile.evaluate(singletonArray, 0, 1), 0);
    109         assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 5), 0);
    110         assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 100), 0);
    111         assertTrue(Double.isNaN(percentile.evaluate(singletonArray, 0, 0)));
    112     }
    113
    114     public void testSpecialValues() {
    115         Percentile percentile = new Percentile(50);
    116         double[] specialValues = new double[] {0d, 1d, 2d, 3d, 4d,  Double.NaN};
    117         assertEquals(2.5d, percentile.evaluate(specialValues), 0);
    118         specialValues =  new double[] {Double.NEGATIVE_INFINITY, 1d, 2d, 3d,
    119                 Double.NaN, Double.POSITIVE_INFINITY};
    120         assertEquals(2.5d, percentile.evaluate(specialValues), 0);
    121         specialValues = new double[] {1d, 1d, Double.POSITIVE_INFINITY,
    122                 Double.POSITIVE_INFINITY};
    123         assertTrue(Double.isInfinite(percentile.evaluate(specialValues)));
    124         specialValues = new double[] {1d, 1d, Double.NaN,
    125                 Double.NaN};
    126         assertTrue(Double.isNaN(percentile.evaluate(specialValues)));
    127         specialValues = new double[] {1d, 1d, Double.NEGATIVE_INFINITY,
    128                 Double.NEGATIVE_INFINITY};
    129         // Interpolation results in NEGATIVE_INFINITY + POSITIVE_INFINITY
    130         assertTrue(Double.isNaN(percentile.evaluate(specialValues)));
    131     }
    132
    133     public void testSetQuantile() {
    134         Percentile percentile = new Percentile(10);
    135         percentile.setQuantile(100); // OK
    136         assertEquals(100, percentile.getQuantile(), 0);
    137         try {
    138             percentile.setQuantile(0);
    139             fail("Expecting IllegalArgumentException");
    140         } catch (IllegalArgumentException ex) {
    141             // expected
    142         }
    143         try {
    144             percentile = new Percentile(0);
    145             fail("Expecting IllegalArgumentException");
    146         } catch (IllegalArgumentException ex) {
    147             // expected
    148         }
    149     }
    150
    151 }