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    import java.io.Serializable;
020    
021    import org.apache.commons.math.MathException;
022    
023    /**
024     * Base class for probability distributions.   
025     *  
026     * @version $Revision: 670469 $ $Date: 2008-06-23 10:01:38 +0200 (lun, 23 jun 2008) $
027     */
028    public abstract class AbstractDistribution
029        implements Distribution, Serializable {
030    
031        /** Serializable version identifier */
032        private static final long serialVersionUID = -38038050983108802L;
033        
034        /**
035         * Default constructor.
036         */
037        protected AbstractDistribution() {
038            super();
039        }
040    
041        /**
042         * For a random variable X whose values are distributed according
043         * to this distribution, this method returns P(x0 ≤ X ≤ x1).
044         * <p>
045         * The default implementation uses the identity</p>
046         * <p>
047         * P(x0 &le; X &le; x1) = P(X &le; x1) - P(X &le; x0) </p>
048         * 
049         * @param x0 the (inclusive) lower bound
050         * @param x1 the (inclusive) upper bound
051         * @return the probability that a random variable with this distribution
052         * will take a value between <code>x0</code> and <code>x1</code>,
053         * including the endpoints.
054         * @throws MathException if the cumulative probability can not be
055         * computed due to convergence or other numerical errors.
056         * @throws IllegalArgumentException if <code>x0 > x1</code>
057         */
058        public double cumulativeProbability(double x0, double x1)
059            throws MathException {
060            if (x0 > x1) {
061                throw new IllegalArgumentException
062                ("lower endpoint must be less than or equal to upper endpoint");
063            }
064            return cumulativeProbability(x1) - cumulativeProbability(x0);
065        }
066    }