001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.statistics.distribution; 018 019/** 020 * Implementation of the Gumbel distribution. 021 * 022 * <p>The probability density function of \( X \) is: 023 * 024 * <p>\[ f(x; \mu, \beta) = \frac{1}{\beta} e^{-(z+e^{-z})} \] 025 * 026 * <p>where \[ z = \frac{x - \mu}{\beta} \] 027 * 028 * <p>for \( \mu \) the location, 029 * \( \beta > 0 \) the scale, and 030 * \( x \in (-\infty, \infty) \). 031 * 032 * @see <a href="https://en.wikipedia.org/wiki/Gumbel_distribution">Gumbel distribution (Wikipedia)</a> 033 * @see <a href="https://mathworld.wolfram.com/GumbelDistribution.html">Gumbel distribution (MathWorld)</a> 034 */ 035public final class GumbelDistribution extends AbstractContinuousDistribution { 036 /** Support lower bound. */ 037 private static final double SUPPORT_LO = Double.NEGATIVE_INFINITY; 038 /** Support upper bound. */ 039 private static final double SUPPORT_HI = Double.POSITIVE_INFINITY; 040 /** π<sup>2</sup>/6. https://oeis.org/A013661. */ 041 private static final double PI_SQUARED_OVER_SIX = 1.644934066848226436472415166646; 042 /** 043 * <a href="https://en.wikipedia.org/wiki/Euler%27s_constant"> 044 * Approximation of Euler's constant</a>. 045 * https://oeis.org/A001620. 046 */ 047 private static final double EULER = 0.5772156649015328606065; 048 /** ln(ln(2)). https://oeis.org/A074785. */ 049 private static final double LN_LN_2 = -0.3665129205816643270124; 050 /** Location parameter. */ 051 private final double mu; 052 /** Scale parameter. */ 053 private final double beta; 054 055 /** 056 * @param mu Location parameter. 057 * @param beta Scale parameter (must be positive). 058 */ 059 private GumbelDistribution(double mu, 060 double beta) { 061 this.beta = beta; 062 this.mu = mu; 063 } 064 065 /** 066 * Creates a Gumbel distribution. 067 * 068 * @param mu Location parameter. 069 * @param beta Scale parameter (must be positive). 070 * @return the distribution 071 * @throws IllegalArgumentException if {@code beta <= 0} 072 */ 073 public static GumbelDistribution of(double mu, 074 double beta) { 075 if (beta <= 0) { 076 throw new DistributionException(DistributionException.NOT_STRICTLY_POSITIVE, beta); 077 } 078 return new GumbelDistribution(mu, beta); 079 } 080 081 /** 082 * Gets the location parameter of this distribution. 083 * 084 * @return the location parameter. 085 */ 086 public double getLocation() { 087 return mu; 088 } 089 090 /** 091 * Gets the scale parameter of this distribution. 092 * 093 * @return the scale parameter. 094 */ 095 public double getScale() { 096 return beta; 097 } 098 099 /** {@inheritDoc} */ 100 @Override 101 public double density(double x) { 102 if (x <= SUPPORT_LO) { 103 return 0; 104 } 105 106 final double z = (x - mu) / beta; 107 final double t = Math.exp(-z); 108 return Math.exp(-z - t) / beta; 109 } 110 111 /** {@inheritDoc} */ 112 @Override 113 public double logDensity(double x) { 114 if (x <= SUPPORT_LO) { 115 return Double.NEGATIVE_INFINITY; 116 } 117 118 final double z = (x - mu) / beta; 119 final double t = Math.exp(-z); 120 return -z - t - Math.log(beta); 121 } 122 123 /** {@inheritDoc} */ 124 @Override 125 public double cumulativeProbability(double x) { 126 final double z = (x - mu) / beta; 127 return Math.exp(-Math.exp(-z)); 128 } 129 130 /** {@inheritDoc} */ 131 @Override 132 public double survivalProbability(double x) { 133 final double z = (x - mu) / beta; 134 return -Math.expm1(-Math.exp(-z)); 135 } 136 137 /** {@inheritDoc} */ 138 @Override 139 public double inverseCumulativeProbability(double p) { 140 ArgumentUtils.checkProbability(p); 141 if (p == 0) { 142 return Double.NEGATIVE_INFINITY; 143 } else if (p == 1) { 144 return Double.POSITIVE_INFINITY; 145 } 146 return mu - Math.log(-Math.log(p)) * beta; 147 } 148 149 /** {@inheritDoc} */ 150 @Override 151 public double inverseSurvivalProbability(double p) { 152 ArgumentUtils.checkProbability(p); 153 if (p == 1) { 154 return Double.NEGATIVE_INFINITY; 155 } else if (p == 0) { 156 return Double.POSITIVE_INFINITY; 157 } 158 return mu - Math.log(-Math.log1p(-p)) * beta; 159 } 160 161 /** 162 * {@inheritDoc} 163 * 164 * <p>For location parameter \( \mu \) and scale parameter \( \beta \), the mean is: 165 * 166 * <p>\[ \mu + \beta \gamma \] 167 * 168 * <p>where \( \gamma \) is the 169 * <a href="https://mathworld.wolfram.com/Euler-MascheroniConstantApproximations.html"> 170 * Euler-Mascheroni constant</a>. 171 */ 172 @Override 173 public double getMean() { 174 return mu + EULER * beta; 175 } 176 177 /** 178 * {@inheritDoc} 179 * 180 * <p>For scale parameter \( \beta \), the variance is: 181 * 182 * <p>\[ \frac{\pi^2}{6} \beta^2 \] 183 */ 184 @Override 185 public double getVariance() { 186 return PI_SQUARED_OVER_SIX * beta * beta; 187 } 188 189 /** 190 * {@inheritDoc} 191 * 192 * <p>The lower bound of the support is always negative infinity. 193 * 194 * @return {@linkplain Double#NEGATIVE_INFINITY negative infinity}. 195 */ 196 @Override 197 public double getSupportLowerBound() { 198 return SUPPORT_LO; 199 } 200 201 /** 202 * {@inheritDoc} 203 * 204 * <p>The upper bound of the support is always positive infinity. 205 * 206 * @return {@linkplain Double#POSITIVE_INFINITY positive infinity}. 207 */ 208 @Override 209 public double getSupportUpperBound() { 210 return SUPPORT_HI; 211 } 212 213 /** {@inheritDoc} */ 214 @Override 215 double getMedian() { 216 // Overridden for the probability(double, double) method. 217 // This is intentionally not a public method. 218 // u - beta * ln(ln(2)) 219 return mu - beta * LN_LN_2; 220 } 221}