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