001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math.distribution;
018:
019: /**
020: * Test cases for GammaDistribution.
021: * Extends ContinuousDistributionAbstractTest. See class javadoc for
022: * ContinuousDistributionAbstractTest for details.
023: *
024: * @version $Revision: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
025: */
026: public class GammaDistributionTest extends
027: ContinuousDistributionAbstractTest {
028:
029: /**
030: * Constructor for GammaDistributionTest.
031: * @param name
032: */
033: public GammaDistributionTest(String name) {
034: super (name);
035: }
036:
037: //-------------- Implementations for abstract methods -----------------------
038:
039: /** Creates the default continuous distribution instance to use in tests. */
040: public ContinuousDistribution makeDistribution() {
041: return DistributionFactory.newInstance()
042: .createGammaDistribution(4d, 2d);
043: }
044:
045: /** Creates the default cumulative probability distribution test input values */
046: public double[] makeCumulativeTestPoints() {
047: // quantiles computed using R version 1.8.1 (linux version)
048: return new double[] { 0.8571048, 1.646497, 2.179731, 2.732637,
049: 3.489539, 26.12448, 20.09024, 17.53455, 15.50731,
050: 13.36157 };
051: }
052:
053: /** Creates the default cumulative probability density test expected values */
054: public double[] makeCumulativeTestValues() {
055: return new double[] { 0.001d, 0.01d, 0.025d, 0.05d, 0.1d,
056: 0.999d, 0.990d, 0.975d, 0.950d, 0.900d };
057: }
058:
059: // --------------------- Override tolerance --------------
060: protected void setup() throws Exception {
061: super .setUp();
062: setTolerance(1E-6);
063: }
064:
065: //---------------------------- Additional test cases -------------------------
066: public void testParameterAccessors() {
067: GammaDistribution distribution = (GammaDistribution) getDistribution();
068: assertEquals(4d, distribution.getAlpha(), 0);
069: distribution.setAlpha(3d);
070: assertEquals(3d, distribution.getAlpha(), 0);
071: assertEquals(2d, distribution.getBeta(), 0);
072: distribution.setBeta(4d);
073: assertEquals(4d, distribution.getBeta(), 0);
074: try {
075: distribution.setAlpha(0d);
076: fail("Expecting IllegalArgumentException for alpha = 0");
077: } catch (IllegalArgumentException ex) {
078: // expected
079: }
080: try {
081: distribution.setBeta(0d);
082: fail("Expecting IllegalArgumentException for beta = 0");
083: } catch (IllegalArgumentException ex) {
084: // expected
085: }
086: }
087:
088: public void testProbabilities() throws Exception {
089: testProbability(-1.000, 4.0, 2.0, .0000);
090: testProbability(15.501, 4.0, 2.0, .9499);
091: testProbability(0.504, 4.0, 1.0, .0018);
092: testProbability(10.011, 1.0, 2.0, .9933);
093: testProbability(5.000, 2.0, 2.0, .7127);
094: }
095:
096: public void testValues() throws Exception {
097: testValue(15.501, 4.0, 2.0, .9499);
098: testValue(0.504, 4.0, 1.0, .0018);
099: testValue(10.011, 1.0, 2.0, .9933);
100: testValue(5.000, 2.0, 2.0, .7127);
101: }
102:
103: private void testProbability(double x, double a, double b,
104: double expected) throws Exception {
105: DistributionFactory factory = DistributionFactory.newInstance();
106: GammaDistribution distribution = factory
107: .createGammaDistribution(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)
113: throws Exception {
114: DistributionFactory factory = DistributionFactory.newInstance();
115: GammaDistribution distribution = factory
116: .createGammaDistribution(a, b);
117: double actual = distribution.inverseCumulativeProbability(p);
118: assertEquals("critical value for " + p, expected, actual, 10e-4);
119: }
120:
121: public void testInverseCumulativeProbabilityExtremes()
122: throws Exception {
123: setInverseCumulativeTestPoints(new double[] { 0, 1 });
124: setInverseCumulativeTestValues(new double[] { 0,
125: Double.POSITIVE_INFINITY });
126: verifyInverseCumulativeProbabilities();
127: }
128: }
|