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.special;
23
24 import org.hipparchus.UnitTestUtils;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.util.FastMath;
27 import org.junit.jupiter.api.Test;
28
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31 import java.util.concurrent.atomic.AtomicReference;
32
33 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.junit.jupiter.api.Assertions.assertThrows;
36 import static org.junit.jupiter.api.Assertions.fail;
37
38
39
40 class BetaTest {
41
42
43
44
45 private static final Method LOG_GAMMA_SUM_METHOD;
46
47 private static final Method LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD;
48
49 private static final Method SUM_DELTA_MINUS_DELTA_SUM_METHOD;
50
51 static {
52 final Class<Beta> b;
53 final Class<Double> d = Double.TYPE;
54 b = Beta.class;
55 AtomicReference<Method> m = new AtomicReference<>();
56 assertDoesNotThrow(() -> {
57 m.set(b.getDeclaredMethod("logGammaSum", d, d));
58 });
59 LOG_GAMMA_SUM_METHOD = m.get();
60 LOG_GAMMA_SUM_METHOD.setAccessible(true);
61
62 m.set(null);
63 assertDoesNotThrow(() -> {
64 m.set(b.getDeclaredMethod("logGammaMinusLogGammaSum", d, d));
65 });
66 LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD = m.get();
67 LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD.setAccessible(true);
68
69 m.set(null);
70 assertDoesNotThrow(() -> {
71 m.set(b.getDeclaredMethod("sumDeltaMinusDeltaSum", d, d));
72 });
73 SUM_DELTA_MINUS_DELTA_SUM_METHOD = m.get();
74 SUM_DELTA_MINUS_DELTA_SUM_METHOD.setAccessible(true);
75 }
76
77 private void testRegularizedBeta(double expected, double x,
78 double a, double b) {
79 double actual = Beta.regularizedBeta(x, a, b);
80 UnitTestUtils.customAssertEquals(expected, actual, 10e-15);
81 }
82
83 private void testLogBeta(double expected, double a, double b) {
84 double actual = Beta.logBeta(a, b);
85 UnitTestUtils.customAssertEquals(expected, actual, 10e-15);
86 }
87
88 @Test
89 void testRegularizedBetaNanPositivePositive() {
90 testRegularizedBeta(Double.NaN, Double.NaN, 1.0, 1.0);
91 }
92
93 @Test
94 void testRegularizedBetaPositiveNanPositive() {
95 testRegularizedBeta(Double.NaN, 0.5, Double.NaN, 1.0);
96 }
97
98 @Test
99 void testRegularizedBetaPositivePositiveNan() {
100 testRegularizedBeta(Double.NaN, 0.5, 1.0, Double.NaN);
101 }
102
103 @Test
104 void testRegularizedBetaNegativePositivePositive() {
105 testRegularizedBeta(Double.NaN, -0.5, 1.0, 2.0);
106 }
107
108 @Test
109 void testRegularizedBetaPositiveNegativePositive() {
110 testRegularizedBeta(Double.NaN, 0.5, -1.0, 2.0);
111 }
112
113 @Test
114 void testRegularizedBetaPositivePositiveNegative() {
115 testRegularizedBeta(Double.NaN, 0.5, 1.0, -2.0);
116 }
117
118 @Test
119 void testRegularizedBetaZeroPositivePositive() {
120 testRegularizedBeta(0.0, 0.0, 1.0, 2.0);
121 }
122
123 @Test
124 void testRegularizedBetaPositiveZeroPositive() {
125 testRegularizedBeta(Double.NaN, 0.5, 0.0, 2.0);
126 }
127
128 @Test
129 void testRegularizedBetaPositivePositiveZero() {
130 testRegularizedBeta(Double.NaN, 0.5, 1.0, 0.0);
131 }
132
133 @Test
134 void testRegularizedBetaPositivePositivePositive() {
135 testRegularizedBeta(0.75, 0.5, 1.0, 2.0);
136 }
137
138 @Test
139 void testRegularizedBetaTinyArgument() {
140 double actual = Beta.regularizedBeta(1e-17, 1.0, 1e12);
141
142 UnitTestUtils.customAssertEquals(9.999950000166648e-6, actual, 1e-16);
143 }
144
145 @Test
146 void testMath1067() {
147 final double x = 0.22580645161290325;
148 final double a = 64.33333333333334;
149 final double b = 223;
150
151 assertDoesNotThrow(() -> {
152 Beta.regularizedBeta(x, a, b, 1e-14, 10000);
153 }, "Infinite recursion");
154 }
155
156 @Test
157 void testLogBetaNanPositive() {
158 testLogBeta(Double.NaN, Double.NaN, 2.0);
159 }
160
161 @Test
162 void testLogBetaPositiveNan() {
163 testLogBeta(Double.NaN, 1.0, Double.NaN);
164 }
165
166 @Test
167 void testLogBetaNegativePositive() {
168 testLogBeta(Double.NaN, -1.0, 2.0);
169 }
170
171 @Test
172 void testLogBetaPositiveNegative() {
173 testLogBeta(Double.NaN, 1.0, -2.0);
174 }
175
176 @Test
177 void testLogBetaZeroPositive() {
178 testLogBeta(Double.NaN, 0.0, 2.0);
179 }
180
181 @Test
182 void testLogBetaPositiveZero() {
183 testLogBeta(Double.NaN, 1.0, 0.0);
184 }
185
186 @Test
187 void testLogBetaPositivePositive() {
188 testLogBeta(-0.693147180559945, 1.0, 2.0);
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 private static final double[][] LOG_GAMMA_SUM_REF = {
213 { 1.0 , 1.0 , 0.0 },
214 { 1.0 , 1.125 , .05775985153034387 },
215 { 1.0 , 1.25 , .1248717148923966 },
216 { 1.0 , 1.375 , .2006984603774558 },
217 { 1.0 , 1.5 , .2846828704729192 },
218 { 1.0 , 1.625 , .3763336820249054 },
219 { 1.0 , 1.75 , .4752146669149371 },
220 { 1.0 , 1.875 , .5809359740231859 },
221 { 1.0 , 2.0 , .6931471805599453 },
222 { 1.125 , 1.0 , .05775985153034387 },
223 { 1.125 , 1.125 , .1248717148923966 },
224 { 1.125 , 1.25 , .2006984603774558 },
225 { 1.125 , 1.375 , .2846828704729192 },
226 { 1.125 , 1.5 , .3763336820249054 },
227 { 1.125 , 1.625 , .4752146669149371 },
228 { 1.125 , 1.75 , .5809359740231859 },
229 { 1.125 , 1.875 , .6931471805599453 },
230 { 1.125 , 2.0 , 0.811531653906724 },
231 { 1.25 , 1.0 , .1248717148923966 },
232 { 1.25 , 1.125 , .2006984603774558 },
233 { 1.25 , 1.25 , .2846828704729192 },
234 { 1.25 , 1.375 , .3763336820249054 },
235 { 1.25 , 1.5 , .4752146669149371 },
236 { 1.25 , 1.625 , .5809359740231859 },
237 { 1.25 , 1.75 , .6931471805599453 },
238 { 1.25 , 1.875 , 0.811531653906724 },
239 { 1.25 , 2.0 , .9358019311087253 },
240 { 1.375 , 1.0 , .2006984603774558 },
241 { 1.375 , 1.125 , .2846828704729192 },
242 { 1.375 , 1.25 , .3763336820249054 },
243 { 1.375 , 1.375 , .4752146669149371 },
244 { 1.375 , 1.5 , .5809359740231859 },
245 { 1.375 , 1.625 , .6931471805599453 },
246 { 1.375 , 1.75 , 0.811531653906724 },
247 { 1.375 , 1.875 , .9358019311087253 },
248 { 1.375 , 2.0 , 1.06569589786406 },
249 { 1.5 , 1.0 , .2846828704729192 },
250 { 1.5 , 1.125 , .3763336820249054 },
251 { 1.5 , 1.25 , .4752146669149371 },
252 { 1.5 , 1.375 , .5809359740231859 },
253 { 1.5 , 1.5 , .6931471805599453 },
254 { 1.5 , 1.625 , 0.811531653906724 },
255 { 1.5 , 1.75 , .9358019311087253 },
256 { 1.5 , 1.875 , 1.06569589786406 },
257 { 1.5 , 2.0 , 1.200973602347074 },
258 { 1.625 , 1.0 , .3763336820249054 },
259 { 1.625 , 1.125 , .4752146669149371 },
260 { 1.625 , 1.25 , .5809359740231859 },
261 { 1.625 , 1.375 , .6931471805599453 },
262 { 1.625 , 1.5 , 0.811531653906724 },
263 { 1.625 , 1.625 , .9358019311087253 },
264 { 1.625 , 1.75 , 1.06569589786406 },
265 { 1.625 , 1.875 , 1.200973602347074 },
266 { 1.625 , 2.0 , 1.341414578068493 },
267 { 1.75 , 1.0 , .4752146669149371 },
268 { 1.75 , 1.125 , .5809359740231859 },
269 { 1.75 , 1.25 , .6931471805599453 },
270 { 1.75 , 1.375 , 0.811531653906724 },
271 { 1.75 , 1.5 , .9358019311087253 },
272 { 1.75 , 1.625 , 1.06569589786406 },
273 { 1.75 , 1.75 , 1.200973602347074 },
274 { 1.75 , 1.875 , 1.341414578068493 },
275 { 1.75 , 2.0 , 1.486815578593417 },
276 { 1.875 , 1.0 , .5809359740231859 },
277 { 1.875 , 1.125 , .6931471805599453 },
278 { 1.875 , 1.25 , 0.811531653906724 },
279 { 1.875 , 1.375 , .9358019311087253 },
280 { 1.875 , 1.5 , 1.06569589786406 },
281 { 1.875 , 1.625 , 1.200973602347074 },
282 { 1.875 , 1.75 , 1.341414578068493 },
283 { 1.875 , 1.875 , 1.486815578593417 },
284 { 1.875 , 2.0 , 1.6369886482725 },
285 { 2.0 , 1.0 , .6931471805599453 },
286 { 2.0 , 1.125 , 0.811531653906724 },
287 { 2.0 , 1.25 , .9358019311087253 },
288 { 2.0 , 1.375 , 1.06569589786406 },
289 { 2.0 , 1.5 , 1.200973602347074 },
290 { 2.0 , 1.625 , 1.341414578068493 },
291 { 2.0 , 1.75 , 1.486815578593417 },
292 { 2.0 , 1.875 , 1.6369886482725 },
293 { 2.0 , 2.0 , 1.791759469228055 },
294 };
295
296 private static double logGammaSum(final double a, final double b) {
297
298
299
300
301 try {
302 return ((Double) LOG_GAMMA_SUM_METHOD.invoke(null, a, b)).doubleValue();
303 } catch (final IllegalAccessException e) {
304 fail(e.getMessage());
305 } catch (final IllegalArgumentException e) {
306 fail(e.getMessage());
307 } catch (final InvocationTargetException e) {
308 final Throwable te = e.getTargetException();
309 if (te instanceof MathIllegalArgumentException) {
310 throw (MathIllegalArgumentException) te;
311 }
312 fail(e.getMessage());
313 }
314 return Double.NaN;
315 }
316
317 @Test
318 void testLogGammaSum() {
319 final int ulps = 2;
320 for (int i = 0; i < LOG_GAMMA_SUM_REF.length; i++) {
321 final double[] ref = LOG_GAMMA_SUM_REF[i];
322 final double a = ref[0];
323 final double b = ref[1];
324 final double expected = ref[2];
325 final double actual = logGammaSum(a, b);
326 final double tol = ulps * FastMath.ulp(expected);
327 final StringBuilder builder = new StringBuilder();
328 builder.append(a).append(", ").append(b);
329 assertEquals(expected, actual, tol, builder.toString());
330 }
331 }
332
333 @Test
334 void testLogGammaSumPrecondition1() {
335 assertThrows(MathIllegalArgumentException.class, () -> {
336
337 logGammaSum(0.0, 1.0);
338 });
339 }
340
341 @Test
342 void testLogGammaSumPrecondition2() {
343 assertThrows(MathIllegalArgumentException.class, () -> {
344
345 logGammaSum(3.0, 1.0);
346 });
347 }
348
349 @Test
350 void testLogGammaSumPrecondition3() {
351 assertThrows(MathIllegalArgumentException.class, () -> {
352
353 logGammaSum(1.0, 0.0);
354 });
355 }
356
357 @Test
358 void testLogGammaSumPrecondition4() {
359 assertThrows(MathIllegalArgumentException.class, () -> {
360
361 logGammaSum(1.0, 3.0);
362 });
363 }
364
365 private static final double[][] LOG_GAMMA_MINUS_LOG_GAMMA_SUM_REF = {
366
367
368 { 0.0 , 10.0 , 0.0 },
369 { 0.0 , 11.0 , 0.0 },
370 { 0.0 , 12.0 , 0.0 },
371 { 0.0 , 13.0 , 0.0 },
372 { 0.0 , 14.0 , 0.0 },
373 { 0.0 , 15.0 , 0.0 },
374 { 0.0 , 16.0 , 0.0 },
375 { 0.0 , 17.0 , 0.0 },
376 { 0.0 , 18.0 , 0.0 },
377
378
379 { 1.0 , 10.0 , - 2.302585092994046 },
380 { 1.0 , 11.0 , - 2.397895272798371 },
381 { 1.0 , 12.0 , - 2.484906649788 },
382 { 1.0 , 13.0 , - 2.564949357461537 },
383 { 1.0 , 14.0 , - 2.639057329615258 },
384 { 1.0 , 15.0 , - 2.70805020110221 },
385 { 1.0 , 16.0 , - 2.772588722239781 },
386 { 1.0 , 17.0 , - 2.833213344056216 },
387 { 1.0 , 18.0 , - 2.890371757896165 },
388
389
390 { 2.0 , 10.0 , - 4.700480365792417 },
391 { 2.0 , 11.0 , - 4.882801922586371 },
392 { 2.0 , 12.0 , - 5.049856007249537 },
393 { 2.0 , 13.0 , - 5.204006687076795 },
394 { 2.0 , 14.0 , - 5.347107530717468 },
395 { 2.0 , 15.0 , - 5.480638923341991 },
396 { 2.0 , 16.0 , - 5.605802066295998 },
397 { 2.0 , 17.0 , - 5.723585101952381 },
398 { 2.0 , 18.0 , - 5.834810737062605 },
399
400
401 { 3.0 , 10.0 , - 7.185387015580416 },
402 { 3.0 , 11.0 , - 7.447751280047908 },
403 { 3.0 , 12.0 , - 7.688913336864796 },
404 { 3.0 , 13.0 , - 7.912056888179006 },
405 { 3.0 , 14.0 , - 8.11969625295725 },
406 { 3.0 , 15.0 , - 8.313852267398207 },
407 { 3.0 , 16.0 , - 8.496173824192162 },
408 { 3.0 , 17.0 , - 8.668024081118821 },
409 { 3.0 , 18.0 , - 8.830543010616596 },
410
411
412 { 4.0 , 10.0 , - 9.750336373041954 },
413 { 4.0 , 11.0 , - 10.08680860966317 },
414 { 4.0 , 12.0 , - 10.39696353796701 },
415 { 4.0 , 13.0 , - 10.68464561041879 },
416 { 4.0 , 14.0 , - 10.95290959701347 },
417 { 4.0 , 15.0 , - 11.20422402529437 },
418 { 4.0 , 16.0 , - 11.4406128033586 },
419 { 4.0 , 17.0 , - 11.66375635467281 },
420 { 4.0 , 18.0 , - 11.87506544834002 },
421
422
423 { 5.0 , 10.0 , - 12.38939370265721 },
424 { 5.0 , 11.0 , - 12.79485881076538 },
425 { 5.0 , 12.0 , - 13.16955226020679 },
426 { 5.0 , 13.0 , - 13.517858954475 },
427 { 5.0 , 14.0 , - 13.84328135490963 },
428 { 5.0 , 15.0 , - 14.14866300446081 },
429 { 5.0 , 16.0 , - 14.43634507691259 },
430 { 5.0 , 17.0 , - 14.70827879239624 },
431 { 5.0 , 18.0 , - 14.96610790169833 },
432
433
434 { 6.0 , 10.0 , - 15.09744390375942 },
435 { 6.0 , 11.0 , - 15.56744753300516 },
436 { 6.0 , 12.0 , - 16.002765604263 },
437 { 6.0 , 13.0 , - 16.40823071237117 },
438 { 6.0 , 14.0 , - 16.78772033407607 },
439 { 6.0 , 15.0 , - 17.14439527801481 },
440 { 6.0 , 16.0 , - 17.48086751463602 },
441 { 6.0 , 17.0 , - 17.79932124575455 },
442 { 6.0 , 18.0 , - 18.10160211762749 },
443
444
445 { 7.0 , 10.0 , - 17.8700326259992 },
446 { 7.0 , 11.0 , - 18.40066087706137 },
447 { 7.0 , 12.0 , - 18.89313736215917 },
448 { 7.0 , 13.0 , - 19.35266969153761 },
449 { 7.0 , 14.0 , - 19.78345260763006 },
450 { 7.0 , 15.0 , - 20.18891771573823 },
451 { 7.0 , 16.0 , - 20.57190996799433 },
452 { 7.0 , 17.0 , - 20.9348154616837 },
453 { 7.0 , 18.0 , - 21.27965594797543 },
454
455
456 { 8.0 , 10.0 , - 20.70324597005542 },
457 { 8.0 , 11.0 , - 21.29103263495754 },
458 { 8.0 , 12.0 , - 21.83757634132561 },
459 { 8.0 , 13.0 , - 22.3484019650916 },
460 { 8.0 , 14.0 , - 22.82797504535349 },
461 { 8.0 , 15.0 , - 23.27996016909654 },
462 { 8.0 , 16.0 , - 23.70740418392348 },
463 { 8.0 , 17.0 , - 24.11286929203165 },
464 { 8.0 , 18.0 , - 24.49853177284363 },
465
466
467 { 9.0 , 10.0 , - 23.59361772795159 },
468 { 9.0 , 11.0 , - 24.23547161412398 },
469 { 9.0 , 12.0 , - 24.8333086148796 },
470 { 9.0 , 13.0 , - 25.39292440281502 },
471 { 9.0 , 14.0 , - 25.9190174987118 },
472 { 9.0 , 15.0 , - 26.41545438502569 },
473 { 9.0 , 16.0 , - 26.88545801427143 },
474 { 9.0 , 17.0 , - 27.33174511689985 },
475 { 9.0 , 18.0 , - 27.75662831086511 },
476
477
478 { 10.0 , 10.0 , - 26.53805670711802 },
479 { 10.0 , 11.0 , - 27.23120388767797 },
480 { 10.0 , 12.0 , - 27.87783105260302 },
481 { 10.0 , 13.0 , - 28.48396685617334 },
482 { 10.0 , 14.0 , - 29.05451171464095 },
483 { 10.0 , 15.0 , - 29.59350821537364 },
484 { 10.0 , 16.0 , - 30.10433383913963 },
485 { 10.0 , 17.0 , - 30.58984165492133 },
486 { 10.0 , 18.0 , - 31.05246517686944 },
487 };
488
489 private static double logGammaMinusLogGammaSum(final double a, final double b) {
490
491
492
493
494 try {
495 final Method m = LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD;
496 return ((Double) m.invoke(null, a, b)).doubleValue();
497 } catch (final IllegalAccessException e) {
498 fail(e.getMessage());
499 } catch (final IllegalArgumentException e) {
500 fail(e.getMessage());
501 } catch (final InvocationTargetException e) {
502 final Throwable te = e.getTargetException();
503 if (te instanceof MathIllegalArgumentException) {
504 throw (MathIllegalArgumentException) te;
505 }
506 fail(e.getMessage());
507 }
508 return Double.NaN;
509 }
510
511 @Test
512 void testLogGammaMinusLogGammaSum() {
513 final int ulps = 4;
514 for (int i = 0; i < LOG_GAMMA_MINUS_LOG_GAMMA_SUM_REF.length; i++) {
515 final double[] ref = LOG_GAMMA_MINUS_LOG_GAMMA_SUM_REF[i];
516 final double a = ref[0];
517 final double b = ref[1];
518 final double expected = ref[2];
519 final double actual = logGammaMinusLogGammaSum(a, b);
520 final double tol = ulps * FastMath.ulp(expected);
521 final StringBuilder builder = new StringBuilder();
522 builder.append(a).append(", ").append(b);
523 assertEquals(expected, actual, tol, builder.toString());
524 }
525 }
526
527 @Test
528 void testLogGammaMinusLogGammaSumPrecondition1() {
529 assertThrows(MathIllegalArgumentException.class, () -> {
530 logGammaMinusLogGammaSum(-1.0, 8.0);
531 });
532 }
533
534 @Test
535 void testLogGammaMinusLogGammaSumPrecondition2() {
536 assertThrows(MathIllegalArgumentException.class, () -> {
537 logGammaMinusLogGammaSum(1.0, 7.0);
538 });
539 }
540
541 private static final double[][] SUM_DELTA_MINUS_DELTA_SUM_REF = {
542 { 10.0 , 10.0 , .01249480717472882 },
543 { 10.0 , 11.0 , .01193628470267385 },
544 { 10.0 , 12.0 , .01148578547212797 },
545 { 10.0 , 13.0 , .01111659739668398 },
546 { 10.0 , 14.0 , .01080991216314295 },
547 { 10.0 , 15.0 , .01055214134859758 },
548 { 10.0 , 16.0 , .01033324912491747 },
549 { 10.0 , 17.0 , .01014568069918883 },
550 { 10.0 , 18.0 , .009983653199146491 },
551 { 10.0 , 19.0 , .009842674320242729 },
552 { 10.0 , 20.0 , 0.0097192081956071 },
553 { 11.0 , 10.0 , .01193628470267385 },
554 { 11.0 , 11.0 , .01135973290745925 },
555 { 11.0 , 12.0 , .01089355537047828 },
556 { 11.0 , 13.0 , .01051064829297728 },
557 { 11.0 , 14.0 , 0.0101918899639826 },
558 { 11.0 , 15.0 , .009923438811859604 },
559 { 11.0 , 16.0 , .009695052724952705 },
560 { 11.0 , 17.0 , 0.00949900745283617 },
561 { 11.0 , 18.0 , .009329379874933402 },
562 { 11.0 , 19.0 , 0.00918156080743147 },
563 { 11.0 , 20.0 , 0.00905191635141762 },
564 { 12.0 , 10.0 , .01148578547212797 },
565 { 12.0 , 11.0 , .01089355537047828 },
566 { 12.0 , 12.0 , .01041365883144029 },
567 { 12.0 , 13.0 , .01001867865848564 },
568 { 12.0 , 14.0 , 0.00968923999191334 },
569 { 12.0 , 15.0 , .009411294976563555 },
570 { 12.0 , 16.0 , .009174432043268762 },
571 { 12.0 , 17.0 , .008970786693291802 },
572 { 12.0 , 18.0 , .008794318926790865 },
573 { 12.0 , 19.0 , .008640321527910711 },
574 { 12.0 , 20.0 , .008505077879954796 },
575 { 13.0 , 10.0 , .01111659739668398 },
576 { 13.0 , 11.0 , .01051064829297728 },
577 { 13.0 , 12.0 , .01001867865848564 },
578 { 13.0 , 13.0 , .009613018147953376 },
579 { 13.0 , 14.0 , .009274085618154277 },
580 { 13.0 , 15.0 , 0.0089876637564166 },
581 { 13.0 , 16.0 , .008743200745261382 },
582 { 13.0 , 17.0 , .008532715206686251 },
583 { 13.0 , 18.0 , .008350069108807093 },
584 { 13.0 , 19.0 , .008190472517984874 },
585 { 13.0 , 20.0 , .008050138630244345 },
586 { 14.0 , 10.0 , .01080991216314295 },
587 { 14.0 , 11.0 , 0.0101918899639826 },
588 { 14.0 , 12.0 , 0.00968923999191334 },
589 { 14.0 , 13.0 , .009274085618154277 },
590 { 14.0 , 14.0 , .008926676241967286 },
591 { 14.0 , 15.0 , .008632654302369184 },
592 { 14.0 , 16.0 , .008381351102615795 },
593 { 14.0 , 17.0 , .008164687232662443 },
594 { 14.0 , 18.0 , .007976441942841219 },
595 { 14.0 , 19.0 , .007811755112234388 },
596 { 14.0 , 20.0 , .007666780069317652 },
597 { 15.0 , 10.0 , .01055214134859758 },
598 { 15.0 , 11.0 , .009923438811859604 },
599 { 15.0 , 12.0 , .009411294976563555 },
600 { 15.0 , 13.0 , 0.0089876637564166 },
601 { 15.0 , 14.0 , .008632654302369184 },
602 { 15.0 , 15.0 , 0.00833179217417291 },
603 { 15.0 , 16.0 , .008074310643041299 },
604 { 15.0 , 17.0 , .007852047581145882 },
605 { 15.0 , 18.0 , .007658712051540045 },
606 { 15.0 , 19.0 , .007489384065757007 },
607 { 15.0 , 20.0 , .007340165635725612 },
608 { 16.0 , 10.0 , .01033324912491747 },
609 { 16.0 , 11.0 , .009695052724952705 },
610 { 16.0 , 12.0 , .009174432043268762 },
611 { 16.0 , 13.0 , .008743200745261382 },
612 { 16.0 , 14.0 , .008381351102615795 },
613 { 16.0 , 15.0 , .008074310643041299 },
614 { 16.0 , 16.0 , .007811229919967624 },
615 { 16.0 , 17.0 , .007583876618287594 },
616 { 16.0 , 18.0 , .007385899933505551 },
617 { 16.0 , 19.0 , .007212328560607852 },
618 { 16.0 , 20.0 , .007059220321091879 },
619 { 17.0 , 10.0 , .01014568069918883 },
620 { 17.0 , 11.0 , 0.00949900745283617 },
621 { 17.0 , 12.0 , .008970786693291802 },
622 { 17.0 , 13.0 , .008532715206686251 },
623 { 17.0 , 14.0 , .008164687232662443 },
624 { 17.0 , 15.0 , .007852047581145882 },
625 { 17.0 , 16.0 , .007583876618287594 },
626 { 17.0 , 17.0 , .007351882161431358 },
627 { 17.0 , 18.0 , .007149662089534654 },
628 { 17.0 , 19.0 , .006972200907152378 },
629 { 17.0 , 20.0 , .006815518216094137 },
630 { 18.0 , 10.0 , .009983653199146491 },
631 { 18.0 , 11.0 , .009329379874933402 },
632 { 18.0 , 12.0 , .008794318926790865 },
633 { 18.0 , 13.0 , .008350069108807093 },
634 { 18.0 , 14.0 , .007976441942841219 },
635 { 18.0 , 15.0 , .007658712051540045 },
636 { 18.0 , 16.0 , .007385899933505551 },
637 { 18.0 , 17.0 , .007149662089534654 },
638 { 18.0 , 18.0 , .006943552208153373 },
639 { 18.0 , 19.0 , .006762516574228829 },
640 { 18.0 , 20.0 , .006602541598043117 },
641 { 19.0 , 10.0 , .009842674320242729 },
642 { 19.0 , 11.0 , 0.00918156080743147 },
643 { 19.0 , 12.0 , .008640321527910711 },
644 { 19.0 , 13.0 , .008190472517984874 },
645 { 19.0 , 14.0 , .007811755112234388 },
646 { 19.0 , 15.0 , .007489384065757007 },
647 { 19.0 , 16.0 , .007212328560607852 },
648 { 19.0 , 17.0 , .006972200907152378 },
649 { 19.0 , 18.0 , .006762516574228829 },
650 { 19.0 , 19.0 , .006578188655176814 },
651 { 19.0 , 20.0 , .006415174623476747 },
652 { 20.0 , 10.0 , 0.0097192081956071 },
653 { 20.0 , 11.0 , 0.00905191635141762 },
654 { 20.0 , 12.0 , .008505077879954796 },
655 { 20.0 , 13.0 , .008050138630244345 },
656 { 20.0 , 14.0 , .007666780069317652 },
657 { 20.0 , 15.0 , .007340165635725612 },
658 { 20.0 , 16.0 , .007059220321091879 },
659 { 20.0 , 17.0 , .006815518216094137 },
660 { 20.0 , 18.0 , .006602541598043117 },
661 { 20.0 , 19.0 , .006415174623476747 },
662 { 20.0 , 20.0 , .006249349445691423 },
663 };
664
665 private static double sumDeltaMinusDeltaSum(final double a,
666 final double b) {
667
668
669
670
671 try {
672 final Method m = SUM_DELTA_MINUS_DELTA_SUM_METHOD;
673 return ((Double) m.invoke(null, a, b)).doubleValue();
674 } catch (final IllegalAccessException e) {
675 fail(e.getMessage());
676 } catch (final IllegalArgumentException e) {
677 fail(e.getMessage());
678 } catch (final InvocationTargetException e) {
679 final Throwable te = e.getTargetException();
680 if (te instanceof MathIllegalArgumentException) {
681 throw (MathIllegalArgumentException) te;
682 }
683 fail(e.getMessage());
684 }
685 return Double.NaN;
686 }
687
688 @Test
689 void testSumDeltaMinusDeltaSum() {
690
691 final int ulps = 3;
692 for (int i = 0; i < SUM_DELTA_MINUS_DELTA_SUM_REF.length; i++) {
693 final double[] ref = SUM_DELTA_MINUS_DELTA_SUM_REF[i];
694 final double a = ref[0];
695 final double b = ref[1];
696 final double expected = ref[2];
697 final double actual = sumDeltaMinusDeltaSum(a, b);
698 final double tol = ulps * FastMath.ulp(expected);
699 final StringBuilder builder = new StringBuilder();
700 builder.append(a).append(", ").append(b);
701 assertEquals(expected, actual, tol, builder.toString());
702 }
703 }
704
705 @Test
706 void testSumDeltaMinusDeltaSumPrecondition1() {
707 assertThrows(MathIllegalArgumentException.class, () -> {
708
709 sumDeltaMinusDeltaSum(9.0, 10.0);
710 });
711 }
712
713 @Test
714 void testSumDeltaMinusDeltaSumPrecondition2() {
715 assertThrows(MathIllegalArgumentException.class, () -> {
716
717 sumDeltaMinusDeltaSum(10.0, 9.0);
718 });
719 }
720
721 private static final double[][] LOG_BETA_REF = {
722 { 0.125 , 0.125 , 2.750814190409515 },
723 { 0.125 , 0.25 , 2.444366899981226 },
724 { 0.125 , 0.5 , 2.230953804989556 },
725 { 0.125 , 1.0 , 2.079441541679836 },
726 { 0.125 , 2.0 , 1.961658506023452 },
727 { 0.125 , 3.0 , 1.901033884207018 },
728 { 0.125 , 4.0 , 1.860211889686763 },
729 { 0.125 , 5.0 , 1.829440231020009 },
730 { 0.125 , 6.0 , 1.804747618429637 },
731 { 0.125 , 7.0 , 1.784128331226902 },
732 { 0.125 , 8.0 , 1.766428754127501 },
733 { 0.125 , 9.0 , 1.750924567591535 },
734 { 0.125 , 10.0 , 1.7371312454592 },
735 { 0.125 , 1000.0 , 1.156003642015969 },
736 { 0.125 , 1001.0 , 1.155878649827818 },
737 { 0.125 , 10000.0 , .8681312798751318 },
738 { 0.25 , 0.125 , 2.444366899981226 },
739 { 0.25 , 0.25 , 2.003680106471455 },
740 { 0.25 , 0.5 , 1.657106516191482 },
741 { 0.25 , 1.0 , 1.386294361119891 },
742 { 0.25 , 2.0 , 1.163150809805681 },
743 { 0.25 , 3.0 , 1.045367774149297 },
744 { 0.25 , 4.0 , 0.965325066475761 },
745 { 0.25 , 5.0 , .9047004446593261 },
746 { 0.25 , 6.0 , .8559102804898941 },
747 { 0.25 , 7.0 , 0.815088285969639 },
748 { 0.25 , 8.0 , .7799969661583689 },
749 { 0.25 , 9.0 , .7492253074916152 },
750 { 0.25 , 10.0 , .7218263333035008 },
751 { 0.25 , 1000.0 , - .4388225372378877 },
752 { 0.25 , 1001.0 , - .4390725059930951 },
753 { 0.25 , 10000.0 , - 1.014553193217846 },
754 { 0.5 , 0.125 , 2.230953804989556 },
755 { 0.5 , 0.25 , 1.657106516191482 },
756 { 0.5 , 0.5 , 1.1447298858494 },
757 { 0.5 , 1.0 , .6931471805599453 },
758 { 0.5 , 2.0 , .2876820724517809 },
759 { 0.5 , 3.0 , .06453852113757118 },
760
761 { 0.5 , 5.0 , - .2073951943460706 },
762 { 0.5 , 6.0 , - .3027053741503954 },
763 { 0.5 , 7.0 , - .3827480818239319 },
764 { 0.5 , 8.0 , - .4517409533108833 },
765 { 0.5 , 9.0 , - .5123655751273182 },
766 { 0.5 , 10.0 , - .5664327963975939 },
767 { 0.5 , 1000.0 , - 2.881387696571577 },
768 { 0.5 , 1001.0 , - 2.881887571613228 },
769 { 0.5 , 10000.0 , - 4.032792743063396 },
770 { 1.0 , 0.125 , 2.079441541679836 },
771 { 1.0 , 0.25 , 1.386294361119891 },
772 { 1.0 , 0.5 , .6931471805599453 },
773 { 1.0 , 1.0 , 0.0 },
774 { 1.0 , 2.0 , - .6931471805599453 },
775 { 1.0 , 3.0 , - 1.09861228866811 },
776 { 1.0 , 4.0 , - 1.386294361119891 },
777 { 1.0 , 5.0 , - 1.6094379124341 },
778 { 1.0 , 6.0 , - 1.791759469228055 },
779 { 1.0 , 7.0 , - 1.945910149055313 },
780 { 1.0 , 8.0 , - 2.079441541679836 },
781 { 1.0 , 9.0 , - 2.19722457733622 },
782 { 1.0 , 10.0 , - 2.302585092994046 },
783 { 1.0 , 1000.0 , - 6.907755278982137 },
784 { 1.0 , 1001.0 , - 6.90875477931522 },
785 { 1.0 , 10000.0 , - 9.210340371976184 },
786 { 2.0 , 0.125 , 1.961658506023452 },
787 { 2.0 , 0.25 , 1.163150809805681 },
788 { 2.0 , 0.5 , .2876820724517809 },
789 { 2.0 , 1.0 , - .6931471805599453 },
790 { 2.0 , 2.0 , - 1.791759469228055 },
791 { 2.0 , 3.0 , - 2.484906649788 },
792 { 2.0 , 4.0 , - 2.995732273553991 },
793 { 2.0 , 5.0 , - 3.401197381662155 },
794 { 2.0 , 6.0 , - 3.737669618283368 },
795 { 2.0 , 7.0 , - 4.02535169073515 },
796 { 2.0 , 8.0 , - 4.276666119016055 },
797 { 2.0 , 9.0 , - 4.499809670330265 },
798 { 2.0 , 10.0 , - 4.700480365792417 },
799 { 2.0 , 1000.0 , - 13.81651005829736 },
800 { 2.0 , 1001.0 , - 13.81850806096003 },
801 { 2.0 , 10000.0 , - 18.4207807389527 },
802 { 3.0 , 0.125 , 1.901033884207018 },
803 { 3.0 , 0.25 , 1.045367774149297 },
804 { 3.0 , 0.5 , .06453852113757118 },
805 { 3.0 , 1.0 , - 1.09861228866811 },
806 { 3.0 , 2.0 , - 2.484906649788 },
807 { 3.0 , 3.0 , - 3.401197381662155 },
808 { 3.0 , 4.0 , - 4.0943445622221 },
809 { 3.0 , 5.0 , - 4.653960350157523 },
810 { 3.0 , 6.0 , - 5.123963979403259 },
811 { 3.0 , 7.0 , - 5.529429087511423 },
812 { 3.0 , 8.0 , - 5.886104031450156 },
813 { 3.0 , 9.0 , - 6.20455776256869 },
814 { 3.0 , 10.0 , - 6.492239835020471 },
815 { 3.0 , 1000.0 , - 20.03311615938222 },
816 { 3.0 , 1001.0 , - 20.03611166836202 },
817 { 3.0 , 10000.0 , - 26.9381739103716 },
818 { 4.0 , 0.125 , 1.860211889686763 },
819 { 4.0 , 0.25 , 0.965325066475761 },
820
821 { 4.0 , 1.0 , - 1.386294361119891 },
822 { 4.0 , 2.0 , - 2.995732273553991 },
823 { 4.0 , 3.0 , - 4.0943445622221 },
824 { 4.0 , 4.0 , - 4.941642422609304 },
825 { 4.0 , 5.0 , - 5.634789603169249 },
826 { 4.0 , 6.0 , - 6.222576268071369 },
827 { 4.0 , 7.0 , - 6.733401891837359 },
828 { 4.0 , 8.0 , - 7.185387015580416 },
829 { 4.0 , 9.0 , - 7.590852123688581 },
830 { 4.0 , 10.0 , - 7.958576903813898 },
831 { 4.0 , 1000.0 , - 25.84525465867605 },
832 { 4.0 , 1001.0 , - 25.84924667994559 },
833 { 4.0 , 10000.0 , - 35.05020194868867 },
834 { 5.0 , 0.125 , 1.829440231020009 },
835 { 5.0 , 0.25 , .9047004446593261 },
836 { 5.0 , 0.5 , - .2073951943460706 },
837 { 5.0 , 1.0 , - 1.6094379124341 },
838 { 5.0 , 2.0 , - 3.401197381662155 },
839 { 5.0 , 3.0 , - 4.653960350157523 },
840 { 5.0 , 4.0 , - 5.634789603169249 },
841 { 5.0 , 5.0 , - 6.445719819385578 },
842 { 5.0 , 6.0 , - 7.138866999945524 },
843 { 5.0 , 7.0 , - 7.745002803515839 },
844 { 5.0 , 8.0 , - 8.283999304248526 },
845 { 5.0 , 9.0 , - 8.769507120030227 },
846 { 5.0 , 10.0 , - 9.211339872309265 },
847 { 5.0 , 1000.0 , - 31.37070759780783 },
848 { 5.0 , 1001.0 , - 31.37569513931887 },
849 { 5.0 , 10000.0 , - 42.87464787956629 },
850 { 6.0 , 0.125 , 1.804747618429637 },
851 { 6.0 , 0.25 , .8559102804898941 },
852 { 6.0 , 0.5 , - .3027053741503954 },
853 { 6.0 , 1.0 , - 1.791759469228055 },
854 { 6.0 , 2.0 , - 3.737669618283368 },
855 { 6.0 , 3.0 , - 5.123963979403259 },
856 { 6.0 , 4.0 , - 6.222576268071369 },
857 { 6.0 , 5.0 , - 7.138866999945524 },
858 { 6.0 , 6.0 , - 7.927324360309794 },
859 { 6.0 , 7.0 , - 8.620471540869739 },
860 { 6.0 , 8.0 , - 9.239510749275963 },
861 { 6.0 , 9.0 , - 9.799126537211386 },
862 { 6.0 , 10.0 , - 10.30995216097738 },
863 { 6.0 , 1000.0 , - 36.67401250586691 },
864 { 6.0 , 1001.0 , - 36.67999457754446 },
865 { 6.0 , 10000.0 , - 50.47605021415003 },
866 { 7.0 , 0.125 , 1.784128331226902 },
867 { 7.0 , 0.25 , 0.815088285969639 },
868 { 7.0 , 0.5 , - .3827480818239319 },
869 { 7.0 , 1.0 , - 1.945910149055313 },
870 { 7.0 , 2.0 , - 4.02535169073515 },
871 { 7.0 , 3.0 , - 5.529429087511423 },
872 { 7.0 , 4.0 , - 6.733401891837359 },
873 { 7.0 , 5.0 , - 7.745002803515839 },
874 { 7.0 , 6.0 , - 8.620471540869739 },
875 { 7.0 , 7.0 , - 9.39366142910322 },
876 { 7.0 , 8.0 , - 10.08680860966317 },
877 { 7.0 , 9.0 , - 10.71541726908554 },
878 { 7.0 , 10.0 , - 11.2907814139891 },
879 { 7.0 , 1000.0 , - 41.79599038729854 },
880 { 7.0 , 1001.0 , - 41.80296600103496 },
881 { 7.0 , 10000.0 , - 57.89523093697012 },
882 { 8.0 , 0.125 , 1.766428754127501 },
883 { 8.0 , 0.25 , .7799969661583689 },
884 { 8.0 , 0.5 , - .4517409533108833 },
885 { 8.0 , 1.0 , - 2.079441541679836 },
886 { 8.0 , 2.0 , - 4.276666119016055 },
887 { 8.0 , 3.0 , - 5.886104031450156 },
888 { 8.0 , 4.0 , - 7.185387015580416 },
889 { 8.0 , 5.0 , - 8.283999304248526 },
890 { 8.0 , 6.0 , - 9.239510749275963 },
891 { 8.0 , 7.0 , - 10.08680860966317 },
892 { 8.0 , 8.0 , - 10.84894866171006 },
893 { 8.0 , 9.0 , - 11.54209584227001 },
894 { 8.0 , 10.0 , - 12.17808460899001 },
895 { 8.0 , 1000.0 , - 46.76481113096179 },
896 { 8.0 , 1001.0 , - 46.77277930061096 },
897 { 8.0 , 10000.0 , - 65.16036091500527 },
898 { 9.0 , 0.125 , 1.750924567591535 },
899 { 9.0 , 0.25 , .7492253074916152 },
900 { 9.0 , 0.5 , - .5123655751273182 },
901 { 9.0 , 1.0 , - 2.19722457733622 },
902 { 9.0 , 2.0 , - 4.499809670330265 },
903 { 9.0 , 3.0 , - 6.20455776256869 },
904 { 9.0 , 4.0 , - 7.590852123688581 },
905 { 9.0 , 5.0 , - 8.769507120030227 },
906 { 9.0 , 6.0 , - 9.799126537211386 },
907 { 9.0 , 7.0 , - 10.71541726908554 },
908 { 9.0 , 8.0 , - 11.54209584227001 },
909 { 9.0 , 9.0 , - 12.29586764464639 },
910 { 9.0 , 10.0 , - 12.98901482520633 },
911 { 9.0 , 1000.0 , - 51.60109303791327 },
912 { 9.0 , 1001.0 , - 51.61005277928474 },
913 { 9.0 , 10000.0 , - 72.29205942547217 },
914 { 10.0 , 0.125 , 1.7371312454592 },
915 { 10.0 , 0.25 , .7218263333035008 },
916 { 10.0 , 0.5 , - .5664327963975939 },
917 { 10.0 , 1.0 , - 2.302585092994046 },
918 { 10.0 , 2.0 , - 4.700480365792417 },
919 { 10.0 , 3.0 , - 6.492239835020471 },
920 { 10.0 , 4.0 , - 7.958576903813898 },
921 { 10.0 , 5.0 , - 9.211339872309265 },
922 { 10.0 , 6.0 , - 10.30995216097738 },
923 { 10.0 , 7.0 , - 11.2907814139891 },
924 { 10.0 , 8.0 , - 12.17808460899001 },
925 { 10.0 , 9.0 , - 12.98901482520633 },
926 { 10.0 , 10.0 , - 13.73622922703655 },
927 { 10.0 , 1000.0 , - 56.32058348093065 },
928 { 10.0 , 1001.0 , - 56.33053381178382 },
929 { 10.0 , 10000.0 , - 79.30607481535498 },
930 { 1000.0 , 0.125 , 1.156003642015969 },
931 { 1000.0 , 0.25 , - .4388225372378877 },
932 { 1000.0 , 0.5 , - 2.881387696571577 },
933 { 1000.0 , 1.0 , - 6.907755278982137 },
934 { 1000.0 , 2.0 , - 13.81651005829736 },
935 { 1000.0 , 3.0 , - 20.03311615938222 },
936 { 1000.0 , 4.0 , - 25.84525465867605 },
937 { 1000.0 , 5.0 , - 31.37070759780783 },
938 { 1000.0 , 6.0 , - 36.67401250586691 },
939 { 1000.0 , 7.0 , - 41.79599038729854 },
940 { 1000.0 , 8.0 , - 46.76481113096179 },
941 { 1000.0 , 9.0 , - 51.60109303791327 },
942 { 1000.0 , 10.0 , - 56.32058348093065 },
943 { 1000.0 , 1000.0 , - 1388.482601635902 },
944 { 1000.0 , 1001.0 , - 1389.175748816462 },
945 { 1000.0 , 10000.0 , - 3353.484270767097 },
946 { 1001.0 , 0.125 , 1.155878649827818 },
947 { 1001.0 , 0.25 , - .4390725059930951 },
948 { 1001.0 , 0.5 , - 2.881887571613228 },
949 { 1001.0 , 1.0 , - 6.90875477931522 },
950 { 1001.0 , 2.0 , - 13.81850806096003 },
951 { 1001.0 , 3.0 , - 20.03611166836202 },
952 { 1001.0 , 4.0 , - 25.84924667994559 },
953 { 1001.0 , 5.0 , - 31.37569513931887 },
954 { 1001.0 , 6.0 , - 36.67999457754446 },
955 { 1001.0 , 7.0 , - 41.80296600103496 },
956 { 1001.0 , 8.0 , - 46.77277930061096 },
957 { 1001.0 , 9.0 , - 51.61005277928474 },
958 { 1001.0 , 10.0 , - 56.33053381178382 },
959 { 1001.0 , 1000.0 , - 1389.175748816462 },
960 { 1001.0 , 1001.0 , - 1389.869395872064 },
961 { 1001.0 , 10000.0 , - 3355.882166039895 },
962 { 10000.0 , 0.125 , .8681312798751318 },
963 { 10000.0 , 0.25 , - 1.014553193217846 },
964 { 10000.0 , 0.5 , - 4.032792743063396 },
965 { 10000.0 , 1.0 , - 9.210340371976184 },
966 { 10000.0 , 2.0 , - 18.4207807389527 },
967 { 10000.0 , 3.0 , - 26.9381739103716 },
968 { 10000.0 , 4.0 , - 35.05020194868867 },
969 { 10000.0 , 5.0 , - 42.87464787956629 },
970 { 10000.0 , 6.0 , - 50.47605021415003 },
971 { 10000.0 , 7.0 , - 57.89523093697012 },
972 { 10000.0 , 8.0 , - 65.16036091500527 },
973 { 10000.0 , 9.0 , - 72.29205942547217 },
974 { 10000.0 , 10.0 , - 79.30607481535498 },
975 { 10000.0 , 1000.0 , - 3353.484270767097 },
976 { 10000.0 , 1001.0 , - 3355.882166039895 },
977 { 10000.0 , 10000.0 , - 13866.28325676141 },
978 };
979
980 @Test
981 void testLogBeta() {
982 final int ulps = 3;
983 for (int i = 0; i < LOG_BETA_REF.length; i++) {
984 final double[] ref = LOG_BETA_REF[i];
985 final double a = ref[0];
986 final double b = ref[1];
987 final double expected = ref[2];
988 final double actual = Beta.logBeta(a, b);
989 final double tol = ulps * FastMath.ulp(expected);
990 final StringBuilder builder = new StringBuilder();
991 builder.append(a).append(", ").append(b);
992 assertEquals(expected, actual, tol, builder.toString());
993 }
994 }}