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