View Javadoc
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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.distribution.continuous;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  /**
29   * Test cases for FDistribution.
30   */
31  public class FDistributionTest extends RealDistributionAbstractTest {
32  
33      //-------------- Implementations for abstract methods -----------------------
34  
35      /** Creates the default continuous distribution instance to use in tests. */
36      @Override
37      public FDistribution makeDistribution() {
38          return new FDistribution(5.0, 6.0);
39      }
40  
41      /** Creates the default cumulative probability distribution test input values */
42      @Override
43      public double[] makeCumulativeTestPoints() {
44          // quantiles computed using R version 2.9.2
45          return new double[] {0.0346808448626, 0.0937009113303, 0.143313661184, 0.202008445998, 0.293728320107,
46                  20.8026639595, 8.74589525602, 5.98756512605, 4.38737418741, 3.10751166664};
47      }
48  
49      /** Creates the default cumulative probability density test expected values */
50      @Override
51      public double[] makeCumulativeTestValues() {
52          return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900};
53      }
54  
55      /** Creates the default probability density test expected values */
56      @Override
57      public double[] makeDensityTestValues() {
58          return new double[] {0.0689156576706, 0.236735653193, 0.364074131941, 0.481570789649, 0.595880479994,
59                  0.000133443915657, 0.00286681303403, 0.00969192007502, 0.0242883861471, 0.0605491314658};
60      }
61  
62      // --------------------- Override tolerance  --------------
63      @Override
64      public void setUp() {
65          super.setUp();
66          setTolerance(1e-9);
67      }
68  
69      //---------------------------- Additional test cases -------------------------
70  
71      @Test
72      public void testCumulativeProbabilityExtremes() {
73          setCumulativeTestPoints(new double[] {-2, 0});
74          setCumulativeTestValues(new double[] {0, 0});
75          verifyCumulativeProbabilities();
76      }
77  
78      @Test
79      public void testInverseCumulativeProbabilityExtremes() {
80          setInverseCumulativeTestPoints(new double[] {0, 1});
81          setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
82          verifyInverseCumulativeProbabilities();
83      }
84  
85      @Test
86      public void testDfAccessors() {
87          FDistribution dist = (FDistribution) getDistribution();
88          Assert.assertEquals(5d, dist.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
89          Assert.assertEquals(6d, dist.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
90      }
91  
92      @Test
93      public void testPreconditions() {
94          try {
95              new FDistribution(0, 1);
96              Assert.fail("Expecting MathIllegalArgumentException for df = 0");
97          } catch (MathIllegalArgumentException ex) {
98              // Expected.
99          }
100         try {
101             new FDistribution(1, 0);
102             Assert.fail("Expecting MathIllegalArgumentException for df = 0");
103         } catch (MathIllegalArgumentException ex) {
104             // Expected.
105         }
106     }
107 
108     @Test
109     public void testLargeDegreesOfFreedom() {
110         FDistribution fd = new FDistribution(100000, 100000);
111         double p = fd.cumulativeProbability(.999);
112         double x = fd.inverseCumulativeProbability(p);
113         Assert.assertEquals(.999, x, 1.0e-5);
114     }
115 
116     @Test
117     public void testSmallDegreesOfFreedom() {
118         FDistribution fd = new FDistribution(1, 1);
119         double p = fd.cumulativeProbability(0.975);
120         double x = fd.inverseCumulativeProbability(p);
121         Assert.assertEquals(0.975, x, 1.0e-5);
122 
123         fd = new FDistribution(1, 2);
124         p = fd.cumulativeProbability(0.975);
125         x = fd.inverseCumulativeProbability(p);
126         Assert.assertEquals(0.975, x, 1.0e-5);
127     }
128 
129     @Test
130     public void testMoments() {
131         final double tol = 1e-9;
132         FDistribution dist;
133 
134         dist = new FDistribution(1, 2);
135         Assert.assertTrue(Double.isNaN(dist.getNumericalMean()));
136         Assert.assertTrue(Double.isNaN(dist.getNumericalVariance()));
137 
138         dist = new FDistribution(1, 3);
139         Assert.assertEquals(dist.getNumericalMean(), 3d / (3d - 2d), tol);
140         Assert.assertTrue(Double.isNaN(dist.getNumericalVariance()));
141 
142         dist = new FDistribution(1, 5);
143         Assert.assertEquals(dist.getNumericalMean(), 5d / (5d - 2d), tol);
144         Assert.assertEquals(dist.getNumericalVariance(), (2d * 5d * 5d * 4d) / 9d, tol);
145     }
146 
147     @Test
148     public void testMath785() {
149         // this test was failing due to inaccurate results from ContinuedFraction.
150 
151         try {
152             double prob = 0.01;
153             FDistribution f = new FDistribution(200000, 200000);
154             double result = f.inverseCumulativeProbability(prob);
155             Assert.assertTrue(result < 1.0);
156         } catch (Exception e) {
157             Assert.fail("Failing to calculate inverse cumulative probability");
158         }
159     }
160 }