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  
18  package org.apache.commons.math.distribution;
19  
20  /**
21   * Test cases for GammaDistribution.
22   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
23   * ContinuousDistributionAbstractTest for details.
24   * 
25   * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
26   */
27  public class GammaDistributionTest extends ContinuousDistributionAbstractTest {
28      
29      /**
30       * Constructor for GammaDistributionTest.
31       * @param name
32       */
33      public GammaDistributionTest(String name) {
34          super(name);
35      }
36  
37      //-------------- Implementations for abstract methods -----------------------
38  
39      /** Creates the default continuous distribution instance to use in tests. */
40      @Override
41      public ContinuousDistribution makeDistribution() {
42          return new GammaDistributionImpl(4d, 2d);
43      }
44  
45      /** Creates the default cumulative probability distribution test input values */
46      @Override
47      public double[] makeCumulativeTestPoints() {
48          // quantiles computed using R version 1.8.1 (linux version)
49          return new double[] {0.8571048, 1.646497, 2.179731, 2.732637,
50              3.489539, 26.12448, 20.09024, 17.53455,
51              15.50731, 13.36157};
52      }
53  
54      /** Creates the default cumulative probability density test expected values */
55      @Override
56      public double[] makeCumulativeTestValues() {
57          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
58                  0.990d, 0.975d, 0.950d, 0.900d};
59      }
60  
61      // --------------------- Override tolerance  --------------
62      @Override
63      protected void setUp() throws Exception {
64          super.setUp();
65          setTolerance(6e-6);
66      }
67  
68      //---------------------------- Additional test cases -------------------------
69      public void testParameterAccessors() {
70          GammaDistribution distribution = (GammaDistribution) getDistribution();
71          assertEquals(4d, distribution.getAlpha(), 0);
72          distribution.setAlpha(3d);
73          assertEquals(3d, distribution.getAlpha(), 0);
74          assertEquals(2d, distribution.getBeta(), 0);
75          distribution.setBeta(4d);
76          assertEquals(4d, distribution.getBeta(), 0);
77          try {
78              distribution.setAlpha(0d);
79              fail("Expecting IllegalArgumentException for alpha = 0");
80          } catch (IllegalArgumentException ex) {
81              // expected
82          }
83          try {
84              distribution.setBeta(0d);
85              fail("Expecting IllegalArgumentException for beta = 0");
86          } catch (IllegalArgumentException ex) {
87              // expected
88          }
89      }
90  
91      public void testProbabilities() throws Exception {
92          testProbability(-1.000, 4.0, 2.0, .0000);
93          testProbability(15.501, 4.0, 2.0, .9499);
94          testProbability(0.504, 4.0, 1.0, .0018);
95          testProbability(10.011, 1.0, 2.0, .9933);
96          testProbability(5.000, 2.0, 2.0, .7127);
97      }
98  
99      public void testValues() throws Exception {
100         testValue(15.501, 4.0, 2.0, .9499);
101         testValue(0.504, 4.0, 1.0, .0018);
102         testValue(10.011, 1.0, 2.0, .9933);
103         testValue(5.000, 2.0, 2.0, .7127);
104     }
105 
106     private void testProbability(double x, double a, double b, double expected) throws Exception {
107         GammaDistribution distribution = new GammaDistributionImpl( a, b );
108         double actual = distribution.cumulativeProbability(x);
109         assertEquals("probability for " + x, expected, actual, 10e-4);
110     }
111 
112     private void testValue(double expected, double a, double b, double p) throws Exception {
113         GammaDistribution distribution = new GammaDistributionImpl( a, b );
114         double actual = distribution.inverseCumulativeProbability(p);
115         assertEquals("critical value for " + p, expected, actual, 10e-4);
116     }
117 
118     public void testDensity() {
119         double[] x = new double[]{-0.1, 1e-6, 0.5, 1, 2, 5};
120         // R2.5: print(dgamma(x, shape=1, rate=1), digits=10)
121         checkDensity(1, 1, x, new double[]{0.000000000000, 0.999999000001, 0.606530659713, 0.367879441171, 0.135335283237, 0.006737946999});
122         // R2.5: print(dgamma(x, shape=2, rate=1), digits=10)
123         checkDensity(2, 1, x, new double[]{0.000000000000, 0.000000999999, 0.303265329856, 0.367879441171, 0.270670566473, 0.033689734995});
124         // R2.5: print(dgamma(x, shape=4, rate=1), digits=10)
125         checkDensity(4, 1, x, new double[]{0.000000000e+00, 1.666665000e-19, 1.263605541e-02, 6.131324020e-02, 1.804470443e-01, 1.403738958e-01});
126         // R2.5: print(dgamma(x, shape=4, rate=10), digits=10)
127         checkDensity(4, 10, x, new double[]{0.000000000e+00, 1.666650000e-15, 1.403738958e+00, 7.566654960e-02, 2.748204830e-05, 4.018228850e-17});
128         // R2.5: print(dgamma(x, shape=.1, rate=10), digits=10)
129         checkDensity(0.1, 10, x, new double[]{0.000000000e+00, 3.323953832e+04, 1.663849010e-03, 6.007786726e-06, 1.461647647e-10, 5.996008322e-24});
130         // R2.5: print(dgamma(x, shape=.1, rate=20), digits=10)
131         checkDensity(0.1, 20, x, new double[]{0.000000000e+00, 3.562489883e+04, 1.201557345e-05, 2.923295295e-10, 3.228910843e-19, 1.239484589e-45});
132         // R2.5: print(dgamma(x, shape=.1, rate=4), digits=10)
133         checkDensity(0.1, 4, x, new double[]{0.000000000e+00, 3.032938388e+04, 3.049322494e-02, 2.211502311e-03, 2.170613371e-05, 5.846590589e-11});
134         // R2.5: print(dgamma(x, shape=.1, rate=1), digits=10)
135         checkDensity(0.1, 1, x, new double[]{0.000000000e+00, 2.640334143e+04, 1.189704437e-01, 3.866916944e-02, 7.623306235e-03, 1.663849010e-04});
136     }
137 
138     private void checkDensity(double alpha, double rate, double[] x, double[] expected) {
139         GammaDistribution d = new GammaDistributionImpl(alpha, 1 / rate);
140         for (int i = 0; i < x.length; i++) {
141             assertEquals(expected[i], d.density(x[i]), 1e-5);
142         }
143     }
144 
145     public void testInverseCumulativeProbabilityExtremes() throws Exception {
146         setInverseCumulativeTestPoints(new double[] {0, 1});
147         setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
148         verifyInverseCumulativeProbabilities();
149     }
150 }