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