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.continuous;
23
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27
28 import org.hipparchus.UnitTestUtils;
29 import org.hipparchus.analysis.UnivariateFunction;
30 import org.hipparchus.analysis.integration.BaseAbstractUnivariateIntegrator;
31 import org.hipparchus.analysis.integration.IterativeLegendreGaussIntegrator;
32 import org.hipparchus.distribution.RealDistribution;
33 import org.hipparchus.exception.MathIllegalArgumentException;
34 import org.hipparchus.util.FastMath;
35 import org.junit.After;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Test;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public abstract class RealDistributionAbstractTest {
73
74
75
76 private RealDistribution distribution;
77
78
79 private double tolerance = 1E-4;
80
81
82 private double[] cumulativeTestPoints;
83
84
85 private double[] cumulativeTestValues;
86
87
88 private double[] inverseCumulativeTestPoints;
89
90
91 private double[] inverseCumulativeTestValues;
92
93
94 private double[] densityTestValues;
95
96
97 private double[] logDensityTestValues;
98
99
100
101
102 public abstract RealDistribution makeDistribution();
103
104
105 public abstract double[] makeCumulativeTestPoints();
106
107
108 public abstract double[] makeCumulativeTestValues();
109
110
111 public abstract double[] makeDensityTestValues();
112
113
114
115
116 public double[] makeLogDensityTestValues() {
117 final double[] densityTestValues = makeDensityTestValues();
118 final double[] logDensityTestValues = new double[densityTestValues.length];
119 for (int i = 0; i < densityTestValues.length; i++) {
120 logDensityTestValues[i] = FastMath.log(densityTestValues[i]);
121 }
122 return logDensityTestValues;
123 }
124
125
126
127
128 public double[] makeInverseCumulativeTestPoints() {
129 return makeCumulativeTestValues();
130 }
131
132
133 public double[] makeInverseCumulativeTestValues() {
134 return makeCumulativeTestPoints();
135 }
136
137
138
139
140
141
142 @Before
143 public void setUp() {
144 distribution = makeDistribution();
145 cumulativeTestPoints = makeCumulativeTestPoints();
146 cumulativeTestValues = makeCumulativeTestValues();
147 inverseCumulativeTestPoints = makeInverseCumulativeTestPoints();
148 inverseCumulativeTestValues = makeInverseCumulativeTestValues();
149 densityTestValues = makeDensityTestValues();
150 logDensityTestValues = makeLogDensityTestValues();
151 }
152
153
154
155
156 @After
157 public void tearDown() {
158 distribution = null;
159 cumulativeTestPoints = null;
160 cumulativeTestValues = null;
161 inverseCumulativeTestPoints = null;
162 inverseCumulativeTestValues = null;
163 densityTestValues = null;
164 logDensityTestValues = null;
165 }
166
167
168
169
170
171
172
173 protected void verifyCumulativeProbabilities() {
174
175 for (int i = 0; i < cumulativeTestPoints.length; i++) {
176 UnitTestUtils.assertEquals("Incorrect cumulative probability value returned for "
177 + cumulativeTestPoints[i], cumulativeTestValues[i],
178 distribution.cumulativeProbability(cumulativeTestPoints[i]),
179 getTolerance());
180 }
181
182 for (int i = 0; i < cumulativeTestPoints.length; i++) {
183 for (int j = 0; j < cumulativeTestPoints.length; j++) {
184 if (cumulativeTestPoints[i] <= cumulativeTestPoints[j]) {
185 UnitTestUtils.assertEquals(cumulativeTestValues[j] - cumulativeTestValues[i],
186 distribution.probability(cumulativeTestPoints[i], cumulativeTestPoints[j]),
187 getTolerance());
188 } else {
189 try {
190 distribution.probability(cumulativeTestPoints[i], cumulativeTestPoints[j]);
191 } catch (MathIllegalArgumentException e) {
192 continue;
193 }
194 Assert.fail("distribution.probability(double, double) should have thrown an exception that second argument is too large");
195 }
196 }
197 }
198 }
199
200
201
202
203
204 protected void verifyInverseCumulativeProbabilities() {
205 for (int i = 0; i < inverseCumulativeTestPoints.length; i++) {
206 UnitTestUtils.assertEquals("Incorrect inverse cumulative probability value returned for "
207 + inverseCumulativeTestPoints[i], inverseCumulativeTestValues[i],
208 distribution.inverseCumulativeProbability(inverseCumulativeTestPoints[i]),
209 getTolerance());
210 }
211 }
212
213
214
215
216 protected void verifyDensities() {
217 for (int i = 0; i < cumulativeTestPoints.length; i++) {
218 UnitTestUtils.assertEquals("Incorrect probability density value returned for "
219 + cumulativeTestPoints[i], densityTestValues[i],
220 distribution.density(cumulativeTestPoints[i]),
221 getTolerance());
222 }
223 }
224
225
226
227
228 protected void verifyLogDensities() {
229 for (int i = 0; i < cumulativeTestPoints.length; i++) {
230 UnitTestUtils.assertEquals("Incorrect probability density value returned for "
231 + cumulativeTestPoints[i], logDensityTestValues[i],
232 distribution.logDensity(cumulativeTestPoints[i]),
233 getTolerance());
234 }
235 }
236
237
238
239
240
241
242
243 @Test
244 public void testCumulativeProbabilities() {
245 verifyCumulativeProbabilities();
246 }
247
248
249
250
251
252 @Test
253 public void testInverseCumulativeProbabilities() {
254 verifyInverseCumulativeProbabilities();
255 }
256
257
258
259
260
261 @Test
262 public void testDensities() {
263 verifyDensities();
264 }
265
266
267
268
269
270 @Test
271 public void testLogDensities() {
272 verifyLogDensities();
273 }
274
275
276
277
278 @Test
279 public void testConsistency() {
280 for (int i=1; i < cumulativeTestPoints.length; i++) {
281
282
283 UnitTestUtils.assertEquals(0d,
284 distribution.probability
285 (cumulativeTestPoints[i], cumulativeTestPoints[i]), tolerance);
286
287
288 double upper = FastMath.max(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
289 double lower = FastMath.min(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
290 double diff = distribution.cumulativeProbability(upper) -
291 distribution.cumulativeProbability(lower);
292 double direct = distribution.probability(lower, upper);
293 UnitTestUtils.assertEquals("Inconsistent probability for ("
294 + lower + "," + upper + ")", diff, direct, tolerance);
295 }
296 }
297
298
299
300
301 @Test
302 public void testIllegalArguments() {
303 try {
304 distribution.probability(1, 0);
305 Assert.fail("Expecting MathIllegalArgumentException for bad cumulativeProbability interval");
306 } catch (MathIllegalArgumentException ex) {
307
308 }
309 try {
310 distribution.inverseCumulativeProbability(-1);
311 Assert.fail("Expecting MathIllegalArgumentException for p = -1");
312 } catch (MathIllegalArgumentException ex) {
313
314 }
315 try {
316 distribution.inverseCumulativeProbability(2);
317 Assert.fail("Expecting MathIllegalArgumentException for p = 2");
318 } catch (MathIllegalArgumentException ex) {
319
320 }
321 }
322
323
324
325
326
327
328
329
330
331 @Test
332 public void testDensityIntegrals() {
333 final double tol = 1.0e-9;
334 final BaseAbstractUnivariateIntegrator integrator =
335 new IterativeLegendreGaussIntegrator(5, 1.0e-12, 1.0e-10);
336 final UnivariateFunction d = new UnivariateFunction() {
337 public double value(double x) {
338 return distribution.density(x);
339 }
340 };
341 final ArrayList<Double> integrationTestPoints = new ArrayList<Double>();
342 for (int i = 0; i < cumulativeTestPoints.length; i++) {
343 if (Double.isNaN(cumulativeTestValues[i]) ||
344 cumulativeTestValues[i] < 1.0e-5 ||
345 cumulativeTestValues[i] > 1 - 1.0e-5) {
346 continue;
347 }
348 integrationTestPoints.add(cumulativeTestPoints[i]);
349 }
350 Collections.sort(integrationTestPoints);
351 for (int i = 1; i < integrationTestPoints.size(); i++) {
352 Assert.assertEquals(
353 distribution.probability(
354 integrationTestPoints.get(0), integrationTestPoints.get(i)),
355 integrator.integrate(
356 1000000,
357 d, integrationTestPoints.get(0),
358 integrationTestPoints.get(i)), tol);
359 }
360 }
361
362
363
364
365
366 protected double[] getCumulativeTestPoints() {
367 return cumulativeTestPoints;
368 }
369
370
371
372
373 protected void setCumulativeTestPoints(double[] cumulativeTestPoints) {
374 this.cumulativeTestPoints = cumulativeTestPoints;
375 }
376
377
378
379
380 protected double[] getCumulativeTestValues() {
381 return cumulativeTestValues;
382 }
383
384
385
386
387 protected void setCumulativeTestValues(double[] cumulativeTestValues) {
388 this.cumulativeTestValues = cumulativeTestValues;
389 }
390
391 protected double[] getDensityTestValues() {
392 return densityTestValues;
393 }
394
395 protected void setDensityTestValues(double[] densityTestValues) {
396 this.densityTestValues = densityTestValues;
397 }
398
399
400
401
402 protected RealDistribution getDistribution() {
403 return distribution;
404 }
405
406
407
408
409 protected void setDistribution(RealDistribution distribution) {
410 this.distribution = distribution;
411 }
412
413
414
415
416 protected double[] getInverseCumulativeTestPoints() {
417 return inverseCumulativeTestPoints;
418 }
419
420
421
422
423 protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) {
424 this.inverseCumulativeTestPoints = inverseCumulativeTestPoints;
425 }
426
427
428
429
430 protected double[] getInverseCumulativeTestValues() {
431 return inverseCumulativeTestValues;
432 }
433
434
435
436
437 protected void setInverseCumulativeTestValues(double[] inverseCumulativeTestValues) {
438 this.inverseCumulativeTestValues = inverseCumulativeTestValues;
439 }
440
441
442
443
444 protected double getTolerance() {
445 return tolerance;
446 }
447
448
449
450
451 protected void setTolerance(double tolerance) {
452 this.tolerance = tolerance;
453 }
454
455 }