1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.hipparchus.distribution.discrete;
15
16 import org.hipparchus.distribution.IntegerDistribution;
17 import org.junit.Assert;
18 import org.junit.Test;
19
20
21
22
23 public class BinomialDistributionTest extends IntegerDistributionAbstractTest {
24
25
26
27
28 public BinomialDistributionTest() {
29 setTolerance(1e-12);
30 }
31
32
33
34
35
36 @Override
37 public IntegerDistribution makeDistribution() {
38 return new BinomialDistribution(10, 0.70);
39 }
40
41
42 @Override
43 public int[] makeDensityTestPoints() {
44 return new int[] { -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
45 }
46
47
48
49
50
51 @Override
52 public double[] makeDensityTestValues() {
53 return new double[] { 0d, 0.0000059049d, 0.000137781d, 0.0014467005,
54 0.009001692, 0.036756909, 0.1029193452, 0.200120949, 0.266827932,
55 0.2334744405, 0.121060821, 0.0282475249, 0d };
56 }
57
58
59 @Override
60 public int[] makeCumulativeTestPoints() {
61 return makeDensityTestPoints();
62 }
63
64
65
66
67
68 @Override
69 public double[] makeCumulativeTestValues() {
70 return new double[] { 0d, 5.9049e-06, 0.0001436859, 0.0015903864, 0.0105920784, 0.0473489874,
71 0.1502683326, 0.3503892816, 0.6172172136, 0.8506916541, 0.9717524751, 1d, 1d };
72 }
73
74
75 @Override
76 public double[] makeInverseCumulativeTestPoints() {
77 return new double[] { 0, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d,
78 0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1 };
79 }
80
81
82
83
84
85 @Override
86 public int[] makeInverseCumulativeTestValues() {
87 return new int[] { 0, 2, 3, 4, 5, 5, 10, 10, 10, 9, 9, 10 };
88 }
89
90
91
92
93 @Test
94 public void testDegenerate0() {
95 BinomialDistribution dist = new BinomialDistribution(5, 0.0d);
96 setDistribution(dist);
97 setCumulativeTestPoints(new int[] { -1, 0, 1, 5, 10 });
98 setCumulativeTestValues(new double[] { 0d, 1d, 1d, 1d, 1d });
99 setDensityTestPoints(new int[] { -1, 0, 1, 10, 11 });
100 setDensityTestValues(new double[] { 0d, 1d, 0d, 0d, 0d });
101 setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
102 setInverseCumulativeTestValues(new int[] { 0, 0 });
103 verifyDensities();
104 verifyCumulativeProbabilities();
105 verifyInverseCumulativeProbabilities();
106 Assert.assertEquals(dist.getSupportLowerBound(), 0);
107 Assert.assertEquals(dist.getSupportUpperBound(), 0);
108 }
109
110
111 @Test
112 public void testDegenerate1() {
113 BinomialDistribution dist = new BinomialDistribution(5, 1.0d);
114 setDistribution(dist);
115 setCumulativeTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
116 setCumulativeTestValues(new double[] { 0d, 0d, 0d, 0d, 1d, 1d });
117 setDensityTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
118 setDensityTestValues(new double[] { 0d, 0d, 0d, 0d, 1d, 0d });
119 setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
120 setInverseCumulativeTestValues(new int[] { 5, 5 });
121 verifyDensities();
122 verifyCumulativeProbabilities();
123 verifyInverseCumulativeProbabilities();
124 Assert.assertEquals(dist.getSupportLowerBound(), 5);
125 Assert.assertEquals(dist.getSupportUpperBound(), 5);
126 }
127
128
129 @Test
130 public void testDegenerate2() {
131 BinomialDistribution dist = new BinomialDistribution(0, 0.01d);
132 setDistribution(dist);
133 setCumulativeTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
134 setCumulativeTestValues(new double[] { 0d, 1d, 1d, 1d, 1d, 1d });
135 setDensityTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
136 setDensityTestValues(new double[] { 0d, 1d, 0d, 0d, 0d, 0d });
137 setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
138 setInverseCumulativeTestValues(new int[] { 0, 0 });
139 verifyDensities();
140 verifyCumulativeProbabilities();
141 verifyInverseCumulativeProbabilities();
142 Assert.assertEquals(dist.getSupportLowerBound(), 0);
143 Assert.assertEquals(dist.getSupportUpperBound(), 0);
144 }
145
146 @Test
147 public void testMoments() {
148 final double tol = 1e-9;
149 BinomialDistribution dist;
150
151 dist = new BinomialDistribution(10, 0.5);
152 Assert.assertEquals(dist.getNumericalMean(), 10d * 0.5d, tol);
153 Assert.assertEquals(dist.getNumericalVariance(), 10d * 0.5d * 0.5d, tol);
154
155 dist = new BinomialDistribution(30, 0.3);
156 Assert.assertEquals(dist.getNumericalMean(), 30d * 0.3d, tol);
157 Assert.assertEquals(dist.getNumericalVariance(), 30d * 0.3d * (1d - 0.3d), tol);
158 }
159
160 @Test
161 public void testMath718() {
162
163
164
165
166 for (int trials = 500000; trials < 20000000; trials += 100000) {
167 BinomialDistribution dist = new BinomialDistribution(trials, 0.5);
168 int p = dist.inverseCumulativeProbability(0.5);
169 Assert.assertEquals(trials / 2, p);
170 }
171 }
172 }