GumbelDistribution.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 Gumbel distribution.
  20.  *
  21.  * <p>The probability density function of \( X \) is:
  22.  *
  23.  * <p>\[ f(x; \mu, \beta) =  \frac{1}{\beta} e^{-(z+e^{-z})} \]
  24.  *
  25.  * <p>where \[ z = \frac{x - \mu}{\beta} \]
  26.  *
  27.  * <p>for \( \mu \) the location,
  28.  * \( \beta &gt; 0 \) the scale, and
  29.  * \( x \in (-\infty, \infty) \).
  30.  *
  31.  * @see <a href="https://en.wikipedia.org/wiki/Gumbel_distribution">Gumbel distribution (Wikipedia)</a>
  32.  * @see <a href="https://mathworld.wolfram.com/GumbelDistribution.html">Gumbel distribution (MathWorld)</a>
  33.  */
  34. public final class GumbelDistribution extends AbstractContinuousDistribution {
  35.     /** Support lower bound. */
  36.     private static final double SUPPORT_LO = Double.NEGATIVE_INFINITY;
  37.     /** Support upper bound. */
  38.     private static final double SUPPORT_HI = Double.POSITIVE_INFINITY;
  39.     /** &pi;<sup>2</sup>/6. https://oeis.org/A013661. */
  40.     private static final double PI_SQUARED_OVER_SIX = 1.644934066848226436472415166646;
  41.     /**
  42.      * <a href="https://en.wikipedia.org/wiki/Euler%27s_constant">
  43.      * Approximation of Euler's constant</a>.
  44.      * https://oeis.org/A001620.
  45.      */
  46.     private static final double EULER = 0.5772156649015328606065;
  47.     /** ln(ln(2)). https://oeis.org/A074785. */
  48.     private static final double LN_LN_2 = -0.3665129205816643270124;
  49.     /** Location parameter. */
  50.     private final double mu;
  51.     /** Scale parameter. */
  52.     private final double beta;

  53.     /**
  54.      * @param mu Location parameter.
  55.      * @param beta Scale parameter (must be positive).
  56.      */
  57.     private GumbelDistribution(double mu,
  58.                                double beta) {
  59.         this.beta = beta;
  60.         this.mu = mu;
  61.     }

  62.     /**
  63.      * Creates a Gumbel distribution.
  64.      *
  65.      * @param mu Location parameter.
  66.      * @param beta Scale parameter (must be positive).
  67.      * @return the distribution
  68.      * @throws IllegalArgumentException if {@code beta <= 0}
  69.      */
  70.     public static GumbelDistribution of(double mu,
  71.                                         double beta) {
  72.         if (beta <= 0) {
  73.             throw new DistributionException(DistributionException.NOT_STRICTLY_POSITIVE, beta);
  74.         }
  75.         return new GumbelDistribution(mu, beta);
  76.     }

  77.     /**
  78.      * Gets the location parameter of this distribution.
  79.      *
  80.      * @return the location parameter.
  81.      */
  82.     public double getLocation() {
  83.         return mu;
  84.     }

  85.     /**
  86.      * Gets the scale parameter of this distribution.
  87.      *
  88.      * @return the scale parameter.
  89.      */
  90.     public double getScale() {
  91.         return beta;
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public double density(double x) {
  96.         if (x <= SUPPORT_LO) {
  97.             return 0;
  98.         }

  99.         final double z = (x - mu) / beta;
  100.         final double t = Math.exp(-z);
  101.         return Math.exp(-z - t) / beta;
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     public double logDensity(double x) {
  106.         if (x <= SUPPORT_LO) {
  107.             return Double.NEGATIVE_INFINITY;
  108.         }

  109.         final double z = (x - mu) / beta;
  110.         final double t = Math.exp(-z);
  111.         return -z - t - Math.log(beta);
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     public double cumulativeProbability(double x) {
  116.         final double z = (x - mu) / beta;
  117.         return Math.exp(-Math.exp(-z));
  118.     }

  119.     /** {@inheritDoc} */
  120.     @Override
  121.     public double survivalProbability(double x) {
  122.         final double z = (x - mu) / beta;
  123.         return -Math.expm1(-Math.exp(-z));
  124.     }

  125.     /** {@inheritDoc} */
  126.     @Override
  127.     public double inverseCumulativeProbability(double p) {
  128.         ArgumentUtils.checkProbability(p);
  129.         if (p == 0) {
  130.             return Double.NEGATIVE_INFINITY;
  131.         } else if (p == 1) {
  132.             return Double.POSITIVE_INFINITY;
  133.         }
  134.         return mu - Math.log(-Math.log(p)) * beta;
  135.     }

  136.     /** {@inheritDoc} */
  137.     @Override
  138.     public double inverseSurvivalProbability(double p) {
  139.         ArgumentUtils.checkProbability(p);
  140.         if (p == 1) {
  141.             return Double.NEGATIVE_INFINITY;
  142.         } else if (p == 0) {
  143.             return Double.POSITIVE_INFINITY;
  144.         }
  145.         return mu - Math.log(-Math.log1p(-p)) * beta;
  146.     }

  147.     /**
  148.      * {@inheritDoc}
  149.      *
  150.      * <p>For location parameter \( \mu \) and scale parameter \( \beta \), the mean is:
  151.      *
  152.      * <p>\[ \mu + \beta \gamma \]
  153.      *
  154.      * <p>where \( \gamma \) is the
  155.      * <a href="https://mathworld.wolfram.com/Euler-MascheroniConstantApproximations.html">
  156.      * Euler-Mascheroni constant</a>.
  157.      */
  158.     @Override
  159.     public double getMean() {
  160.         return mu + EULER * beta;
  161.     }

  162.     /**
  163.      * {@inheritDoc}
  164.      *
  165.      * <p>For scale parameter \( \beta \), the variance is:
  166.      *
  167.      * <p>\[ \frac{\pi^2}{6} \beta^2 \]
  168.      */
  169.     @Override
  170.     public double getVariance() {
  171.         return PI_SQUARED_OVER_SIX * beta * beta;
  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.         // u - beta * ln(ln(2))
  201.         return mu - beta * LN_LN_2;
  202.     }
  203. }