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.Assert;
28  import org.junit.Test;
29  
30  /**
31   * Test cases for the BinomialTest class.
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              // expected exception;
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              // expected exception;
65          }
66          try {
67              testStatistic.binomialTest(10, 11, 0.5, null);
68              Assert.fail("Expected illegal argument exception");
69          } catch (MathIllegalArgumentException e) {
70              // expected exception;
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       * All successes with p >> 0.5 - p-value picks up all mass points.
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       * All successes with p = 0.5 - p-value is the sum of the two tails.
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      * All successes with p = 0.5 - p-value is the sum of the two tails.
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      * All successes with p < 0.5 - p-value is 5 mass point.
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      * No successes, p > 0.5 - p-value is 0 mass point.
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      * In this case, the distribution looks like this:
146      *    0: 0.32768
147      *    1: 0.4096
148      *    2: 0.2048
149      *    3: 0.0512
150      *    4: 0.0064
151      *    5: 3.2E-4
152      *  Algorithm picks up 5, 4, 3, 2 and then 0, so result is 1 - mass at 1.
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      * No successes has highest mass, so end up with everything here.
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 }