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