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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public abstract class IntegerDistributionAbstractTest extends TestCase {
44
45
46
47 private IntegerDistribution distribution;
48
49
50 private double tolerance = 1E-4;
51
52
53 private int[] densityTestPoints;
54
55
56 private double[] densityTestValues;
57
58
59 private int[] cumulativeTestPoints;
60
61
62 private double[] cumulativeTestValues;
63
64
65 private double[] inverseCumulativeTestPoints;
66
67
68 private int[] inverseCumulativeTestValues;
69
70
71
72
73
74
75
76 public IntegerDistributionAbstractTest(String name) {
77 super(name);
78 }
79
80
81
82
83 public abstract IntegerDistribution makeDistribution();
84
85
86 public abstract int[] makeDensityTestPoints();
87
88
89 public abstract double[] makeDensityTestValues();
90
91
92 public abstract int[] makeCumulativeTestPoints();
93
94
95 public abstract double[] makeCumulativeTestValues();
96
97
98 public abstract double[] makeInverseCumulativeTestPoints();
99
100
101 public abstract int[] makeInverseCumulativeTestValues();
102
103
104
105
106
107
108 @Override
109 protected void setUp() throws Exception {
110 super.setUp();
111 distribution = makeDistribution();
112 densityTestPoints = makeDensityTestPoints();
113 densityTestValues = makeDensityTestValues();
114 cumulativeTestPoints = makeCumulativeTestPoints();
115 cumulativeTestValues = makeCumulativeTestValues();
116 inverseCumulativeTestPoints = makeInverseCumulativeTestPoints();
117 inverseCumulativeTestValues = makeInverseCumulativeTestValues();
118 }
119
120
121
122
123 @Override
124 protected void tearDown() throws Exception {
125 super.tearDown();
126 distribution = null;
127 densityTestPoints = null;
128 densityTestValues = null;
129 cumulativeTestPoints = null;
130 cumulativeTestValues = null;
131 inverseCumulativeTestPoints = null;
132 inverseCumulativeTestValues = null;
133 }
134
135
136
137
138
139
140
141 protected void verifyDensities() throws Exception {
142 for (int i = 0; i < densityTestPoints.length; i++) {
143 assertEquals("Incorrect density value returned for " + densityTestPoints[i],
144 densityTestValues[i],
145 distribution.probability(densityTestPoints[i]), tolerance);
146 }
147 }
148
149
150
151
152
153 protected void verifyCumulativeProbabilities() throws Exception {
154 for (int i = 0; i < cumulativeTestPoints.length; i++) {
155 assertEquals("Incorrect cumulative probability value returned for " + cumulativeTestPoints[i],
156 cumulativeTestValues[i],
157 distribution.cumulativeProbability(cumulativeTestPoints[i]), tolerance);
158 }
159 }
160
161
162
163
164
165
166 protected void verifyInverseCumulativeProbabilities() throws Exception {
167 for (int i = 0; i < inverseCumulativeTestPoints.length; i++) {
168 assertEquals("Incorrect inverse cumulative probability value returned for "
169 + inverseCumulativeTestPoints[i], inverseCumulativeTestValues[i],
170 distribution.inverseCumulativeProbability(inverseCumulativeTestPoints[i]));
171 }
172 }
173
174
175
176
177
178
179
180 public void testDensities() throws Exception {
181 verifyDensities();
182 }
183
184
185
186
187
188 public void testCumulativeProbabilities() throws Exception {
189 verifyCumulativeProbabilities();
190 }
191
192
193
194
195
196
197 public void testFloatingPointArguments() throws Exception {
198 for (int i = 0; i < cumulativeTestPoints.length; i++) {
199 double arg = cumulativeTestPoints[i];
200 assertEquals(
201 "Incorrect cumulative probability value returned for " +
202 cumulativeTestPoints[i],
203 cumulativeTestValues[i],
204 distribution.cumulativeProbability(arg), tolerance);
205 if (i < cumulativeTestPoints.length - 1) {
206 double arg2 = cumulativeTestPoints[i + 1];
207 assertEquals("Inconsistent probability for discrete range " +
208 "[ " + arg + "," + arg2 + " ]",
209 distribution.cumulativeProbability(
210 cumulativeTestPoints[i],
211 cumulativeTestPoints[i + 1]),
212 distribution.cumulativeProbability(arg, arg2), tolerance);
213 arg = arg - Math.random();
214 arg2 = arg2 + Math.random();
215 assertEquals("Inconsistent probability for discrete range " +
216 "[ " + arg + "," + arg2 + " ]",
217 distribution.cumulativeProbability(
218 cumulativeTestPoints[i],
219 cumulativeTestPoints[i + 1]),
220 distribution.cumulativeProbability(arg, arg2), tolerance);
221 }
222 }
223 int one = 1;
224 int ten = 10;
225 int two = 2;
226 double oned = one;
227 double twod = two;
228 double tend = ten;
229 assertEquals(distribution.cumulativeProbability(one, two),
230 distribution.cumulativeProbability(oned, twod), tolerance);
231 assertEquals(distribution.cumulativeProbability(one, two),
232 distribution.cumulativeProbability(oned - tolerance,
233 twod + 0.9), tolerance);
234 assertEquals(distribution.cumulativeProbability(two, ten),
235 distribution.cumulativeProbability(twod, tend), tolerance);
236 assertEquals(distribution.cumulativeProbability(two, ten),
237 distribution.cumulativeProbability(twod - tolerance,
238 tend + 0.9), tolerance);
239 }
240
241
242
243
244
245 public void testInverseCumulativeProbabilities() throws Exception {
246 verifyInverseCumulativeProbabilities();
247 }
248
249
250
251
252 public void testIllegalArguments() throws Exception {
253 try {
254 distribution.cumulativeProbability(1, 0);
255 fail("Expecting IllegalArgumentException for bad cumulativeProbability interval");
256 } catch (IllegalArgumentException ex) {
257
258 }
259 try {
260 distribution.inverseCumulativeProbability(-1);
261 fail("Expecting IllegalArgumentException for p = -1");
262 } catch (IllegalArgumentException ex) {
263
264 }
265 try {
266 distribution.inverseCumulativeProbability(2);
267 fail("Expecting IllegalArgumentException for p = 2");
268 } catch (IllegalArgumentException ex) {
269
270 }
271 }
272
273
274
275
276
277 protected int[] getCumulativeTestPoints() {
278 return cumulativeTestPoints;
279 }
280
281
282
283
284 protected void setCumulativeTestPoints(int[] cumulativeTestPoints) {
285 this.cumulativeTestPoints = cumulativeTestPoints;
286 }
287
288
289
290
291 protected double[] getCumulativeTestValues() {
292 return cumulativeTestValues;
293 }
294
295
296
297
298 protected void setCumulativeTestValues(double[] cumulativeTestValues) {
299 this.cumulativeTestValues = cumulativeTestValues;
300 }
301
302
303
304
305 protected int[] getDensityTestPoints() {
306 return densityTestPoints;
307 }
308
309
310
311
312 protected void setDensityTestPoints(int[] densityTestPoints) {
313 this.densityTestPoints = densityTestPoints;
314 }
315
316
317
318
319 protected double[] getDensityTestValues() {
320 return densityTestValues;
321 }
322
323
324
325
326 protected void setDensityTestValues(double[] densityTestValues) {
327 this.densityTestValues = densityTestValues;
328 }
329
330
331
332
333 protected IntegerDistribution getDistribution() {
334 return distribution;
335 }
336
337
338
339
340 protected void setDistribution(IntegerDistribution distribution) {
341 this.distribution = distribution;
342 }
343
344
345
346
347 protected double[] getInverseCumulativeTestPoints() {
348 return inverseCumulativeTestPoints;
349 }
350
351
352
353
354 protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) {
355 this.inverseCumulativeTestPoints = inverseCumulativeTestPoints;
356 }
357
358
359
360
361 protected int[] getInverseCumulativeTestValues() {
362 return inverseCumulativeTestValues;
363 }
364
365
366
367
368 protected void setInverseCumulativeTestValues(int[] inverseCumulativeTestValues) {
369 this.inverseCumulativeTestValues = inverseCumulativeTestValues;
370 }
371
372
373
374
375 protected double getTolerance() {
376 return tolerance;
377 }
378
379
380
381
382 protected void setTolerance(double tolerance) {
383 this.tolerance = tolerance;
384 }
385
386 }