001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math.stat.descriptive.rank;
018
019 import junit.framework.Test;
020 import junit.framework.TestSuite;
021
022 import org.apache.commons.math.stat.descriptive.UnivariateStatistic;
023 import org.apache.commons.math.stat.descriptive.UnivariateStatisticAbstractTest;
024
025 /**
026 * Test cases for the {@link UnivariateStatistic} class.
027 * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
028 */
029 public class PercentileTest extends UnivariateStatisticAbstractTest{
030
031 protected Percentile stat;
032
033 /**
034 * @param name
035 */
036 public PercentileTest(String name) {
037 super(name);
038 }
039
040 public static Test suite() {
041 TestSuite suite = new TestSuite(PercentileTest.class);
042 suite.setName("Percentile Tests");
043 return suite;
044 }
045
046 /**
047 * {@inheritDoc}
048 */
049 @Override
050 public UnivariateStatistic getUnivariateStatistic() {
051 return new Percentile(95.0);
052 }
053
054 /**
055 * {@inheritDoc}
056 */
057 @Override
058 public double expectedValue() {
059 return this.percentile95;
060 }
061
062 public void testHighPercentile(){
063 double[] d = new double[]{1, 2, 3};
064 Percentile p = new Percentile(75);
065 assertEquals(3.0, p.evaluate(d), 1.0e-5);
066 }
067
068 public void testPercentile() {
069 double[] d = new double[] {1, 3, 2, 4};
070 Percentile p = new Percentile(30);
071 assertEquals(1.5, p.evaluate(d), 1.0e-5);
072 p.setQuantile(25);
073 assertEquals(1.25, p.evaluate(d), 1.0e-5);
074 p.setQuantile(75);
075 assertEquals(3.75, p.evaluate(d), 1.0e-5);
076 p.setQuantile(50);
077 assertEquals(2.5, p.evaluate(d), 1.0e-5);
078
079 // invalid percentiles
080 try {
081 p.evaluate(d, 0, d.length, -1.0);
082 fail();
083 } catch (IllegalArgumentException ex) {
084 // success
085 }
086 try {
087 p.evaluate(d, 0, d.length, 101.0);
088 fail();
089 } catch (IllegalArgumentException ex) {
090 // success
091 }
092 }
093
094 public void testNISTExample() {
095 double[] d = new double[] {95.1772, 95.1567, 95.1937, 95.1959,
096 95.1442, 95.0610, 95.1591, 95.1195, 95.1772, 95.0925, 95.1990, 95.1682
097 };
098 Percentile p = new Percentile(90);
099 assertEquals(95.1981, p.evaluate(d), 1.0e-4);
100 assertEquals(95.1990, p.evaluate(d,0,d.length, 100d), 0);
101 }
102
103 public void test5() {
104 Percentile percentile = new Percentile(5);
105 assertEquals(this.percentile5, percentile.evaluate(testArray), getTolerance());
106 }
107
108 public void testNullEmpty() {
109 Percentile percentile = new Percentile(50);
110 double[] nullArray = null;
111 double[] emptyArray = new double[] {};
112 try {
113 percentile.evaluate(nullArray);
114 fail("Expecting IllegalArgumentException for null array");
115 } catch (IllegalArgumentException ex) {
116 // expected
117 }
118 assertTrue(Double.isNaN(percentile.evaluate(emptyArray)));
119 }
120
121 public void testSingleton() {
122 Percentile percentile = new Percentile(50);
123 double[] singletonArray = new double[] {1d};
124 assertEquals(1d, percentile.evaluate(singletonArray), 0);
125 assertEquals(1d, percentile.evaluate(singletonArray, 0, 1), 0);
126 assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 5), 0);
127 assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 100), 0);
128 assertTrue(Double.isNaN(percentile.evaluate(singletonArray, 0, 0)));
129 }
130
131 public void testSpecialValues() {
132 Percentile percentile = new Percentile(50);
133 double[] specialValues = new double[] {0d, 1d, 2d, 3d, 4d, Double.NaN};
134 assertEquals(2.5d, percentile.evaluate(specialValues), 0);
135 specialValues = new double[] {Double.NEGATIVE_INFINITY, 1d, 2d, 3d,
136 Double.NaN, Double.POSITIVE_INFINITY};
137 assertEquals(2.5d, percentile.evaluate(specialValues), 0);
138 specialValues = new double[] {1d, 1d, Double.POSITIVE_INFINITY,
139 Double.POSITIVE_INFINITY};
140 assertTrue(Double.isInfinite(percentile.evaluate(specialValues)));
141 specialValues = new double[] {1d, 1d, Double.NaN,
142 Double.NaN};
143 assertTrue(Double.isNaN(percentile.evaluate(specialValues)));
144 specialValues = new double[] {1d, 1d, Double.NEGATIVE_INFINITY,
145 Double.NEGATIVE_INFINITY};
146 // Interpolation results in NEGATIVE_INFINITY + POSITIVE_INFINITY
147 assertTrue(Double.isNaN(percentile.evaluate(specialValues)));
148 }
149
150 public void testSetQuantile() {
151 Percentile percentile = new Percentile(10);
152 percentile.setQuantile(100); // OK
153 assertEquals(100, percentile.getQuantile(), 0);
154 try {
155 percentile.setQuantile(0);
156 fail("Expecting IllegalArgumentException");
157 } catch (IllegalArgumentException ex) {
158 // expected
159 }
160 try {
161 new Percentile(0);
162 fail("Expecting IllegalArgumentException");
163 } catch (IllegalArgumentException ex) {
164 // expected
165 }
166 }
167
168 }