1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.stat.inference;
18
19 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
20 import org.apache.commons.math4.legacy.exception.NotPositiveException;
21 import org.junit.Assert;
22 import org.junit.Test;
23
24
25
26
27 public class BinomialTestTest {
28
29 protected BinomialTest testStatistic = new BinomialTest();
30
31 private static int successes = 51;
32 private static int trials = 235;
33 private static double probability = 1.0 / 6.0;
34
35 @Test
36 public void testBinomialTestPValues() {
37 Assert.assertEquals(0.04375, testStatistic.binomialTest(
38 trials, successes, probability, AlternativeHypothesis.TWO_SIDED), 1E-4);
39 Assert.assertEquals(0.02654, testStatistic.binomialTest(
40 trials, successes, probability, AlternativeHypothesis.GREATER_THAN), 1E-4);
41 Assert.assertEquals(0.982, testStatistic.binomialTest(
42 trials, successes, probability, AlternativeHypothesis.LESS_THAN), 1E-4);
43
44
45 Assert.assertEquals(1, testStatistic.binomialTest(
46 3, 3, 1, AlternativeHypothesis.TWO_SIDED), 1E-4);
47 Assert.assertEquals(1, testStatistic.binomialTest(
48 3, 3, 0.9, AlternativeHypothesis.TWO_SIDED), 1E-4);
49 Assert.assertEquals(1, testStatistic.binomialTest(
50 3, 3, 0.8, AlternativeHypothesis.TWO_SIDED), 1E-4);
51 Assert.assertEquals(0.559, testStatistic.binomialTest(
52 3, 3, 0.7, AlternativeHypothesis.TWO_SIDED), 1E-4);
53 Assert.assertEquals(0.28, testStatistic.binomialTest(
54 3, 3, 0.6, AlternativeHypothesis.TWO_SIDED), 1E-4);
55 Assert.assertEquals(0.25, testStatistic.binomialTest(
56 3, 3, 0.5, AlternativeHypothesis.TWO_SIDED), 1E-4);
57 Assert.assertEquals(0.064, testStatistic.binomialTest(
58 3, 3, 0.4, AlternativeHypothesis.TWO_SIDED), 1E-4);
59 Assert.assertEquals(0.027, testStatistic.binomialTest(
60 3, 3, 0.3, AlternativeHypothesis.TWO_SIDED), 1E-4);
61 Assert.assertEquals(0.008, testStatistic.binomialTest(
62 3, 3, 0.2, AlternativeHypothesis.TWO_SIDED), 1E-4);
63 Assert.assertEquals(0.001, testStatistic.binomialTest(
64 3, 3, 0.1, AlternativeHypothesis.TWO_SIDED), 1E-4);
65 Assert.assertEquals(0, testStatistic.binomialTest(
66 3, 3, 0.0, AlternativeHypothesis.TWO_SIDED), 1E-4);
67
68 Assert.assertEquals(0, testStatistic.binomialTest(
69 3, 0, 1, AlternativeHypothesis.TWO_SIDED), 1E-4);
70 Assert.assertEquals(0.001, testStatistic.binomialTest(
71 3, 0, 0.9, AlternativeHypothesis.TWO_SIDED), 1E-4);
72 Assert.assertEquals(0.008, testStatistic.binomialTest(
73 3, 0, 0.8, AlternativeHypothesis.TWO_SIDED), 1E-4);
74 Assert.assertEquals(0.027, testStatistic.binomialTest(
75 3, 0, 0.7, AlternativeHypothesis.TWO_SIDED), 1E-4);
76 Assert.assertEquals(0.064, testStatistic.binomialTest(
77 3, 0, 0.6, AlternativeHypothesis.TWO_SIDED), 1E-4);
78 Assert.assertEquals(0.25, testStatistic.binomialTest(
79 3, 0, 0.5, AlternativeHypothesis.TWO_SIDED), 1E-4);
80 Assert.assertEquals(0.28, testStatistic.binomialTest(
81 3, 0, 0.4, AlternativeHypothesis.TWO_SIDED), 1E-4);
82 Assert.assertEquals(0.559, testStatistic.binomialTest(
83 3, 0, 0.3, AlternativeHypothesis.TWO_SIDED), 1E-4);
84 Assert.assertEquals(1, testStatistic.binomialTest(
85 3, 0, 0.2, AlternativeHypothesis.TWO_SIDED), 1E-4);
86 Assert.assertEquals(1, testStatistic.binomialTest(
87 3, 0, 0.1, AlternativeHypothesis.TWO_SIDED), 1E-4);
88 Assert.assertEquals(1, testStatistic.binomialTest(
89 3, 0, 0.0, AlternativeHypothesis.TWO_SIDED), 1E-4);
90 }
91
92 @Test
93 public void testBinomialTestExceptions() {
94 try {
95 testStatistic.binomialTest(10, -1, 0.5, AlternativeHypothesis.TWO_SIDED);
96 Assert.fail("Expected not positive exception");
97 } catch (NotPositiveException e) {
98
99 }
100
101 try {
102 testStatistic.binomialTest(10, 11, 0.5, AlternativeHypothesis.TWO_SIDED);
103 Assert.fail("Expected illegal argument exception");
104 } catch (MathIllegalArgumentException e) {
105
106 }
107 try {
108 testStatistic.binomialTest(10, 11, 0.5, null);
109 Assert.fail("Expected illegal argument exception");
110 } catch (MathIllegalArgumentException e) {
111
112 }
113 }
114
115 @Test
116 public void testBinomialTestAcceptReject() {
117 double alpha05 = 0.05;
118 double alpha01 = 0.01;
119
120 Assert.assertTrue(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.TWO_SIDED, alpha05));
121 Assert.assertTrue(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.GREATER_THAN, alpha05));
122 Assert.assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.LESS_THAN, alpha05));
123
124 Assert.assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.TWO_SIDED, alpha01));
125 Assert.assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.GREATER_THAN, alpha01));
126 Assert.assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.LESS_THAN, alpha05));
127 }
128
129 @Test
130 public void testMath1644() {
131 final BinomialTest bt = new BinomialTest();
132 final double pval = bt.binomialTest(10, 5, 0.5, AlternativeHypothesis.TWO_SIDED);
133 Assert.assertTrue("pval=" + pval, pval <= 1);
134 }
135 }