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    *      http://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  package org.apache.commons.math.distribution;
18  
19  
20  /**
21   * Test cases for FDistribution.
22   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
23   * ContinuousDistributionAbstractTest for details.
24   * 
25   * @version $Revision: 563850 $ $Date: 2007-08-08 06:18:46 -0700 (Wed, 08 Aug 2007) $
26   */
27  public class FDistributionTest extends ContinuousDistributionAbstractTest {
28  
29      /**
30       * Constructor for FDistributionTest.
31       * @param name
32       */
33      public FDistributionTest(String name) {
34          super(name);
35      }
36  
37      //-------------- Implementations for abstract methods -----------------------
38      
39      /** Creates the default continuous distribution instance to use in tests. */
40      public ContinuousDistribution makeDistribution() {
41          return new FDistributionImpl(5.0, 6.0);
42      }   
43      
44      /** Creates the default cumulative probability distribution test input values */
45      public double[] makeCumulativeTestPoints() {
46          // quantiles computed using R version 1.8.1 (linux version)
47          return new double[] {0.03468084d ,0.09370091d, 0.1433137d,
48              0.2020084d, 0.2937283d, 20.80266d, 8.745895d, 5.987565d, 
49              4.387374d, 3.107512d};
50      }
51      
52      /** Creates the default cumulative probability density test expected values */
53      public double[] makeCumulativeTestValues() {
54          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
55                  0.990d, 0.975d, 0.950d, 0.900d}; 
56      }
57      
58      // --------------------- Override tolerance  --------------
59      protected void setUp() throws Exception {
60          super.setUp();
61          setTolerance(4e-6);
62      }
63  
64      //---------------------------- Additional test cases -------------------------
65  
66      public void testCumulativeProbabilityExtremes() throws Exception {
67          setCumulativeTestPoints(new double[] {-2, 0});
68          setCumulativeTestValues(new double[] {0, 0});
69          verifyCumulativeProbabilities();
70      }
71  
72      public void testInverseCumulativeProbabilityExtremes() throws Exception {
73          setInverseCumulativeTestPoints(new double[] {0, 1});
74          setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
75          verifyInverseCumulativeProbabilities();
76      }
77      
78      public void testDfAccessors() {
79          FDistribution distribution = (FDistribution) getDistribution();
80          assertEquals(5d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
81          distribution.setNumeratorDegreesOfFreedom(4d);
82          assertEquals(4d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
83          assertEquals(6d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
84          distribution.setDenominatorDegreesOfFreedom(4d);
85          assertEquals(4d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
86          try {
87              distribution.setNumeratorDegreesOfFreedom(0d);
88              fail("Expecting IllegalArgumentException for df = 0");
89          } catch (IllegalArgumentException ex) {
90              // expected
91          }
92          try {
93              distribution.setDenominatorDegreesOfFreedom(0d);
94              fail("Expecting IllegalArgumentException for df = 0");
95          } catch (IllegalArgumentException ex) {
96              // expected
97          }
98      } 
99  
100     public void testLargeDegreesOfFreedom() throws Exception {
101         org.apache.commons.math.distribution.FDistributionImpl fd =
102             new org.apache.commons.math.distribution.FDistributionImpl(
103                 100000., 100000.);
104         double p = fd.cumulativeProbability(.999);
105         double x = fd.inverseCumulativeProbability(p);
106         assertEquals(.999, x, 1.0e-5);
107     }
108 }