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    *      http://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  package org.apache.commons.math4.legacy.stat.interval;
18  
19  import org.junit.Assert;
20  import org.junit.Test;
21  
22  /**
23   * Test cases for the ClopperPearsonInterval class.
24   *
25   */
26  public class ClopperPearsonIntervalTest extends BinomialConfidenceIntervalAbstractTest {
27  
28      @Override
29      protected BinomialConfidenceInterval createBinomialConfidenceInterval() {
30          return new ClopperPearsonInterval();
31      }
32  
33      @Test
34      public void testStandardInterval() {
35          ConfidenceInterval confidenceInterval = createStandardTestInterval();
36          Assert.assertEquals(0.07873857, confidenceInterval.getLowerBound(), 1E-5);
37          Assert.assertEquals(0.1248658, confidenceInterval.getUpperBound(), 1E-5);
38      }
39  
40      @Test
41      public void testMath1401() {
42          ConfidenceInterval interval = new ClopperPearsonInterval().createInterval(1, 1, 0.95);
43          Assert.assertEquals(0.025, interval.getLowerBound(), 1e-16);
44          Assert.assertEquals(1, interval.getUpperBound(), 0d);
45      }
46  
47      // number of successes = 0, number of trials = N
48      @Test
49      public void testCase1() {
50          // Check correctness against values obtained with the Python statsmodels.stats.proportion.proportion_confint
51          final int successes = 0;
52          final int trials = 10;
53          final double confidenceLevel = 0.95;
54  
55          // proportion_confint(0,10,method='beta') = (0, 0.3084971078187608)
56          final ConfidenceInterval expected = new ConfidenceInterval(0,
57                                                                     0.3084971078187608,
58                                                                     confidenceLevel);
59  
60          check(expected, createBinomialConfidenceInterval().createInterval(trials, successes, confidenceLevel));
61      }
62  
63      // number of successes = number of trials = N
64      @Test
65      public void testCase2() {
66          // Check correctness against values obtained with the Python statsmodels.stats.proportion.proportion_confint
67          final int successes = 10;
68          final int trials = 10;
69          final double confidenceLevel = 0.95;
70  
71          // prop.proportion_confint(10,10,method='beta') = (0.6915028921812392, 1)
72          final ConfidenceInterval expected = new ConfidenceInterval(0.6915028921812392,
73                                                                     1,
74                                                                     confidenceLevel);
75  
76          check(expected, createBinomialConfidenceInterval().createInterval(trials, successes, confidenceLevel));
77      }
78  
79      // number of successes = k, number of trials = N, 0 < k < N
80      @Test
81      public void testCase3() {
82          // Check correctness against values obtained with the Python statsmodels.stats.proportion.proportion_confint
83          final int successes = 3;
84          final int trials = 10;
85          final double confidenceLevel = 0.95;
86  
87          // prop.proportion_confint(3,10,method='beta') = (0.06673951117773447, 0.6524528500599972)
88          final ConfidenceInterval expected = new ConfidenceInterval(0.06673951117773447,
89                                                                     0.6524528500599972,
90                                                                     confidenceLevel);
91  
92          check(expected, createBinomialConfidenceInterval().createInterval(trials, successes, confidenceLevel));
93      }
94  
95      private void check(ConfidenceInterval expected,
96                         ConfidenceInterval actual) {
97          final double relTol = 1.0e-6; // Reasonable relative tolerance for floating point comparison
98          // Compare bounds using a relative tolerance
99          Assert.assertEquals(expected.getLowerBound(),
100                             actual.getLowerBound(),
101                             relTol * (1.0 + Math.abs(expected.getLowerBound())));
102         Assert.assertEquals(expected.getUpperBound(),
103                             actual.getUpperBound(),
104                             relTol * (1.0 + Math.abs(expected.getUpperBound())));
105         // The confidence level must be exact
106         Assert.assertEquals(expected.getConfidenceLevel(),
107                             actual.getConfidenceLevel(),
108                             0.0);
109     }
110 }