View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.statistics.distribution;
19  
20  import java.util.stream.IntStream;
21  import org.apache.commons.rng.UniformRandomProvider;
22  import org.apache.commons.rng.simple.RandomSource;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test code used in the distributions section of the user guide.
28   */
29  class UserGuideTest {
30      @Test
31      void testCDF() {
32          TDistribution t = TDistribution.of(29);
33          double lowerTail = t.cumulativeProbability(-2.656);   // P(T(29) <= -2.656)
34          double upperTail = t.survivalProbability(2.75);       // P(T(29) > 2.75)
35  
36          Assertions.assertTrue(lowerTail > upperTail,
37              () -> String.format("Since 2.75 > |-2.656|, expected %s > %s", lowerTail, upperTail));
38      }
39  
40      @Test
41      void testProbability() {
42          PoissonDistribution pd = PoissonDistribution.of(1.23);
43          double p1 = pd.probability(5);
44          double p2 = pd.probability(5, 5);
45          double p3 = pd.probability(4, 5);
46  
47          Assertions.assertEquals(0, p2);
48          Assertions.assertEquals(p1, p3);
49      }
50  
51      @Test
52      void testInverseCDF() {
53          NormalDistribution n = NormalDistribution.of(0, 1);
54          double x1 = n.inverseCumulativeProbability(1e-300);
55          double x2 = n.inverseSurvivalProbability(1e-300);
56  
57          Assertions.assertEquals(x1, -x2);
58          Assertions.assertEquals(-37.0471, x1, 1e-3);
59      }
60  
61      @Test
62      void testProperties() {
63          ChiSquaredDistribution chi2 = ChiSquaredDistribution.of(42);
64          double df = chi2.getDegreesOfFreedom();    // 42
65          double mean = chi2.getMean();              // 42
66          double var = chi2.getVariance();           // 84
67  
68          CauchyDistribution cauchy = CauchyDistribution.of(1.23, 4.56);
69          double location = cauchy.getLocation();    // 1.23
70          double scale = cauchy.getScale();          // 4.56
71          double undefined1 = cauchy.getMean();      // NaN
72          double undefined2 = cauchy.getVariance();  // NaN
73  
74          Assertions.assertEquals(42, df);
75          Assertions.assertEquals(42, mean);
76          Assertions.assertEquals(84, var);
77          Assertions.assertEquals(1.23, location);
78          Assertions.assertEquals(4.56, scale);
79          Assertions.assertEquals(Double.NaN, undefined1);
80          Assertions.assertEquals(Double.NaN, undefined2);
81      }
82  
83      @Test
84      void testDomain() {
85          BinomialDistribution b = BinomialDistribution.of(13, 0.15);
86          int lower = b.getSupportLowerBound();  // 0
87          int upper = b.getSupportUpperBound();  // 13
88  
89          Assertions.assertEquals(0, lower);
90          Assertions.assertEquals(13, upper);
91      }
92  
93      @Test
94      void testSampling() {
95          // From Commons RNG Simple
96          UniformRandomProvider rng = RandomSource.KISS.create(123L);
97  
98          NormalDistribution n = NormalDistribution.of(0, 1);
99          double x = n.createSampler(rng).sample();
100 
101         // Generate a number of samples
102         GeometricDistribution g = GeometricDistribution.of(0.75);
103         int[] k = IntStream.generate(g.createSampler(rng)::sample).limit(100).toArray();
104 
105         Assertions.assertTrue(-5 < x && x < 5, () -> Double.toString(x));
106         Assertions.assertEquals(100, k.length);
107     }
108 
109     @Test
110     void testComplement() {
111         ChiSquaredDistribution chi2 = ChiSquaredDistribution.of(42);
112         double q1 = 1 - chi2.cumulativeProbability(168);
113         double q2 = chi2.survivalProbability(168);
114 
115         Assertions.assertEquals(0, q1);
116         Assertions.assertNotEquals(0, q2);
117 
118         // For the table
119         final double eps = Math.pow(2, -53);
120         Assertions.assertEquals(1.110223e-16, eps, 1e-3);
121         Assertions.assertEquals(eps, 1 - chi2.cumulativeProbability(166));
122         Assertions.assertEquals(eps, 1 - chi2.cumulativeProbability(167));
123         Assertions.assertEquals(0, 1 - chi2.cumulativeProbability(168));
124         Assertions.assertEquals(0, 1 - chi2.cumulativeProbability(200));
125         Assertions.assertEquals(1.16583e-16, chi2.survivalProbability(166), 1e-3);
126         Assertions.assertEquals(7.95907e-17, chi2.survivalProbability(167), 1e-3);
127         Assertions.assertEquals(5.42987e-17, chi2.survivalProbability(168), 1e-3);
128         Assertions.assertEquals(1.19056e-22, chi2.survivalProbability(200), 1e-3);
129     }
130 
131     @Test
132     void testInverseComplement() {
133         ChiSquaredDistribution chi2 = ChiSquaredDistribution.of(42);
134         double q = 5.43e-17;
135         // Incorrect: p = 1 - q == 1.0 !!!
136         double x1 = chi2.inverseCumulativeProbability(1 - q);
137         // Correct: invert q
138         double x2 = chi2.inverseSurvivalProbability(q);
139 
140         Assertions.assertEquals(Double.POSITIVE_INFINITY, x1);
141         Assertions.assertEquals(168.0, x2, 0.1);
142     }
143 }