1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.distribution.continuous;
24
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertThrows;
31
32
33
34
35 public class TriangularDistributionTest extends RealDistributionAbstractTest {
36
37
38
39 @BeforeEach
40 @Override
41 public void setUp() {
42 super.setUp();
43 setTolerance(1e-4);
44 }
45
46
47
48
49
50
51 @Override
52 public TriangularDistribution makeDistribution() {
53
54 return new TriangularDistribution(-3, 2, 12);
55 }
56
57
58
59
60
61 @Override
62 public double[] makeCumulativeTestPoints() {
63 return new double[] { -3.0001,
64 -3.0,
65 -2.0, -1.0, 0.0, 1.0,
66 2.0,
67 3.0, 4.0, 10.0, 11.0,
68 12.0,
69 12.0001
70 };
71 }
72
73
74
75
76 @Override
77 public double[] makeCumulativeTestValues() {
78
79
80
81
82
83
84 double third = 1 / 3.0;
85 double left = 18.75;
86 double area = 56.25;
87 return new double[] { 0.0,
88 0.0,
89 0.75 / area, 3 / area, 6.75 / area, 12 / area,
90 third,
91 (left + 7.125) / area, (left + 13.5) / area,
92 (left + 36) / area, (left + 37.125) / area,
93 1.0,
94 1.0
95 };
96 }
97
98
99
100
101
102 @Override
103 public double[] makeInverseCumulativeTestPoints() {
104
105
106
107 double[] points = makeCumulativeTestValues();
108 double[] points2 = new double[points.length-2];
109 System.arraycopy(points, 1, points2, 0, points2.length);
110 return points2;
111
112 }
113
114
115
116
117
118 @Override
119 public double[] makeInverseCumulativeTestValues() {
120
121
122
123 double[] points = makeCumulativeTestPoints();
124 double[] points2 = new double[points.length-2];
125 System.arraycopy(points, 1, points2, 0, points2.length);
126 return points2;
127
128 }
129
130
131 @Override
132 public double[] makeDensityTestValues() {
133 return new double[] { 0,
134 0,
135 2 / 75.0, 4 / 75.0, 6 / 75.0, 8 / 75.0,
136 10 / 75.0,
137 9 / 75.0, 8 / 75.0, 2 / 75.0, 1 / 75.0,
138 0,
139 0
140 };
141 }
142
143
144
145
146 @Test
147 void testGetLowerBound() {
148 TriangularDistribution distribution = makeDistribution();
149 assertEquals(-3.0, distribution.getSupportLowerBound(), 0);
150 }
151
152
153 @Test
154 void testGetUpperBound() {
155 TriangularDistribution distribution = makeDistribution();
156 assertEquals(12.0, distribution.getSupportUpperBound(), 0);
157 }
158
159
160 @Test
161 void testPreconditions1() {
162 assertThrows(MathIllegalArgumentException.class, () -> {
163 new TriangularDistribution(0, 0, 0);
164 });
165 }
166
167
168 @Test
169 void testPreconditions2() {
170 assertThrows(MathIllegalArgumentException.class, () -> {
171 new TriangularDistribution(1, 1, 0);
172 });
173 }
174
175
176 @Test
177 void testPreconditions3() {
178 assertThrows(MathIllegalArgumentException.class, () -> {
179 new TriangularDistribution(0, 2, 1);
180 });
181 }
182
183
184 @Test
185 void testPreconditions4() {
186 assertThrows(MathIllegalArgumentException.class, () -> {
187 new TriangularDistribution(2, 1, 3);
188 });
189 }
190
191
192 @Test
193 void testMeanVariance() {
194 TriangularDistribution dist;
195
196 dist = new TriangularDistribution(0, 0.5, 1.0);
197 assertEquals(0.5, dist.getNumericalMean(), 0);
198 assertEquals(dist.getNumericalVariance(), 1 / 24.0, 0);
199
200 dist = new TriangularDistribution(0, 1, 1);
201 assertEquals(dist.getNumericalMean(), 2 / 3.0, 0);
202 assertEquals(dist.getNumericalVariance(), 1 / 18.0, 0);
203
204 dist = new TriangularDistribution(-3, 2, 12);
205 assertEquals(dist.getNumericalMean(), 3 + (2 / 3.0), 0);
206 assertEquals(dist.getNumericalVariance(), 175 / 18.0, 0);
207 }
208 }