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 /**
20 * Implementation of the Gumbel distribution.
21 *
22 * <p>The probability density function of \( X \) is:
23 *
24 * <p>\[ f(x; \mu, \beta) = \frac{1}{\beta} e^{-(z+e^{-z})} \]
25 *
26 * <p>where \[ z = \frac{x - \mu}{\beta} \]
27 *
28 * <p>for \( \mu \) the location,
29 * \( \beta > 0 \) the scale, and
30 * \( x \in (-\infty, \infty) \).
31 *
32 * @see <a href="https://en.wikipedia.org/wiki/Gumbel_distribution">Gumbel distribution (Wikipedia)</a>
33 * @see <a href="https://mathworld.wolfram.com/GumbelDistribution.html">Gumbel distribution (MathWorld)</a>
34 */
35 public final class GumbelDistribution extends AbstractContinuousDistribution {
36 /** Support lower bound. */
37 private static final double SUPPORT_LO = Double.NEGATIVE_INFINITY;
38 /** Support upper bound. */
39 private static final double SUPPORT_HI = Double.POSITIVE_INFINITY;
40 /** π<sup>2</sup>/6. https://oeis.org/A013661. */
41 private static final double PI_SQUARED_OVER_SIX = 1.644934066848226436472415166646;
42 /**
43 * <a href="https://en.wikipedia.org/wiki/Euler%27s_constant">
44 * Approximation of Euler's constant</a>.
45 * https://oeis.org/A001620.
46 */
47 private static final double EULER = 0.5772156649015328606065;
48 /** ln(ln(2)). https://oeis.org/A074785. */
49 private static final double LN_LN_2 = -0.3665129205816643270124;
50 /** Location parameter. */
51 private final double mu;
52 /** Scale parameter. */
53 private final double beta;
54
55 /**
56 * @param mu Location parameter.
57 * @param beta Scale parameter (must be positive).
58 */
59 private GumbelDistribution(double mu,
60 double beta) {
61 this.beta = beta;
62 this.mu = mu;
63 }
64
65 /**
66 * Creates a Gumbel distribution.
67 *
68 * @param mu Location parameter.
69 * @param beta Scale parameter (must be positive).
70 * @return the distribution
71 * @throws IllegalArgumentException if {@code beta <= 0}
72 */
73 public static GumbelDistribution of(double mu,
74 double beta) {
75 if (beta <= 0) {
76 throw new DistributionException(DistributionException.NOT_STRICTLY_POSITIVE, beta);
77 }
78 return new GumbelDistribution(mu, beta);
79 }
80
81 /**
82 * Gets the location parameter of this distribution.
83 *
84 * @return the location parameter.
85 */
86 public double getLocation() {
87 return mu;
88 }
89
90 /**
91 * Gets the scale parameter of this distribution.
92 *
93 * @return the scale parameter.
94 */
95 public double getScale() {
96 return beta;
97 }
98
99 /** {@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 }