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.Assert;
28 import org.junit.Test;
29
30
31
32
33 public class UniformIntegerDistributionTest extends IntegerDistributionAbstractTest {
34
35
36
37 @Override
38 public void setUp() {
39 super.setUp();
40 setTolerance(1e-9);
41 }
42
43
44
45
46 @Override
47 public IntegerDistribution makeDistribution() {
48 return new UniformIntegerDistribution(-3, 5);
49 }
50
51
52 @Override
53 public int[] makeDensityTestPoints() {
54 return new int[] {-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6};
55 }
56
57
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
65 @Override
66 public int[] makeCumulativeTestPoints() {
67 return makeDensityTestPoints();
68 }
69
70
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
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
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
91
92
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
107 @Test
108 public void testPreconditionUpperBoundInclusive() {
109 try {
110 new UniformIntegerDistribution(1, 0);
111 } catch (MathIllegalArgumentException e) {
112
113 }
114
115
116 new UniformIntegerDistribution(0, 0);
117 }
118 }