1   /*
2    * Copyright 2003-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.math.distribution;
17  
18  
19  /**
20   * Test cases for FDistribution.
21   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
22   * ContinuousDistributionAbstractTest for details.
23   * 
24   * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
25   */
26  public class FDistributionTest extends ContinuousDistributionAbstractTest {
27  
28      /**
29       * Constructor for FDistributionTest.
30       * @param name
31       */
32      public FDistributionTest(String name) {
33          super(name);
34      }
35  
36      //-------------- Implementations for abstract methods -----------------------
37      
38      /** Creates the default continuous distribution instance to use in tests. */
39      public ContinuousDistribution makeDistribution() {
40          return DistributionFactory.newInstance().createFDistribution(5.0, 6.0);
41      }   
42      
43      /** Creates the default cumulative probability distribution test input values */
44      public double[] makeCumulativeTestPoints() {
45          // quantiles computed using R version 1.8.1 (linux version)
46          return new double[] {0.03468084d ,0.09370091d, 0.1433137d,
47              0.2020084d, 0.2937283d, 20.80266d, 8.745895d, 5.987565d, 
48              4.387374d, 3.107512d};
49      }
50      
51      /** Creates the default cumulative probability density test expected values */
52      public double[] makeCumulativeTestValues() {
53          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
54                  0.990d, 0.975d, 0.950d, 0.900d}; 
55      }
56      
57      // --------------------- Override tolerance  --------------
58      protected void setup() throws Exception {
59          super.setUp();
60          setTolerance(1E-6);
61      }
62  
63      //---------------------------- Additional test cases -------------------------
64  
65      public void testCumulativeProbabilityExtremes() throws Exception {
66          setCumulativeTestPoints(new double[] {-2, 0});
67          setCumulativeTestValues(new double[] {0, 0});
68          verifyCumulativeProbabilities();
69      }
70  
71      public void testInverseCumulativeProbabilityExtremes() throws Exception {
72          setInverseCumulativeTestPoints(new double[] {0, 1});
73          setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
74          verifyInverseCumulativeProbabilities();
75      }
76      
77      public void testDfAccessors() {
78          FDistribution distribution = (FDistribution) getDistribution();
79          assertEquals(5d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
80          distribution.setNumeratorDegreesOfFreedom(4d);
81          assertEquals(4d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
82          assertEquals(6d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
83          distribution.setDenominatorDegreesOfFreedom(4d);
84          assertEquals(4d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
85          try {
86              distribution.setNumeratorDegreesOfFreedom(0d);
87              fail("Expecting IllegalArgumentException for df = 0");
88          } catch (IllegalArgumentException ex) {
89              // expected
90          }
91          try {
92              distribution.setDenominatorDegreesOfFreedom(0d);
93              fail("Expecting IllegalArgumentException for df = 0");
94          } catch (IllegalArgumentException ex) {
95              // expected
96          }
97      } 
98  
99      public void testLargeDegreesOfFreedom() throws Exception {
100         org.apache.commons.math.distribution.FDistributionImpl fd =
101             new org.apache.commons.math.distribution.FDistributionImpl(
102                 100000., 100000.);
103         double p = fd.cumulativeProbability(.999);
104         double x = fd.inverseCumulativeProbability(p);
105         assertEquals(.999, x, 1.0e-5);
106     }
107 }