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    /**
021     * Test cases for FDistribution.
022     * Extends ContinuousDistributionAbstractTest.  See class javadoc for
023     * ContinuousDistributionAbstractTest for details.
024     * 
025     * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
026     */
027    public class FDistributionTest extends ContinuousDistributionAbstractTest {
028    
029        /**
030         * Constructor for FDistributionTest.
031         * @param name
032         */
033        public FDistributionTest(String name) {
034            super(name);
035        }
036    
037        //-------------- Implementations for abstract methods -----------------------
038        
039        /** Creates the default continuous distribution instance to use in tests. */
040        @Override
041        public ContinuousDistribution makeDistribution() {
042            return new FDistributionImpl(5.0, 6.0);
043        }   
044        
045        /** Creates the default cumulative probability distribution test input values */
046        @Override
047        public double[] makeCumulativeTestPoints() {
048            // quantiles computed using R version 1.8.1 (linux version)
049            return new double[] {0.03468084d ,0.09370091d, 0.1433137d,
050                0.2020084d, 0.2937283d, 20.80266d, 8.745895d, 5.987565d, 
051                4.387374d, 3.107512d};
052        }
053        
054        /** Creates the default cumulative probability density test expected values */
055        @Override
056        public double[] makeCumulativeTestValues() {
057            return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
058                    0.990d, 0.975d, 0.950d, 0.900d}; 
059        }
060        
061        // --------------------- Override tolerance  --------------
062        @Override
063        protected void setUp() throws Exception {
064            super.setUp();
065            setTolerance(4e-6);
066        }
067    
068        //---------------------------- Additional test cases -------------------------
069    
070        public void testCumulativeProbabilityExtremes() throws Exception {
071            setCumulativeTestPoints(new double[] {-2, 0});
072            setCumulativeTestValues(new double[] {0, 0});
073            verifyCumulativeProbabilities();
074        }
075    
076        public void testInverseCumulativeProbabilityExtremes() throws Exception {
077            setInverseCumulativeTestPoints(new double[] {0, 1});
078            setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
079            verifyInverseCumulativeProbabilities();
080        }
081        
082        public void testDfAccessors() {
083            FDistribution distribution = (FDistribution) getDistribution();
084            assertEquals(5d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
085            distribution.setNumeratorDegreesOfFreedom(4d);
086            assertEquals(4d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
087            assertEquals(6d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
088            distribution.setDenominatorDegreesOfFreedom(4d);
089            assertEquals(4d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
090            try {
091                distribution.setNumeratorDegreesOfFreedom(0d);
092                fail("Expecting IllegalArgumentException for df = 0");
093            } catch (IllegalArgumentException ex) {
094                // expected
095            }
096            try {
097                distribution.setDenominatorDegreesOfFreedom(0d);
098                fail("Expecting IllegalArgumentException for df = 0");
099            } catch (IllegalArgumentException ex) {
100                // expected
101            }
102        } 
103    
104        public void testLargeDegreesOfFreedom() throws Exception {
105            org.apache.commons.math.distribution.FDistributionImpl fd =
106                new org.apache.commons.math.distribution.FDistributionImpl(
107                    100000., 100000.);
108            double p = fd.cumulativeProbability(.999);
109            double x = fd.inverseCumulativeProbability(p);
110            assertEquals(.999, x, 1.0e-5);
111        }
112    
113        public void testSmallDegreesOfFreedom() throws Exception {
114            org.apache.commons.math.distribution.FDistributionImpl fd =
115                new org.apache.commons.math.distribution.FDistributionImpl(
116                    1.0, 1.0);
117            double p = fd.cumulativeProbability(0.975);
118            double x = fd.inverseCumulativeProbability(p);
119            assertEquals(0.975, x, 1.0e-5);
120    
121            fd.setDenominatorDegreesOfFreedom(2.0);
122            p = fd.cumulativeProbability(0.975);
123            x = fd.inverseCumulativeProbability(p);
124            assertEquals(0.975, x, 1.0e-5);
125        }
126    
127    }