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  package org.apache.commons.math.distribution;
18  
19  /**
20   * Test cases for TDistribution.
21   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
22   * ContinuousDistributionAbstractTest for details.
23   *
24   * @version $Revision: 563850 $ $Date: 2007-08-08 06:18:46 -0700 (Wed, 08 Aug 2007) $
25   */
26  public class TDistributionTest extends ContinuousDistributionAbstractTest {
27  
28      /**
29       * Constructor for TDistributionTest.
30       * @param name
31       */
32      public TDistributionTest(String name) {
33          super(name);
34      }
35  
36  //-------------- Implementations for abstract methods -----------------------
37  
38      /** Creates the default continuous distribution instance to use in tests. */
39      public ContinuousDistribution makeDistribution() {
40          return new TDistributionImpl(5.0);
41      }
42  
43      /** Creates the default cumulative probability distribution test input values */
44      public double[] makeCumulativeTestPoints() {
45          // quantiles computed using R version 1.8.1 (linux version)
46          return new double[] {-5.89343,-3.36493, -2.570582, -2.015048,
47              -1.475884, 0.0, 5.89343, 3.36493, 2.570582,
48              2.015048, 1.475884};
49      }
50  
51      /** Creates the default cumulative probability density test expected values */
52      public double[] makeCumulativeTestValues() {
53          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.5d, 0.999d,
54                  0.990d, 0.975d, 0.950d, 0.900d};
55      }
56  
57      // --------------------- Override tolerance  --------------
58      protected void setUp() throws Exception {
59          super.setUp();
60          setTolerance(1E-6);
61      }
62  
63      //---------------------------- Additional test cases -------------------------
64      /**
65       * @see <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=27243">
66       *      Bug report that prompted this unit test.</a>
67       */
68      public void testCumulativeProbabilityAgaintStackOverflow() throws Exception {
69          TDistributionImpl td = new TDistributionImpl(5.);
70          td.cumulativeProbability(.1);
71          td.cumulativeProbability(.01);
72      }
73  
74      public void testSmallDf() throws Exception {
75          setDistribution(new TDistributionImpl(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, 0.0, 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 }