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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.distribution.continuous;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.util.FastMath;
  25. import org.hipparchus.util.MathUtils;

  26. /**
  27.  * Implementation of the exponential distribution.
  28.  *
  29.  * @see <a href="http://en.wikipedia.org/wiki/Exponential_distribution">Exponential distribution (Wikipedia)</a>
  30.  * @see <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">Exponential distribution (MathWorld)</a>
  31.  */
  32. public class ExponentialDistribution extends AbstractRealDistribution {
  33.     /** Serializable version identifier */
  34.     private static final long serialVersionUID = 20160320L;
  35.     /** The mean of this distribution. */
  36.     private final double mean;
  37.     /** The logarithm of the mean, stored to reduce computing time. **/
  38.     private final double logMean;

  39.     /**
  40.      * Create an exponential distribution with the given mean.
  41.      *
  42.      * @param mean Mean of this distribution.
  43.      * @throws MathIllegalArgumentException if {@code mean <= 0}.
  44.      */
  45.     public ExponentialDistribution(double mean)
  46.         throws MathIllegalArgumentException {
  47.         if (mean <= 0) {
  48.             throw new MathIllegalArgumentException(LocalizedCoreFormats.MEAN, mean);
  49.         }

  50.         this.mean    = mean;
  51.         this.logMean = FastMath.log(mean);
  52.     }

  53.     /**
  54.      * Access the mean.
  55.      *
  56.      * @return the mean.
  57.      */
  58.     public double getMean() {
  59.         return mean;
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public double density(double x) {
  64.         final double logDensity = logDensity(x);
  65.         return logDensity == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logDensity);
  66.     }

  67.     /** {@inheritDoc} **/
  68.     @Override
  69.     public double logDensity(double x) {
  70.         if (x < 0) {
  71.             return Double.NEGATIVE_INFINITY;
  72.         }
  73.         return -x / mean - logMean;
  74.     }

  75.     /**
  76.      * {@inheritDoc}
  77.      *
  78.      * The implementation of this method is based on:
  79.      * <ul>
  80.      * <li>
  81.      * <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">
  82.      * Exponential Distribution</a>, equation (1).</li>
  83.      * </ul>
  84.      */
  85.     @Override
  86.     public double cumulativeProbability(double x)  {
  87.         double ret;
  88.         if (x <= 0.0) {
  89.             ret = 0.0;
  90.         } else {
  91.             ret = 1.0 - FastMath.exp(-x / mean);
  92.         }
  93.         return ret;
  94.     }

  95.     /**
  96.      * {@inheritDoc}
  97.      *
  98.      * Returns {@code 0} when {@code p= = 0} and
  99.      * {@code Double.POSITIVE_INFINITY} when {@code p == 1}.
  100.      */
  101.     @Override
  102.     public double inverseCumulativeProbability(double p) throws MathIllegalArgumentException {
  103.         MathUtils.checkRangeInclusive(p, 0, 1);

  104.         double ret;
  105.         if (p == 1.0) {
  106.             ret = Double.POSITIVE_INFINITY;
  107.         } else {
  108.             ret = -mean * FastMath.log(1.0 - p);
  109.         }

  110.         return ret;
  111.     }

  112.     /**
  113.      * {@inheritDoc}
  114.      *
  115.      * For mean parameter {@code k}, the mean is {@code k}.
  116.      */
  117.     @Override
  118.     public double getNumericalMean() {
  119.         return getMean();
  120.     }

  121.     /**
  122.      * {@inheritDoc}
  123.      *
  124.      * For mean parameter {@code k}, the variance is {@code k^2}.
  125.      */
  126.     @Override
  127.     public double getNumericalVariance() {
  128.         final double m = getMean();
  129.         return m * m;
  130.     }

  131.     /**
  132.      * {@inheritDoc}
  133.      *
  134.      * The lower bound of the support is always 0 no matter the mean parameter.
  135.      *
  136.      * @return lower bound of the support (always 0)
  137.      */
  138.     @Override
  139.     public double getSupportLowerBound() {
  140.         return 0;
  141.     }

  142.     /**
  143.      * {@inheritDoc}
  144.      *
  145.      * The upper bound of the support is always positive infinity
  146.      * no matter the mean parameter.
  147.      *
  148.      * @return upper bound of the support (always Double.POSITIVE_INFINITY)
  149.      */
  150.     @Override
  151.     public double getSupportUpperBound() {
  152.         return Double.POSITIVE_INFINITY;
  153.     }

  154.     /**
  155.      * {@inheritDoc}
  156.      *
  157.      * The support of this distribution is connected.
  158.      *
  159.      * @return {@code true}
  160.      */
  161.     @Override
  162.     public boolean isSupportConnected() {
  163.         return true;
  164.     }
  165. }