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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.stat.descriptive.rank;
19
import junit.framework.Test;
20
import junit.framework.TestSuite;
22
import org.apache.commons.math.stat.descriptive.UnivariateStatistic;
23
import org.apache.commons.math.stat.descriptive.UnivariateStatisticAbstractTest;
26
* Test cases for the {@link UnivariateStatistic} class.
27
* @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
29
public class PercentileTest extends UnivariateStatisticAbstractTest{
31
protected Percentile stat;
36
public PercentileTest(String name) {
40
public static Test suite() {
41
TestSuite suite = new TestSuite(PercentileTest.class);
42
suite.setName("Percentile Tests");
50
public UnivariateStatistic getUnivariateStatistic() {
51
return new Percentile(95.0);
58
public double expectedValue() {
59
return this.percentile95;
62
public void testHighPercentile(){
63
double[] d = new double[]{1, 2, 3};
64
Percentile p = new Percentile(75);
65
assertEquals(3.0, p.evaluate(d), 1.0e-5);
68
public void testPercentile() {
69
double[] d = new double[] {1, 3, 2, 4};
70
Percentile p = new Percentile(30);
71
assertEquals(1.5, p.evaluate(d), 1.0e-5);
73
assertEquals(1.25, p.evaluate(d), 1.0e-5);
75
assertEquals(3.75, p.evaluate(d), 1.0e-5);
77
assertEquals(2.5, p.evaluate(d), 1.0e-5);
79
// invalid percentiles
81
p.evaluate(d, 0, d.length, -1.0);
83
} catch (IllegalArgumentException ex) {
87
p.evaluate(d, 0, d.length, 101.0);
89
} catch (IllegalArgumentException ex) {
94
public void testNISTExample() {
95
double[] d = new double[] {95.1772, 95.1567, 95.1937, 95.1959,
96
95.1442, 95.0610, 95.1591, 95.1195, 95.1772, 95.0925, 95.1990, 95.1682
98
Percentile p = new Percentile(90);
99
assertEquals(95.1981, p.evaluate(d), 1.0e-4);
100
assertEquals(95.1990, p.evaluate(d,0,d.length, 100d), 0);
103
public void test5() {
104
Percentile percentile = new Percentile(5);
105
assertEquals(this.percentile5, percentile.evaluate(testArray), getTolerance());
108
public void testNullEmpty() {
109
Percentile percentile = new Percentile(50);
110
double[] nullArray = null;
111
double[] emptyArray = new double[] {};
113
percentile.evaluate(nullArray);
114
fail("Expecting IllegalArgumentException for null array");
115
} catch (IllegalArgumentException ex) {
118
assertTrue(Double.isNaN(percentile.evaluate(emptyArray)));
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)));
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,
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)));
150
public void testSetQuantile() {
151
Percentile percentile = new Percentile(10);
152
percentile.setQuantile(100); // OK
153
assertEquals(100, percentile.getQuantile(), 0);
155
percentile.setQuantile(0);
156
fail("Expecting IllegalArgumentException");
157
} catch (IllegalArgumentException ex) {
162
fail("Expecting IllegalArgumentException");
163
} catch (IllegalArgumentException ex) {