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