LogisticDistribution.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. /**
  19.  * Implementation of the logistic distribution.
  20.  *
  21.  * <p>The probability density function of \( X \) is:
  22.  *
  23.  * <p>\[ f(x; \mu, s) = \frac{e^{-(x-\mu)/s}} {s\left(1+e^{-(x-\mu)/s}\right)^2} \]
  24.  *
  25.  * <p>for \( \mu \) the location,
  26.  * \( s &gt; 0 \) the scale, and
  27.  * \( x \in (-\infty, \infty) \).
  28.  *
  29.  * @see <a href="https://en.wikipedia.org/wiki/Logistic_distribution">Logistic distribution (Wikipedia)</a>
  30.  * @see <a href="https://mathworld.wolfram.com/LogisticDistribution.html">Logistic distribution (MathWorld)</a>
  31.  */
  32. public final class LogisticDistribution extends AbstractContinuousDistribution {
  33.     /** Support lower bound. */
  34.     private static final double SUPPORT_LO = Double.NEGATIVE_INFINITY;
  35.     /** Support upper bound. */
  36.     private static final double SUPPORT_HI = Double.POSITIVE_INFINITY;
  37.     /** &pi;<sup>2</sup>/3. https://oeis.org/A195055. */
  38.     private static final double PI_SQUARED_OVER_THREE = 3.289868133696452872944830;
  39.     /** Location parameter. */
  40.     private final double mu;
  41.     /** Scale parameter. */
  42.     private final double scale;
  43.     /** Logarithm of "scale". */
  44.     private final double logScale;

  45.     /**
  46.      * @param mu Location parameter.
  47.      * @param scale Scale parameter (must be positive).
  48.      */
  49.     private LogisticDistribution(double mu,
  50.                                  double scale) {
  51.         this.mu = mu;
  52.         this.scale = scale;
  53.         this.logScale = Math.log(scale);
  54.     }

  55.     /**
  56.      * Creates a logistic distribution.
  57.      *
  58.      * @param mu Location parameter.
  59.      * @param scale Scale parameter (must be positive).
  60.      * @return the distribution
  61.      * @throws IllegalArgumentException if {@code scale <= 0}.
  62.      */
  63.     public static LogisticDistribution of(double mu,
  64.                                           double scale) {
  65.         if (scale <= 0) {
  66.             throw new DistributionException(DistributionException.NOT_STRICTLY_POSITIVE,
  67.                                             scale);
  68.         }
  69.         return new LogisticDistribution(mu, scale);
  70.     }

  71.     /**
  72.      * Gets the location parameter of this distribution.
  73.      *
  74.      * @return the location parameter.
  75.      */
  76.     public double getLocation() {
  77.         return mu;
  78.     }

  79.     /**
  80.      * Gets the scale parameter of this distribution.
  81.      *
  82.      * @return the scale parameter.
  83.      */
  84.     public double getScale() {
  85.         return scale;
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public double density(double x) {
  90.         if (x <= SUPPORT_LO ||
  91.             x >= SUPPORT_HI) {
  92.             return 0;
  93.         }

  94.         // Ensure symmetry around location by using the absolute.
  95.         // This also ensures exp(z) is between 1 and 0 and avoids
  96.         // overflow for large negative values of (x - mu).
  97.         // Exploits the reciprocal relation: exp(-x) == 1 / exp(x)
  98.         //     exp(-z)                   1                exp(z)     exp(z)
  99.         // --------------- = -------------------------- * ------ = --------------
  100.         // (1 + exp(-z))^2    exp(z) (1 + 1 / exp(z))^2   exp(z)   (1 + exp(z))^2
  101.         final double z = -Math.abs(x - mu) / scale;
  102.         final double v = Math.exp(z);
  103.         return v / ((1 + v) * (1 + v)) / scale;
  104.     }

  105.     /** {@inheritDoc} */
  106.     @Override
  107.     public double logDensity(double x) {
  108.         if (x <= SUPPORT_LO ||
  109.             x >= SUPPORT_HI) {
  110.             return Double.NEGATIVE_INFINITY;
  111.         }

  112.         // Ensure symmetry around location by using the absolute
  113.         final double z = -Math.abs(x - mu) / scale;
  114.         final double v = Math.exp(z);
  115.         return z - 2 * Math.log1p(v) - logScale;
  116.     }

  117.     /** {@inheritDoc} */
  118.     @Override
  119.     public double cumulativeProbability(double x) {
  120.         final double z = (x - mu) / scale;
  121.         return 1 / (1 + Math.exp(-z));
  122.     }

  123.     /** {@inheritDoc} */
  124.     @Override
  125.     public double survivalProbability(double x) {
  126.         final double z = (x - mu) / scale;
  127.         return 1 / (1 + Math.exp(z));
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public double inverseCumulativeProbability(double p) {
  132.         ArgumentUtils.checkProbability(p);
  133.         if (p == 0) {
  134.             return SUPPORT_LO;
  135.         } else if (p == 1) {
  136.             return SUPPORT_HI;
  137.         } else {
  138.             return scale * Math.log(p / (1 - p)) + mu;
  139.         }
  140.     }

  141.     /** {@inheritDoc} */
  142.     @Override
  143.     public double inverseSurvivalProbability(double p) {
  144.         ArgumentUtils.checkProbability(p);
  145.         if (p == 1) {
  146.             return SUPPORT_LO;
  147.         } else if (p == 0) {
  148.             return SUPPORT_HI;
  149.         } else {
  150.             return scale * -Math.log(p / (1 - p)) + mu;
  151.         }
  152.     }

  153.     /**
  154.      * {@inheritDoc}
  155.      *
  156.      * <p>The mean is equal to the {@linkplain #getLocation() location}.
  157.      */
  158.     @Override
  159.     public double getMean() {
  160.         return getLocation();
  161.     }

  162.     /**
  163.      * {@inheritDoc}
  164.      *
  165.      * <p>For scale parameter \( s \), the variance is:
  166.      *
  167.      * <p>\[ \frac{s^2 \pi^2}{3} \]
  168.      */
  169.     @Override
  170.     public double getVariance() {
  171.         return scale * scale * PI_SQUARED_OVER_THREE;
  172.     }

  173.     /**
  174.      * {@inheritDoc}
  175.      *
  176.      * <p>The lower bound of the support is always negative infinity.
  177.      *
  178.      * @return {@linkplain Double#NEGATIVE_INFINITY negative infinity}.
  179.      */
  180.     @Override
  181.     public double getSupportLowerBound() {
  182.         return SUPPORT_LO;
  183.     }

  184.     /**
  185.      * {@inheritDoc}
  186.      *
  187.      * <p>The upper bound of the support is always positive infinity.
  188.      *
  189.      * @return {@linkplain Double#POSITIVE_INFINITY positive infinity}.
  190.      */
  191.     @Override
  192.     public double getSupportUpperBound() {
  193.         return SUPPORT_HI;
  194.     }

  195.     /** {@inheritDoc} */
  196.     @Override
  197.     double getMedian() {
  198.         // Overridden for the probability(double, double) method.
  199.         // This is intentionally not a public method.
  200.         return mu;
  201.     }
  202. }