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