SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
FrechetDist.java
1/*
2 * Class: FrechetDist
3 * Description: Fréchet 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.*;
28import optimization.*;
29import umontreal.ssj.functions.MathFunction;
30
52public class FrechetDist extends ContinuousDistribution {
53 private double delta;
54 private double beta;
55 private double alpha;
56
57 private static class Optim implements Lmder_fcn {
58 protected double[] x;
59 protected int n;
60
61 public Optim(double[] x, int n) {
62 this.n = n;
63 this.x = x;
64 }
65
66 public void fcn(int m, int n, double[] par, double[] fvec, double[][] fjac, int iflag[]) {
67 if (par[1] <= 0.0 || par[2] <= 0.0) {
68 final double BIG = 1.0e100;
69 fvec[1] = BIG;
70 fvec[2] = BIG;
71 fvec[3] = BIG;
72 return;
73 }
74
75 double sum1, sum2, sumb, sum4, sum5;
76 double z, w, v;
77 double alpha = par[1];
78 double beta = par[2];
79 double mu = par[3];
80
81 if (iflag[1] == 1) {
82 sum1 = sum2 = sumb = sum4 = sum5 = 0;
83 for (int i = 0; i < n; i++) {
84 z = (x[i] - mu) / beta;
85 sum1 += 1.0 / z;
86 v = Math.pow(z, -alpha);
87 sum2 += v / z;
88 sumb += v;
89 w = Math.log(z);
90 sum4 += w;
91 sum5 += v * w;
92 }
93
94 fvec[2] = sumb - n; // eq. for beta
95 fvec[3] = (alpha + 1) * sum1 - alpha * sum2; // eq. for mu
96 fvec[1] = n / alpha + sum5 - sum4; // eq. for alpha
97
98 } else if (iflag[1] == 2) {
99 throw new IllegalArgumentException("iflag = 2");
100 // The 3 X 3 Jacobian must be calculated and put in fjac
101 }
102 }
103 }
104
105 private static class Function implements MathFunction {
106 private int n;
107 private double[] x;
108 private double delta;
109 public double sumxi;
110 public double dif;
111
112 public Function(double[] y, int n, double delta) {
113 this.n = n;
114 this.x = y;
115 this.delta = delta;
116 double xmin = Double.MAX_VALUE;
117 for (int i = 0; i < n; i++) {
118 if ((y[i] < xmin) && (y[i] > delta))
119 xmin = y[i];
120 }
121 dif = xmin - delta;
122 }
123
124 public double evaluate(double alpha) {
125 if (alpha <= 0.0)
126 return 1.0e100;
127 double v, w;
128 double sum1 = 0, sum2 = 0, sum3 = 0;
129 for (int i = 0; i < n; i++) {
130 if (x[i] <= delta)
131 continue;
132 v = Math.log(x[i] - delta);
133 w = Math.pow(dif / (x[i] - delta), alpha);
134 sum1 += v;
135 sum2 += w;
136 sum3 += v * w;
137 }
138
139 sum1 /= n;
140 sumxi = sum2 / n;
141 return 1 / alpha + sum3 / sum2 - sum1;
142 }
143 }
144
149 public FrechetDist(double alpha) {
150 setParams(alpha, 1.0, 0.0);
151 }
152
157 public FrechetDist(double alpha, double beta, double delta) {
158 setParams(alpha, beta, delta);
159 }
160
161 public double density(double x) {
162 return density(alpha, beta, delta, x);
163 }
164
165 public double cdf(double x) {
166 return cdf(alpha, beta, delta, x);
167 }
168
169 public double barF(double x) {
170 return barF(alpha, beta, delta, x);
171 }
172
173 public double inverseF(double u) {
174 return inverseF(alpha, beta, delta, u);
175 }
176
177 public double getMean() {
178 return getMean(alpha, beta, delta);
179 }
180
181 public double getVariance() {
182 return getVariance(alpha, beta, delta);
183 }
184
185 public double getStandardDeviation() {
186 return getStandardDeviation(alpha, beta, delta);
187 }
188
192 public static double density(double alpha, double beta, double delta, double x) {
193 if (beta <= 0)
194 throw new IllegalArgumentException("beta <= 0");
195 if (alpha <= 0)
196 throw new IllegalArgumentException("alpha <= 0");
197 final double z = (x - delta) / beta;
198 if (z <= 0.0)
199 return 0.0;
200 double t = Math.pow(z, -alpha);
201 return alpha * t * Math.exp(-t) / (z * beta);
202 }
203
207 public static double cdf(double alpha, double beta, double delta, double x) {
208 if (beta <= 0)
209 throw new IllegalArgumentException("beta <= 0");
210 if (alpha <= 0)
211 throw new IllegalArgumentException("alpha <= 0");
212 final double z = (x - delta) / beta;
213 if (z <= 0.0)
214 return 0.0;
215 double t = Math.pow(z, -alpha);
216 return Math.exp(-t);
217 }
218
222 public static double barF(double alpha, double beta, double delta, double x) {
223 if (beta <= 0)
224 throw new IllegalArgumentException("beta <= 0");
225 if (alpha <= 0)
226 throw new IllegalArgumentException("alpha <= 0");
227 final double z = (x - delta) / beta;
228 if (z <= 0.0)
229 return 1.0;
230 double t = Math.pow(z, -alpha);
231 return -Math.expm1(-t);
232 }
233
237 public static double inverseF(double alpha, double beta, double delta, double u) {
238 if (u < 0.0 || u > 1.0)
239 throw new IllegalArgumentException("u not in [0, 1]");
240 if (beta <= 0)
241 throw new IllegalArgumentException("beta <= 0");
242 if (alpha <= 0)
243 throw new IllegalArgumentException("alpha <= 0");
244 if (u >= 1.0)
245 return Double.POSITIVE_INFINITY;
246 if (u <= 0.0)
247 return delta;
248 double t = Math.pow(-Math.log(u), 1.0 / alpha);
249 if (t <= Double.MIN_NORMAL)
250 return Double.MAX_VALUE;
251 return delta + beta / t;
252 }
253
275 public static double[] getMLE(double[] x, int n, double delta) {
276 if (n <= 1)
277 throw new IllegalArgumentException("n <= 1");
278
279 Function func = new Function(x, n, delta);
280 double a = 1e-4;
281 double b = 1.0e12;
282 double alpha = RootFinder.brentDekker(a, b, func, 1e-12);
283 double par[] = new double[2];
284 par[0] = alpha;
285 par[1] = func.dif * Math.pow(func.sumxi, -1.0 / alpha);
286 return par;
287 }
288
299 public static FrechetDist getInstanceFromMLE(double[] x, int n, double delta) {
300 double par[] = getMLE(x, n, delta);
301 return new FrechetDist(par[0], par[1], delta);
302 }
303
310 public static double getMean(double alpha, double beta, double delta) {
311 if (beta <= 0)
312 throw new IllegalArgumentException("beta <= 0");
313 if (alpha <= 1)
314 throw new IllegalArgumentException("alpha <= 1");
315 double t = Num.lnGamma(1.0 - 1.0 / alpha);
316 return delta + beta * Math.exp(t);
317 }
318
325 public static double getVariance(double alpha, double beta, double delta) {
326 if (beta <= 0)
327 throw new IllegalArgumentException("beta <= 0");
328 if (alpha <= 2)
329 throw new IllegalArgumentException("alpha <= 2");
330 double t = Num.lnGamma(1.0 - 1.0 / alpha);
331 double mu = Math.exp(t);
332 double v = Math.exp(Num.lnGamma(1.0 - 2.0 / alpha));
333 return beta * beta * (v - mu * mu);
334 }
335
342 public static double getStandardDeviation(double alpha, double beta, double delta) {
343 return Math.sqrt(getVariance(alpha, beta, delta));
344 }
345
349 public double getAlpha() {
350 return alpha;
351 }
352
356 public double getBeta() {
357 return beta;
358 }
359
363 public double getDelta() {
364 return delta;
365 }
366
371 public void setParams(double alpha, double beta, double delta) {
372 if (beta <= 0)
373 throw new IllegalArgumentException("beta <= 0");
374 if (alpha <= 0)
375 throw new IllegalArgumentException("alpha <= 0");
376 this.delta = delta;
377 this.beta = beta;
378 this.alpha = alpha;
379 }
380
385 public double[] getParams() {
386 double[] retour = { alpha, beta, delta };
387 return retour;
388 }
389
393 public String toString() {
394 return getClass().getSimpleName() + " : alpha = " + alpha + ", beta = " + beta + ", delta = " + delta;
395 }
396
397}
Classes implementing continuous distributions should inherit from this base class.
double inverseF(double u)
Returns the inverse distribution function .
String toString()
Returns a String containing information about the current distribution.
double cdf(double x)
Returns the distribution function .
static FrechetDist getInstanceFromMLE(double[] x, int n, double delta)
Given delta, creates a new instance of a Fréchet distribution with parameters and.
static double getMean(double alpha, double beta, double delta)
Returns the mean of the Fréchet distribution with parameters , and .
double[] getParams()
Return an array containing the parameters of the current object in regular order: [ ,...
static double inverseF(double alpha, double beta, double delta, double u)
Computes and returns the inverse distribution function.
FrechetDist(double alpha, double beta, double delta)
Constructs a FrechetDist object with parameters = alpha, = beta and = delta.
FrechetDist(double alpha)
Constructor for the standard Fréchet distribution with parameters = 1 and = 0.
void setParams(double alpha, double beta, double delta)
Sets the parameters , and of this object.
double barF(double x)
Returns the complementary distribution function.
static double cdf(double alpha, double beta, double delta, double x)
Computes and returns the distribution function.
double getStandardDeviation()
Returns the standard deviation.
double getBeta()
Returns the parameter of this object.
static double density(double alpha, double beta, double delta, double x)
Computes and returns the density function.
static double getStandardDeviation(double alpha, double beta, double delta)
Returns the standard deviation of the Fréchet distribution with parameters , and .
double density(double x)
Returns , the density evaluated at .
double getVariance()
Returns the variance.
double getAlpha()
Returns the parameter of this object.
double getMean()
Returns the mean.
static double[] getMLE(double[] x, int n, double delta)
Given delta, estimates the parameters of the Fréchet distribution using the maximum likelihood meth...
double getDelta()
Returns the parameter of this object.
static double barF(double alpha, double beta, double delta, double x)
Computes and returns the complementary distribution function .
static double getVariance(double alpha, double beta, double delta)
Returns the variance of the Fréchet distribution with parameters , and .
This class provides various constants and methods to compute numerical quantities such as factorials,...
Definition Num.java:35
static double lnGamma(double x)
Returns the natural logarithm of the gamma function evaluated at x.
Definition Num.java:417
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.