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.continuous;
24
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.junit.Assert;
27 import org.junit.Test;
28
29
30
31
32 public class UniformRealDistributionTest extends RealDistributionAbstractTest {
33
34
35
36 @Override
37 public void setUp() {
38 super.setUp();
39 setTolerance(1e-4);
40 }
41
42
43
44
45 @Override
46 public UniformRealDistribution makeDistribution() {
47 return new UniformRealDistribution(-0.5, 1.25);
48 }
49
50
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
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
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
73
74
75 @Test
76 public void testGetLowerBound() {
77 UniformRealDistribution distribution = makeDistribution();
78 Assert.assertEquals(-0.5, distribution.getSupportLowerBound(), 0);
79 }
80
81
82 @Test
83 public void testGetUpperBound() {
84 UniformRealDistribution distribution = makeDistribution();
85 Assert.assertEquals(1.25, distribution.getSupportUpperBound(), 0);
86 }
87
88
89 @Test(expected=MathIllegalArgumentException.class)
90 public void testPreconditions1() {
91 new UniformRealDistribution(0, 0);
92 }
93
94
95 @Test(expected=MathIllegalArgumentException.class)
96 public void testPreconditions2() {
97 new UniformRealDistribution(1, 0);
98 }
99
100
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
120
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 }