1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.math4.legacy.distribution;
18
19 import org.apache.commons.statistics.distribution.DiscreteDistribution;
20 import org.apache.commons.math4.legacy.exception.MathInternalError;
21 import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
22 import org.apache.commons.math4.legacy.exception.OutOfRangeException;
23 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
24 import org.apache.commons.rng.UniformRandomProvider;
25 import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler;
26 import org.apache.commons.math4.core.jdkmath.JdkMath;
27
28 /**
29 * Base class for integer-valued discrete distributions. Default
30 * implementations are provided for some of the methods that do not vary
31 * from distribution to distribution.
32 *
33 */
34 public abstract class AbstractIntegerDistribution
35 implements DiscreteDistribution {
36 /**
37 * {@inheritDoc}
38 *
39 * The default implementation uses the identity
40 * <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p>
41 *
42 * @since 4.0, was previously named cumulativeProbability
43 */
44 @Override
45 public double probability(int x0, int x1) throws NumberIsTooLargeException {
46 if (x1 < x0) {
47 throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
48 x0, x1, true);
49 }
50 return cumulativeProbability(x1) - cumulativeProbability(x0);
51 }
52
53 /**
54 * {@inheritDoc}
55 *
56 * The default implementation returns
57 * <ul>
58 * <li>{@link #getSupportLowerBound()} for {@code p = 0},</li>
59 * <li>{@link #getSupportUpperBound()} for {@code p = 1}, and</li>
60 * <li>{@link #solveInverseCumulativeProbability(double, int, int)} for
61 * {@code 0 < p < 1}.</li>
62 * </ul>
63 */
64 @Override
65 public int inverseCumulativeProbability(final double p) throws OutOfRangeException {
66 if (p < 0.0 || p > 1.0) {
67 throw new OutOfRangeException(p, 0, 1);
68 }
69
70 int lower = getSupportLowerBound();
71 if (p == 0.0) {
72 return lower;
73 }
74 if (lower == Integer.MIN_VALUE) {
75 if (checkedCumulativeProbability(lower) >= p) {
76 return lower;
77 }
78 } else {
79 lower -= 1; // this ensures cumulativeProbability(lower) < p, which
80 // is important for the solving step
81 }
82
83 int upper = getSupportUpperBound();
84 if (p == 1.0) {
85 return upper;
86 }
87
88 // use the one-sided Chebyshev inequality to narrow the bracket
89 // cf. AbstractRealDistribution.inverseCumulativeProbability(double)
90 final double mu = getMean();
91 final double sigma = JdkMath.sqrt(getVariance());
92 final boolean chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) ||
93 Double.isInfinite(sigma) || Double.isNaN(sigma) || sigma == 0.0);
94 if (chebyshevApplies) {
95 double k = JdkMath.sqrt((1.0 - p) / p);
96 double tmp = mu - k * sigma;
97 if (tmp > lower) {
98 lower = ((int) JdkMath.ceil(tmp)) - 1;
99 }
100 k = 1.0 / k;
101 tmp = mu + k * sigma;
102 if (tmp < upper) {
103 upper = ((int) JdkMath.ceil(tmp)) - 1;
104 }
105 }
106
107 return solveInverseCumulativeProbability(p, lower, upper);
108 }
109
110 /**
111 * This is a utility function used by {@link
112 * #inverseCumulativeProbability(double)}. It assumes {@code 0 < p < 1} and
113 * that the inverse cumulative probability lies in the bracket {@code
114 * (lower, upper]}. The implementation does simple bisection to find the
115 * smallest {@code p}-quantile {@code inf{x in Z | P(X<=x) >= p}}.
116 *
117 * @param p the cumulative probability
118 * @param lower a value satisfying {@code cumulativeProbability(lower) < p}
119 * @param upper a value satisfying {@code p <= cumulativeProbability(upper)}
120 * @return the smallest {@code p}-quantile of this distribution
121 */
122 protected int solveInverseCumulativeProbability(final double p, int lower, int upper) {
123 while (lower + 1 < upper) {
124 int xm = (lower + upper) / 2;
125 if (xm < lower || xm > upper) {
126 /*
127 * Overflow.
128 * There will never be an overflow in both calculation methods
129 * for xm at the same time
130 */
131 xm = lower + (upper - lower) / 2;
132 }
133
134 double pm = checkedCumulativeProbability(xm);
135 if (pm >= p) {
136 upper = xm;
137 } else {
138 lower = xm;
139 }
140 }
141 return upper;
142 }
143
144 /**
145 * Computes the cumulative probability function and checks for {@code NaN}
146 * values returned. Throws {@code MathInternalError} if the value is
147 * {@code NaN}. Rethrows any exception encountered evaluating the cumulative
148 * probability function. Throws {@code MathInternalError} if the cumulative
149 * probability function returns {@code NaN}.
150 *
151 * @param argument input value
152 * @return the cumulative probability
153 * @throws MathInternalError if the cumulative probability is {@code NaN}
154 */
155 private double checkedCumulativeProbability(int argument)
156 throws MathInternalError {
157 final double result = cumulativeProbability(argument);
158 if (Double.isNaN(result)) {
159 throw new MathInternalError(LocalizedFormats
160 .DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument);
161 }
162 return result;
163 }
164
165 /**
166 * {@inheritDoc}
167 * <p>
168 * The default implementation simply computes the logarithm of {@code probability(x)}.
169 */
170 @Override
171 public double logProbability(int x) {
172 return JdkMath.log(probability(x));
173 }
174
175 /**
176 * Utility function for allocating an array and filling it with {@code n}
177 * samples generated by the given {@code sampler}.
178 *
179 * @param n Number of samples.
180 * @param sampler Sampler.
181 * @return an array of size {@code n}.
182 */
183 public static int[] sample(int n,
184 DiscreteDistribution.Sampler sampler) {
185 final int[] samples = new int[n];
186 for (int i = 0; i < n; i++) {
187 samples[i] = sampler.sample();
188 }
189 return samples;
190 }
191
192 /**{@inheritDoc} */
193 @Override
194 public DiscreteDistribution.Sampler createSampler(final UniformRandomProvider rng) {
195 // Inversion method distribution sampler.
196 return InverseTransformDiscreteSampler.of(rng, this::inverseCumulativeProbability)::sample;
197 }
198 }