Class AbstractIntegerDistribution
- java.lang.Object
-
- org.apache.commons.math3.distribution.AbstractIntegerDistribution
-
- All Implemented Interfaces:
java.io.Serializable,IntegerDistribution
- Direct Known Subclasses:
BinomialDistribution,EnumeratedIntegerDistribution,GeometricDistribution,HypergeometricDistribution,PascalDistribution,PoissonDistribution,UniformIntegerDistribution,ZipfDistribution
public abstract class AbstractIntegerDistribution extends java.lang.Object implements IntegerDistribution, java.io.Serializable
Base class for integer-valued discrete distributions. Default implementations are provided for some of the methods that do not vary from distribution to distribution.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected RandomGeneratorrandomRNG instance used to generate samples from the distribution.protected RandomDataImplrandomDataDeprecated.As of 3.1, to be removed in 4.0.private static longserialVersionUIDSerializable version identifier
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractIntegerDistribution()Deprecated.As of 3.1, to be removed in 4.0.protectedAbstractIntegerDistribution(RandomGenerator rng)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private doublecheckedCumulativeProbability(int argument)Computes the cumulative probability function and checks forNaNvalues returned.doublecumulativeProbability(int x0, int x1)For a random variableXwhose values are distributed according to this distribution, this method returnsP(x0 < X <= x1).intinverseCumulativeProbability(double p)Computes the quantile function of this distribution.doublelogProbability(int x)For a random variableXwhose values are distributed according to this distribution, this method returnslog(P(X = x)), wherelogis the natural logarithm.voidreseedRandomGenerator(long seed)Reseed the random generator used to generate samples.intsample()Generate a random value sampled from this distribution.int[]sample(int sampleSize)Generate a random sample from the distribution.protected intsolveInverseCumulativeProbability(double p, int lower, int upper)This is a utility function used byinverseCumulativeProbability(double).-
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.math3.distribution.IntegerDistribution
cumulativeProbability, getNumericalMean, getNumericalVariance, getSupportLowerBound, getSupportUpperBound, isSupportConnected, probability
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serializable version identifier- See Also:
- Constant Field Values
-
randomData
@Deprecated protected final RandomDataImpl randomData
Deprecated.As of 3.1, to be removed in 4.0. Please use therandominstance variable instead.RandomData instance used to generate samples from the distribution.
-
random
protected final RandomGenerator random
RNG instance used to generate samples from the distribution.- Since:
- 3.1
-
-
Constructor Detail
-
AbstractIntegerDistribution
@Deprecated protected AbstractIntegerDistribution()
Deprecated.As of 3.1, to be removed in 4.0. Please useAbstractIntegerDistribution(RandomGenerator)instead.
-
AbstractIntegerDistribution
protected AbstractIntegerDistribution(RandomGenerator rng)
- Parameters:
rng- Random number generator.- Since:
- 3.1
-
-
Method Detail
-
cumulativeProbability
public double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeExceptionFor a random variableXwhose 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)- Specified by:
cumulativeProbabilityin interfaceIntegerDistribution- Parameters:
x0- the exclusive lower boundx1- the inclusive upper bound- Returns:
- the probability that a random variable with this distribution
will take a value between
x0andx1, excluding the lower and including the upper endpoint - Throws:
NumberIsTooLargeException- ifx0 > x1
-
inverseCumulativeProbability
public int inverseCumulativeProbability(double p) throws OutOfRangeExceptionComputes the quantile function of this distribution. For a random variableXdistributed according to this distribution, the returned value isinf{x in Z | P(X<=x) >= p}for0 < p <= 1,inf{x in Z | P(X<=x) > 0}forp = 0.
int, thenInteger.MIN_VALUEorInteger.MAX_VALUEis returned. The default implementation returnsIntegerDistribution.getSupportLowerBound()forp = 0,IntegerDistribution.getSupportUpperBound()forp = 1, andsolveInverseCumulativeProbability(double, int, int)for0 < p < 1.
- Specified by:
inverseCumulativeProbabilityin interfaceIntegerDistribution- Parameters:
p- the cumulative probability- Returns:
- the smallest
p-quantile of this distribution (largest 0-quantile forp = 0) - Throws:
OutOfRangeException- ifp < 0orp > 1
-
solveInverseCumulativeProbability
protected int solveInverseCumulativeProbability(double p, int lower, int upper)This is a utility function used byinverseCumulativeProbability(double). It assumes0 < p < 1and that the inverse cumulative probability lies in the bracket(lower, upper]. The implementation does simple bisection to find the smallestp-quantileinf{x in Z | P(X<=x) >= p}.- Parameters:
p- the cumulative probabilitylower- a value satisfyingcumulativeProbability(lower) < pupper- a value satisfyingp <= cumulativeProbability(upper)- Returns:
- the smallest
p-quantile of this distribution
-
reseedRandomGenerator
public void reseedRandomGenerator(long seed)
Reseed the random generator used to generate samples.- Specified by:
reseedRandomGeneratorin interfaceIntegerDistribution- Parameters:
seed- the new seed
-
sample
public int sample()
Generate a random value sampled from this distribution. The default implementation uses the inversion method.- Specified by:
samplein interfaceIntegerDistribution- Returns:
- a random value
-
sample
public int[] sample(int sampleSize)
Generate a random sample from the distribution. The default implementation generates the sample by callingsample()in a loop.- Specified by:
samplein interfaceIntegerDistribution- Parameters:
sampleSize- the number of random values to generate- Returns:
- an array representing the random sample
-
checkedCumulativeProbability
private double checkedCumulativeProbability(int argument) throws MathInternalErrorComputes the cumulative probability function and checks forNaNvalues returned. ThrowsMathInternalErrorif the value isNaN. Rethrows any exception encountered evaluating the cumulative probability function. ThrowsMathInternalErrorif the cumulative probability function returnsNaN.- Parameters:
argument- input value- Returns:
- the cumulative probability
- Throws:
MathInternalError- if the cumulative probability isNaN
-
logProbability
public double logProbability(int x)
For a random variableXwhose values are distributed according to this distribution, this method returnslog(P(X = x)), wherelogis the natural logarithm. In other words, this method represents the logarithm of the probability mass function (PMF) for the distribution. Note that due to the floating point precision and under/overflow issues, this method will for some distributions be more precise and faster than computing the logarithm ofIntegerDistribution.probability(int).The default implementation simply computes the logarithm of
probability(x).- Parameters:
x- the point at which the PMF is evaluated- Returns:
- the logarithm of the value of the probability mass function at
x
-
-