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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22 package org.hipparchus.distribution.continuous;
23
24 import org.hipparchus.exception.LocalizedCoreFormats;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.util.FastMath;
27 import org.hipparchus.util.MathUtils;
28
29 /**
30 * This class implements the Gumbel distribution.
31 *
32 * @see <a href="http://en.wikipedia.org/wiki/Gumbel_distribution">Gumbel Distribution (Wikipedia)</a>
33 * @see <a href="http://mathworld.wolfram.com/GumbelDistribution.html">Gumbel Distribution (Mathworld)</a>
34 */
35 public class GumbelDistribution extends AbstractRealDistribution {
36
37 /** Serializable version identifier. */
38 private static final long serialVersionUID = 20141003L;
39
40 /**
41 * Approximation of Euler's constant
42 * see <a href="https://mathworld.wolfram.com/Euler-MascheroniConstantApproximations.html">Euler-Mascheroni
43 * Constant Approximations</a>
44 */
45 private static final double EULER = FastMath.PI / (2 * FastMath.E);
46
47 /** The location parameter. */
48 private final double mu;
49 /** The scale parameter. */
50 private final double beta;
51
52 /**
53 * Build a new instance.
54 *
55 * @param mu location parameter
56 * @param beta scale parameter (must be positive)
57 * @throws MathIllegalArgumentException if {@code beta <= 0}
58 */
59 public GumbelDistribution(double mu, double beta)
60 throws MathIllegalArgumentException {
61 if (beta <= 0) {
62 throw new MathIllegalArgumentException(LocalizedCoreFormats.SCALE, beta);
63 }
64
65 this.beta = beta;
66 this.mu = mu;
67 }
68
69 /**
70 * Access the location parameter, {@code mu}.
71 *
72 * @return the location parameter.
73 */
74 public double getLocation() {
75 return mu;
76 }
77
78 /**
79 * Access the scale parameter, {@code beta}.
80 *
81 * @return the scale parameter.
82 */
83 public double getScale() {
84 return beta;
85 }
86
87 /** {@inheritDoc} */
88 @Override
89 public double density(double x) {
90 final double z = (x - mu) / beta;
91 final double t = FastMath.exp(-z);
92 return FastMath.exp(-z - t) / beta;
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 public double cumulativeProbability(double x) {
98 final double z = (x - mu) / beta;
99 return FastMath.exp(-FastMath.exp(-z));
100 }
101
102 /** {@inheritDoc} */
103 @Override
104 public double inverseCumulativeProbability(double p) throws MathIllegalArgumentException {
105 MathUtils.checkRangeInclusive(p, 0, 1);
106
107 if (p == 0) {
108 return Double.NEGATIVE_INFINITY;
109 } else if (p == 1) {
110 return Double.POSITIVE_INFINITY;
111 }
112 return mu - FastMath.log(-FastMath.log(p)) * beta;
113 }
114
115 /** {@inheritDoc} */
116 @Override
117 public double getNumericalMean() {
118 return mu + EULER * beta;
119 }
120
121 /** {@inheritDoc} */
122 @Override
123 public double getNumericalVariance() {
124 return (MathUtils.PI_SQUARED) / 6.0 * (beta * beta);
125 }
126
127 /** {@inheritDoc} */
128 @Override
129 public double getSupportLowerBound() {
130 return Double.NEGATIVE_INFINITY;
131 }
132
133 /** {@inheritDoc} */
134 @Override
135 public double getSupportUpperBound() {
136 return Double.POSITIVE_INFINITY;
137 }
138
139 /** {@inheritDoc} */
140 @Override
141 public boolean isSupportConnected() {
142 return true;
143 }
144
145 }