Class AbstractDiscreteDistribution
- All Implemented Interfaces:
DiscreteDistribution
- Direct Known Subclasses:
BinomialDistribution
,GeometricDistribution
,HypergeometricDistribution
,PascalDistribution
,PoissonDistribution
,UniformDiscreteDistribution
,ZipfDistribution
This base class provides a default factory method for creating
a sampler instance
that uses the
inversion method for generating random samples that follow the
distribution.
The class provides functionality to evaluate the probability in a range
using either the cumulative probability or the survival probability.
The survival probability is used if both arguments to
probability(int, int)
are above the median.
Child classes with a known median can override the default getMedian()
method.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.commons.statistics.distribution.DiscreteDistribution
DiscreteDistribution.Sampler
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateSampler
(org.apache.commons.rng.UniformRandomProvider rng) Creates a sampler.(package private) int
Gets the median.int
inverseCumulativeProbability
(double p) Computes the quantile function of this distribution.private int
inverseProbability
(double p, double q, boolean complement) Implementation for the inverse cumulative or survival probability.int
inverseSurvivalProbability
(double p) Computes the inverse survival probability function of this distribution.double
probability
(int x0, int x1) For a random variableX
whose values are distributed according to this distribution, this method returnsP(x0 < X <= x1)
.private static int
solveInverseProbability
(IntUnaryOperator fun, int lowerBound, int upperBound) This is a utility function used byinverseProbability(double, double, boolean)
.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.commons.statistics.distribution.DiscreteDistribution
cumulativeProbability, getMean, getSupportLowerBound, getSupportUpperBound, getVariance, logProbability, probability, survivalProbability
-
Field Details
-
NO_MEDIAN
private static final long NO_MEDIANMarker value for no median. This is a long to be outside the value of any possible int valued median.- See Also:
-
median
private long medianCached value of the median.
-
-
Constructor Details
-
AbstractDiscreteDistribution
AbstractDiscreteDistribution()
-
-
Method Details
-
getMedian
int getMedian()Gets the median. This is used to determine if the arguments to theprobability(int, int)
function are in the upper or lower domain.The default implementation calls
inverseCumulativeProbability(double)
with a value of 0.5.- Returns:
- the median
-
probability
public double probability(int x0, int x1) For a random variableX
whose values are distributed according to this distribution, this method returnsP(x0 < X <= x1)
. The default implementation uses the identityP(x0 < X <= x1) = P(X <= x1) - P(X <= x0)
Special cases:
- returns
0.0
ifx0 == x1
; - returns
probability(x1)
ifx0 + 1 == x1
;
- Specified by:
probability
in interfaceDiscreteDistribution
- Parameters:
x0
- Lower bound (exclusive).x1
- Upper bound (inclusive).- Returns:
- the probability that a random variable with this distribution
takes a value between
x0
andx1
, excluding the lower and including the upper endpoint.
- returns
-
inverseCumulativeProbability
public int inverseCumulativeProbability(double p) Computes the quantile function of this distribution. For a random variableX
distributed according to this distribution, the returned value is:\[ x = \begin{cases} \inf \{ x \in \mathbb Z : P(X \le x) \ge p\} & \text{for } 0 \lt p \le 1 \\ \inf \{ x \in \mathbb Z : P(X \le x) \gt 0 \} & \text{for } p = 0 \end{cases} \]
If the result exceeds the range of the data type
int
, thenInteger.MIN_VALUE
orInteger.MAX_VALUE
is returned. In this case the result ofcumulativeProbability(x)
called using the returnedp
-quantile may not compute the originalp
.The default implementation returns:
DiscreteDistribution.getSupportLowerBound()
forp = 0
,DiscreteDistribution.getSupportUpperBound()
forp = 1
, or- the result of a binary search between the lower and upper bound using
cumulativeProbability(x)
. The bounds may be bracketed for efficiency.
- Specified by:
inverseCumulativeProbability
in interfaceDiscreteDistribution
- Parameters:
p
- Cumulative probability.- Returns:
- the smallest
p
-quantile of this distribution (largest 0-quantile forp = 0
). - Throws:
IllegalArgumentException
- ifp < 0
orp > 1
-
inverseSurvivalProbability
public int inverseSurvivalProbability(double p) Computes the inverse survival probability function of this distribution. For a random variableX
distributed according to this distribution, the returned value is:\[ x = \begin{cases} \inf \{ x \in \mathbb Z : P(X \ge x) \le p\} & \text{for } 0 \le p \lt 1 \\ \inf \{ x \in \mathbb Z : P(X \ge x) \lt 1 \} & \text{for } p = 1 \end{cases} \]
If the result exceeds the range of the data type
int
, thenInteger.MIN_VALUE
orInteger.MAX_VALUE
is returned. In this case the result ofsurvivalProbability(x)
called using the returned(1-p)
-quantile may not compute the originalp
.By default, this is defined as
inverseCumulativeProbability(1 - p)
, but the specific implementation may be more accurate.The default implementation returns:
DiscreteDistribution.getSupportLowerBound()
forp = 1
,DiscreteDistribution.getSupportUpperBound()
forp = 0
, or- the result of a binary search between the lower and upper bound using
survivalProbability(x)
. The bounds may be bracketed for efficiency.
- Specified by:
inverseSurvivalProbability
in interfaceDiscreteDistribution
- Parameters:
p
- Cumulative probability.- Returns:
- the smallest
(1-p)
-quantile of this distribution (largest 0-quantile forp = 1
). - Throws:
IllegalArgumentException
- ifp < 0
orp > 1
-
inverseProbability
private int inverseProbability(double p, double q, boolean complement) Implementation for the inverse cumulative or survival probability.- Parameters:
p
- Cumulative probability.q
- Survival probability.complement
- Set to true to compute the inverse survival probability- Returns:
- the value
-
solveInverseProbability
This is a utility function used byinverseProbability(double, double, boolean)
. It assumes that the inverse probability lies in the bracket(lower, upper]
. The implementation does simple bisection to find the smallestx
such thatfun(x) >= 0
.- Parameters:
fun
- Probability function.lowerBound
- Value satisfyingfun(lower) < 0
.upperBound
- Value satisfyingfun(upper) >= 0
.- Returns:
- the smallest x
-
createSampler
Creates a sampler.- Specified by:
createSampler
in interfaceDiscreteDistribution
- Parameters:
rng
- Generator of uniformly distributed numbers.- Returns:
- a sampler that produces random numbers according this distribution.
-