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    *      http://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  package org.apache.commons.math.distribution;
18  
19  /**
20   * Test cases for ExponentialDistribution.
21   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
22   * ContinuousDistributionAbstractTest for details.
23   * 
24   * @version $Revision: 764769 $ $Date: 2009-04-14 09:13:54 -0400 (Tue, 14 Apr 2009) $
25   */
26  public class ExponentialDistributionTest extends ContinuousDistributionAbstractTest {
27  
28      /**
29       * Constructor for ExponentialDistributionTest.
30       * @param name
31       */
32      public ExponentialDistributionTest(String name) {
33          super(name);
34      }
35  
36      //-------------- Implementations for abstract methods -----------------------
37      
38      /** Creates the default continuous distribution instance to use in tests. */
39      @Override
40      public ContinuousDistribution makeDistribution() {
41          return new ExponentialDistributionImpl(5.0);
42      }   
43      
44      /** Creates the default cumulative probability distribution test input values */
45      @Override
46      public double[] makeCumulativeTestPoints() {
47          // quantiles computed using R version 1.8.1 (linux version)
48          return new double[] {0.005002502d, 0.05025168d, 0.1265890d, 0.2564665d, 0.5268026d, 
49                  34.53878d, 23.02585d, 18.44440d, 14.97866d, 11.51293d};
50      }
51      
52      /** Creates the default cumulative probability density test expected values */
53      @Override
54      public double[] makeCumulativeTestValues() {
55          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
56                  0.990d, 0.975d, 0.950d, 0.900d}; 
57      }
58      
59      //------------ Additional tests -------------------------------------------
60   
61      public void testCumulativeProbabilityExtremes() throws Exception {
62          setCumulativeTestPoints(new double[] {-2, 0});
63          setCumulativeTestValues(new double[] {0, 0});
64          verifyCumulativeProbabilities();
65      }
66  
67      public void testInverseCumulativeProbabilityExtremes() throws Exception {
68           setInverseCumulativeTestPoints(new double[] {0, 1});
69           setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
70           verifyInverseCumulativeProbabilities();
71      }
72  
73      public void testCumulativeProbability2() throws Exception {
74          double actual = getDistribution().cumulativeProbability(0.25, 0.75);
75          assertEquals(0.0905214, actual, 10e-4);
76      }
77  
78      public void testDensity() {
79          ExponentialDistribution d1 = new ExponentialDistributionImpl(1);
80          assertEquals(0.0, d1.density(-1e-9));
81          assertEquals(1.0, d1.density(0.0));
82          assertEquals(0.0, d1.density(1000.0));
83          assertEquals(Math.exp(-1), d1.density(1.0));
84          assertEquals(Math.exp(-2), d1.density(2.0));
85  
86          ExponentialDistribution d2 = new ExponentialDistributionImpl(3);
87          assertEquals(1/3.0, d2.density(0.0));
88          // computed using  print(dexp(1, rate=1/3), digits=10) in R 2.5
89          assertEquals(0.2388437702, d2.density(1.0), 1e-8);
90  
91          // computed using  print(dexp(2, rate=1/3), digits=10) in R 2.5
92          assertEquals(0.1711390397, d2.density(2.0), 1e-8);
93      }
94      
95      public void testMeanAccessors() {
96          ExponentialDistribution distribution = (ExponentialDistribution) getDistribution();
97          assertEquals(5d, distribution.getMean(), Double.MIN_VALUE);
98          distribution.setMean(2d);
99          assertEquals(2d, distribution.getMean(), Double.MIN_VALUE);
100         try {
101             distribution.setMean(0);
102             fail("Expecting IllegalArgumentException for 0 mean");
103         } catch (IllegalArgumentException ex) {
104             // expected
105         }
106     }
107    
108 }