001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.distribution;
018    
019    /**
020     * Test cases for ExponentialDistribution.
021     * Extends ContinuousDistributionAbstractTest.  See class javadoc for
022     * ContinuousDistributionAbstractTest for details.
023     * 
024     * @version $Revision: 764769 $ $Date: 2009-04-14 09:13:54 -0400 (Tue, 14 Apr 2009) $
025     */
026    public class ExponentialDistributionTest extends ContinuousDistributionAbstractTest {
027    
028        /**
029         * Constructor for ExponentialDistributionTest.
030         * @param name
031         */
032        public ExponentialDistributionTest(String name) {
033            super(name);
034        }
035    
036        //-------------- Implementations for abstract methods -----------------------
037        
038        /** Creates the default continuous distribution instance to use in tests. */
039        @Override
040        public ContinuousDistribution makeDistribution() {
041            return new ExponentialDistributionImpl(5.0);
042        }   
043        
044        /** Creates the default cumulative probability distribution test input values */
045        @Override
046        public double[] makeCumulativeTestPoints() {
047            // quantiles computed using R version 1.8.1 (linux version)
048            return new double[] {0.005002502d, 0.05025168d, 0.1265890d, 0.2564665d, 0.5268026d, 
049                    34.53878d, 23.02585d, 18.44440d, 14.97866d, 11.51293d};
050        }
051        
052        /** Creates the default cumulative probability density test expected values */
053        @Override
054        public double[] makeCumulativeTestValues() {
055            return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
056                    0.990d, 0.975d, 0.950d, 0.900d}; 
057        }
058        
059        //------------ Additional tests -------------------------------------------
060     
061        public void testCumulativeProbabilityExtremes() throws Exception {
062            setCumulativeTestPoints(new double[] {-2, 0});
063            setCumulativeTestValues(new double[] {0, 0});
064            verifyCumulativeProbabilities();
065        }
066    
067        public void testInverseCumulativeProbabilityExtremes() throws Exception {
068             setInverseCumulativeTestPoints(new double[] {0, 1});
069             setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
070             verifyInverseCumulativeProbabilities();
071        }
072    
073        public void testCumulativeProbability2() throws Exception {
074            double actual = getDistribution().cumulativeProbability(0.25, 0.75);
075            assertEquals(0.0905214, actual, 10e-4);
076        }
077    
078        public void testDensity() {
079            ExponentialDistribution d1 = new ExponentialDistributionImpl(1);
080            assertEquals(0.0, d1.density(-1e-9));
081            assertEquals(1.0, d1.density(0.0));
082            assertEquals(0.0, d1.density(1000.0));
083            assertEquals(Math.exp(-1), d1.density(1.0));
084            assertEquals(Math.exp(-2), d1.density(2.0));
085    
086            ExponentialDistribution d2 = new ExponentialDistributionImpl(3);
087            assertEquals(1/3.0, d2.density(0.0));
088            // computed using  print(dexp(1, rate=1/3), digits=10) in R 2.5
089            assertEquals(0.2388437702, d2.density(1.0), 1e-8);
090    
091            // computed using  print(dexp(2, rate=1/3), digits=10) in R 2.5
092            assertEquals(0.1711390397, d2.density(2.0), 1e-8);
093        }
094        
095        public void testMeanAccessors() {
096            ExponentialDistribution distribution = (ExponentialDistribution) getDistribution();
097            assertEquals(5d, distribution.getMean(), Double.MIN_VALUE);
098            distribution.setMean(2d);
099            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    }