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