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