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 TriangularDistributionTest extends BaseContinuousDistributionTest {
32 @Override
33 ContinuousDistribution makeDistribution(Object... parameters) {
34 final double lower = (Double) parameters[0];
35 final double mode = (Double) parameters[1];
36 final double upper = (Double) parameters[2];
37 return TriangularDistribution.of(lower, mode, upper);
38 }
39
40
41 @Override
42 Object[][] makeInvalidParameters() {
43 return new Object[][] {
44 {0.0, 0.0, 0.0},
45
46 {4.0, 2.0, 3.0},
47 {3.0, 2.0, 3.0},
48 {2.5, 2.0, 3.0},
49 {1.0, 0.0, 3.0},
50 {1.0, 4.0, 3.0},
51 {1.0, 2.0, -1.0},
52 {1.0, 2.0, 1.5},
53 };
54 }
55
56 @Override
57 String[] getParameterNames() {
58 return new String[] {"SupportLowerBound", "Mode", "SupportUpperBound"};
59 }
60
61 @Override
62 protected double getRelativeTolerance() {
63
64 return 20 * RELATIVE_EPS;
65 }
66
67
68
69 @ParameterizedTest
70 @MethodSource
71 void testAdditionalMoments(double a, double b, double c, double mean, double variance) {
72 final TriangularDistribution dist = TriangularDistribution.of(a, b, c);
73 testMoments(dist, mean, variance, DoubleTolerances.equals());
74 }
75
76 static Stream<Arguments> testAdditionalMoments() {
77 return Stream.of(
78 Arguments.of(0, 0.5, 1.0, 0.5, 1 / 24.0),
79 Arguments.of(0, 1, 1, 2 / 3.0, 1 / 18.0),
80 Arguments.of(-3, 2, 12, 3 + (2 / 3.0), 175 / 18.0)
81 );
82 }
83
84 @ParameterizedTest
85 @CsvSource({
86 "1, 2, 3",
87 "0.12, 3.45, 12.56",
88 })
89 void testAdditionalParameterAccessors(double lower, double mode, double upper) {
90 final TriangularDistribution dist = TriangularDistribution.of(lower, mode, upper);
91 Assertions.assertEquals(lower, dist.getSupportLowerBound());
92 Assertions.assertEquals(mode, dist.getMode());
93 Assertions.assertEquals(upper, dist.getSupportUpperBound());
94 }
95 }