Class TrapezoidalDistribution
- java.lang.Object
-
- org.apache.commons.statistics.distribution.TrapezoidalDistribution
-
- All Implemented Interfaces:
ContinuousDistribution
public abstract class TrapezoidalDistribution extends Object
Implementation of the trapezoidal distribution.The probability density function of \( X \) is:
\[ f(x; a, b, c, d) = \begin{cases} \frac{2}{d+c-a-b}\frac{x-a}{b-a} & \text{for } a\le x \lt b \\ \frac{2}{d+c-a-b} & \text{for } b\le x \lt c \\ \frac{2}{d+c-a-b}\frac{d-x}{d-c} & \text{for } c\le x \le d \end{cases} \]
for \( -\infty \lt a \le b \le c \le d \lt \infty \) and \( x \in [a, d] \).
Note the special cases:
- \( b = c \) is the triangular distribution
- \( a = b \) and \( c = d \) is the uniform distribution
- See Also:
- Trapezoidal distribution (Wikipedia)
-
-
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 protected doubleaLower limit of this distribution (inclusive).protected doublebStart of the trapezoid constant density.protected doublecEnd of the trapezoid constant density.protected doubledUpper limit of this distribution (inclusive).
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description ContinuousDistribution.SamplercreateSampler(org.apache.commons.rng.UniformRandomProvider rng)Creates a sampler.doublegetB()Gets the start of the constant region of the density function.doublegetC()Gets the end of the constant region of the density function.abstract doublegetMean()Gets the mean of this distribution.doublegetSupportLowerBound()Gets the lower bound of the support.doublegetSupportUpperBound()Gets the upper bound of the support.abstract doublegetVariance()Gets the variance of this distribution.doubleinverseCumulativeProbability(double p)Computes the quantile function of this distribution.doubleinverseSurvivalProbability(double p)Computes the inverse survival probability function of this distribution.static TrapezoidalDistributionof(double a, double b, double c, double d)Creates a trapezoidal distribution.doubleprobability(double x0, double x1)For a random variableXwhose values are distributed according to this distribution, this method returnsP(x0 < X <= x1).-
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, logDensity, survivalProbability
-
-
-
-
Method Detail
-
of
public static TrapezoidalDistribution of(double a, double b, double c, double d)
Creates a trapezoidal distribution.The distribution density is represented as an up sloping line from
atob, constant frombtoc, and then a down sloping line fromctod.- Parameters:
a- Lower limit of this distribution (inclusive).b- Start of the trapezoid constant density (first shape parameter).c- End of the trapezoid constant density (second shape parameter).d- Upper limit of this distribution (inclusive).- Returns:
- the distribution
- Throws:
IllegalArgumentException- ifa >= d, ifb < a, ifc < bor ifc > d.
-
getMean
public abstract double getMean()
Gets the mean of this distribution.For lower limit \( a \), start of the density constant region \( b \), end of the density constant region \( c \) and upper limit \( d \), the mean is:
\[ \frac{1}{3(d+c-b-a)}\left(\frac{d^3-c^3}{d-c}-\frac{b^3-a^3}{b-a}\right) \]
- Returns:
- the mean.
-
getVariance
public abstract double getVariance()
Gets the variance of this distribution.For lower limit \( a \), start of the density constant region \( b \), end of the density constant region \( c \) and upper limit \( d \), the variance is:
\[ \frac{1}{6(d+c-b-a)}\left(\frac{d^4-c^4}{d-c}-\frac{b^4-a^4}{b-a}\right) - \mu^2 \]
where \( \mu \) is the mean.
- Returns:
- the variance.
-
getB
public double getB()
Gets the start of the constant region of the density function.This is the first shape parameter
bof the distribution.- Returns:
- the first shape parameter
b
-
getC
public double getC()
Gets the end of the constant region of the density function.This is the second shape parameter
cof the distribution.- Returns:
- the second shape parameter
c
-
getSupportLowerBound
public double getSupportLowerBound()
Gets the lower bound of the support. It must return the same value asinverseCumulativeProbability(0), i.e. \( \inf \{ x \in \mathbb R : P(X \le x) \gt 0 \} \).The lower bound of the support is equal to the lower limit parameter
aof the distribution.- Returns:
- the lower bound of the support.
-
getSupportUpperBound
public double getSupportUpperBound()
Gets the upper bound of the support. It must return the same value asinverseCumulativeProbability(1), i.e. \( \inf \{ x \in \mathbb R : P(X \le x) = 1 \} \).The upper bound of the support is equal to the upper limit parameter
dof the distribution.- Returns:
- the upper bound of the support.
-
probability
public double probability(double x0, double x1)For 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:
probabilityin interfaceContinuousDistribution- Parameters:
x0- Lower bound (exclusive).x1- Upper bound (inclusive).- Returns:
- the probability that a random variable with this distribution
takes a value between
x0andx1, excluding the lower and including the upper endpoint.
-
inverseCumulativeProbability
public double inverseCumulativeProbability(double p)
Computes the quantile function of this distribution. For a random variableXdistributed 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:
inverseCumulativeProbabilityin interfaceContinuousDistribution- Parameters:
p- Cumulative probability.- Returns:
- the smallest
p-quantile of this distribution (largest 0-quantile forp = 0). - Throws:
IllegalArgumentException- ifp < 0orp > 1
-
inverseSurvivalProbability
public double inverseSurvivalProbability(double p)
Computes the inverse survival probability function of this distribution. For a random variableXdistributed 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:
inverseSurvivalProbabilityin interfaceContinuousDistribution- Parameters:
p- Survival probability.- Returns:
- the smallest
(1-p)-quantile of this distribution (largest 0-quantile forp = 1). - Throws:
IllegalArgumentException- ifp < 0orp > 1
-
createSampler
public ContinuousDistribution.Sampler createSampler(org.apache.commons.rng.UniformRandomProvider rng)
Creates a sampler.- Specified by:
createSamplerin interfaceContinuousDistribution- Parameters:
rng- Generator of uniformly distributed numbers.- Returns:
- a sampler that produces random numbers according this distribution.
-
-