ExponentialDistribution.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.statistics.distribution;

  18. import org.apache.commons.rng.UniformRandomProvider;
  19. import org.apache.commons.rng.sampling.distribution.ZigguratSampler;

  20. /**
  21.  * Implementation of the exponential distribution.
  22.  *
  23.  * <p>The probability density function of \( X \) is:
  24.  *
  25.  * <p>\[ f(x; \mu) = \frac{1}{\mu} e^{-x / \mu} \]
  26.  *
  27.  * <p>for \( \mu &gt; 0 \) the mean and
  28.  * \( x \in [0, \infty) \).
  29.  *
  30.  * <p>This implementation uses the scale parameter \( \mu \) which is the mean of the distribution.
  31.  * A common alternative parameterization uses the rate parameter \( \lambda \) which is the reciprocal
  32.  * of the mean. The distribution can be be created using \( \mu  = \frac{1}{\lambda} \).
  33.  *
  34.  * @see <a href="https://en.wikipedia.org/wiki/Exponential_distribution">Exponential distribution (Wikipedia)</a>
  35.  * @see <a href="https://mathworld.wolfram.com/ExponentialDistribution.html">Exponential distribution (MathWorld)</a>
  36.  */
  37. public final class ExponentialDistribution extends AbstractContinuousDistribution {
  38.     /** Support lower bound. */
  39.     private static final double SUPPORT_LO = 0;
  40.     /** Support upper bound. */
  41.     private static final double SUPPORT_HI = Double.POSITIVE_INFINITY;
  42.     /** The mean of this distribution. */
  43.     private final double mean;
  44.     /** The logarithm of the mean, stored to reduce computing time. */
  45.     private final double logMean;

  46.     /**
  47.      * @param mean Mean of this distribution.
  48.      */
  49.     private ExponentialDistribution(double mean) {
  50.         this.mean = mean;
  51.         logMean = Math.log(mean);
  52.     }

  53.     /**
  54.      * Creates an exponential distribution.
  55.      *
  56.      * @param mean Mean of this distribution. This is a scale parameter.
  57.      * @return the distribution
  58.      * @throws IllegalArgumentException if {@code mean <= 0}.
  59.      */
  60.     public static ExponentialDistribution of(double mean) {
  61.         if (mean <= 0) {
  62.             throw new DistributionException(DistributionException.NOT_STRICTLY_POSITIVE, mean);
  63.         }
  64.         return new ExponentialDistribution(mean);
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public double density(double x) {
  69.         if (x < SUPPORT_LO) {
  70.             return 0;
  71.         }
  72.         return Math.exp(-x / mean) / mean;
  73.     }

  74.     /** {@inheritDoc} **/
  75.     @Override
  76.     public double logDensity(double x) {
  77.         if (x < SUPPORT_LO) {
  78.             return Double.NEGATIVE_INFINITY;
  79.         }
  80.         return -x / mean - logMean;
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public double cumulativeProbability(double x)  {
  85.         if (x <= SUPPORT_LO) {
  86.             return 0;
  87.         }
  88.         return -Math.expm1(-x / mean);
  89.     }

  90.     /** {@inheritDoc} */
  91.     @Override
  92.     public double survivalProbability(double x)  {
  93.         if (x <= SUPPORT_LO) {
  94.             return 1;
  95.         }
  96.         return Math.exp(-x / mean);
  97.     }

  98.     /**
  99.      * {@inheritDoc}
  100.      *
  101.      * <p>Returns {@code 0} when {@code p == 0} and
  102.      * {@link Double#POSITIVE_INFINITY} when {@code p == 1}.
  103.      */
  104.     @Override
  105.     public double inverseCumulativeProbability(double p) {
  106.         ArgumentUtils.checkProbability(p);
  107.         if (p == 1) {
  108.             return Double.POSITIVE_INFINITY;
  109.         }
  110.         // Subtract from zero to prevent returning -0.0 for p=-0.0
  111.         return 0 - mean * Math.log1p(-p);
  112.     }

  113.     /**
  114.      * {@inheritDoc}
  115.      *
  116.      * <p>Returns {@code 0} when {@code p == 1} and
  117.      * {@link Double#POSITIVE_INFINITY} when {@code p == 0}.
  118.      */
  119.     @Override
  120.     public double inverseSurvivalProbability(double p) {
  121.         ArgumentUtils.checkProbability(p);
  122.         if (p == 0) {
  123.             return Double.POSITIVE_INFINITY;
  124.         }
  125.         // Subtract from zero to prevent returning -0.0 for p=1
  126.         return 0 - mean * Math.log(p);
  127.     }

  128.     /** {@inheritDoc} */
  129.     @Override
  130.     public double getMean() {
  131.         return mean;
  132.     }

  133.     /**
  134.      * {@inheritDoc}
  135.      *
  136.      * <p>For mean \( \mu \), the variance is \( \mu^2 \).
  137.      */
  138.     @Override
  139.     public double getVariance() {
  140.         return mean * mean;
  141.     }

  142.     /**
  143.      * {@inheritDoc}
  144.      *
  145.      * <p>The lower bound of the support is always 0.
  146.      *
  147.      * @return 0.
  148.      */
  149.     @Override
  150.     public double getSupportLowerBound() {
  151.         return SUPPORT_LO;
  152.     }

  153.     /**
  154.      * {@inheritDoc}
  155.      *
  156.      * <p>The upper bound of the support is always positive infinity.
  157.      *
  158.      * @return {@linkplain Double#POSITIVE_INFINITY positive infinity}.
  159.      */
  160.     @Override
  161.     public double getSupportUpperBound() {
  162.         return SUPPORT_HI;
  163.     }

  164.     /** {@inheritDoc} */
  165.     @Override
  166.     double getMedian() {
  167.         // Overridden for the probability(double, double) method.
  168.         // This is intentionally not a public method.
  169.         // ln(2) / rate = mean * ln(2)
  170.         return mean * Constants.LN_TWO;
  171.     }

  172.     /** {@inheritDoc} */
  173.     @Override
  174.     public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
  175.         // Exponential distribution sampler.
  176.         return ZigguratSampler.Exponential.of(rng, getMean())::sample;
  177.     }
  178. }