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