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: 545192 $ $Date: 2007-06-07 07:35:04 -0700 (Thu, 07 Jun 2007) $
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      public ContinuousDistribution makeDistribution() {
40          return new ExponentialDistributionImpl(5.0);
41      }   
42      
43      /** Creates the default cumulative probability distribution test input values */
44      public double[] makeCumulativeTestPoints() {
45          // quantiles computed using R version 1.8.1 (linux version)
46          return new double[] {0.005002502d, 0.05025168d, 0.1265890d, 0.2564665d, 0.5268026d, 
47                  34.53878d, 23.02585d, 18.44440d, 14.97866d, 11.51293d};
48      }
49      
50      /** Creates the default cumulative probability density test expected values */
51      public double[] makeCumulativeTestValues() {
52          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
53                  0.990d, 0.975d, 0.950d, 0.900d}; 
54      }
55      
56      //------------ Additional tests -------------------------------------------
57   
58      public void testCumulativeProbabilityExtremes() throws Exception {
59          setCumulativeTestPoints(new double[] {-2, 0});
60          setCumulativeTestValues(new double[] {0, 0});
61          verifyCumulativeProbabilities();
62      }
63  
64      public void testInverseCumulativeProbabilityExtremes() throws Exception {
65           setInverseCumulativeTestPoints(new double[] {0, 1});
66           setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
67           verifyInverseCumulativeProbabilities();
68      }
69  
70      public void testCumulativeProbability2() throws Exception {
71          double actual = getDistribution().cumulativeProbability(0.25, 0.75);
72          assertEquals(0.0905214, actual, 10e-4);
73      }
74      
75      public void testMeanAccessors() {
76          ExponentialDistribution distribution = (ExponentialDistribution) getDistribution();
77          assertEquals(5d, distribution.getMean(), Double.MIN_VALUE);
78          distribution.setMean(2d);
79          assertEquals(2d, distribution.getMean(), Double.MIN_VALUE);
80          try {
81              distribution.setMean(0);
82              fail("Expecting IllegalArgumentException for 0 mean");
83          } catch (IllegalArgumentException ex) {
84              // expected
85          }
86      }
87     
88  }