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    
018    package org.apache.commons.math.distribution;
019    
020    /**
021     * Test cases for WeibullDistribution.
022     * Extends ContinuousDistributionAbstractTest.  See class javadoc for
023     * ContinuousDistributionAbstractTest for details.
024     * 
025     * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
026     */
027    public class WeibullDistributionTest extends ContinuousDistributionAbstractTest  {
028        
029        /**
030         * Constructor for CauchyDistributionTest.
031         * @param arg0
032         */
033        public WeibullDistributionTest(String arg0) {
034            super(arg0);
035        }
036        
037        //-------------- Implementations for abstract methods -----------------------
038        
039        /** Creates the default continuous distribution instance to use in tests. */
040        @Override
041        public ContinuousDistribution makeDistribution() {
042            return new WeibullDistributionImpl(1.2, 2.1);
043        }   
044        
045        /** Creates the default cumulative probability distribution test input values */
046        @Override
047        public double[] makeCumulativeTestPoints() {
048            // quantiles computed using Mathematica 
049            return new double[] {0.00664355181d, 0.04543282833d, 0.09811627374d,
050                    0.1767135246d, 0.3219468654d, 4.207902826d, 5.23968437d,
051                    6.232056007d, 7.497630467d, 10.51154969d};
052        }
053        
054        /** Creates the default cumulative probability density test expected values */
055        @Override
056        public double[] makeCumulativeTestValues() {
057            return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.900d, 0.950d,
058                    0.975d, 0.990d, 0.999d};
059        }
060        
061        //---------------------------- Additional test cases -------------------------
062        
063        public void testInverseCumulativeProbabilityExtremes() throws Exception {
064            setInverseCumulativeTestPoints(new double[] {0.0, 1.0});
065            setInverseCumulativeTestValues(
066                    new double[] {0.0, Double.POSITIVE_INFINITY});
067            verifyInverseCumulativeProbabilities();
068        }
069        
070        public void testAlpha() {
071            WeibullDistribution distribution = (WeibullDistribution) getDistribution();
072            double expected = Math.random();
073            distribution.setShape(expected);
074            assertEquals(expected, distribution.getShape(), 0.0);
075        }
076        
077        public void testBeta() {
078            WeibullDistribution distribution = (WeibullDistribution) getDistribution();
079            double expected = Math.random();
080            distribution.setScale(expected);
081            assertEquals(expected, distribution.getScale(), 0.0);
082        }
083        
084        public void testSetAlpha() {
085            WeibullDistribution distribution = (WeibullDistribution) getDistribution();
086            try {
087                distribution.setShape(0.0);
088                fail("Can not have 0.0 alpha.");
089            } catch (IllegalArgumentException ex) {
090                // success
091            }
092            
093            try {
094                distribution.setShape(-1.0);
095                fail("Can not have negative alpha.");
096            } catch (IllegalArgumentException ex) {
097                // success
098            }
099        }
100        
101        public void testSetBeta() {
102            WeibullDistribution distribution = (WeibullDistribution) getDistribution();
103            try {
104                distribution.setScale(0.0);
105                fail("Can not have 0.0 beta.");
106            } catch (IllegalArgumentException ex) {
107                // success
108            }
109            
110            try {
111                distribution.setScale(-1.0);
112                fail("Can not have negative beta.");
113            } catch (IllegalArgumentException ex) {
114                // success
115            }
116        }
117    }