1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.statistics.distribution;
19
20 import java.util.stream.Stream;
21 import org.junit.jupiter.api.Assertions;
22 import org.junit.jupiter.params.ParameterizedTest;
23 import org.junit.jupiter.params.provider.Arguments;
24 import org.junit.jupiter.params.provider.CsvSource;
25 import org.junit.jupiter.params.provider.MethodSource;
26
27
28
29
30
31 class LogUniformDistributionTest extends BaseContinuousDistributionTest {
32 @Override
33 ContinuousDistribution makeDistribution(Object... parameters) {
34 final double a = (Double) parameters[0];
35 final double b = (Double) parameters[1];
36 return LogUniformDistribution.of(a, b);
37 }
38
39 @Override
40 Object[][] makeInvalidParameters() {
41 return new Object[][] {
42
43 {0.0, 0.0},
44 {1.0, 0.5},
45
46 {Double.NaN, 1.0},
47 {0.5, Double.NaN},
48
49 {-1.0, 1.0},
50 {0.0, 1.0},
51 };
52 }
53
54 @Override
55 String[] getParameterNames() {
56 return new String[] {"SupportLowerBound", "SupportUpperBound"};
57 }
58
59 @Override
60 protected double getRelativeTolerance() {
61 return 5e-15;
62 }
63
64
65
66
67
68
69 @ParameterizedTest
70 @MethodSource
71 void testAdditionalMoments(double a, double b) {
72 final double diff = b - a;
73 final double denom = Math.log(b / a);
74 final double mean = diff / denom;
75
76 final double variance = mean * (b + a) / 2 - mean * mean;
77 TestUtils.assertEquals(mean, LogUniformDistribution.of(a, b).getMean(),
78 DoubleTolerances.relative(1e-14), "Mean");
79 TestUtils.assertEquals(variance, LogUniformDistribution.of(a, b).getVariance(),
80 DoubleTolerances.relative(1e-10), "Variance");
81 }
82
83 static Stream<Arguments> testAdditionalMoments() {
84 final Stream.Builder<Arguments> builder = Stream.builder();
85 for (final double a : new double[] {1, 10, 100}) {
86 for (final double x : new double[] {10, 20, 40}) {
87 builder.add(Arguments.of(a, a + x));
88 }
89 }
90 return builder.build();
91 }
92
93
94
95
96
97 @ParameterizedTest
98 @CsvSource({
99 "1e-100, 1e100",
100 "1e-10, 1e10",
101 })
102 void testExtremeSurvivalFunction(double a, double b) {
103 final LogUniformDistribution d = LogUniformDistribution.of(a, b);
104
105 final double u = Math.ulp(b);
106 int i = 1;
107 double p;
108 do {
109 p = d.survivalProbability(b - i * u);
110 i *= 2;
111 } while (p == 0);
112 Assertions.assertTrue(p < 0x1.0p-53, "sf is not small enough for high precision");
113 Assertions.assertNotEquals(1 - d.cumulativeProbability(b - i * u), p, "sf is not high precision: sf == 1 - cdf");
114 }
115 }