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.distribution.discrete;
23
24 import org.hipparchus.exception.MathIllegalArgumentException;
25 import org.hipparchus.exception.MathRuntimeException;
26 import org.hipparchus.util.Pair;
27 import org.junit.jupiter.api.Test;
28
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import static org.junit.jupiter.api.Assertions.assertEquals;
34 import static org.junit.jupiter.api.Assertions.assertNull;
35 import static org.junit.jupiter.api.Assertions.assertTrue;
36 import static org.junit.jupiter.api.Assertions.fail;
37
38
39
40
41 public class EnumeratedIntegerDistributionTest {
42
43
44
45
46 private final EnumeratedIntegerDistribution testDistribution;
47
48
49
50
51 public EnumeratedIntegerDistributionTest() {
52
53
54 testDistribution = new EnumeratedIntegerDistribution(
55 new int[]{3, -1, 3, 7, -2, 8},
56 new double[]{0.2, 0.2, 0.3, 0.3, 0.0, 0.0});
57 }
58
59
60
61
62
63 @Test
64 void testExceptions() {
65 EnumeratedIntegerDistribution invalid = null;
66 try {
67 new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0});
68 fail("Expected MathIllegalArgumentException");
69 } catch (MathIllegalArgumentException e) {
70 }
71 try {
72 new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, -1.0});
73 fail("Expected MathIllegalArgumentException");
74 } catch (MathIllegalArgumentException e) {
75 }
76 try {
77 new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, 0.0});
78 fail("Expected MathRuntimeException");
79 } catch (MathRuntimeException e) {
80 }
81 try {
82 new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, Double.NaN});
83 fail("Expected MathIllegalArgumentException");
84 } catch (MathIllegalArgumentException e) {
85 }
86 try {
87 new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, Double.POSITIVE_INFINITY});
88 fail("Expected NotFiniteNumberException");
89 } catch (MathIllegalArgumentException e) {
90 }
91 assertNull(invalid, "Expected non-initialized DiscreteRealDistribution");
92 }
93
94
95
96
97 @Test
98 void testProbability() {
99 int[] points = new int[]{-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8};
100 double[] results = new double[]{0, 0.2, 0, 0, 0, 0.5, 0, 0, 0, 0.3, 0};
101 for (int p = 0; p < points.length; p++) {
102 double probability = testDistribution.probability(points[p]);
103 assertEquals(results[p], probability, 0.0);
104 }
105 }
106
107
108
109
110 @Test
111 void testCumulativeProbability() {
112 int[] points = new int[]{-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8};
113 double[] results = new double[]{0, 0.2, 0.2, 0.2, 0.2, 0.7, 0.7, 0.7, 0.7, 1.0, 1.0};
114 for (int p = 0; p < points.length; p++) {
115 double probability = testDistribution.cumulativeProbability(points[p]);
116 assertEquals(results[p], probability, 1e-10);
117 }
118 }
119
120
121
122
123 @Test
124 void testGetNumericalMean() {
125 assertEquals(3.4, testDistribution.getNumericalMean(), 1e-10);
126 }
127
128
129
130
131 @Test
132 void testGetNumericalVariance() {
133 assertEquals(7.84, testDistribution.getNumericalVariance(), 1e-10);
134 }
135
136
137
138
139 @Test
140 void testGetSupportLowerBound() {
141 assertEquals(-1, testDistribution.getSupportLowerBound());
142 }
143
144
145
146
147 @Test
148 void testGetSupportUpperBound() {
149 assertEquals(7, testDistribution.getSupportUpperBound());
150 }
151
152
153
154
155 @Test
156 void testIsSupportConnected() {
157 assertTrue(testDistribution.isSupportConnected());
158 }
159
160 @Test
161 void testCreateFromIntegers() {
162 final int[] data = new int[] {0, 1, 1, 2, 2, 2};
163 EnumeratedIntegerDistribution distribution = new EnumeratedIntegerDistribution(data);
164 assertEquals(0.5, distribution.probability(2), 0);
165 assertEquals(0.5, distribution.cumulativeProbability(1), 0);
166 }
167
168 @Test
169 void testGetPmf() {
170 final int[] values = new int[] {0,1,2,3,4};
171 final double[] masses = new double[] {0.2, 0.2, 0.4, 0.1, 0.1};
172 final EnumeratedIntegerDistribution distribution = new EnumeratedIntegerDistribution(values, masses);
173 final List<Pair<Integer, Double>> pmf = distribution.getPmf();
174 assertEquals(5, pmf.size());
175 final Map<Integer, Double> pmfMap = new HashMap<Integer, Double>();
176 for (int i = 0; i < 5; i++) {
177 pmfMap.put(i, masses[i]);
178 }
179 for (int i = 0; i < 5; i++) {
180 assertEquals(pmf.get(i).getSecond(), pmfMap.get(pmf.get(i).getFirst()), 0);
181 }
182 }
183 }