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: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
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      @Override
40      public ContinuousDistribution makeDistribution() {
41          return new TDistributionImpl(5.0);
42      }
43  
44      /** Creates the default cumulative probability distribution test input values */
45      @Override
46      public double[] makeCumulativeTestPoints() {
47          // quantiles computed using R version 1.8.1 (linux version)
48          return new double[] {-5.89343,-3.36493, -2.570582, -2.015048,
49              -1.475884, 0.0, 5.89343, 3.36493, 2.570582,
50              2.015048, 1.475884};
51      }
52  
53      /** Creates the default cumulative probability density test expected values */
54      @Override
55      public double[] makeCumulativeTestValues() {
56          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.5d, 0.999d,
57                  0.990d, 0.975d, 0.950d, 0.900d};
58      }
59  
60      // --------------------- Override tolerance  --------------
61      @Override
62      protected void setUp() throws Exception {
63          super.setUp();
64          setTolerance(1E-6);
65      }
66  
67      //---------------------------- Additional test cases -------------------------
68      /**
69       * @see <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=27243">
70       *      Bug report that prompted this unit test.</a>
71       */
72      public void testCumulativeProbabilityAgaintStackOverflow() throws Exception {
73          TDistributionImpl td = new TDistributionImpl(5.);
74          td.cumulativeProbability(.1);
75          td.cumulativeProbability(.01);
76      }
77  
78      public void testSmallDf() throws Exception {
79          setDistribution(new TDistributionImpl(1d));
80          setTolerance(1E-4);
81          // quantiles computed using R version 1.8.1 (linux version)
82          setCumulativeTestPoints(new double[] {-318.3088, -31.82052, -12.70620, -6.313752,
83              -3.077684, 0.0, 318.3088, 31.82052, 12.70620,
84              6.313752, 3.077684});
85          setInverseCumulativeTestValues(getCumulativeTestPoints());
86          verifyCumulativeProbabilities();
87          verifyInverseCumulativeProbabilities();
88      }
89  
90      public void testInverseCumulativeProbabilityExtremes() throws Exception {
91          setInverseCumulativeTestPoints(new double[] {0, 1});
92          setInverseCumulativeTestValues(
93                  new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
94          verifyInverseCumulativeProbabilities();
95      }
96  
97      public void testDfAccessors() {
98          TDistribution distribution = (TDistribution) getDistribution();
99          assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
100         distribution.setDegreesOfFreedom(4d);
101         assertEquals(4d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
102         try {
103             distribution.setDegreesOfFreedom(0d);
104             fail("Expecting IllegalArgumentException for df = 0");
105         } catch (IllegalArgumentException ex) {
106             // expected
107         }
108     }
109 
110 }