001: /*
002: * Copyright 2003-2005 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: package org.apache.commons.math.distribution;
017:
018: /**
019: * Test cases for TDistribution.
020: * Extends ContinuousDistributionAbstractTest. See class javadoc for
021: * ContinuousDistributionAbstractTest for details.
022: *
023: * @version $Revision: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
024: */
025: public class TDistributionTest extends
026: ContinuousDistributionAbstractTest {
027:
028: /**
029: * Constructor for TDistributionTest.
030: * @param name
031: */
032: public TDistributionTest(String name) {
033: super (name);
034: }
035:
036: //-------------- Implementations for abstract methods -----------------------
037:
038: /** Creates the default continuous distribution instance to use in tests. */
039: public ContinuousDistribution makeDistribution() {
040: return DistributionFactory.newInstance().createTDistribution(
041: 5.0);
042: }
043:
044: /** Creates the default cumulative probability distribution test input values */
045: public double[] makeCumulativeTestPoints() {
046: // quantiles computed using R version 1.8.1 (linux version)
047: return new double[] { -5.89343, -3.36493, -2.570582, -2.015048,
048: -1.475884, 0.0, 5.89343, 3.36493, 2.570582, 2.015048,
049: 1.475884 };
050: }
051:
052: /** Creates the default cumulative probability density test expected values */
053: public double[] makeCumulativeTestValues() {
054: return new double[] { 0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.5d,
055: 0.999d, 0.990d, 0.975d, 0.950d, 0.900d };
056: }
057:
058: // --------------------- Override tolerance --------------
059: protected void setup() throws Exception {
060: super .setUp();
061: setTolerance(1E-6);
062: }
063:
064: //---------------------------- Additional test cases -------------------------
065: /**
066: * @see <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=27243">
067: * Bug report that prompted this unit test.</a>
068: */
069: public void testCumulativeProbabilityAgaintStackOverflow()
070: throws Exception {
071: TDistributionImpl td = new TDistributionImpl(5.);
072: double est;
073: est = td.cumulativeProbability(.1);
074: est = td.cumulativeProbability(.01);
075: }
076:
077: public void testSmallDf() throws Exception {
078: setDistribution(DistributionFactory.newInstance()
079: .createTDistribution(1d));
080: setTolerance(1E-4);
081: // quantiles computed using R version 1.8.1 (linux version)
082: setCumulativeTestPoints(new double[] { -318.3088, -31.82052,
083: -12.70620, -6.313752, -3.077684, 0.0, 318.3088,
084: 31.82052, 12.70620, 6.313752, 3.077684 });
085: setInverseCumulativeTestValues(getCumulativeTestPoints());
086: verifyCumulativeProbabilities();
087: verifyInverseCumulativeProbabilities();
088: }
089:
090: public void testInverseCumulativeProbabilityExtremes()
091: throws Exception {
092: setInverseCumulativeTestPoints(new double[] { 0, 1 });
093: setInverseCumulativeTestValues(new double[] {
094: Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY });
095: verifyInverseCumulativeProbabilities();
096: }
097:
098: public void testDfAccessors() {
099: TDistribution distribution = (TDistribution) getDistribution();
100: assertEquals(5d, distribution.getDegreesOfFreedom(),
101: Double.MIN_VALUE);
102: distribution.setDegreesOfFreedom(4d);
103: assertEquals(4d, distribution.getDegreesOfFreedom(),
104: Double.MIN_VALUE);
105: try {
106: distribution.setDegreesOfFreedom(0d);
107: fail("Expecting IllegalArgumentException for df = 0");
108: } catch (IllegalArgumentException ex) {
109: // expected
110: }
111: }
112:
113: }
|