View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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   * Test cases for the BinomialTest class.
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              // expected exception;
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              // expected exception;
69          }
70          try {
71              testStatistic.binomialTest(10, 11, 0.5, null);
72              fail("Expected illegal argument exception");
73          } catch (MathIllegalArgumentException e) {
74              // expected exception;
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       * All successes with p >> 0.5 - p-value picks up all mass points.
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      * All successes with p = 0.5 - p-value is the sum of the two tails.
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      * All successes with p = 0.5 - p-value is the sum of the two tails.
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      * All successes with p < 0.5 - p-value is 5 mass point.
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      * No successes, p > 0.5 - p-value is 0 mass point.
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      * In this case, the distribution looks like this:
150      *    0: 0.32768
151      *    1: 0.4096
152      *    2: 0.2048
153      *    3: 0.0512
154      *    4: 0.0064
155      *    5: 3.2E-4
156      *  Algorithm picks up 5, 4, 3, 2 and then 0, so result is 1 - mass at 1.
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      * No successes has highest mass, so end up with everything here.
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 }