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