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