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.discrete;
24
25 import static org.junit.Assert.assertEquals;
26
27 import org.hipparchus.UnitTestUtils;
28 import org.hipparchus.distribution.IntegerDistribution;
29 import org.hipparchus.exception.MathIllegalArgumentException;
30 import org.hipparchus.util.Precision;
31 import org.junit.Assert;
32 import org.junit.Test;
33
34
35
36
37 public class HypergeometricDistributionTest extends IntegerDistributionAbstractTest {
38
39
40
41
42 public HypergeometricDistributionTest() {
43 setTolerance(1e-12);
44 }
45
46
47
48
49 @Override
50 public IntegerDistribution makeDistribution() {
51 return new HypergeometricDistribution(10, 5, 5);
52 }
53
54
55 @Override
56 public int[] makeDensityTestPoints() {
57 return new int[] {-1, 0, 1, 2, 3, 4, 5, 10};
58 }
59
60
61
62
63
64 @Override
65 public double[] makeDensityTestValues() {
66 return new double[] {0d, 0.00396825396825, 0.0992063492063, 0.396825396825, 0.396825396825,
67 0.0992063492063, 0.00396825396825, 0d};
68 }
69
70
71
72
73
74 @Override
75 public double[] makeLogDensityTestValues() {
76
77 return new double[] {Double.NEGATIVE_INFINITY, -5.52942908751142, -2.31055326264322, -0.924258901523332,
78 -0.924258901523332, -2.31055326264322, -5.52942908751142, Double.NEGATIVE_INFINITY};
79 }
80
81
82 @Override
83 public int[] makeCumulativeTestPoints() {
84 return makeDensityTestPoints();
85 }
86
87
88
89
90
91 @Override
92 public double[] makeCumulativeTestValues() {
93 return new double[] {0d, 0.00396825396825, 0.103174603175, .5, 0.896825396825, 0.996031746032,
94 1, 1};
95 }
96
97
98 @Override
99 public double[] makeInverseCumulativeTestPoints() {
100 return new double[] {0d, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d, 0.999d,
101 0.990d, 0.975d, 0.950d, 0.900d, 1d};
102 }
103
104
105 @Override
106 public int[] makeInverseCumulativeTestValues() {
107 return new int[] {0, 0, 1, 1, 1, 1, 5, 4, 4, 4, 4, 5};
108 }
109
110
111
112
113 @Test
114 public void testDegenerateNoFailures() {
115 HypergeometricDistribution dist = new HypergeometricDistribution(5,5,3);
116 setDistribution(dist);
117 setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
118 setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d});
119 setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
120 setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d});
121 setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
122 setInverseCumulativeTestValues(new int[] {3, 3});
123 verifyDensities();
124 verifyCumulativeProbabilities();
125 verifyInverseCumulativeProbabilities();
126 Assert.assertEquals(dist.getSupportLowerBound(), 3);
127 Assert.assertEquals(dist.getSupportUpperBound(), 3);
128 }
129
130
131 @Test
132 public void testDegenerateNoSuccesses() {
133 HypergeometricDistribution dist = new HypergeometricDistribution(5,0,3);
134 setDistribution(dist);
135 setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
136 setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d});
137 setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
138 setDensityTestValues(new double[] {0d, 1d, 0d, 0d, 0d});
139 setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
140 setInverseCumulativeTestValues(new int[] {0, 0});
141 verifyDensities();
142 verifyCumulativeProbabilities();
143 verifyInverseCumulativeProbabilities();
144 Assert.assertEquals(dist.getSupportLowerBound(), 0);
145 Assert.assertEquals(dist.getSupportUpperBound(), 0);
146 }
147
148
149 @Test
150 public void testDegenerateFullSample() {
151 HypergeometricDistribution dist = new HypergeometricDistribution(5,3,5);
152 setDistribution(dist);
153 setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
154 setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d});
155 setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
156 setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d});
157 setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
158 setInverseCumulativeTestValues(new int[] {3, 3});
159 verifyDensities();
160 verifyCumulativeProbabilities();
161 verifyInverseCumulativeProbabilities();
162 Assert.assertEquals(dist.getSupportLowerBound(), 3);
163 Assert.assertEquals(dist.getSupportUpperBound(), 3);
164 }
165
166 @Test
167 public void testPreconditions() {
168 try {
169 new HypergeometricDistribution(0, 3, 5);
170 Assert.fail("negative population size. MathIllegalArgumentException expected");
171 } catch(MathIllegalArgumentException ex) {
172
173 }
174 try {
175 new HypergeometricDistribution(5, -1, 5);
176 Assert.fail("negative number of successes. MathIllegalArgumentException expected");
177 } catch(MathIllegalArgumentException ex) {
178
179 }
180 try {
181 new HypergeometricDistribution(5, 3, -1);
182 Assert.fail("negative sample size. MathIllegalArgumentException expected");
183 } catch(MathIllegalArgumentException ex) {
184
185 }
186 try {
187 new HypergeometricDistribution(5, 6, 5);
188 Assert.fail("numberOfSuccesses > populationSize. MathIllegalArgumentException expected");
189 } catch(MathIllegalArgumentException ex) {
190
191 }
192 try {
193 new HypergeometricDistribution(5, 3, 6);
194 Assert.fail("sampleSize > populationSize. MathIllegalArgumentException expected");
195 } catch(MathIllegalArgumentException ex) {
196
197 }
198 }
199
200 @Test
201 public void testAccessors() {
202 HypergeometricDistribution dist = new HypergeometricDistribution(5, 3, 4);
203 Assert.assertEquals(5, dist.getPopulationSize());
204 Assert.assertEquals(3, dist.getNumberOfSuccesses());
205 Assert.assertEquals(4, dist.getSampleSize());
206 }
207
208 @Test
209 public void testLargeValues() {
210 int populationSize = 3456;
211 int sampleSize = 789;
212 int numberOfSucceses = 101;
213 double[][] data = {
214 {0.0, 2.75646034603961e-12, 2.75646034603961e-12, 1.0},
215 {1.0, 8.55705370142386e-11, 8.83269973602783e-11, 0.999999999997244},
216 {2.0, 1.31288129219665e-9, 1.40120828955693e-9, 0.999999999911673},
217 {3.0, 1.32724172984193e-8, 1.46736255879763e-8, 0.999999998598792},
218 {4.0, 9.94501711734089e-8, 1.14123796761385e-7, 0.999999985326375},
219 {5.0, 5.89080768883643e-7, 7.03204565645028e-7, 0.999999885876203},
220 {20.0, 0.0760051397707708, 0.27349758476299, 0.802507555007781},
221 {21.0, 0.087144222047629, 0.360641806810619, 0.72650241523701},
222 {22.0, 0.0940378846881819, 0.454679691498801, 0.639358193189381},
223 {23.0, 0.0956897500614809, 0.550369441560282, 0.545320308501199},
224 {24.0, 0.0919766921922999, 0.642346133752582, 0.449630558439718},
225 {25.0, 0.083641637261095, 0.725987771013677, 0.357653866247418},
226 {96.0, 5.93849188852098e-57, 1.0, 6.01900244560712e-57},
227 {97.0, 7.96593036832547e-59, 1.0, 8.05105570861321e-59},
228 {98.0, 8.44582921934367e-61, 1.0, 8.5125340287733e-61},
229 {99.0, 6.63604297068222e-63, 1.0, 6.670480942963e-63},
230 {100.0, 3.43501099007557e-65, 1.0, 3.4437972280786e-65},
231 {101.0, 8.78623800302957e-68, 1.0, 8.78623800302957e-68},
232 };
233
234 testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data);
235 }
236
237 private void testHypergeometricDistributionProbabilities(int populationSize, int sampleSize, int numberOfSucceses, double[][] data) {
238 HypergeometricDistribution dist = new HypergeometricDistribution(populationSize, numberOfSucceses, sampleSize);
239 for (int i = 0; i < data.length; ++i) {
240 int x = (int)data[i][0];
241 double pmf = data[i][1];
242 double actualPmf = dist.probability(x);
243 UnitTestUtils.assertRelativelyEquals("Expected equals for <"+x+"> pmf",pmf, actualPmf, 1.0e-9);
244
245 double cdf = data[i][2];
246 double actualCdf = dist.cumulativeProbability(x);
247 UnitTestUtils.assertRelativelyEquals("Expected equals for <"+x+"> cdf",cdf, actualCdf, 1.0e-9);
248
249 double cdf1 = data[i][3];
250 double actualCdf1 = dist.upperCumulativeProbability(x);
251 UnitTestUtils.assertRelativelyEquals("Expected equals for <"+x+"> cdf1",cdf1, actualCdf1, 1.0e-9);
252 }
253 }
254
255 @Test
256 public void testMoreLargeValues() {
257 int populationSize = 26896;
258 int sampleSize = 895;
259 int numberOfSucceses = 55;
260 double[][] data = {
261 {0.0, 0.155168304750504, 0.155168304750504, 1.0},
262 {1.0, 0.29437545000746, 0.449543754757964, 0.844831695249496},
263 {2.0, 0.273841321577003, 0.723385076334967, 0.550456245242036},
264 {3.0, 0.166488572570786, 0.889873648905753, 0.276614923665033},
265 {4.0, 0.0743969744713231, 0.964270623377076, 0.110126351094247},
266 {5.0, 0.0260542785784855, 0.990324901955562, 0.0357293766229237},
267 {20.0, 3.57101101678792e-16, 1.0, 3.78252101622096e-16},
268 {21.0, 2.00551638598312e-17, 1.0, 2.11509999433041e-17},
269 {22.0, 1.04317070180562e-18, 1.0, 1.09583608347287e-18},
270 {23.0, 5.03153504903308e-20, 1.0, 5.266538166725e-20},
271 {24.0, 2.2525984149695e-21, 1.0, 2.35003117691919e-21},
272 {25.0, 9.3677424515947e-23, 1.0, 9.74327619496943e-23},
273 {50.0, 9.83633962945521e-69, 1.0, 9.8677629437617e-69},
274 {51.0, 3.13448949497553e-71, 1.0, 3.14233143064882e-71},
275 {52.0, 7.82755221928122e-74, 1.0, 7.84193567329055e-74},
276 {53.0, 1.43662126065532e-76, 1.0, 1.43834540093295e-76},
277 {54.0, 1.72312692517348e-79, 1.0, 1.7241402776278e-79},
278 {55.0, 1.01335245432581e-82, 1.0, 1.01335245432581e-82},
279 };
280 testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data);
281 }
282
283 @Test
284 public void testMoments() {
285 final double tol = 1e-9;
286 HypergeometricDistribution dist;
287
288 dist = new HypergeometricDistribution(1500, 40, 100);
289 Assert.assertEquals(dist.getNumericalMean(), 40d * 100d / 1500d, tol);
290 Assert.assertEquals(dist.getNumericalVariance(), ( 100d * 40d * (1500d - 100d) * (1500d - 40d) ) / ( (1500d * 1500d * 1499d) ), tol);
291
292 dist = new HypergeometricDistribution(3000, 55, 200);
293 Assert.assertEquals(dist.getNumericalMean(), 55d * 200d / 3000d, tol);
294 Assert.assertEquals(dist.getNumericalVariance(), ( 200d * 55d * (3000d - 200d) * (3000d - 55d) ) / ( (3000d * 3000d * 2999d) ), tol);
295 }
296
297 @Test
298 public void testMath644() {
299 int N = 14761461;
300 int m = 1035;
301 int n = 1841;
302
303 int k = 0;
304 final HypergeometricDistribution dist = new HypergeometricDistribution(N, m, n);
305
306 Assert.assertTrue(Precision.compareTo(1.0, dist.upperCumulativeProbability(k), 1) == 0);
307 Assert.assertTrue(Precision.compareTo(dist.cumulativeProbability(k), 0.0, 1) > 0);
308
309
310 double upper = 1.0 - dist.cumulativeProbability(k) + dist.probability(k);
311 Assert.assertTrue(Precision.compareTo(1.0, upper, 1) == 0);
312 }
313
314 @Test
315 public void testMath1356() {
316 HypergeometricDistribution dist = new HypergeometricDistribution(11, 11, 1);
317 assertEquals(1.0, dist.probability(1), 1e-6);
318 assertEquals(0.0, dist.probability(0), 1e-6);
319
320 dist = new HypergeometricDistribution(11, 11, 11);
321 assertEquals(0.0, dist.probability(0), 1e-6);
322 assertEquals(0.0, dist.probability(1), 1e-6);
323 assertEquals(0.0, dist.probability(10), 1e-6);
324 assertEquals(1.0, dist.probability(11), 1e-6);
325 }
326 }