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.UnitTestUtils;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertThrows;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32
33
34
35
36 public class TDistributionTest extends RealDistributionAbstractTest {
37
38
39
40
41 @Override
42 public TDistribution makeDistribution() {
43 return new TDistribution(5.0);
44 }
45
46
47 @Override
48 public double[] makeCumulativeTestPoints() {
49
50 return new double[] {-5.89342953136, -3.36492999891, -2.57058183564, -2.01504837333, -1.47588404882,
51 5.89342953136, 3.36492999891, 2.57058183564, 2.01504837333, 1.47588404882};
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,
58 0.990, 0.975, 0.950, 0.900};
59 }
60
61
62 @Override
63 public double[] makeDensityTestValues() {
64 return new double[] {0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005,
65 0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005};
66 }
67
68
69 @BeforeEach
70 @Override
71 public void setUp() {
72 super.setUp();
73 setTolerance(1E-9);
74 }
75
76
77
78
79
80
81 @Test
82 void testCumulativeProbabilityAgainstStackOverflow() {
83 TDistribution td = new TDistribution(5.);
84 td.cumulativeProbability(.1);
85 td.cumulativeProbability(.01);
86 }
87
88 @Test
89 void testSmallDf() {
90 setDistribution(new TDistribution(1d));
91
92 setCumulativeTestPoints(new double[] {-318.308838986, -31.8205159538, -12.7062047362,
93 -6.31375151468, -3.07768353718, 318.308838986, 31.8205159538, 12.7062047362,
94 6.31375151468, 3.07768353718});
95 setDensityTestValues(new double[] {3.14158231817e-06, 0.000314055924703, 0.00195946145194,
96 0.00778959736375, 0.0303958893917, 3.14158231817e-06, 0.000314055924703,
97 0.00195946145194, 0.00778959736375, 0.0303958893917});
98 setInverseCumulativeTestValues(getCumulativeTestPoints());
99 verifyCumulativeProbabilities();
100 verifyInverseCumulativeProbabilities();
101 verifyDensities();
102 }
103
104 @Test
105 void testInverseCumulativeProbabilityExtremes() {
106 setInverseCumulativeTestPoints(new double[] {0, 1});
107 setInverseCumulativeTestValues(
108 new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
109 verifyInverseCumulativeProbabilities();
110 }
111
112 @Test
113 void testCumulativeProbablilityExtremes() {
114 TDistribution dist;
115 for (int i = 1; i < 11; i++) {
116 dist = new TDistribution(i * 5);
117 assertEquals(1,
118 dist.cumulativeProbability(Double.POSITIVE_INFINITY), Double.MIN_VALUE);
119 assertEquals(0,
120 dist.cumulativeProbability(Double.NEGATIVE_INFINITY), Double.MIN_VALUE);
121 }
122 }
123
124 @Test
125 void testDfAccessors() {
126 TDistribution dist = (TDistribution) getDistribution();
127 assertEquals(5d, dist.getDegreesOfFreedom(), Double.MIN_VALUE);
128 }
129
130 @Test
131 void testPreconditions() {
132 assertThrows(MathIllegalArgumentException.class, () -> new TDistribution(0));
133 }
134
135 @Test
136 void testMoments() {
137 final double tol = 1e-9;
138 TDistribution dist;
139
140 dist = new TDistribution(1);
141 assertTrue(Double.isNaN(dist.getNumericalMean()));
142 assertTrue(Double.isNaN(dist.getNumericalVariance()));
143
144 dist = new TDistribution(1.5);
145 assertEquals(0, dist.getNumericalMean(), tol);
146 assertTrue(Double.isInfinite(dist.getNumericalVariance()));
147
148 dist = new TDistribution(5);
149 assertEquals(0, dist.getNumericalMean(), tol);
150 assertEquals(dist.getNumericalVariance(), 5d / (5d - 2d), tol);
151 }
152
153
154
155
156
157
158
159 @Test
160 void nistData(){
161 double[] prob = new double[]{ 0.10,0.05,0.025,0.01,0.005,0.001};
162 double[] args2 = new double[]{1.886,2.920,4.303,6.965,9.925,22.327};
163 double[] args10 = new double[]{1.372,1.812,2.228,2.764,3.169,4.143};
164 double[] args30 = new double[]{1.310,1.697,2.042,2.457,2.750,3.385};
165 double[] args100= new double[]{1.290,1.660,1.984,2.364,2.626,3.174};
166 UnitTestUtils.customAssertEquals(prob, makeNistResults(args2, 2), 1.0e-4);
167 UnitTestUtils.customAssertEquals(prob, makeNistResults(args10, 10), 1.0e-4);
168 UnitTestUtils.customAssertEquals(prob, makeNistResults(args30, 30), 1.0e-4);
169 UnitTestUtils.customAssertEquals(prob, makeNistResults(args100, 100), 1.0e-4);
170 return;
171 }
172 private double[] makeNistResults(double[] args, int df){
173 TDistribution td = new TDistribution(df);
174 double[] res = new double[ args.length ];
175 for( int i = 0 ; i < res.length ; i++){
176 res[i] = 1.0 - td.cumulativeProbability(args[i]);
177 }
178 return res;
179 }
180 }