1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
36
37 public class FDistributionTest extends RealDistributionAbstractTest {
38
39
40
41
42 @Override
43 public FDistribution makeDistribution() {
44 return new FDistribution(5.0, 6.0);
45 }
46
47
48 @Override
49 public double[] makeCumulativeTestPoints() {
50
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
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
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
69 @BeforeEach
70 @Override
71 public void setUp() {
72 super.setUp();
73 setTolerance(1e-9);
74 }
75
76
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
106 }
107 try {
108 new FDistribution(1, 0);
109 fail("Expecting MathIllegalArgumentException for df = 0");
110 } catch (MathIllegalArgumentException ex) {
111
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
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 }