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.stat.inference;
23
24 import org.hipparchus.distribution.discrete.BinomialDistribution;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.util.FastMath;
27 import org.junit.jupiter.api.Test;
28
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertFalse;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import static org.junit.jupiter.api.Assertions.fail;
33
34
35
36
37 class BinomialTestTest {
38
39 protected BinomialTest testStatistic = new BinomialTest();
40
41 private static int successes = 51;
42 private static int trials = 235;
43 private static double probability = 1.0 / 6.0;
44
45 @Test
46 void testBinomialTestPValues() {
47 assertEquals(0.04375, testStatistic.binomialTest(
48 trials, successes, probability, AlternativeHypothesis.TWO_SIDED), 1E-4);
49 assertEquals(0.02654, testStatistic.binomialTest(
50 trials, successes, probability, AlternativeHypothesis.GREATER_THAN), 1E-4);
51 assertEquals(0.982, testStatistic.binomialTest(
52 trials, successes, probability, AlternativeHypothesis.LESS_THAN), 1E-4);
53 }
54
55 @Test
56 void testBinomialTestExceptions() {
57 try {
58 testStatistic.binomialTest(10, -1, 0.5, AlternativeHypothesis.TWO_SIDED);
59 fail("Expected not positive exception");
60 } catch (MathIllegalArgumentException e) {
61
62 }
63
64 try {
65 testStatistic.binomialTest(10, 11, 0.5, AlternativeHypothesis.TWO_SIDED);
66 fail("Expected illegal argument exception");
67 } catch (MathIllegalArgumentException e) {
68
69 }
70 try {
71 testStatistic.binomialTest(10, 11, 0.5, null);
72 fail("Expected illegal argument exception");
73 } catch (MathIllegalArgumentException e) {
74
75 }
76 }
77
78 @Test
79 void testBinomialTestAcceptReject() {
80 double alpha05 = 0.05;
81 double alpha01 = 0.01;
82
83 assertTrue(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.TWO_SIDED, alpha05));
84 assertTrue(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.GREATER_THAN, alpha05));
85 assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.LESS_THAN, alpha05));
86
87 assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.TWO_SIDED, alpha01));
88 assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.GREATER_THAN, alpha01));
89 assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.LESS_THAN, alpha05));
90 }
91
92
93
94
95 @Test
96 void testAllSuccessesTwoSidedHighP() {
97 assertEquals(1d, testStatistic.binomialTest(200, 200, 0.9950429, AlternativeHypothesis.TWO_SIDED),
98 Double.MIN_VALUE);
99 }
100
101
102
103
104 @Test
105 void testAllSuccessesTwoSidedEvenP() {
106 assertEquals(2 * FastMath.pow(0.5, 5),
107 testStatistic.binomialTest(5, 5, 0.5,
108 AlternativeHypothesis.TWO_SIDED),
109 Double.MIN_VALUE);
110 }
111
112
113
114
115 @Test
116 void testNoSuccessesTwoSidedEvenP() {
117 assertEquals(2 * FastMath.pow(0.5, 5),
118 testStatistic.binomialTest(5, 0, 0.5,
119 AlternativeHypothesis.TWO_SIDED),
120 Double.MIN_VALUE);
121 }
122
123
124
125
126 @Test
127 void testAllSuccessesTwoSidedLowP() {
128 final BinomialDistribution dist = new BinomialDistribution(5, 0.4);
129 assertEquals(dist.probability(5),
130 testStatistic.binomialTest(5, 5, 0.4,
131 AlternativeHypothesis.TWO_SIDED),
132 Double.MIN_VALUE);
133 }
134
135
136
137
138 @Test
139 void testNoSuccessesTwoSidedHighP() {
140 final BinomialDistribution dist = new BinomialDistribution(5, 0.9);
141 assertEquals(dist.probability(0),
142 testStatistic.binomialTest(5, 0, 0.9,
143 AlternativeHypothesis.TWO_SIDED),
144 Double.MIN_VALUE);
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158 @Test
159 void testNoSuccessesTwoSidedLowP() {
160 final BinomialDistribution dist = new BinomialDistribution(5, 0.2);
161 assertEquals(1 - dist.probability(1),
162 testStatistic.binomialTest(5, 0, 0.2,
163 AlternativeHypothesis.TWO_SIDED),
164 Double.MIN_VALUE);
165 }
166
167
168
169
170 @Test
171 void testNoSuccessesTwoSidedVeryLowP() {
172 assertEquals(1d,
173 testStatistic.binomialTest(5, 0, 0.001,
174 AlternativeHypothesis.TWO_SIDED),
175 Double.MIN_VALUE);
176 }
177 }