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.  *      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.  * This class implements the Gumbel distribution.
  28.  *
  29.  * @see <a href="http://en.wikipedia.org/wiki/Gumbel_distribution">Gumbel Distribution (Wikipedia)</a>
  30.  * @see <a href="http://mathworld.wolfram.com/GumbelDistribution.html">Gumbel Distribution (Mathworld)</a>
  31.  */
  32. public class GumbelDistribution extends AbstractRealDistribution {

  33.     /** Serializable version identifier. */
  34.     private static final long serialVersionUID = 20141003L;

  35.     /**
  36.      * Approximation of Euler's constant
  37.      * see <a href="https://mathworld.wolfram.com/Euler-MascheroniConstantApproximations.html">Euler-Mascheroni
  38.      * Constant Approximations</a>
  39.      */
  40.     private static final double EULER = FastMath.PI / (2 * FastMath.E);

  41.     /** The location parameter. */
  42.     private final double mu;
  43.     /** The scale parameter. */
  44.     private final double beta;

  45.     /**
  46.      * Build a new instance.
  47.      *
  48.      * @param mu location parameter
  49.      * @param beta scale parameter (must be positive)
  50.      * @throws MathIllegalArgumentException if {@code beta <= 0}
  51.      */
  52.     public GumbelDistribution(double mu, double beta)
  53.         throws MathIllegalArgumentException {
  54.         if (beta <= 0) {
  55.             throw new MathIllegalArgumentException(LocalizedCoreFormats.SCALE, beta);
  56.         }

  57.         this.beta = beta;
  58.         this.mu   = mu;
  59.     }

  60.     /**
  61.      * Access the location parameter, {@code mu}.
  62.      *
  63.      * @return the location parameter.
  64.      */
  65.     public double getLocation() {
  66.         return mu;
  67.     }

  68.     /**
  69.      * Access the scale parameter, {@code beta}.
  70.      *
  71.      * @return the scale parameter.
  72.      */
  73.     public double getScale() {
  74.         return beta;
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public double density(double x) {
  79.         final double z = (x - mu) / beta;
  80.         final double t = FastMath.exp(-z);
  81.         return FastMath.exp(-z - t) / beta;
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public double cumulativeProbability(double x) {
  86.         final double z = (x - mu) / beta;
  87.         return FastMath.exp(-FastMath.exp(-z));
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public double inverseCumulativeProbability(double p) throws MathIllegalArgumentException {
  92.         MathUtils.checkRangeInclusive(p, 0, 1);

  93.         if (p == 0) {
  94.             return Double.NEGATIVE_INFINITY;
  95.         } else if (p == 1) {
  96.             return Double.POSITIVE_INFINITY;
  97.         }
  98.         return mu - FastMath.log(-FastMath.log(p)) * beta;
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public double getNumericalMean() {
  103.         return mu + EULER * beta;
  104.     }

  105.     /** {@inheritDoc} */
  106.     @Override
  107.     public double getNumericalVariance() {
  108.         return (MathUtils.PI_SQUARED) / 6.0 * (beta * beta);
  109.     }

  110.     /** {@inheritDoc} */
  111.     @Override
  112.     public double getSupportLowerBound() {
  113.         return Double.NEGATIVE_INFINITY;
  114.     }

  115.     /** {@inheritDoc} */
  116.     @Override
  117.     public double getSupportUpperBound() {
  118.         return Double.POSITIVE_INFINITY;
  119.     }

  120.     /** {@inheritDoc} */
  121.     @Override
  122.     public boolean isSupportConnected() {
  123.         return true;
  124.     }

  125. }