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 */
017package org.apache.commons.math3.distribution;
018
019import java.io.Serializable;
020
021import org.apache.commons.math3.exception.MathInternalError;
022import org.apache.commons.math3.exception.NotStrictlyPositiveException;
023import org.apache.commons.math3.exception.NumberIsTooLargeException;
024import org.apache.commons.math3.exception.OutOfRangeException;
025import org.apache.commons.math3.exception.util.LocalizedFormats;
026import org.apache.commons.math3.random.RandomGenerator;
027import org.apache.commons.math3.random.RandomDataImpl;
028import org.apache.commons.math3.util.FastMath;
029
030/**
031 * Base class for integer-valued discrete distributions.  Default
032 * implementations are provided for some of the methods that do not vary
033 * from distribution to distribution.
034 *
035 * @version $Id: AbstractIntegerDistribution.java 1533974 2013-10-20 20:42:41Z psteitz $
036 */
037public abstract class AbstractIntegerDistribution implements IntegerDistribution, Serializable {
038
039    /** Serializable version identifier */
040    private static final long serialVersionUID = -1146319659338487221L;
041
042    /**
043     * RandomData instance used to generate samples from the distribution.
044     * @deprecated As of 3.1, to be removed in 4.0. Please use the
045     * {@link #random} instance variable instead.
046     */
047    @Deprecated
048    protected final RandomDataImpl randomData = new RandomDataImpl();
049
050    /**
051     * RNG instance used to generate samples from the distribution.
052     * @since 3.1
053     */
054    protected final RandomGenerator random;
055
056    /**
057     * @deprecated As of 3.1, to be removed in 4.0. Please use
058     * {@link #AbstractIntegerDistribution(RandomGenerator)} instead.
059     */
060    @Deprecated
061    protected AbstractIntegerDistribution() {
062        // Legacy users are only allowed to access the deprecated "randomData".
063        // New users are forbidden to use this constructor.
064        random = null;
065    }
066
067    /**
068     * @param rng Random number generator.
069     * @since 3.1
070     */
071    protected AbstractIntegerDistribution(RandomGenerator rng) {
072        random = rng;
073    }
074
075    /**
076     * {@inheritDoc}
077     *
078     * The default implementation uses the identity
079     * <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p>
080     */
081    public double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException {
082        if (x1 < x0) {
083            throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
084                    x0, x1, true);
085        }
086        return cumulativeProbability(x1) - cumulativeProbability(x0);
087    }
088
089    /**
090     * {@inheritDoc}
091     *
092     * The default implementation returns
093     * <ul>
094     * <li>{@link #getSupportLowerBound()} for {@code p = 0},</li>
095     * <li>{@link #getSupportUpperBound()} for {@code p = 1}, and</li>
096     * <li>{@link #solveInverseCumulativeProbability(double, int, int)} for
097     *     {@code 0 < p < 1}.</li>
098     * </ul>
099     */
100    public int inverseCumulativeProbability(final double p) throws OutOfRangeException {
101        if (p < 0.0 || p > 1.0) {
102            throw new OutOfRangeException(p, 0, 1);
103        }
104
105        int lower = getSupportLowerBound();
106        if (p == 0.0) {
107            return lower;
108        }
109        if (lower == Integer.MIN_VALUE) {
110            if (checkedCumulativeProbability(lower) >= p) {
111                return lower;
112            }
113        } else {
114            lower -= 1; // this ensures cumulativeProbability(lower) < p, which
115                        // is important for the solving step
116        }
117
118        int upper = getSupportUpperBound();
119        if (p == 1.0) {
120            return upper;
121        }
122
123        // use the one-sided Chebyshev inequality to narrow the bracket
124        // cf. AbstractRealDistribution.inverseCumulativeProbability(double)
125        final double mu = getNumericalMean();
126        final double sigma = FastMath.sqrt(getNumericalVariance());
127        final boolean chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) ||
128                Double.isInfinite(sigma) || Double.isNaN(sigma) || sigma == 0.0);
129        if (chebyshevApplies) {
130            double k = FastMath.sqrt((1.0 - p) / p);
131            double tmp = mu - k * sigma;
132            if (tmp > lower) {
133                lower = ((int) Math.ceil(tmp)) - 1;
134            }
135            k = 1.0 / k;
136            tmp = mu + k * sigma;
137            if (tmp < upper) {
138                upper = ((int) Math.ceil(tmp)) - 1;
139            }
140        }
141
142        return solveInverseCumulativeProbability(p, lower, upper);
143    }
144
145    /**
146     * This is a utility function used by {@link
147     * #inverseCumulativeProbability(double)}. It assumes {@code 0 < p < 1} and
148     * that the inverse cumulative probability lies in the bracket {@code
149     * (lower, upper]}. The implementation does simple bisection to find the
150     * smallest {@code p}-quantile <code>inf{x in Z | P(X<=x) >= p}</code>.
151     *
152     * @param p the cumulative probability
153     * @param lower a value satisfying {@code cumulativeProbability(lower) < p}
154     * @param upper a value satisfying {@code p <= cumulativeProbability(upper)}
155     * @return the smallest {@code p}-quantile of this distribution
156     */
157    protected int solveInverseCumulativeProbability(final double p, int lower, int upper) {
158        while (lower + 1 < upper) {
159            int xm = (lower + upper) / 2;
160            if (xm < lower || xm > upper) {
161                /*
162                 * Overflow.
163                 * There will never be an overflow in both calculation methods
164                 * for xm at the same time
165                 */
166                xm = lower + (upper - lower) / 2;
167            }
168
169            double pm = checkedCumulativeProbability(xm);
170            if (pm >= p) {
171                upper = xm;
172            } else {
173                lower = xm;
174            }
175        }
176        return upper;
177    }
178
179    /** {@inheritDoc} */
180    public void reseedRandomGenerator(long seed) {
181        random.setSeed(seed);
182        randomData.reSeed(seed);
183    }
184
185    /**
186     * {@inheritDoc}
187     *
188     * The default implementation uses the
189     * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling">
190     * inversion method</a>.
191     */
192    public int sample() {
193        return inverseCumulativeProbability(random.nextDouble());
194    }
195
196    /**
197     * {@inheritDoc}
198     *
199     * The default implementation generates the sample by calling
200     * {@link #sample()} in a loop.
201     */
202    public int[] sample(int sampleSize) {
203        if (sampleSize <= 0) {
204            throw new NotStrictlyPositiveException(
205                    LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize);
206        }
207        int[] out = new int[sampleSize];
208        for (int i = 0; i < sampleSize; i++) {
209            out[i] = sample();
210        }
211        return out;
212    }
213
214    /**
215     * Computes the cumulative probability function and checks for {@code NaN}
216     * values returned. Throws {@code MathInternalError} if the value is
217     * {@code NaN}. Rethrows any exception encountered evaluating the cumulative
218     * probability function. Throws {@code MathInternalError} if the cumulative
219     * probability function returns {@code NaN}.
220     *
221     * @param argument input value
222     * @return the cumulative probability
223     * @throws MathInternalError if the cumulative probability is {@code NaN}
224     */
225    private double checkedCumulativeProbability(int argument)
226        throws MathInternalError {
227        double result = Double.NaN;
228        result = cumulativeProbability(argument);
229        if (Double.isNaN(result)) {
230            throw new MathInternalError(LocalizedFormats
231                    .DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument);
232        }
233        return result;
234    }
235
236    /**
237     * For a random variable {@code X} whose values are distributed according to
238     * this distribution, this method returns {@code log(P(X = x))}, where
239     * {@code log} is the natural logarithm. In other words, this method
240     * represents the logarithm of the probability mass function (PMF) for the
241     * distribution. Note that due to the floating point precision and
242     * under/overflow issues, this method will for some distributions be more
243     * precise and faster than computing the logarithm of
244     * {@link #probability(int)}.
245     * <p>
246     * The default implementation simply computes the logarithm of {@code probability(x)}.</p>
247     *
248     * @param x the point at which the PMF is evaluated
249     * @return the logarithm of the value of the probability mass function at {@code x}
250     */
251    public double logProbability(int x) {
252        return FastMath.log(probability(x));
253    }
254}