001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math3.distribution;
018    
019    import org.apache.commons.math3.exception.NotStrictlyPositiveException;
020    import org.junit.Assert;
021    import org.junit.Test;
022    import org.apache.commons.math3.TestUtils;
023    /**
024     * Test cases for TDistribution.
025     * Extends ContinuousDistributionAbstractTest.  See class javadoc for
026     * ContinuousDistributionAbstractTest for details.
027     *
028     * @version $Id: TDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $
029     */
030    public class TDistributionTest extends RealDistributionAbstractTest {
031    
032    //-------------- Implementations for abstract methods -----------------------
033    
034        /** Creates the default continuous distribution instance to use in tests. */
035        @Override
036        public TDistribution makeDistribution() {
037            return new TDistribution(5.0);
038        }
039    
040        /** Creates the default cumulative probability distribution test input values */
041        @Override
042        public double[] makeCumulativeTestPoints() {
043            // quantiles computed using R version 2.9.2
044            return new double[] {-5.89342953136, -3.36492999891, -2.57058183564, -2.01504837333, -1.47588404882,
045                    5.89342953136, 3.36492999891, 2.57058183564, 2.01504837333, 1.47588404882};
046        }
047    
048        /** Creates the default cumulative probability density test expected values */
049        @Override
050        public double[] makeCumulativeTestValues() {
051            return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999,
052                    0.990, 0.975, 0.950, 0.900};
053        }
054    
055        /** Creates the default probability density test expected values */
056        @Override
057        public double[] makeDensityTestValues() {
058            return new double[] {0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005,
059                    0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005};
060        }
061    
062        // --------------------- Override tolerance  --------------
063        @Override
064        public void setUp() {
065            super.setUp();
066            setTolerance(1E-9);
067        }
068    
069        //---------------------------- Additional test cases -------------------------
070        /**
071         * @see <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=27243">
072         *      Bug report that prompted this unit test.</a>
073         */
074        @Test
075        public void testCumulativeProbabilityAgainstStackOverflow() {
076            TDistribution td = new TDistribution(5.);
077            td.cumulativeProbability(.1);
078            td.cumulativeProbability(.01);
079        }
080    
081        @Test
082        public void testSmallDf() {
083            setDistribution(new TDistribution(1d));
084            // quantiles computed using R version 2.9.2
085            setCumulativeTestPoints(new double[] {-318.308838986, -31.8205159538, -12.7062047362,
086                    -6.31375151468, -3.07768353718, 318.308838986, 31.8205159538, 12.7062047362,
087                     6.31375151468, 3.07768353718});
088            setDensityTestValues(new double[] {3.14158231817e-06, 0.000314055924703, 0.00195946145194,
089                    0.00778959736375, 0.0303958893917, 3.14158231817e-06, 0.000314055924703,
090                    0.00195946145194, 0.00778959736375, 0.0303958893917});
091            setInverseCumulativeTestValues(getCumulativeTestPoints());
092            verifyCumulativeProbabilities();
093            verifyInverseCumulativeProbabilities();
094            verifyDensities();
095        }
096    
097        @Test
098        public void testInverseCumulativeProbabilityExtremes() {
099            setInverseCumulativeTestPoints(new double[] {0, 1});
100            setInverseCumulativeTestValues(
101                    new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
102            verifyInverseCumulativeProbabilities();
103        }
104    
105        @Test
106        public void testDfAccessors() {
107            TDistribution dist = (TDistribution) getDistribution();
108            Assert.assertEquals(5d, dist.getDegreesOfFreedom(), Double.MIN_VALUE);
109        }
110    
111        @Test(expected=NotStrictlyPositiveException.class)
112        public void testPreconditions() {
113            new TDistribution(0);
114        }
115    
116        @Test
117        public void testMoments() {
118            final double tol = 1e-9;
119            TDistribution dist;
120    
121            dist = new TDistribution(1);
122            Assert.assertTrue(Double.isNaN(dist.getNumericalMean()));
123            Assert.assertTrue(Double.isNaN(dist.getNumericalVariance()));
124    
125            dist = new TDistribution(1.5);
126            Assert.assertEquals(dist.getNumericalMean(), 0, tol);
127            Assert.assertTrue(Double.isInfinite(dist.getNumericalVariance()));
128    
129            dist = new TDistribution(5);
130            Assert.assertEquals(dist.getNumericalMean(), 0, tol);
131            Assert.assertEquals(dist.getNumericalVariance(), 5d / (5d - 2d), tol);
132        }
133    
134        /*
135         * Adding this test to benchmark against tables published by NIST
136         * http://itl.nist.gov/div898/handbook/eda/section3/eda3672.htm
137         * Have chosen tabulated results for degrees of freedom 2,10,30,100
138         * Have chosen problevels from 0.10 to 0.001
139         */
140        @Test
141        public void nistData(){
142            double[] prob = new double[]{ 0.10,0.05,0.025,0.01,0.005,0.001};
143            double[] args2 = new double[]{1.886,2.920,4.303,6.965,9.925,22.327};
144            double[] args10 = new double[]{1.372,1.812,2.228,2.764,3.169,4.143};
145            double[] args30 = new double[]{1.310,1.697,2.042,2.457,2.750,3.385};
146            double[] args100= new double[]{1.290,1.660,1.984,2.364,2.626,3.174};
147            TestUtils.assertEquals(prob, makeNistResults(args2, 2), 1.0e-4);
148            TestUtils.assertEquals(prob, makeNistResults(args10, 10), 1.0e-4);
149            TestUtils.assertEquals(prob, makeNistResults(args30, 30), 1.0e-4);
150            TestUtils.assertEquals(prob, makeNistResults(args100, 100), 1.0e-4);
151            return;
152        }
153        private double[] makeNistResults(double[] args, int df){
154            TDistribution td =  new TDistribution(df);
155            double[] res  = new double[ args.length ];
156            for( int i = 0 ; i < res.length ; i++){
157                res[i] = 1.0 - td.cumulativeProbability(args[i]);
158            }
159            return res;
160        }
161    }