Class AbstractContinuousDistribution
- java.lang.Object
-
- org.apache.commons.statistics.distribution.AbstractContinuousDistribution
-
- All Implemented Interfaces:
ContinuousDistribution
- Direct Known Subclasses:
BetaDistribution
,CauchyDistribution
,ChiSquaredDistribution
,ExponentialDistribution
,FDistribution
,FoldedNormalDistribution
,GammaDistribution
,GumbelDistribution
,LaplaceDistribution
,LevyDistribution
,LogisticDistribution
,LogNormalDistribution
,LogUniformDistribution
,NakagamiDistribution
,NormalDistribution
,ParetoDistribution
,TDistribution
,TrapezoidalDistribution
,TriangularDistribution
,TruncatedNormalDistribution
,UniformContinuousDistribution
,WeibullDistribution
abstract class AbstractContinuousDistribution extends java.lang.Object implements ContinuousDistribution
Base class for probability distributions on the reals. Default implementations are provided for some of the methods that do not vary from distribution to distribution.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(double, double)
are above the median. Child classes with a known median can override the defaultgetMedian()
method.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.commons.statistics.distribution.ContinuousDistribution
ContinuousDistribution.Sampler
-
-
Field Summary
Fields Modifier and Type Field Description private double
median
Cached value of the median.private static double
SOLVER_ABSOLUTE_ACCURACY
BrentSolver absolute accuracy.private static double
SOLVER_FUNCTION_VALUE_ACCURACY
BrentSolver function value accuracy.private static double
SOLVER_RELATIVE_ACCURACY
BrentSolver relative accuracy.
-
Constructor Summary
Constructors Constructor Description AbstractContinuousDistribution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private double
createFiniteLowerBound(double p, double q, boolean complement, double upperBound, double mu, double sig, boolean chebyshevApplies)
Create a finite lower bound.private double
createFiniteUpperBound(double p, double q, boolean complement, double lowerBound, double mu, double sig, boolean chebyshevApplies)
Create a finite upper bound.ContinuousDistribution.Sampler
createSampler(org.apache.commons.rng.UniformRandomProvider rng)
Creates a sampler.(package private) double
getMedian()
Gets the median.double
inverseCumulativeProbability(double p)
Computes the quantile function of this distribution.private double
inverseProbability(double p, double q, boolean complement)
Implementation for the inverse cumulative or survival probability.double
inverseSurvivalProbability(double p)
Computes the inverse survival probability function of this distribution.(package private) boolean
isSupportConnected()
Indicates whether the support is connected, i.e.double
probability(double x0, double x1)
For a random variableX
whose values are distributed according to this distribution, this method returnsP(x0 < X <= x1)
.private double
searchPlateau(boolean complement, double lower, double x)
Test the probability function for a plateau at the point x.-
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.ContinuousDistribution
cumulativeProbability, density, getMean, getSupportLowerBound, getSupportUpperBound, getVariance, logDensity, survivalProbability
-
-
-
-
Field Detail
-
SOLVER_RELATIVE_ACCURACY
private static final double SOLVER_RELATIVE_ACCURACY
BrentSolver relative accuracy. This is used withtol = 2 * relEps * abs(b) + absEps
so the minimum non-zero value with an effect is half of machine epsilon (2^-53).- See Also:
- Constant Field Values
-
SOLVER_ABSOLUTE_ACCURACY
private static final double SOLVER_ABSOLUTE_ACCURACY
BrentSolver absolute accuracy. This is used withtol = 2 * relEps * abs(b) + absEps
so set to MIN_VALUE so that when the relative epsilon has no effect (as b is too small) the tolerance is at least 1 ULP for sub-normal numbers.- See Also:
- Constant Field Values
-
SOLVER_FUNCTION_VALUE_ACCURACY
private static final double SOLVER_FUNCTION_VALUE_ACCURACY
BrentSolver function value accuracy. Determines if the Brent solver performs a search. It is not used during the search. Set to a very low value to search using Brent's method unless the starting point is correct, or within 1 ULP for sub-normal probabilities.- See Also:
- Constant Field Values
-
median
private double median
Cached value of the median.
-
-
Method Detail
-
getMedian
double getMedian()
Gets the median. This is used to determine if the arguments to theprobability(double, double)
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(double x0, double 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)
- Specified by:
probability
in interfaceContinuousDistribution
- 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.
-
inverseCumulativeProbability
public double 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 R : P(X \le x) \ge p\} & \text{for } 0 \lt p \le 1 \\ \inf \{ x \in \mathbb R : P(X \le x) \gt 0 \} & \text{for } p = 0 \end{cases} \]
The default implementation returns:
ContinuousDistribution.getSupportLowerBound()
forp = 0
,ContinuousDistribution.getSupportUpperBound()
forp = 1
, or- the result of a search for a root between the lower and upper bound using
cumulativeProbability(x) - p
. The bounds may be bracketed for efficiency.
- Specified by:
inverseCumulativeProbability
in interfaceContinuousDistribution
- Parameters:
p
- Cumulative probability.- Returns:
- the smallest
p
-quantile of this distribution (largest 0-quantile forp = 0
). - Throws:
java.lang.IllegalArgumentException
- ifp < 0
orp > 1
-
inverseSurvivalProbability
public double 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 R : P(X \gt x) \le p\} & \text{for } 0 \le p \lt 1 \\ \inf \{ x \in \mathbb R : P(X \gt x) \lt 1 \} & \text{for } p = 1 \end{cases} \]
By default, this is defined as
inverseCumulativeProbability(1 - p)
, but the specific implementation may be more accurate.The default implementation returns:
ContinuousDistribution.getSupportLowerBound()
forp = 1
,ContinuousDistribution.getSupportUpperBound()
forp = 0
, or- the result of a search for a root between the lower and upper bound using
survivalProbability(x) - p
. The bounds may be bracketed for efficiency.
- Specified by:
inverseSurvivalProbability
in interfaceContinuousDistribution
- Parameters:
p
- Survival probability.- Returns:
- the smallest
(1-p)
-quantile of this distribution (largest 0-quantile forp = 1
). - Throws:
java.lang.IllegalArgumentException
- ifp < 0
orp > 1
-
inverseProbability
private double 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
-
createFiniteLowerBound
private double createFiniteLowerBound(double p, double q, boolean complement, double upperBound, double mu, double sig, boolean chebyshevApplies)
Create a finite lower bound. Assumes the current lower bound is negative infinity.- Parameters:
p
- Cumulative probability.q
- Survival probability.complement
- Set to true to compute the inverse survival probabilityupperBound
- Current upper boundmu
- Meansig
- Standard deviationchebyshevApplies
- True if the Chebyshev inequality applies (mean is finite andsig > 0
}- Returns:
- the finite lower bound
-
createFiniteUpperBound
private double createFiniteUpperBound(double p, double q, boolean complement, double lowerBound, double mu, double sig, boolean chebyshevApplies)
Create a finite upper bound. Assumes the current upper bound is positive infinity.- Parameters:
p
- Cumulative probability.q
- Survival probability.complement
- Set to true to compute the inverse survival probabilitylowerBound
- Current lower boundmu
- Meansig
- Standard deviationchebyshevApplies
- True if the Chebyshev inequality applies (mean is finite andsig > 0
}- Returns:
- the finite lower bound
-
isSupportConnected
boolean isSupportConnected()
Indicates whether the support is connected, i.e. whether all values between the lower and upper bound of the support are included in the support.This method is used in the default implementation of the inverse cumulative and survival probability functions.
The default value is true which assumes the cdf and sf have no plateau regions where the same probability value is returned for a large range of x. Override this method if there are gaps in the support of the cdf and sf.
If false then the inverse will perform an additional step to ensure that the lower-bound of the interval on which the cdf is constant should be returned. This will search from the initial point x downwards if a smaller value also has the same cumulative (survival) probability.
Any plateau with a width in x smaller than the inverse absolute accuracy will not be searched.
Note: This method was public in commons math. It has been reduced to package private in commons statistics as it is an implementation detail.
- Returns:
- whether the support is connected.
- See Also:
- MATH-699
-
searchPlateau
private double searchPlateau(boolean complement, double lower, double x)
Test the probability function for a plateau at the point x. If detected search the plateau for the lowest point y such thatinf{y in R | P(y) == P(x)}
.This function is used when the distribution support is not connected to satisfy the inverse probability requirements of
ContinuousDistribution
on the returned value.- Parameters:
complement
- Set to true to search the survival probability.lower
- Lower bound used to limit the search downwards.x
- Current value.- Returns:
- the infimum y
-
createSampler
public ContinuousDistribution.Sampler createSampler(org.apache.commons.rng.UniformRandomProvider rng)
Creates a sampler.- Specified by:
createSampler
in interfaceContinuousDistribution
- Parameters:
rng
- Generator of uniformly distributed numbers.- Returns:
- a sampler that produces random numbers according this distribution.
-
-