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