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.math.distribution;
018    
019    /**
020     * Test cases for TDistribution.
021     * Extends ContinuousDistributionAbstractTest.  See class javadoc for
022     * ContinuousDistributionAbstractTest for details.
023     *
024     * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
025     */
026    public class TDistributionTest extends ContinuousDistributionAbstractTest {
027    
028        /**
029         * Constructor for TDistributionTest.
030         * @param name
031         */
032        public TDistributionTest(String name) {
033            super(name);
034        }
035    
036    //-------------- Implementations for abstract methods -----------------------
037    
038        /** Creates the default continuous distribution instance to use in tests. */
039        @Override
040        public ContinuousDistribution makeDistribution() {
041            return new TDistributionImpl(5.0);
042        }
043    
044        /** Creates the default cumulative probability distribution test input values */
045        @Override
046        public double[] makeCumulativeTestPoints() {
047            // quantiles computed using R version 1.8.1 (linux version)
048            return new double[] {-5.89343,-3.36493, -2.570582, -2.015048,
049                -1.475884, 0.0, 5.89343, 3.36493, 2.570582,
050                2.015048, 1.475884};
051        }
052    
053        /** Creates the default cumulative probability density test expected values */
054        @Override
055        public double[] makeCumulativeTestValues() {
056            return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.5d, 0.999d,
057                    0.990d, 0.975d, 0.950d, 0.900d};
058        }
059    
060        // --------------------- Override tolerance  --------------
061        @Override
062        protected void setUp() throws Exception {
063            super.setUp();
064            setTolerance(1E-6);
065        }
066    
067        //---------------------------- Additional test cases -------------------------
068        /**
069         * @see <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=27243">
070         *      Bug report that prompted this unit test.</a>
071         */
072        public void testCumulativeProbabilityAgaintStackOverflow() throws Exception {
073            TDistributionImpl td = new TDistributionImpl(5.);
074            td.cumulativeProbability(.1);
075            td.cumulativeProbability(.01);
076        }
077    
078        public void testSmallDf() throws Exception {
079            setDistribution(new TDistributionImpl(1d));
080            setTolerance(1E-4);
081            // quantiles computed using R version 1.8.1 (linux version)
082            setCumulativeTestPoints(new double[] {-318.3088, -31.82052, -12.70620, -6.313752,
083                -3.077684, 0.0, 318.3088, 31.82052, 12.70620,
084                6.313752, 3.077684});
085            setInverseCumulativeTestValues(getCumulativeTestPoints());
086            verifyCumulativeProbabilities();
087            verifyInverseCumulativeProbabilities();
088        }
089    
090        public void testInverseCumulativeProbabilityExtremes() throws Exception {
091            setInverseCumulativeTestPoints(new double[] {0, 1});
092            setInverseCumulativeTestValues(
093                    new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
094            verifyInverseCumulativeProbabilities();
095        }
096    
097        public void testDfAccessors() {
098            TDistribution distribution = (TDistribution) getDistribution();
099            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    }