View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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   * Test cases for ExponentialDistribution.
36   */
37  public class ExponentialDistributionTest extends RealDistributionAbstractTest {
38  
39      // --------------------- Override tolerance  --------------
40      @BeforeEach
41      @Override
42      public void setUp() {
43          super.setUp();
44          setTolerance(1E-9);
45      }
46  
47      //-------------- Implementations for abstract methods -----------------------
48  
49      /** Creates the default continuous distribution instance to use in tests. */
50      @Override
51      public ExponentialDistribution makeDistribution() {
52          return new ExponentialDistribution(5.0);
53      }
54  
55      /** Creates the default cumulative probability distribution test input values */
56      @Override
57      public double[] makeCumulativeTestPoints() {
58          // quantiles computed using R version 2.9.2
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      /** Creates the default cumulative probability density test expected values */
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      /** Creates the default probability density test expected values */
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      //------------ Additional tests -------------------------------------------
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         // computed using  print(dexp(1, rate=1/3), digits=10) in R 2.5
111         assertEquals(0.2388437702, d2.density(1.0), 1e-8);
112 
113         // computed using  print(dexp(2, rate=1/3), digits=10) in R 2.5
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 }