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.math.distribution;
18  
19  /**
20   * Test cases for BinomialDistribution.
21   * Extends IntegerDistributionAbstractTest.  See class javadoc for
22   * IntegerDistributionAbstractTest for details.
23   * 
24   * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
25   */
26  public class BinomialDistributionTest extends IntegerDistributionAbstractTest {
27      
28      /**
29       * Constructor for BinomialDistributionTest.
30       * @param name
31       */
32      public BinomialDistributionTest(String name) {
33          super(name);
34      }
35      
36      //-------------- Implementations for abstract methods -----------------------
37      
38      /** Creates the default discrete distribution instance to use in tests. */
39      @Override
40      public IntegerDistribution makeDistribution() {
41          return new BinomialDistributionImpl(10,0.70);
42      }
43      
44      /** Creates the default probability density test input values */
45      @Override
46      public int[] makeDensityTestPoints() {
47          return new int[] {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
48      }
49      
50      /** Creates the default probability density test expected values */
51      @Override
52      public double[] makeDensityTestValues() {
53          return new double[] {0d, 0.0000d, 0.0001d, 0.0014d, 0.0090d, 0.0368d, 0.1029d, 
54                  0.2001d, 0.2668d, 0.2335d, 0.1211d, 0.0282d, 0d};
55      }
56      
57      /** Creates the default cumulative probability density test input values */
58      @Override
59      public int[] makeCumulativeTestPoints() {
60          return makeDensityTestPoints();
61      }
62      
63      /** Creates the default cumulative probability density test expected values */
64      @Override
65      public double[] makeCumulativeTestValues() {
66          return new double[] {0d, 0.0000d, 0.0001d, 0.0016d, 0.0106d, 0.0473d,
67                  0.1503d, 0.3504d, 0.6172d, 0.8507d, 0.9718d, 1d, 1d};
68          }
69      
70      /** Creates the default inverse cumulative probability test input values */
71      @Override
72      public double[] makeInverseCumulativeTestPoints() {
73          return new double[] {0, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d, 0.999d,
74                  0.990d, 0.975d, 0.950d, 0.900d,1}; 
75          }
76      
77      /** Creates the default inverse cumulative probability density test expected values */
78      @Override
79      public int[] makeInverseCumulativeTestValues() {
80          return new int[] {-1, 1, 2, 3, 4, 4, 9, 9, 9, 8, 8, Integer.MAX_VALUE};
81      }
82  
83      //----------------- Additional test cases ---------------------------------
84     
85      /** Test degenerate case p = 0   */
86      public void testDegenerate0() throws Exception {
87          setDistribution(new BinomialDistributionImpl(5,0.0d));
88          setCumulativeTestPoints(new int[] {-1, 0, 1, 5, 10 });
89          setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d});
90          setDensityTestPoints(new int[] {-1, 0, 1, 10, 11});
91          setDensityTestValues(new double[] {0d, 1d, 0d, 0d, 0d});
92          setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
93          setInverseCumulativeTestValues(new int[] {-1, -1});
94          verifyDensities();
95          verifyCumulativeProbabilities();
96          verifyInverseCumulativeProbabilities();     
97      }
98      
99      /** Test degenerate case p = 1   */
100     public void testDegenerate1() throws Exception {
101         setDistribution(new BinomialDistributionImpl(5,1.0d));
102         setCumulativeTestPoints(new int[] {-1, 0, 1, 2, 5, 10 });
103         setCumulativeTestValues(new double[] {0d, 0d, 0d, 0d, 1d, 1d});
104         setDensityTestPoints(new int[] {-1, 0, 1, 2, 5, 10});
105         setDensityTestValues(new double[] {0d, 0d, 0d, 0d, 1d, 0d});
106         setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
107         setInverseCumulativeTestValues(new int[] {4, 4});
108         verifyDensities();
109         verifyCumulativeProbabilities();
110         verifyInverseCumulativeProbabilities();     
111     }
112 
113 }