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