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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.math.distribution;
21
* Test cases for GammaDistribution.
22
* Extends ContinuousDistributionAbstractTest. See class javadoc for
23
* ContinuousDistributionAbstractTest for details.
25
* @version $Revision: 563850 $ $Date: 2007-08-08 06:18:46 -0700 (Wed, 08 Aug 2007) $
27
public class GammaDistributionTest extends ContinuousDistributionAbstractTest {
30
* Constructor for GammaDistributionTest.
33
public GammaDistributionTest(String name) {
37
//-------------- Implementations for abstract methods -----------------------
39
/** Creates the default continuous distribution instance to use in tests. */
40
public ContinuousDistribution makeDistribution() {
41
return new GammaDistributionImpl(4d, 2d);
44
/** Creates the default cumulative probability distribution test input values */
45
public double[] makeCumulativeTestPoints() {
46
// quantiles computed using R version 1.8.1 (linux version)
47
return new double[] {0.8571048, 1.646497, 2.179731, 2.732637,
48
3.489539, 26.12448, 20.09024, 17.53455,
52
/** Creates the default cumulative probability density test expected values */
53
public double[] makeCumulativeTestValues() {
54
return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
55
0.990d, 0.975d, 0.950d, 0.900d};
58
// --------------------- Override tolerance --------------
59
protected void setUp() throws Exception {
64
//---------------------------- Additional test cases -------------------------
65
public void testParameterAccessors() {
66
GammaDistribution distribution = (GammaDistribution) getDistribution();
67
assertEquals(4d, distribution.getAlpha(), 0);
68
distribution.setAlpha(3d);
69
assertEquals(3d, distribution.getAlpha(), 0);
70
assertEquals(2d, distribution.getBeta(), 0);
71
distribution.setBeta(4d);
72
assertEquals(4d, distribution.getBeta(), 0);
74
distribution.setAlpha(0d);
75
fail("Expecting IllegalArgumentException for alpha = 0");
76
} catch (IllegalArgumentException ex) {
80
distribution.setBeta(0d);
81
fail("Expecting IllegalArgumentException for beta = 0");
82
} catch (IllegalArgumentException ex) {
87
public void testProbabilities() throws Exception {
88
testProbability(-1.000, 4.0, 2.0, .0000);
89
testProbability(15.501, 4.0, 2.0, .9499);
90
testProbability(0.504, 4.0, 1.0, .0018);
91
testProbability(10.011, 1.0, 2.0, .9933);
92
testProbability(5.000, 2.0, 2.0, .7127);
95
public void testValues() throws Exception {
96
testValue(15.501, 4.0, 2.0, .9499);
97
testValue(0.504, 4.0, 1.0, .0018);
98
testValue(10.011, 1.0, 2.0, .9933);
99
testValue(5.000, 2.0, 2.0, .7127);
102
private void testProbability(double x, double a, double b, double expected) throws Exception {
103
GammaDistribution distribution = new GammaDistributionImpl( a, b );
104
double actual = distribution.cumulativeProbability(x);
105
assertEquals("probability for " + x, expected, actual, 10e-4);
108
private void testValue(double expected, double a, double b, double p) throws Exception {
109
GammaDistribution distribution = new GammaDistributionImpl( a, b );
110
double actual = distribution.inverseCumulativeProbability(p);
111
assertEquals("critical value for " + p, expected, actual, 10e-4);
114
public void testInverseCumulativeProbabilityExtremes() throws Exception {
115
setInverseCumulativeTestPoints(new double[] {0, 1});
116
setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
117
verifyInverseCumulativeProbabilities();