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