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