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.discrete;
24  
25  import org.hipparchus.distribution.IntegerDistribution;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /**
31   * Test cases for UniformIntegerDistribution.
32   */
33  public class UniformIntegerDistributionTest extends IntegerDistributionAbstractTest {
34  
35      // --- Override tolerance -------------------------------------------------
36  
37      @Override
38      public void setUp() {
39          super.setUp();
40          setTolerance(1e-9);
41      }
42  
43      //--- Implementations for abstract methods --------------------------------
44  
45      /** Creates the default discrete distribution instance to use in tests. */
46      @Override
47      public IntegerDistribution makeDistribution() {
48          return new UniformIntegerDistribution(-3, 5);
49      }
50  
51      /** Creates the default probability density test input values. */
52      @Override
53      public int[] makeDensityTestPoints() {
54          return new int[] {-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6};
55      }
56  
57      /** Creates the default probability density test expected values. */
58      @Override
59      public double[] makeDensityTestValues() {
60          double d = 1.0 / (5 - -3 + 1);
61          return new double[] {0, d, d, d, d, d, d, d, d, d, 0};
62      }
63  
64      /** Creates the default cumulative probability density test input values. */
65      @Override
66      public int[] makeCumulativeTestPoints() {
67          return makeDensityTestPoints();
68      }
69  
70      /** Creates the default cumulative probability density test expected values. */
71      @Override
72      public double[] makeCumulativeTestValues() {
73          return new double[] {0, 1 / 9.0, 2 / 9.0, 3 / 9.0, 4 / 9.0, 5 / 9.0,
74                               6 / 9.0, 7 / 9.0, 8 / 9.0, 1, 1};
75      }
76  
77      /** Creates the default inverse cumulative probability test input values */
78      @Override
79      public double[] makeInverseCumulativeTestPoints() {
80          return new double[] {0, 0.001, 0.010, 0.025, 0.050, 0.100, 0.200,
81                               0.5, 0.999, 0.990, 0.975, 0.950, 0.900, 1};
82      }
83  
84      /** Creates the default inverse cumulative probability density test expected values */
85      @Override
86      public int[] makeInverseCumulativeTestValues() {
87          return new int[] {-3, -3, -3, -3, -3, -3, -2, 1, 5, 5, 5, 5, 5, 5};
88      }
89  
90      //--- Additional test cases -----------------------------------------------
91  
92      /** Test mean/variance. */
93      @Test
94      public void testMoments() {
95          UniformIntegerDistribution dist;
96  
97          dist = new UniformIntegerDistribution(0, 5);
98          Assert.assertEquals(dist.getNumericalMean(), 2.5, 0);
99          Assert.assertEquals(dist.getNumericalVariance(), 35 / 12.0, 0);
100 
101         dist = new UniformIntegerDistribution(0, 1);
102         Assert.assertEquals(dist.getNumericalMean(), 0.5, 0);
103         Assert.assertEquals(dist.getNumericalVariance(), 3 / 12.0, 0);
104     }
105 
106     // MATH-1141
107     @Test
108     public void testPreconditionUpperBoundInclusive() {
109         try {
110             new UniformIntegerDistribution(1, 0);
111         } catch (MathIllegalArgumentException e) {
112             // Expected.
113         }
114 
115         // Degenerate case is allowed.
116         new UniformIntegerDistribution(0, 0);
117     }
118 }