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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.distribution.continuous;
24  
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  
32  /**
33   * Test cases for UniformRealDistribution.
34   */
35  public class UniformRealDistributionTest extends RealDistributionAbstractTest {
36  
37      // --- Override tolerance -------------------------------------------------
38  
39      @BeforeEach
40      @Override
41      public void setUp() {
42          super.setUp();
43          setTolerance(1e-4);
44      }
45  
46      //--- Implementations for abstract methods --------------------------------
47  
48      /** Creates the default uniform real distribution instance to use in tests. */
49      @Override
50      public UniformRealDistribution makeDistribution() {
51          return new UniformRealDistribution(-0.5, 1.25);
52      }
53  
54      /** Creates the default cumulative probability distribution test input values */
55      @Override
56      public double[] makeCumulativeTestPoints() {
57          return new double[] {-0.5001, -0.5, -0.4999, -0.25, -0.0001, 0.0,
58                               0.0001, 0.25, 1.0, 1.2499, 1.25, 1.2501};
59      }
60  
61      /** Creates the default cumulative probability density test expected values */
62      @Override
63      public double[] makeCumulativeTestValues() {
64          return new double[] {0.0, 0.0, 0.0001, 0.25/1.75, 0.4999/1.75,
65                               0.5/1.75, 0.5001/1.75, 0.75/1.75, 1.5/1.75,
66                               1.7499/1.75, 1.0, 1.0};
67      }
68  
69      /** Creates the default probability density test expected values */
70      @Override
71      public double[] makeDensityTestValues() {
72          double d = 1 / 1.75;
73          return new double[] {0, d, d, d, d, d, d, d, d, d, d, 0};
74      }
75  
76      //--- Additional test cases -----------------------------------------------
77  
78      /** Test lower bound getter. */
79      @Test
80      void testGetLowerBound() {
81          UniformRealDistribution distribution = makeDistribution();
82          assertEquals(-0.5, distribution.getSupportLowerBound(), 0);
83      }
84  
85      /** Test upper bound getter. */
86      @Test
87      void testGetUpperBound() {
88          UniformRealDistribution distribution = makeDistribution();
89          assertEquals(1.25, distribution.getSupportUpperBound(), 0);
90      }
91  
92      /** Test pre-condition for equal lower/upper bound. */
93      @Test
94      void testPreconditions1() {
95          assertThrows(MathIllegalArgumentException.class, () -> {
96              new UniformRealDistribution(0, 0);
97          });
98      }
99  
100     /** Test pre-condition for lower bound larger than upper bound. */
101     @Test
102     void testPreconditions2() {
103         assertThrows(MathIllegalArgumentException.class, () -> {
104             new UniformRealDistribution(1, 0);
105         });
106     }
107 
108     /** Test mean/variance. */
109     @Test
110     void testMeanVariance() {
111         UniformRealDistribution dist;
112 
113         dist = new UniformRealDistribution(0, 1);
114         assertEquals(0.5, dist.getNumericalMean(), 0);
115         assertEquals(dist.getNumericalVariance(), 1/12.0, 0);
116 
117         dist = new UniformRealDistribution(-1.5, 0.6);
118         assertEquals(dist.getNumericalMean(), -0.45, 0);
119         assertEquals(0.3675, dist.getNumericalVariance(), 0);
120 
121         dist = new UniformRealDistribution(-0.5, 1.25);
122         assertEquals(0.375, dist.getNumericalMean(), 0);
123         assertEquals(0.2552083333333333, dist.getNumericalVariance(), 0);
124     }
125 
126     /**
127      * Check accuracy of analytical inverse CDF. Fails if a solver is used
128      * with the default accuracy.
129      */
130     @Test
131     void testInverseCumulativeDistribution() {
132         UniformRealDistribution dist = new UniformRealDistribution(0, 1e-9);
133 
134         assertEquals(2.5e-10, dist.inverseCumulativeProbability(0.25), 0);
135     }
136 }