1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.distribution.continuous;
24
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27
28 import static org.junit.jupiter.api.Assertions.assertEquals;
29
30
31
32
33
34
35 public class ChiSquaredDistributionTest extends RealDistributionAbstractTest {
36
37
38
39
40 @Override
41 public ChiSquaredDistribution makeDistribution() {
42 return new ChiSquaredDistribution(5.0);
43 }
44
45
46 @Override
47 public double[] makeCumulativeTestPoints() {
48
49 return new double[] {
50 0.210212602629, 0.554298076728, 0.831211613487, 1.14547622606, 1.61030798696,
51 20.5150056524, 15.0862724694, 12.8325019940, 11.0704976935, 9.23635689978
52 };
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[] makeInverseCumulativeTestPoints() {
64 return new double[] { 0, 0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1 };
65 }
66
67
68 @Override
69 public double[] makeInverseCumulativeTestValues() {
70 return new double[] {
71 0, 0.210212602629, 0.554298076728, 0.831211613487, 1.14547622606, 1.61030798696, 20.5150056524,
72 15.0862724694, 12.8325019940, 11.0704976935, 9.23635689978, Double.POSITIVE_INFINITY
73 };
74 }
75
76
77 @Override
78 public double[] makeDensityTestValues() {
79 return new double[] {
80 0.0115379817652, 0.0415948507811, 0.0665060119842, 0.0919455953114, 0.121472591024,
81 0.000433630076361, 0.00412780610309, 0.00999340341045, 0.0193246438937, 0.0368460089216
82 };
83 }
84
85
86 @BeforeEach
87 @Override
88 public void setUp() {
89 super.setUp();
90 setTolerance(1e-9);
91 }
92
93
94
95 @Test
96 void testSmallDf() {
97 setDistribution(new ChiSquaredDistribution(0.1d));
98 setTolerance(1E-4);
99
100 setCumulativeTestPoints(new double[] {
101 1.168926E-60, 1.168926E-40, 1.063132E-32, 1.144775E-26, 1.168926E-20,
102 5.472917, 2.175255, 1.13438, 0.5318646, 0.1526342
103 });
104 setInverseCumulativeTestValues(getCumulativeTestPoints());
105 setInverseCumulativeTestPoints(getCumulativeTestValues());
106 verifyCumulativeProbabilities();
107 verifyInverseCumulativeProbabilities();
108 }
109
110 @Test
111 void testDfAccessors() {
112 ChiSquaredDistribution distribution = (ChiSquaredDistribution) getDistribution();
113 assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
114 }
115
116 @Test
117 void testDensity() {
118 double[] x = new double[]{-0.1, 1e-6, 0.5, 1, 2, 5};
119
120 checkDensity(1, x, new double[] {
121 0.00000000000, 398.94208093034, 0.43939128947, 0.24197072452, 0.10377687436, 0.01464498256
122 });
123
124 checkDensity(0.1, x, new double[] {
125 0.000000000e+00, 2.486453997e+04, 7.464238732e-02, 3.009077718e-02, 9.447299159e-03, 8.827199396e-04
126 });
127
128 checkDensity(2, x, new double[] {
129 0.00000000000, 0.49999975000, 0.38940039154, 0.30326532986, 0.18393972059, 0.04104249931
130 });
131
132 checkDensity(10, x, new double[] {
133 0.000000000e+00, 1.302082682e-27, 6.337896998e-05, 7.897534632e-04, 7.664155024e-03, 6.680094289e-02
134 });
135 }
136
137 private void checkDensity(double df, double[] x, double[] expected) {
138 ChiSquaredDistribution d = new ChiSquaredDistribution(df);
139 for (int i = 0; i < x.length; i++) {
140 assertEquals(expected[i], d.density(x[i]), 1e-5);
141 }
142 }
143
144 @Test
145 void testMoments() {
146 final double tol = 1e-9;
147 ChiSquaredDistribution dist;
148
149 dist = new ChiSquaredDistribution(1500);
150 assertEquals(1500, dist.getNumericalMean(), tol);
151 assertEquals(3000, dist.getNumericalVariance(), tol);
152
153 dist = new ChiSquaredDistribution(1.12);
154 assertEquals(1.12, dist.getNumericalMean(), tol);
155 assertEquals(2.24, dist.getNumericalVariance(), tol);
156 }
157 }