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.Assert;
28  import org.junit.Test;
29  
30  /**
31   * Test cases for ExponentialDistribution.
32   */
33  public class ExponentialDistributionTest extends RealDistributionAbstractTest {
34  
35      // --------------------- Override tolerance  --------------
36      @Override
37      public void setUp() {
38          super.setUp();
39          setTolerance(1E-9);
40      }
41  
42      //-------------- Implementations for abstract methods -----------------------
43  
44      /** Creates the default continuous distribution instance to use in tests. */
45      @Override
46      public ExponentialDistribution makeDistribution() {
47          return new ExponentialDistribution(5.0);
48      }
49  
50      /** Creates the default cumulative probability distribution test input values */
51      @Override
52      public double[] makeCumulativeTestPoints() {
53          // quantiles computed using R version 2.9.2
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      /** Creates the default cumulative probability density test expected values */
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      /** Creates the default probability density test expected values */
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      //------------ Additional tests -------------------------------------------
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         // computed using  print(dexp(1, rate=1/3), digits=10) in R 2.5
106         Assert.assertEquals(0.2388437702, d2.density(1.0), 1e-8);
107 
108         // computed using  print(dexp(2, rate=1/3), digits=10) in R 2.5
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 }