View Javadoc
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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.distribution.continuous;
23  
24  import org.hipparchus.UnitTestUtils;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.junit.Assert;
27  import org.junit.Test;
28  /**
29   * Test cases for TDistribution.
30   */
31  public class TDistributionTest extends RealDistributionAbstractTest {
32  
33  //-------------- Implementations for abstract methods -----------------------
34  
35      /** Creates the default continuous distribution instance to use in tests. */
36      @Override
37      public TDistribution makeDistribution() {
38          return new TDistribution(5.0);
39      }
40  
41      /** Creates the default cumulative probability distribution test input values */
42      @Override
43      public double[] makeCumulativeTestPoints() {
44          // quantiles computed using R version 2.9.2
45          return new double[] {-5.89342953136, -3.36492999891, -2.57058183564, -2.01504837333, -1.47588404882,
46                  5.89342953136, 3.36492999891, 2.57058183564, 2.01504837333, 1.47588404882};
47      }
48  
49      /** Creates the default cumulative probability density test expected values */
50      @Override
51      public double[] makeCumulativeTestValues() {
52          return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999,
53                  0.990, 0.975, 0.950, 0.900};
54      }
55  
56      /** Creates the default probability density test expected values */
57      @Override
58      public double[] makeDensityTestValues() {
59          return new double[] {0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005,
60                  0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005};
61      }
62  
63      // --------------------- Override tolerance  --------------
64      @Override
65      public void setUp() {
66          super.setUp();
67          setTolerance(1E-9);
68      }
69  
70      //---------------------------- Additional test cases -------------------------
71      /**
72       * @see <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=27243">
73       *      Bug report that prompted this unit test.</a>
74       */
75      @Test
76      public void testCumulativeProbabilityAgainstStackOverflow() {
77          TDistribution td = new TDistribution(5.);
78          td.cumulativeProbability(.1);
79          td.cumulativeProbability(.01);
80      }
81  
82      @Test
83      public void testSmallDf() {
84          setDistribution(new TDistribution(1d));
85          // quantiles computed using R version 2.9.2
86          setCumulativeTestPoints(new double[] {-318.308838986, -31.8205159538, -12.7062047362,
87                  -6.31375151468, -3.07768353718, 318.308838986, 31.8205159538, 12.7062047362,
88                   6.31375151468, 3.07768353718});
89          setDensityTestValues(new double[] {3.14158231817e-06, 0.000314055924703, 0.00195946145194,
90                  0.00778959736375, 0.0303958893917, 3.14158231817e-06, 0.000314055924703,
91                  0.00195946145194, 0.00778959736375, 0.0303958893917});
92          setInverseCumulativeTestValues(getCumulativeTestPoints());
93          verifyCumulativeProbabilities();
94          verifyInverseCumulativeProbabilities();
95          verifyDensities();
96      }
97  
98      @Test
99      public void testInverseCumulativeProbabilityExtremes() {
100         setInverseCumulativeTestPoints(new double[] {0, 1});
101         setInverseCumulativeTestValues(
102                 new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
103         verifyInverseCumulativeProbabilities();
104     }
105 
106     @Test
107     public void testCumulativeProbablilityExtremes() {
108         TDistribution dist;
109         for (int i = 1; i < 11; i++) {
110             dist = new TDistribution(i * 5);
111             Assert.assertEquals(1,
112                 dist.cumulativeProbability(Double.POSITIVE_INFINITY), Double.MIN_VALUE);
113             Assert.assertEquals(0,
114                 dist.cumulativeProbability(Double.NEGATIVE_INFINITY), Double.MIN_VALUE);
115         }
116     }
117 
118     @Test
119     public void testDfAccessors() {
120         TDistribution dist = (TDistribution) getDistribution();
121         Assert.assertEquals(5d, dist.getDegreesOfFreedom(), Double.MIN_VALUE);
122     }
123 
124     @Test(expected=MathIllegalArgumentException.class)
125     public void testPreconditions() {
126         new TDistribution(0);
127     }
128 
129     @Test
130     public void testMoments() {
131         final double tol = 1e-9;
132         TDistribution dist;
133 
134         dist = new TDistribution(1);
135         Assert.assertTrue(Double.isNaN(dist.getNumericalMean()));
136         Assert.assertTrue(Double.isNaN(dist.getNumericalVariance()));
137 
138         dist = new TDistribution(1.5);
139         Assert.assertEquals(dist.getNumericalMean(), 0, tol);
140         Assert.assertTrue(Double.isInfinite(dist.getNumericalVariance()));
141 
142         dist = new TDistribution(5);
143         Assert.assertEquals(dist.getNumericalMean(), 0, tol);
144         Assert.assertEquals(dist.getNumericalVariance(), 5d / (5d - 2d), tol);
145     }
146 
147     /*
148      * Adding this test to benchmark against tables published by NIST
149      * http://itl.nist.gov/div898/handbook/eda/section3/eda3672.htm
150      * Have chosen tabulated results for degrees of freedom 2,10,30,100
151      * Have chosen problevels from 0.10 to 0.001
152      */
153     @Test
154     public void nistData(){
155         double[] prob = new double[]{ 0.10,0.05,0.025,0.01,0.005,0.001};
156         double[] args2 = new double[]{1.886,2.920,4.303,6.965,9.925,22.327};
157         double[] args10 = new double[]{1.372,1.812,2.228,2.764,3.169,4.143};
158         double[] args30 = new double[]{1.310,1.697,2.042,2.457,2.750,3.385};
159         double[] args100= new double[]{1.290,1.660,1.984,2.364,2.626,3.174};
160         UnitTestUtils.assertEquals(prob, makeNistResults(args2, 2), 1.0e-4);
161         UnitTestUtils.assertEquals(prob, makeNistResults(args10, 10), 1.0e-4);
162         UnitTestUtils.assertEquals(prob, makeNistResults(args30, 30), 1.0e-4);
163         UnitTestUtils.assertEquals(prob, makeNistResults(args100, 100), 1.0e-4);
164         return;
165     }
166     private double[] makeNistResults(double[] args, int df){
167         TDistribution td =  new TDistribution(df);
168         double[] res  = new double[ args.length ];
169         for( int i = 0 ; i < res.length ; i++){
170             res[i] = 1.0 - td.cumulativeProbability(args[i]);
171         }
172         return res;
173     }
174 }