SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
GumbelDist.java
1/*
2 * Class: GumbelDist
3 * Description: Gumbel distribution
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.probdist;
26
27import umontreal.ssj.util.Num;
28import umontreal.ssj.util.RootFinder;
29import umontreal.ssj.functions.MathFunction;
30
46public class GumbelDist extends ContinuousDistribution {
47 private double delta;
48 private double beta;
49
50 private static class FunctionPlus implements MathFunction {
51 // when beta > 0
52 protected int n;
53 protected double mean;
54 protected double[] x;
55 private double minx; // min of all {x[i]}
56
57 public FunctionPlus(double[] y, int n, double mean, double minx) {
58 this.n = n;
59 this.mean = mean;
60 this.x = y;
61 this.minx = minx;
62 }
63
64 public double evaluate(double lam) {
65 if (lam <= 0.0)
66 return 1.0e100;
67 double tem;
68 double sum2 = 0.0;
69 double sum1 = 0.0;
70
71 for (int i = 0; i < n; i++) {
72 tem = Math.exp(-(x[i] - minx) * lam);
73 sum1 += tem;
74 sum2 += x[i] * tem;
75 }
76
77 return (mean - 1 / lam) * sum1 - sum2;
78 }
79 }
80
81 private static class FunctionMinus implements MathFunction {
82 // when beta < 0
83 protected int n;
84 protected double mean;
85 protected double[] x;
86 protected double xmax;
87
88 public FunctionMinus(double[] y, int n, double mean, double xmax) {
89 this.n = n;
90 this.mean = mean;
91 this.x = y;
92 this.xmax = xmax;
93 }
94
95 public double evaluate(double lam) {
96 if (lam >= 0.0)
97 return 1.0e100;
98 double tem;
99 double sum2 = 0.0;
100 double sum1 = 0.0;
101
102 for (int i = 0; i < n; i++) {
103 tem = Math.exp((xmax - x[i]) * lam);
104 sum1 += tem;
105 sum2 += x[i] * tem;
106 }
107
108 return (mean - 1 / lam) * sum1 - sum2;
109 }
110 }
111
117 public GumbelDist() {
118 setParams(1.0, 0.0);
119 }
120
125 public GumbelDist(double beta, double delta) {
126 setParams(beta, delta);
127 }
128
129 public double density(double x) {
130 return density(beta, delta, x);
131 }
132
133 public double cdf(double x) {
134 return cdf(beta, delta, x);
135 }
136
137 public double barF(double x) {
138 return barF(beta, delta, x);
139 }
140
141 public double inverseF(double u) {
142 return inverseF(beta, delta, u);
143 }
144
145 public double getMean() {
146 return GumbelDist.getMean(beta, delta);
147 }
148
149 public double getVariance() {
150 return GumbelDist.getVariance(beta, delta);
151 }
152
153 public double getStandardDeviation() {
154 return GumbelDist.getStandardDeviation(beta, delta);
155 }
156
160 public static double density(double beta, double delta, double x) {
161 if (beta == 0.)
162 throw new IllegalArgumentException("beta = 0");
163 final double z = (x - delta) / beta;
164 if (z <= -10.0)
165 return 0.0;
166 double t = Math.exp(-z);
167 return t * Math.exp(-t) / Math.abs(beta);
168 }
169
173 public static double cdf(double beta, double delta, double x) {
174 if (beta == 0.)
175 throw new IllegalArgumentException("beta = 0");
176 final double z = (x - delta) / beta;
177 if (beta > 0.) {
178 if (z <= -7.0)
179 return 0.0;
180 return Math.exp(-Math.exp(-z));
181 } else { // beta < 0
182 if (z <= -7.0)
183 return 1.0;
184 return -Math.expm1(-Math.exp(-z));
185 }
186 }
187
191 public static double barF(double beta, double delta, double x) {
192 if (beta == 0.)
193 throw new IllegalArgumentException("beta = 0");
194 final double z = (x - delta) / beta;
195 if (beta > 0.) {
196 if (z <= -7.0)
197 return 1.0;
198 return -Math.expm1(-Math.exp(-z));
199 } else { // beta < 0
200 if (z <= -7.0)
201 return 0.0;
202 return Math.exp(-Math.exp(-z));
203 }
204 }
205
209 public static double inverseF(double beta, double delta, double u) {
210 if (u < 0.0 || u > 1.0)
211 throw new IllegalArgumentException("u not in [0, 1]");
212 if (beta == 0.)
213 throw new IllegalArgumentException("beta = 0");
214 if (u >= 1.0)
215 return Double.POSITIVE_INFINITY;
216 if (u <= 0.0)
217 return Double.NEGATIVE_INFINITY;
218 if (beta > 0.)
219 return delta - Math.log(-Math.log(u)) * beta;
220 else
221 return delta - Math.log(-Math.log1p(-u)) * beta;
222 }
223
243 public static double[] getMLE(double[] x, int n) {
244 if (n <= 1)
245 throw new IllegalArgumentException("n <= 1");
246 int i;
247 double par[] = new double[2];
248
249 double xmin = Double.MAX_VALUE;
250 double sum = 0;
251 for (i = 0; i < n; i++) {
252 sum += x[i];
253 if (x[i] < xmin)
254 xmin = x[i];
255 }
256 double mean = sum / (double) n;
257
258 sum = 0;
259 for (i = 0; i < n; i++)
260 sum += (x[i] - mean) * (x[i] - mean);
261 double variance = sum / (n - 1.0);
262
263 FunctionPlus func = new FunctionPlus(x, n, mean, xmin);
264
265 double lam = 1.0 / (0.7797 * Math.sqrt(variance));
266 final double EPS = 0.02;
267 double a = (1.0 - EPS) * lam - 5.0;
268 if (a <= 0)
269 a = 1e-15;
270 double b = (1.0 + EPS) * lam + 5.0;
271 lam = RootFinder.brentDekker(a, b, func, 1e-8);
272 par[0] = 1.0 / lam;
273
274 sum = 0;
275 for (i = 0; i < n; i++)
276 sum += Math.exp(-(x[i] - xmin) * lam);
277 par[1] = xmin - Math.log(sum / n) / lam;
278 return par;
279 }
280
289 public static double[] getMLEmin(double[] x, int n) {
290 if (n <= 1)
291 throw new IllegalArgumentException("n <= 1");
292
293 int i;
294 double par[] = new double[2];
295 double xmax = -Double.MAX_VALUE;
296 double sum = 0.0;
297 for (i = 0; i < n; i++) {
298 sum += x[i];
299 if (x[i] > xmax)
300 xmax = x[i];
301 }
302 double mean = sum / (double) n;
303
304 sum = 0.0;
305 for (i = 0; i < n; i++)
306 sum += (x[i] - mean) * (x[i] - mean);
307 double variance = sum / (n - 1.0);
308
309 FunctionMinus func = new FunctionMinus(x, n, mean, xmax);
310
311 double lam = -1.0 / (0.7797 * Math.sqrt(variance));
312 final double EPS = 0.02;
313 double a = (1.0 + EPS) * lam - 2.0;
314 double b = (1.0 - EPS) * lam + 2.0;
315 if (b >= 0)
316 b = -1e-15;
317 lam = RootFinder.brentDekker(a, b, func, 1e-12);
318 par[0] = 1.0 / lam;
319
320 sum = 0.0;
321 for (i = 0; i < n; i++)
322 sum += Math.exp((xmax - x[i]) * lam);
323 par[0] = 1.0 / lam;
324 par[1] = xmax - Math.log(sum / n) / lam;
325
326 return par;
327 }
328
338 public static GumbelDist getInstanceFromMLE(double[] x, int n) {
339 double parameters[] = getMLE(x, n);
340 return new GumbelDist(parameters[0], parameters[1]);
341 }
342
349 public static GumbelDist getInstanceFromMLEmin(double[] x, int n) {
350 double parameters[] = getMLEmin(x, n);
351 return new GumbelDist(parameters[0], parameters[1]);
352 }
353
362 public static double getMean(double beta, double delta) {
363 if (beta == 0.0)
364 throw new IllegalArgumentException("beta = 0");
365
366 return delta + Num.EULER * beta;
367 }
368
376 public static double getVariance(double beta, double delta) {
377 if (beta == 0.0)
378 throw new IllegalArgumentException("beta = 0");
379
380 return Math.PI * Math.PI * beta * beta / 6.0;
381 }
382
389 public static double getStandardDeviation(double beta, double delta) {
390 if (beta == 0.0)
391 throw new IllegalArgumentException("beta = 0");
392
393 return Math.sqrt(getVariance(beta, delta));
394 }
395
399 public double getBeta() {
400 return beta;
401 }
402
406 public double getDelta() {
407 return delta;
408 }
409
413 public void setParams(double beta, double delta) {
414 if (beta == 0)
415 throw new IllegalArgumentException("beta = 0");
416 this.delta = delta;
417 this.beta = beta;
418 }
419
424 public double[] getParams() {
425 double[] retour = { beta, delta };
426 return retour;
427 }
428
432 public String toString() {
433 return getClass().getSimpleName() + " : beta = " + beta + ", delta = " + delta;
434 }
435
436}
Classes implementing continuous distributions should inherit from this base class.
GumbelDist(double beta, double delta)
Constructs a GumbelDist object with parameters = beta and = delta.
double density(double x)
Returns , the density evaluated at .
String toString()
Returns a String containing information about the current distribution.
double getMean()
Returns the mean.
static GumbelDist getInstanceFromMLEmin(double[] x, int n)
Similar to getInstanceFromMLE, but for the case .
GumbelDist()
Constructor for the standard Gumbel distribution with parameters.
void setParams(double beta, double delta)
Sets the parameters and of this object.
double getBeta()
Returns the parameter of this object.
static double[] getMLEmin(double[] x, int n)
Similar to getMLE, but for the case .
static double[] getMLE(double[] x, int n)
Estimates the parameters of the Gumbel distribution, assuming that , and using the maximum likelihoo...
static double barF(double beta, double delta, double x)
Computes and returns the complementary distribution function .
static double cdf(double beta, double delta, double x)
Computes and returns the distribution function.
double getDelta()
Returns the parameter of this object.
double getVariance()
Returns the variance.
double cdf(double x)
Returns the distribution function .
static double inverseF(double beta, double delta, double u)
Computes and returns the inverse distribution function.
double[] getParams()
Return a table containing the parameters of the current distribution.
static GumbelDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of an Gumbel distribution with parameters.
double getStandardDeviation()
Returns the standard deviation.
static double getStandardDeviation(double beta, double delta)
Returns the standard deviation of the Gumbel distribution with parameters and .
static double getVariance(double beta, double delta)
Returns the variance of the Gumbel distribution with parameters and.
static double getMean(double beta, double delta)
Returns the mean, , of the Gumbel distribution with parameters and , where.
double barF(double x)
Returns the complementary distribution function.
double inverseF(double u)
Returns the inverse distribution function .
static double density(double beta, double delta, double x)
Computes and returns the density function.
static final double EULER
The Euler-Mascheroni constant.
Definition Num.java:133
This class provides numerical methods to solve non-linear equations.
static double brentDekker(double a, double b, MathFunction f, double tol)
Computes a root of the function in f using the Brent-Dekker method.
This interface should be implemented by classes which represent univariate mathematical functions.