    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  package org.apache.commons.math.distribution;
    17
    18  /***
    19   * Test cases for TDistribution.
    20   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
    21   * ContinuousDistributionAbstractTest for details.
    22   *
    23   * @version $Revision: 1.15 $ $Date: 2004/07/24 21:41:37 $
    24   */
    25  public class TDistributionTest extends ContinuousDistributionAbstractTest {
    26
    27      /***
    28       * Constructor for TDistributionTest.
    29       * @param name
    30       */
    31      public TDistributionTest(String name) {
    32          super(name);
    33      }
    34
    35  //-------------- Implementations for abstract methods -----------------------
    36
    37      /*** Creates the default continuous distribution instance to use in tests. */
    38      public ContinuousDistribution makeDistribution() {
    39          return DistributionFactory.newInstance().createTDistribution(5.0);
    40      }
    41
    42      /*** Creates the default cumulative probability distribution test input values */
    43      public double[] makeCumulativeTestPoints() {
    44          // quantiles computed using R version 1.8.1 (linux version)
    45          return new double[] {-5.89343,-3.36493, -2.570582, -2.015048,
    46              -1.475884, 5.89343, 3.36493, 2.570582,
    47              2.015048, 1.475884};
    48      }
    49
    50      /*** Creates the default cumulative probability density test expected values */
    51      public double[] makeCumulativeTestValues() {
    52          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
    53                  0.990d, 0.975d, 0.950d, 0.900d};
    54      }
    55
    56      // --------------------- Override tolerance  --------------
    57      protected void setup() throws Exception {
    58          super.setUp();
    59          setTolerance(1E-6);
    60      }
    61
    62      //---------------------------- Additional test cases -------------------------
    63      /***
    64       * @see <a href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=27243">
    65       *      Bug report that prompted this unit test.</a>
    66       */
    67      public void testCumulativeProbabilityAgaintStackOverflow() throws Exception {
    68      	TDistributionImpl td = new TDistributionImpl(5.);
    69      	double est;
    70      	est = td.cumulativeProbability(.1);
    71      	est = td.cumulativeProbability(.01);
    72      }
    73
    74      public void testSmallDf() throws Exception {
    75          setDistribution(DistributionFactory.newInstance().createTDistribution(1d));
    76          setTolerance(1E-4);
    77          // quantiles computed using R version 1.8.1 (linux version)
    78          setCumulativeTestPoints(new double[] {-318.3088, -31.82052, -12.70620, -6.313752,
    79              -3.077684, 318.3088, 31.82052, 12.70620,
    80              6.313752, 3.077684});
    81          setInverseCumulativeTestValues(getCumulativeTestPoints());
    82          verifyCumulativeProbabilities();
    83          verifyInverseCumulativeProbabilities();
    84      }
    85
    86      public void testInverseCumulativeProbabilityExtremes() throws Exception {
    87          setInverseCumulativeTestPoints(new double[] {0, 1});
    88          setInverseCumulativeTestValues(
    89                  new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
    90          verifyInverseCumulativeProbabilities();
    91      }
    92
    93      public void testDfAccessors() {
    94          TDistribution distribution = (TDistribution) getDistribution();
    95          assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
    96          distribution.setDegreesOfFreedom(4d);
    97          assertEquals(4d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
    98          try {
    99              distribution.setDegreesOfFreedom(0d);
    100             fail("Expecting IllegalArgumentException for df = 0");
    101         } catch (IllegalArgumentException ex) {
    102             // expected
    103         }
    104     }
    105
    106 }