1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27
28 public class PercentileTest extends UnivariateStatisticAbstractTest{
29
30 protected Percentile stat;
31
32
33
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
46
47
48 public UnivariateStatistic getUnivariateStatistic() {
49 return new Percentile(95.0);
50 }
51
52
53
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 try {
78 p.evaluate(d, 0, d.length, -1.0);
79 fail();
80 } catch (IllegalArgumentException ex) {
81
82 }
83 try {
84 p.evaluate(d, 0, d.length, 101.0);
85 fail();
86 } catch (IllegalArgumentException ex) {
87
88 }
89 }
90
91 public void testNISTExample() {
92 double[] d = new double[] {95.1772, 95.1567, 95.1937, 95.1959,
93 95.1442, 95.0610, 95.1591, 95.1195, 95.1772, 95.0925, 95.1990, 95.1682
94 };
95 Percentile p = new Percentile(90);
96 assertEquals(95.1981, p.evaluate(d), 1.0e-4);
97 assertEquals(95.1990, p.evaluate(d,0,d.length, 100d), 0);
98 }
99
100 public void test5() {
101 Percentile percentile = new Percentile(5);
102 assertEquals(this.percentile5, percentile.evaluate(testArray), getTolerance());
103 }
104
105 public void testNullEmpty() {
106 Percentile percentile = new Percentile(50);
107 double[] nullArray = null;
108 double[] emptyArray = new double[] {};
109 try {
110 percentile.evaluate(nullArray);
111 fail("Expecting IllegalArgumentException for null array");
112 } catch (IllegalArgumentException ex) {
113
114 }
115 assertTrue(Double.isNaN(percentile.evaluate(emptyArray)));
116 }
117
118 public void testSingleton() {
119 Percentile percentile = new Percentile(50);
120 double[] singletonArray = new double[] {1d};
121 assertEquals(1d, percentile.evaluate(singletonArray), 0);
122 assertEquals(1d, percentile.evaluate(singletonArray, 0, 1), 0);
123 assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 5), 0);
124 assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 100), 0);
125 assertTrue(Double.isNaN(percentile.evaluate(singletonArray, 0, 0)));
126 }
127
128 public void testSpecialValues() {
129 Percentile percentile = new Percentile(50);
130 double[] specialValues = new double[] {0d, 1d, 2d, 3d, 4d, Double.NaN};
131 assertEquals(2.5d, percentile.evaluate(specialValues), 0);
132 specialValues = new double[] {Double.NEGATIVE_INFINITY, 1d, 2d, 3d,
133 Double.NaN, Double.POSITIVE_INFINITY};
134 assertEquals(2.5d, percentile.evaluate(specialValues), 0);
135 specialValues = new double[] {1d, 1d, Double.POSITIVE_INFINITY,
136 Double.POSITIVE_INFINITY};
137 assertTrue(Double.isInfinite(percentile.evaluate(specialValues)));
138 specialValues = new double[] {1d, 1d, Double.NaN,
139 Double.NaN};
140 assertTrue(Double.isNaN(percentile.evaluate(specialValues)));
141 specialValues = new double[] {1d, 1d, Double.NEGATIVE_INFINITY,
142 Double.NEGATIVE_INFINITY};
143
144 assertTrue(Double.isNaN(percentile.evaluate(specialValues)));
145 }
146
147 public void testSetQuantile() {
148 Percentile percentile = new Percentile(10);
149 percentile.setQuantile(100);
150 assertEquals(100, percentile.getQuantile(), 0);
151 try {
152 percentile.setQuantile(0);
153 fail("Expecting IllegalArgumentException");
154 } catch (IllegalArgumentException ex) {
155
156 }
157 try {
158 percentile = new Percentile(0);
159 fail("Expecting IllegalArgumentException");
160 } catch (IllegalArgumentException ex) {
161
162 }
163 }
164
165 }