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 IntervalUtils class.
24   *
25   */
26  public class IntervalUtilsTest {
27  
28      private final int successes = 50;
29      private final int trials = 500;
30      private final double confidenceLevel = 0.9;
31  
32      // values to test must be exactly the same
33      private final double eps = 0.0;
34  
35      @Test
36      public void testAgrestiCoull() {
37          checkConfidenceIntervals(new AgrestiCoullInterval().createInterval(trials, successes, confidenceLevel),
38                                   IntervalUtils.getAgrestiCoullInterval(trials, successes, confidenceLevel));
39      }
40  
41      @Test
42      public void testClopperPearson() {
43          checkConfidenceIntervals(new ClopperPearsonInterval().createInterval(trials, successes, confidenceLevel),
44                                   IntervalUtils.getClopperPearsonInterval(trials, successes, confidenceLevel));
45      }
46  
47      @Test
48      public void testNormalApproximation() {
49          checkConfidenceIntervals(new NormalApproximationInterval().createInterval(trials, successes, confidenceLevel),
50                                   IntervalUtils.getNormalApproximationInterval(trials, successes, confidenceLevel));
51      }
52  
53      @Test
54      public void testWilsonScore() {
55          checkConfidenceIntervals(new WilsonScoreInterval().createInterval(trials, successes, confidenceLevel),
56                                   IntervalUtils.getWilsonScoreInterval(trials, successes, confidenceLevel));
57      }
58  
59      private void checkConfidenceIntervals(ConfidenceInterval expected, ConfidenceInterval actual) {
60          Assert.assertEquals(expected.getLowerBound(), actual.getLowerBound(), eps);
61          Assert.assertEquals(expected.getUpperBound(), actual.getUpperBound(), eps);
62          Assert.assertEquals(expected.getConfidenceLevel(), actual.getConfidenceLevel(), eps);
63      }
64  }