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: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
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      @Override
41      public ContinuousDistribution makeDistribution() {
42          return new FDistributionImpl(5.0, 6.0);
43      }   
44      
45      /** Creates the default cumulative probability distribution test input values */
46      @Override
47      public double[] makeCumulativeTestPoints() {
48          // quantiles computed using R version 1.8.1 (linux version)
49          return new double[] {0.03468084d ,0.09370091d, 0.1433137d,
50              0.2020084d, 0.2937283d, 20.80266d, 8.745895d, 5.987565d, 
51              4.387374d, 3.107512d};
52      }
53      
54      /** Creates the default cumulative probability density test expected values */
55      @Override
56      public double[] makeCumulativeTestValues() {
57          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
58                  0.990d, 0.975d, 0.950d, 0.900d}; 
59      }
60      
61      // --------------------- Override tolerance  --------------
62      @Override
63      protected void setUp() throws Exception {
64          super.setUp();
65          setTolerance(4e-6);
66      }
67  
68      //---------------------------- Additional test cases -------------------------
69  
70      public void testCumulativeProbabilityExtremes() throws Exception {
71          setCumulativeTestPoints(new double[] {-2, 0});
72          setCumulativeTestValues(new double[] {0, 0});
73          verifyCumulativeProbabilities();
74      }
75  
76      public void testInverseCumulativeProbabilityExtremes() throws Exception {
77          setInverseCumulativeTestPoints(new double[] {0, 1});
78          setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
79          verifyInverseCumulativeProbabilities();
80      }
81      
82      public void testDfAccessors() {
83          FDistribution distribution = (FDistribution) getDistribution();
84          assertEquals(5d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
85          distribution.setNumeratorDegreesOfFreedom(4d);
86          assertEquals(4d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
87          assertEquals(6d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
88          distribution.setDenominatorDegreesOfFreedom(4d);
89          assertEquals(4d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
90          try {
91              distribution.setNumeratorDegreesOfFreedom(0d);
92              fail("Expecting IllegalArgumentException for df = 0");
93          } catch (IllegalArgumentException ex) {
94              // expected
95          }
96          try {
97              distribution.setDenominatorDegreesOfFreedom(0d);
98              fail("Expecting IllegalArgumentException for df = 0");
99          } 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 }