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.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
35
36 public class FDistributionTest extends RealDistributionAbstractTest {
37
38
39
40
41 @Override
42 public FDistribution makeDistribution() {
43 return new FDistribution(5.0, 6.0);
44 }
45
46
47 @Override
48 public double[] makeCumulativeTestPoints() {
49
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
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
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
68 @BeforeEach
69 @Override
70 public void setUp() {
71 super.setUp();
72 setTolerance(1e-9);
73 }
74
75
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
105 }
106 try {
107 new FDistribution(1, 0);
108 fail("Expecting MathIllegalArgumentException for df = 0");
109 } catch (MathIllegalArgumentException ex) {
110
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
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 }