    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.math.distribution;
    17
    18  /***
    19   * Test cases for BinomialDistribution.
    20   * Extends IntegerDistributionAbstractTest.  See class javadoc for
    21   * IntegerDistributionAbstractTest for details.
    22   *
    23   * @version $Revision: 1.16 $ $Date: 2004/11/07 03:32:49 $
    24   */
    25  public class BinomialDistributionTest extends IntegerDistributionAbstractTest {
    26
    27      /***
    28       * Constructor for BinomialDistributionTest.
    29       * @param name
    30       */
    31      public BinomialDistributionTest(String name) {
    32          super(name);
    33      }
    34
    35      //-------------- Implementations for abstract methods -----------------------
    36
    37      /*** Creates the default discrete distribution instance to use in tests. */
    38      public IntegerDistribution makeDistribution() {
    39          return DistributionFactory.newInstance().createBinomialDistribution(10,0.70);
    40      }
    41
    42      /*** Creates the default probability density test input values */
    43      public int[] makeDensityTestPoints() {
    44          return new int[] {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    45      }
    46
    47      /*** Creates the default probability density test expected values */
    48      public double[] makeDensityTestValues() {
    49          return new double[] {0d, 0.0000d, 0.0001d, 0.0014d, 0.0090d, 0.0368d, 0.1029d,
    50                  0.2001d, 0.2668d, 0.2335d, 0.1211d, 0.0282d, 0d};
    51      }
    52
    53      /*** Creates the default cumulative probability density test input values */
    54      public int[] makeCumulativeTestPoints() {
    55          return makeDensityTestPoints();
    56      }
    57
    58      /*** Creates the default cumulative probability density test expected values */
    59      public double[] makeCumulativeTestValues() {
    60          return new double[] {0d, 0.0000d, 0.0001d, 0.0016d, 0.0106d, 0.0473d,
    61                  0.1503d, 0.3504d, 0.6172d, 0.8507d, 0.9718d, 1d, 1d};
    62          }
    63
    64      /*** Creates the default inverse cumulative probability test input values */
    65      public double[] makeInverseCumulativeTestPoints() {
    66          return new double[] {0, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d, 0.999d,
    67                  0.990d, 0.975d, 0.950d, 0.900d,1};
    68          }
    69
    70      /*** Creates the default inverse cumulative probability density test expected values */
    71      public int[] makeInverseCumulativeTestValues() {
    72          return new int[] {-1, 1, 2, 3, 4, 4, 9, 9, 9, 8, 8, Integer.MAX_VALUE};
    73      }
    74
    75      //----------------- Additional test cases ---------------------------------
    76
    77      /*** Test degenerate case p = 0   */
    78      public void testDegenerate0() throws Exception {
    79          setDistribution(DistributionFactory.newInstance().createBinomialDistribution(5,0.0d));
    80          setCumulativeTestPoints(new int[] {-1, 0, 1, 5, 10 });
    81          setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d});
    82          setDensityTestPoints(new int[] {-1, 0, 1, 10, 11});
    83          setDensityTestValues(new double[] {0d, 1d, 0d, 0d, 0d});
    84          setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
    85          setInverseCumulativeTestValues(new int[] {-1, -1});
    86          verifyDensities();
    87          verifyCumulativeProbabilities();
    88          verifyInverseCumulativeProbabilities();
    89      }
    90
    91      /*** Test degenerate case p = 1   */
    92      public void testDegenerate1() throws Exception {
    93          setDistribution(DistributionFactory.newInstance().createBinomialDistribution(5,1.0d));
    94          setCumulativeTestPoints(new int[] {-1, 0, 1, 2, 5, 10 });
    95          setCumulativeTestValues(new double[] {0d, 0d, 0d, 0d, 1d, 1d});
    96          setDensityTestPoints(new int[] {-1, 0, 1, 2, 5, 10});
    97          setDensityTestValues(new double[] {0d, 0d, 0d, 0d, 1d, 0d});
    98          setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
    99          setInverseCumulativeTestValues(new int[] {4, 4});
    100         verifyDensities();
    101         verifyCumulativeProbabilities();
    102         verifyInverseCumulativeProbabilities();
    103     }
    104
    105 }