    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
    17  package org.apache.commons.math.distribution;
    18
    19  /***
    20   * Test cases for GammaDistribution.
    21   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
    22   * ContinuousDistributionAbstractTest for details.
    23   *
    24   * @version $Revision: 1.18 $ $Date: 2004/07/24 21:41:37 $
    25   */
    26  public class GammaDistributionTest extends ContinuousDistributionAbstractTest {
    27
    28      /***
    29       * Constructor for GammaDistributionTest.
    30       * @param name
    31       */
    32      public GammaDistributionTest(String name) {
    33          super(name);
    34      }
    35
    36      //-------------- Implementations for abstract methods -----------------------
    37
    38      /*** Creates the default continuous distribution instance to use in tests. */
    39      public ContinuousDistribution makeDistribution() {
    40          return DistributionFactory.newInstance().createGammaDistribution(4d, 2d);
    41      }
    42
    43      /*** Creates the default cumulative probability distribution test input values */
    44      public double[] makeCumulativeTestPoints() {
    45          // quantiles computed using R version 1.8.1 (linux version)
    46          return new double[] {0.8571048, 1.646497, 2.179731, 2.732637,
    47              3.489539, 26.12448, 20.09024, 17.53455,
    48              15.50731, 13.36157};
    49      }
    50
    51      /*** Creates the default cumulative probability density test expected values */
    52      public double[] makeCumulativeTestValues() {
    53          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
    54                  0.990d, 0.975d, 0.950d, 0.900d};
    55      }
    56
    57      // --------------------- Override tolerance  --------------
    58      protected void setup() throws Exception {
    59          super.setUp();
    60          setTolerance(1E-6);
    61      }
    62
    63      //---------------------------- Additional test cases -------------------------
    64      public void testParameterAccessors() {
    65          GammaDistribution distribution = (GammaDistribution) getDistribution();
    66          assertEquals(4d, distribution.getAlpha(), 0);
    67          distribution.setAlpha(3d);
    68          assertEquals(3d, distribution.getAlpha(), 0);
    69          assertEquals(2d, distribution.getBeta(), 0);
    70          distribution.setBeta(4d);
    71          assertEquals(4d, distribution.getBeta(), 0);
    72          try {
    73              distribution.setAlpha(0d);
    74              fail("Expecting IllegalArgumentException for alpha = 0");
    75          } catch (IllegalArgumentException ex) {
    76              // expected
    77          }
    78          try {
    79              distribution.setBeta(0d);
    80              fail("Expecting IllegalArgumentException for beta = 0");
    81          } catch (IllegalArgumentException ex) {
    82              // expected
    83          }
    84      }
    85
    86      public void testProbabilities() throws Exception {
    87          testProbability(-1.000, 4.0, 2.0, .0000);
    88          testProbability(15.501, 4.0, 2.0, .9499);
    89          testProbability(0.504, 4.0, 1.0, .0018);
    90          testProbability(10.011, 1.0, 2.0, .9933);
    91          testProbability(5.000, 2.0, 2.0, .7127);
    92      }
    93
    94      public void testValues() throws Exception {
    95          testValue(15.501, 4.0, 2.0, .9499);
    96          testValue(0.504, 4.0, 1.0, .0018);
    97          testValue(10.011, 1.0, 2.0, .9933);
    98          testValue(5.000, 2.0, 2.0, .7127);
    99      }
    100
    101     private void testProbability(double x, double a, double b, double expected) throws Exception {
    102         DistributionFactory factory = DistributionFactory.newInstance();
    103 		GammaDistribution distribution = factory.createGammaDistribution( a, b );
    104         double actual = distribution.cumulativeProbability(x);
    105         assertEquals("probability for " + x, expected, actual, 10e-4);
    106     }
    107
    108     private void testValue(double expected, double a, double b, double p) throws Exception {
    109 		DistributionFactory factory = DistributionFactory.newInstance();
    110 		GammaDistribution distribution = factory.createGammaDistribution( a, b );
    111         double actual = distribution.inverseCumulativeProbability(p);
    112         assertEquals("critical value for " + p, expected, actual, 10e-4);
    113     }
    114
    115     public void testInverseCumulativeProbabilityExtremes() throws Exception {
    116         setInverseCumulativeTestPoints(new double[] {0, 1});
    117         setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
    118         verifyInverseCumulativeProbabilities();
    119     }
    120 }