1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.distribution.continuous;
23
24 import org.hipparchus.exception.MathIllegalArgumentException;
25 import org.hipparchus.util.FastMath;
26 import org.hipparchus.util.Precision;
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 import static org.junit.jupiter.api.Assertions.assertThrows;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
33
34
35
36
37 public class ExponentialDistributionTest extends RealDistributionAbstractTest {
38
39
40 @BeforeEach
41 @Override
42 public void setUp() {
43 super.setUp();
44 setTolerance(1E-9);
45 }
46
47
48
49
50 @Override
51 public ExponentialDistribution makeDistribution() {
52 return new ExponentialDistribution(5.0);
53 }
54
55
56 @Override
57 public double[] makeCumulativeTestPoints() {
58
59 return new double[] {0.00500250166792, 0.0502516792675, 0.126589039921, 0.256466471938,
60 0.526802578289, 34.5387763949, 23.0258509299, 18.4443972706, 14.9786613678, 11.5129254650};
61 }
62
63
64 @Override
65 public double[] makeCumulativeTestValues() {
66 return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999,
67 0.990, 0.975, 0.950, 0.900};
68 }
69
70
71 @Override
72 public double[] makeDensityTestValues() {
73 return new double[] {0.1998, 0.198, 0.195, 0.19, 0.18, 0.000200000000000,
74 0.00200000000002, 0.00499999999997, 0.00999999999994, 0.0199999999999};
75 }
76
77
78
79 @Test
80 void testCumulativeProbabilityExtremes() {
81 setCumulativeTestPoints(new double[] {-2, 0});
82 setCumulativeTestValues(new double[] {0, 0});
83 verifyCumulativeProbabilities();
84 }
85
86 @Test
87 void testInverseCumulativeProbabilityExtremes() {
88 setInverseCumulativeTestPoints(new double[] {0, 1});
89 setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
90 verifyInverseCumulativeProbabilities();
91 }
92
93 @Test
94 void testCumulativeProbability2() {
95 double actual = getDistribution().probability(0.25, 0.75);
96 assertEquals(0.0905214, actual, 10e-4);
97 }
98
99 @Test
100 void testDensity() {
101 ExponentialDistribution d1 = new ExponentialDistribution(1);
102 assertTrue(Precision.equals(0.0, d1.density(-1e-9), 1));
103 assertTrue(Precision.equals(1.0, d1.density(0.0), 1));
104 assertTrue(Precision.equals(0.0, d1.density(1000.0), 1));
105 assertTrue(Precision.equals(FastMath.exp(-1), d1.density(1.0), 1));
106 assertTrue(Precision.equals(FastMath.exp(-2), d1.density(2.0), 1));
107
108 ExponentialDistribution d2 = new ExponentialDistribution(3);
109 assertTrue(Precision.equals(1/3.0, d2.density(0.0), 1));
110
111 assertEquals(0.2388437702, d2.density(1.0), 1e-8);
112
113
114 assertEquals(0.1711390397, d2.density(2.0), 1e-8);
115 }
116
117 @Test
118 void testMeanAccessors() {
119 ExponentialDistribution distribution = (ExponentialDistribution) getDistribution();
120 assertEquals(5d, distribution.getMean(), Double.MIN_VALUE);
121 }
122
123 @Test
124 void testPreconditions() {
125 assertThrows(MathIllegalArgumentException.class, () -> {
126 new ExponentialDistribution(0);
127 });
128 }
129
130 @Test
131 void testMoments() {
132 final double tol = 1e-9;
133 ExponentialDistribution dist;
134
135 dist = new ExponentialDistribution(11d);
136 assertEquals(11d, dist.getNumericalMean(), tol);
137 assertEquals(dist.getNumericalVariance(), 11d * 11d, tol);
138
139 dist = new ExponentialDistribution(10.5d);
140 assertEquals(10.5d, dist.getNumericalMean(), tol);
141 assertEquals(dist.getNumericalVariance(), 10.5d * 10.5d, tol);
142 }
143 }