1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.distribution;
18
19 import junit.framework.TestCase;
20
21 import org.apache.commons.math.TestUtils;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public abstract class ContinuousDistributionAbstractTest extends TestCase {
57
58
59
60 private ContinuousDistribution distribution;
61
62
63 private double tolerance = 1E-4;
64
65
66 private double[] cumulativeTestPoints;
67
68
69 private double[] cumulativeTestValues;
70
71
72 private double[] inverseCumulativeTestPoints;
73
74
75 private double[] inverseCumulativeTestValues;
76
77
78
79
80
81
82
83 public ContinuousDistributionAbstractTest(String name) {
84 super(name);
85 }
86
87
88
89
90 public abstract ContinuousDistribution makeDistribution();
91
92
93 public abstract double[] makeCumulativeTestPoints();
94
95
96 public abstract double[] makeCumulativeTestValues();
97
98
99
100
101 public double[] makeInverseCumulativeTestPoints() {
102 return makeCumulativeTestValues();
103 }
104
105
106 public double[] makeInverseCumulativeTestValues() {
107 return makeCumulativeTestPoints();
108 }
109
110
111
112
113
114
115 protected void setUp() throws Exception {
116 super.setUp();
117 distribution = makeDistribution();
118 cumulativeTestPoints = makeCumulativeTestPoints();
119 cumulativeTestValues = makeCumulativeTestValues();
120 inverseCumulativeTestPoints = makeInverseCumulativeTestPoints();
121 inverseCumulativeTestValues = makeInverseCumulativeTestValues();
122 }
123
124
125
126
127 protected void tearDown() throws Exception {
128 super.tearDown();
129 distribution = null;
130 cumulativeTestPoints = null;
131 cumulativeTestValues = null;
132 inverseCumulativeTestPoints = null;
133 inverseCumulativeTestValues = null;
134 }
135
136
137
138
139
140
141
142 protected void verifyCumulativeProbabilities() throws Exception {
143 for (int i = 0; i < cumulativeTestPoints.length; i++) {
144 TestUtils.assertEquals("Incorrect cumulative probability value returned for "
145 + cumulativeTestPoints[i], cumulativeTestValues[i],
146 distribution.cumulativeProbability(cumulativeTestPoints[i]),
147 getTolerance());
148 }
149 }
150
151
152
153
154
155 protected void verifyInverseCumulativeProbabilities() throws Exception {
156 for (int i = 0; i < inverseCumulativeTestPoints.length; i++) {
157 TestUtils.assertEquals("Incorrect inverse cumulative probability value returned for "
158 + inverseCumulativeTestPoints[i], inverseCumulativeTestValues[i],
159 distribution.inverseCumulativeProbability(inverseCumulativeTestPoints[i]),
160 getTolerance());
161 }
162 }
163
164
165
166
167
168
169
170 public void testCumulativeProbabilities() throws Exception {
171 verifyCumulativeProbabilities();
172 }
173
174
175
176
177
178 public void testInverseCumulativeProbabilities() throws Exception {
179 verifyInverseCumulativeProbabilities();
180 }
181
182
183
184
185 public void testConsistency() throws Exception {
186 for (int i=1; i < cumulativeTestPoints.length; i++) {
187
188
189 TestUtils.assertEquals(0d,
190 distribution.cumulativeProbability
191 (cumulativeTestPoints[i], cumulativeTestPoints[i]), tolerance);
192
193
194 double upper = Math.max(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
195 double lower = Math.min(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
196 double diff = distribution.cumulativeProbability(upper) -
197 distribution.cumulativeProbability(lower);
198 double direct = distribution.cumulativeProbability(lower, upper);
199 TestUtils.assertEquals("Inconsistent cumulative probabilities for ("
200 + lower + "," + upper + ")", diff, direct, tolerance);
201 }
202 }
203
204
205
206
207 public void testIllegalArguments() throws Exception {
208 try {
209 distribution.cumulativeProbability(1, 0);
210 fail("Expecting IllegalArgumentException for bad cumulativeProbability interval");
211 } catch (IllegalArgumentException ex) {
212
213 }
214 try {
215 distribution.inverseCumulativeProbability(-1);
216 fail("Expecting IllegalArgumentException for p = -1");
217 } catch (IllegalArgumentException ex) {
218
219 }
220 try {
221 distribution.inverseCumulativeProbability(2);
222 fail("Expecting IllegalArgumentException for p = 2");
223 } catch (IllegalArgumentException ex) {
224
225 }
226 }
227
228
229
230
231
232 protected double[] getCumulativeTestPoints() {
233 return cumulativeTestPoints;
234 }
235
236
237
238
239 protected void setCumulativeTestPoints(double[] cumulativeTestPoints) {
240 this.cumulativeTestPoints = cumulativeTestPoints;
241 }
242
243
244
245
246 protected double[] getCumulativeTestValues() {
247 return cumulativeTestValues;
248 }
249
250
251
252
253 protected void setCumulativeTestValues(double[] cumulativeTestValues) {
254 this.cumulativeTestValues = cumulativeTestValues;
255 }
256
257
258
259
260 protected ContinuousDistribution getDistribution() {
261 return distribution;
262 }
263
264
265
266
267 protected void setDistribution(ContinuousDistribution distribution) {
268 this.distribution = distribution;
269 }
270
271
272
273
274 protected double[] getInverseCumulativeTestPoints() {
275 return inverseCumulativeTestPoints;
276 }
277
278
279
280
281 protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) {
282 this.inverseCumulativeTestPoints = inverseCumulativeTestPoints;
283 }
284
285
286
287
288 protected double[] getInverseCumulativeTestValues() {
289 return inverseCumulativeTestValues;
290 }
291
292
293
294
295 protected void setInverseCumulativeTestValues(double[] inverseCumulativeTestValues) {
296 this.inverseCumulativeTestValues = inverseCumulativeTestValues;
297 }
298
299
300
301
302 protected double getTolerance() {
303 return tolerance;
304 }
305
306
307
308
309 protected void setTolerance(double tolerance) {
310 this.tolerance = tolerance;
311 }
312
313 }