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.Assert;
26 import org.junit.Test;
27
28
29
30
31 public class FDistributionTest extends RealDistributionAbstractTest {
32
33
34
35
36 @Override
37 public FDistribution makeDistribution() {
38 return new FDistribution(5.0, 6.0);
39 }
40
41
42 @Override
43 public double[] makeCumulativeTestPoints() {
44
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
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
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
63 @Override
64 public void setUp() {
65 super.setUp();
66 setTolerance(1e-9);
67 }
68
69
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
99 }
100 try {
101 new FDistribution(1, 0);
102 Assert.fail("Expecting MathIllegalArgumentException for df = 0");
103 } catch (MathIllegalArgumentException ex) {
104
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
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 }