Class AbstractContinuousDistribution

java.lang.Object
org.apache.commons.statistics.distribution.AbstractContinuousDistribution
All Implemented Interfaces:
ContinuousDistribution
Direct Known Subclasses:
BetaDistribution, CauchyDistribution, ChiSquaredDistribution, ExponentialDistribution, FDistribution, GammaDistribution, GumbelDistribution, LaplaceDistribution, LevyDistribution, LogisticDistribution, LogNormalDistribution, NakagamiDistribution, NormalDistribution, ParetoDistribution, TDistribution, TrapezoidalDistribution, TriangularDistribution, TruncatedNormalDistribution, UniformContinuousDistribution, WeibullDistribution

abstract class AbstractContinuousDistribution extends 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 default getMedian() method.

  • Field Details

    • SOLVER_RELATIVE_ACCURACY

      private static final double SOLVER_RELATIVE_ACCURACY
      BrentSolver relative accuracy. This is used with tol = 2 * relEps * abs(b) + absEps so the minimum non-zero value with an effect is half of machine epsilon (2^-53).
      See Also:
    • SOLVER_ABSOLUTE_ACCURACY

      private static final double SOLVER_ABSOLUTE_ACCURACY
      BrentSolver absolute accuracy. This is used with tol = 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:
    • 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:
    • median

      private double median
      Cached value of the median.
  • Constructor Details

    • AbstractContinuousDistribution

      AbstractContinuousDistribution()
  • Method Details

    • getMedian

      double getMedian()
      Gets the median. This is used to determine if the arguments to the probability(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 variable X whose values are distributed according to this distribution, this method returns P(x0 < X <= x1). The default implementation uses the identity P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)
      Specified by:
      probability in interface ContinuousDistribution
      Parameters:
      x0 - Lower bound (exclusive).
      x1 - Upper bound (inclusive).
      Returns:
      the probability that a random variable with this distribution takes a value between x0 and x1, excluding the lower and including the upper endpoint.
    • inverseCumulativeProbability

      public double inverseCumulativeProbability(double p)
      Computes the quantile function of this distribution. For a random variable X 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:

      Specified by:
      inverseCumulativeProbability in interface ContinuousDistribution
      Parameters:
      p - Cumulative probability.
      Returns:
      the smallest p-quantile of this distribution (largest 0-quantile for p = 0).
      Throws:
      IllegalArgumentException - if p < 0 or p > 1
    • inverseSurvivalProbability

      public double inverseSurvivalProbability(double p)
      Computes the inverse survival probability function of this distribution. For a random variable X distributed according to this distribution, the returned value is:

      \[ x = \begin{cases} \inf \{ x \in \mathbb R : P(X \ge x) \le p\} & \text{for } 0 \le p \lt 1 \\ \inf \{ x \in \mathbb R : P(X \ge 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:

      Specified by:
      inverseSurvivalProbability in interface ContinuousDistribution
      Parameters:
      p - Survival probability.
      Returns:
      the smallest (1-p)-quantile of this distribution (largest 0-quantile for p = 1).
      Throws:
      IllegalArgumentException - if p < 0 or p > 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 probability
      upperBound - Current upper bound
      mu - Mean
      sig - Standard deviation
      chebyshevApplies - True if the Chebyshev inequality applies (mean is finite and sig > 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 probability
      lowerBound - Current lower bound
      mu - Mean
      sig - Standard deviation
      chebyshevApplies - True if the Chebyshev inequality applies (mean is finite and sig > 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:
    • 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 that inf{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 interface ContinuousDistribution
      Parameters:
      rng - Generator of uniformly distributed numbers.
      Returns:
      a sampler that produces random numbers according this distribution.