1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
34
35 public class UniformIntegerDistributionTest extends IntegerDistributionAbstractTest {
36
37
38
39 @BeforeEach
40 @Override
41 public void setUp() {
42 super.setUp();
43 setTolerance(1e-9);
44 }
45
46
47
48
49 @Override
50 public IntegerDistribution makeDistribution() {
51 return new UniformIntegerDistribution(-3, 5);
52 }
53
54
55 @Override
56 public int[] makeDensityTestPoints() {
57 return new int[] {-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6};
58 }
59
60
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
68 @Override
69 public int[] makeCumulativeTestPoints() {
70 return makeDensityTestPoints();
71 }
72
73
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
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
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
94
95
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
110 @Test
111 void testPreconditionUpperBoundInclusive() {
112 try {
113 new UniformIntegerDistribution(1, 0);
114 } catch (MathIllegalArgumentException e) {
115
116 }
117
118
119 new UniformIntegerDistribution(0, 0);
120 }
121 }