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.Stream;
21  import org.apache.commons.rng.UniformRandomProvider;
22  import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
23  import org.apache.commons.rng.sampling.distribution.ContinuousUniformSampler;
24  import org.apache.commons.rng.simple.RandomSource;
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.params.ParameterizedTest;
28  import org.junit.jupiter.params.provider.Arguments;
29  import org.junit.jupiter.params.provider.CsvSource;
30  import org.junit.jupiter.params.provider.MethodSource;
31  
32  /**
33   * Test cases for {@link UniformContinuousDistribution}.
34   * Extends {@link BaseContinuousDistributionTest}. See javadoc of that class for details.
35   */
36  class UniformContinuousDistributionTest extends BaseContinuousDistributionTest {
37      @Override
38      ContinuousDistribution makeDistribution(Object... parameters) {
39          final double lower = (Double) parameters[0];
40          final double upper = (Double) parameters[1];
41          return UniformContinuousDistribution.of(lower, upper);
42      }
43  
44      @Override
45      Object[][] makeInvalidParameters() {
46          return new Object[][] {
47              {0.0, 0.0},
48              {1.0, 0.0},
49              // Range not finite
50              {-Double.MAX_VALUE, Double.MAX_VALUE},
51              {Double.NaN, 1.0},
52              {0.0, Double.NaN},
53          };
54      }
55  
56      @Override
57      String[] getParameterNames() {
58          return new String[] {"SupportLowerBound", "SupportUpperBound"};
59      }
60  
61      @Override
62      protected double getRelativeTolerance() {
63          // Tolerance is 4.440892098500626E-16
64          return 2 * RELATIVE_EPS;
65      }
66  
67      //-------------------- Additional test cases -------------------------------
68  
69      @ParameterizedTest
70      @MethodSource
71      void testAdditionalMoments(double lower, double upper, double mean, double variance) {
72          final UniformContinuousDistribution dist = UniformContinuousDistribution.of(lower, upper);
73          testMoments(dist, mean, variance, DoubleTolerances.equals());
74      }
75  
76      static Stream<Arguments> testAdditionalMoments() {
77          return Stream.of(
78              Arguments.of(0, 1, 0.5, 1 / 12.0),
79              Arguments.of(-1.5, 0.6, -0.45, 0.3675),
80              Arguments.of(Double.MAX_VALUE / 2, Double.MAX_VALUE, Double.MAX_VALUE - Double.MAX_VALUE / 4, Double.POSITIVE_INFINITY)
81          );
82      }
83  
84      /**
85       * Check accuracy of analytical inverse CDF. Fails if a solver is used
86       * with the default accuracy.
87       */
88      @Test
89      void testInverseCumulativeDistribution() {
90          final double upper = 1e-9;
91          final double tiny = 0x1.0p-100;
92  
93          final UniformContinuousDistribution dist = UniformContinuousDistribution.of(0, upper);
94          Assertions.assertEquals(2.5e-10, dist.inverseCumulativeProbability(0.25));
95          Assertions.assertEquals(tiny * upper, dist.inverseCumulativeProbability(tiny));
96  
97          final UniformContinuousDistribution dist2 = UniformContinuousDistribution.of(-upper, 0);
98          // This is inexact
99          Assertions.assertEquals(-7.5e-10, dist2.inverseCumulativeProbability(0.25), Math.ulp(-7.5e-10));
100         Assertions.assertEquals(-upper + tiny * upper, dist2.inverseCumulativeProbability(tiny));
101     }
102 
103     /**
104      * Test the probability in a range uses the exact computation of
105      * {@code (x1 - x0) / (upper - lower)} assuming x0 and x1 are within [lower, upper].
106      * This test will fail if the distribution uses the default implementation in
107      * {@link AbstractContinuousDistribution}.
108      */
109     @ParameterizedTest
110     @CsvSource(value = {
111         "-1.6358421681, -0.566237287234",
112         "-10.23678, 234.234",
113         "234.2342, 54322342.13",
114     })
115     void testProbabilityRange(double lower, double upper) {
116         final UniformContinuousDistribution dist = UniformContinuousDistribution.of(lower, upper);
117         final double r = upper - lower;
118         final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_128_PP.create();
119         final ContinuousSampler sampler = ContinuousUniformSampler.of(rng, lower, upper);
120         for (int i = 0; i < 100; i++) {
121             double x0 = sampler.sample();
122             double x1 = sampler.sample();
123             if (x1 < x0) {
124                 final double tmp = x0;
125                 x1 = x0;
126                 x0 = tmp;
127             }
128             Assertions.assertEquals((x1 - x0) / r, dist.probability(x0, x1));
129         }
130     }
131 
132     @Test
133     void testProbabilityRangeEdgeCases() {
134         final UniformContinuousDistribution dist = UniformContinuousDistribution.of(0, 11);
135 
136         Assertions.assertThrows(DistributionException.class, () -> dist.probability(4, 3));
137 
138         // x0 >= upper
139         Assertions.assertEquals(0, dist.probability(11, 16));
140         Assertions.assertEquals(0, dist.probability(15, 16));
141         // x1 < lower
142         Assertions.assertEquals(0, dist.probability(-3, -1));
143 
144         // x0 == x1
145         Assertions.assertEquals(0, dist.probability(4.12, 4.12));
146         Assertions.assertEquals(0, dist.probability(5.68, 5.68));
147 
148         // x1 > upper
149         Assertions.assertEquals(1, dist.probability(0, 16));
150         Assertions.assertEquals((11 - 3.45) / 11, dist.probability(3.45, 16));
151         Assertions.assertEquals((11 - 4.89) / 11, dist.probability(4.89, 16));
152         Assertions.assertEquals(0, dist.probability(11, 16));
153 
154         // x0 < lower
155         Assertions.assertEquals(2.0 / 11, dist.probability(-2, 2));
156         Assertions.assertEquals(3.0 / 11, dist.probability(-2, 3));
157         Assertions.assertEquals(4.0 / 11, dist.probability(-2, 4));
158         Assertions.assertEquals(1.0, dist.probability(-2, 11));
159 
160         // x1 > upper && x0 < lower
161         Assertions.assertEquals(1, dist.probability(-2, 16));
162     }
163 }