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 this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to You under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
9    * or agreed to in writing, software distributed under the License is
10   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11   * KIND, either express or implied. See the License for the specific language
12   * governing permissions and limitations under the License.
13   */
14  package org.hipparchus.distribution.discrete;
15  
16  import org.hipparchus.distribution.IntegerDistribution;
17  import org.junit.Assert;
18  import org.junit.Test;
19  
20  /**
21   * Test cases for BinomialDistribution.
22   */
23  public class BinomialDistributionTest extends IntegerDistributionAbstractTest {
24  
25      /**
26       * Constructor to override default tolerance.
27       */
28      public BinomialDistributionTest() {
29          setTolerance(1e-12);
30      }
31  
32      // -------------- Implementations for abstract methods
33      // -----------------------
34  
35      /** Creates the default discrete distribution instance to use in tests. */
36      @Override
37      public IntegerDistribution makeDistribution() {
38          return new BinomialDistribution(10, 0.70);
39      }
40  
41      /** Creates the default probability density test input values. */
42      @Override
43      public int[] makeDensityTestPoints() {
44          return new int[] { -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
45      }
46  
47      /**
48       * Creates the default probability density test expected values.
49       * Reference values are from R, version 2.15.3.
50       */
51      @Override
52      public double[] makeDensityTestValues() {
53          return new double[] { 0d, 0.0000059049d, 0.000137781d, 0.0014467005,
54              0.009001692, 0.036756909, 0.1029193452, 0.200120949, 0.266827932,
55              0.2334744405, 0.121060821, 0.0282475249, 0d };
56      }
57  
58      /** Creates the default cumulative probability density test input values */
59      @Override
60      public int[] makeCumulativeTestPoints() {
61          return makeDensityTestPoints();
62      }
63  
64      /**
65       * Creates the default cumulative probability density test expected values.
66       * Reference values are from R, version 2.15.3.
67       */
68      @Override
69      public double[] makeCumulativeTestValues() {
70          return new double[] { 0d, 5.9049e-06, 0.0001436859, 0.0015903864, 0.0105920784,  0.0473489874,
71              0.1502683326, 0.3503892816, 0.6172172136, 0.8506916541, 0.9717524751, 1d, 1d };
72      }
73  
74      /** Creates the default inverse cumulative probability test input values */
75      @Override
76      public double[] makeInverseCumulativeTestPoints() {
77          return new double[] { 0, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d,
78                  0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1 };
79      }
80  
81      /**
82       * Creates the default inverse cumulative probability density test expected
83       * values
84       */
85      @Override
86      public int[] makeInverseCumulativeTestValues() {
87          return new int[] { 0, 2, 3, 4, 5, 5, 10, 10, 10, 9, 9, 10 };
88      }
89  
90      // ----------------- Additional test cases ---------------------------------
91  
92      /** Test degenerate case p = 0 */
93      @Test
94      public void testDegenerate0() {
95          BinomialDistribution dist = new BinomialDistribution(5, 0.0d);
96          setDistribution(dist);
97          setCumulativeTestPoints(new int[] { -1, 0, 1, 5, 10 });
98          setCumulativeTestValues(new double[] { 0d, 1d, 1d, 1d, 1d });
99          setDensityTestPoints(new int[] { -1, 0, 1, 10, 11 });
100         setDensityTestValues(new double[] { 0d, 1d, 0d, 0d, 0d });
101         setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
102         setInverseCumulativeTestValues(new int[] { 0, 0 });
103         verifyDensities();
104         verifyCumulativeProbabilities();
105         verifyInverseCumulativeProbabilities();
106         Assert.assertEquals(dist.getSupportLowerBound(), 0);
107         Assert.assertEquals(dist.getSupportUpperBound(), 0);
108     }
109 
110     /** Test degenerate case p = 1 */
111     @Test
112     public void testDegenerate1() {
113         BinomialDistribution dist = new BinomialDistribution(5, 1.0d);
114         setDistribution(dist);
115         setCumulativeTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
116         setCumulativeTestValues(new double[] { 0d, 0d, 0d, 0d, 1d, 1d });
117         setDensityTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
118         setDensityTestValues(new double[] { 0d, 0d, 0d, 0d, 1d, 0d });
119         setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
120         setInverseCumulativeTestValues(new int[] { 5, 5 });
121         verifyDensities();
122         verifyCumulativeProbabilities();
123         verifyInverseCumulativeProbabilities();
124         Assert.assertEquals(dist.getSupportLowerBound(), 5);
125         Assert.assertEquals(dist.getSupportUpperBound(), 5);
126     }
127 
128     /** Test degenerate case n = 0 */
129     @Test
130     public void testDegenerate2() {
131         BinomialDistribution dist = new BinomialDistribution(0, 0.01d);
132         setDistribution(dist);
133         setCumulativeTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
134         setCumulativeTestValues(new double[] { 0d, 1d, 1d, 1d, 1d, 1d });
135         setDensityTestPoints(new int[] { -1, 0, 1, 2, 5, 10 });
136         setDensityTestValues(new double[] { 0d, 1d, 0d, 0d, 0d, 0d });
137         setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
138         setInverseCumulativeTestValues(new int[] { 0, 0 });
139         verifyDensities();
140         verifyCumulativeProbabilities();
141         verifyInverseCumulativeProbabilities();
142         Assert.assertEquals(dist.getSupportLowerBound(), 0);
143         Assert.assertEquals(dist.getSupportUpperBound(), 0);
144     }
145 
146     @Test
147     public void testMoments() {
148         final double tol = 1e-9;
149         BinomialDistribution dist;
150 
151         dist = new BinomialDistribution(10, 0.5);
152         Assert.assertEquals(dist.getNumericalMean(), 10d * 0.5d, tol);
153         Assert.assertEquals(dist.getNumericalVariance(), 10d * 0.5d * 0.5d, tol);
154 
155         dist = new BinomialDistribution(30, 0.3);
156         Assert.assertEquals(dist.getNumericalMean(), 30d * 0.3d, tol);
157         Assert.assertEquals(dist.getNumericalVariance(), 30d * 0.3d * (1d - 0.3d), tol);
158     }
159 
160     @Test
161     public void testMath718() {
162         // for large trials the evaluation of ContinuedFraction was inaccurate
163         // do a sweep over several large trials to test if the current implementation is
164         // numerically stable.
165 
166         for (int trials = 500000; trials < 20000000; trials += 100000) {
167             BinomialDistribution dist = new BinomialDistribution(trials, 0.5);
168             int p = dist.inverseCumulativeProbability(0.5);
169             Assert.assertEquals(trials / 2, p);
170         }
171     }
172 }