SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
LoglogisticDist.java
1/*
2 * Class: LoglogisticDist
3 * Description: log-logistic 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.Misc;
29import optimization.*;
30
50 private double alpha;
51 private double beta;
52
53 private static class Optim implements Uncmin_methods {
54 private int n;
55 private double[] xi;
56
57 public Optim(double[] x, int n) {
58 this.n = n;
59 this.xi = new double[n];
60 System.arraycopy(x, 0, this.xi, 0, n);
61 }
62
63 public double f_to_minimize(double[] p) {
64 if ((p[1] <= 0.0) || (p[2] <= 0.0))
65 return 1e200;
66
67 double sum = 0.0;
68 for (int i = 0; i < n; i++) {
69 double tmp = density(p[1], p[2], xi[i]);
70 if (tmp > 0.0)
71 sum -= Math.log(tmp);
72 else
73 sum += 709.0; // log (Double.MIN_VALUE)
74 }
75 return sum;
76 }
77
78 public void gradient(double[] x, double[] g) {
79 }
80
81 public void hessian(double[] x, double[][] h) {
82 }
83 }
84
89 public LoglogisticDist(double alpha, double beta) {
90 setParams(alpha, beta);
91 }
92
93 public double density(double x) {
94 return density(alpha, beta, x);
95 }
96
97 public double cdf(double x) {
98 return cdf(alpha, beta, x);
99 }
100
101 public double barF(double x) {
102 return barF(alpha, beta, x);
103 }
104
105 public double inverseF(double u) {
106 return inverseF(alpha, beta, u);
107 }
108
109 public double getMean() {
110 return getMean(alpha, beta);
111 }
112
113 public double getVariance() {
114 return getVariance(alpha, beta);
115 }
116
117 public double getStandardDeviation() {
118 return getStandardDeviation(alpha, beta);
119 }
120
128 public static double density(double alpha, double beta, double x) {
129 double denominateur;
130
131 if (alpha <= 0.0)
132 throw new IllegalArgumentException("alpha <= 0");
133 if (beta <= 0.0)
134 throw new IllegalArgumentException("beta <= 0");
135 if (x <= 0.0 || x >= Double.MAX_VALUE / 2.0)
136 return 0.0;
137
138 if (x <= beta) {
139 double v = Math.pow(x / beta, alpha);
140 denominateur = 1.0 + v;
141 denominateur *= denominateur * beta;
142 return alpha * v * beta / (x * denominateur);
143 } else {
144 double v = Math.pow(beta / x, alpha);
145 denominateur = 1.0 + v;
146 denominateur *= denominateur * beta;
147 return alpha * v * beta / (x * denominateur);
148 }
149 }
150
158 public static double cdf(double alpha, double beta, double x) {
159 if (alpha <= 0.0)
160 throw new IllegalArgumentException("alpha <= 0");
161 if (beta <= 0.0)
162 throw new IllegalArgumentException("beta <= 0");
163 if (x <= 0.0)
164 return 0.0;
165 if (x >= Double.MAX_VALUE / 2.0)
166 return 1.0;
167 double z = x / beta;
168 if (z >= 1.0)
169 return 1.0 / (1.0 + Math.pow(1.0 / z, alpha));
170 double v = Math.pow(z, alpha);
171 return v / (v + 1.0);
172 }
173
181 public static double barF(double alpha, double beta, double x) {
182 double power;
183
184 if (alpha <= 0.0)
185 throw new IllegalArgumentException("alpha <= 0");
186 if (beta <= 0.0)
187 throw new IllegalArgumentException("beta <= 0");
188 if (x <= 0.0)
189 return 1.0;
190 if (x >= Double.MAX_VALUE / 2.0)
191 return 0.0;
192
193 double z = x / beta;
194 if (z <= 1.0)
195 return 1.0 / (1.0 + Math.pow(z, alpha));
196 double v = Math.pow(1.0 / z, alpha);
197 return v / (v + 1.0);
198 }
199
204 public static double inverseF(double alpha, double beta, double u) {
205 if (alpha <= 0.0)
206 throw new IllegalArgumentException("alpha <= 0");
207 if (beta <= 0.0)
208 throw new IllegalArgumentException("beta <= 0");
209 if (u < 0.0 || u > 1.0)
210 throw new IllegalArgumentException("u not in (0, 1]");
211 if (u >= 1.0)
212 return Double.POSITIVE_INFINITY;
213 if (u <= 0.0)
214 return 0.0;
215
216 if (u <= 0.5)
217 return (beta * Math.pow(u / (1.0 - u), 1.0 / alpha));
218 else
219 return (beta / Math.pow((1.0 - u) / u, 1.0 / alpha));
220 }
221
236 public static double[] getMLE(double[] x, int n) {
237 double sum = 0.0;
238
239 if (n <= 0)
240 throw new IllegalArgumentException("n <= 0");
241
242 Optim system = new Optim(x, n);
243
244 double[] parameters = new double[2];
245 double[] xpls = new double[3];
246 double[] param = new double[3];
247 double[] fpls = new double[3];
248 double[] gpls = new double[3];
249 int[] itrcmd = new int[2];
250 double[][] a = new double[3][3];
251 double[] udiag = new double[3];
252
253 param[2] = EmpiricalDist.getMedian(x, n);
254
255 if (param[2] < 0)
256 throw new IllegalArgumentException("median < 0");
257 if (param[2] <= 0)
258 param[2] = 1.0;
259
260 int m = Math.round((float) n / 4.0f);
261 double q1 = Misc.quickSelect(x, n, m);
262
263 if (q1 < 0)
264 throw new IllegalArgumentException("x[i] < 0");
265 if (q1 > 0)
266 param[1] = Math.log(3) / (Math.log(param[2]) - Math.log(q1));
267 else
268 param[1] = 1.0;
269
270 Uncmin_f77.optif0_f77(2, param, system, xpls, fpls, gpls, itrcmd, a, udiag);
271
272 for (int i = 0; i < 2; i++)
273 parameters[i] = xpls[i + 1];
274
275 return parameters;
276 }
277
287 public static LoglogisticDist getInstanceFromMLE(double[] x, int n) {
288 double parameters[] = getMLE(x, n);
289 return new LoglogisticDist(parameters[0], parameters[1]);
290 }
291
301 public static double getMean(double alpha, double beta) {
302 double theta;
303
304 if (alpha <= 1.0)
305 throw new IllegalArgumentException("alpha <= 1");
306 if (beta <= 0.0)
307 throw new IllegalArgumentException("beta <= 0");
308
309 theta = Math.PI / alpha;
310
311 return (beta * theta / Math.sin(theta));
312 }
313
326 public static double getVariance(double alpha, double beta) {
327 double theta;
328
329 if (alpha <= 2.0)
330 throw new IllegalArgumentException("alpha <= 2");
331 if (beta <= 0.0)
332 throw new IllegalArgumentException("beta <= 0");
333
334 theta = Math.PI / alpha;
335
336 return (beta * beta * theta * ((2.0 / Math.sin(2.0 * theta)) - (theta / (Math.sin(theta) * Math.sin(theta)))));
337 }
338
345 public static double getStandardDeviation(double alpha, double beta) {
346 return Math.sqrt(getVariance(alpha, beta));
347 }
348
352 public double getAlpha() {
353 return alpha;
354 }
355
359 public double getBeta() {
360 return beta;
361 }
362
366 public void setParams(double alpha, double beta) {
367 if (alpha <= 0.0)
368 throw new IllegalArgumentException("alpha <= 0");
369 if (beta <= 0.0)
370 throw new IllegalArgumentException("beta <= 0");
371
372 this.alpha = alpha;
373 this.beta = beta;
374 supportA = 0.0;
375 }
376
381 public double[] getParams() {
382 double[] retour = { alpha, beta };
383 return retour;
384 }
385
389 public String toString() {
390 return getClass().getSimpleName() + " : alpha = " + alpha + ", beta = " + beta;
391 }
392
393}
Classes implementing continuous distributions should inherit from this base class.
Extends DiscreteDistribution to an empirical distribution function, based on the observations (sorte...
double getMedian()
Returns the median.
static double getMean(double alpha, double beta)
Computes and returns the mean of the log-logistic distribution with parameters and.
double getVariance()
Returns the variance.
static double getStandardDeviation(double alpha, double beta)
Computes and returns the standard deviation of the log-logistic distribution with parameters and .
double getAlpha()
Return the parameter of this object.
double cdf(double x)
Returns the distribution function .
double getStandardDeviation()
Returns the standard deviation.
static double getVariance(double alpha, double beta)
Computes and returns the variance.
double density(double x)
Returns , the density evaluated at .
static double inverseF(double alpha, double beta, double u)
Computes the inverse of the log-logistic distribution with parameters and .
static LoglogisticDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a log-logistic distribution with parameters and estimated using the maxim...
double inverseF(double u)
Returns the inverse distribution function .
double barF(double x)
Returns the complementary distribution function.
double[] getParams()
Return a table containing the parameters of the current distribution.
static double barF(double alpha, double beta, double x)
Computes the complementary distribution function ( Fbarloglogistic ) of the log-logistic distribution...
void setParams(double alpha, double beta)
Sets the parameters and of this object.
static double cdf(double alpha, double beta, double x)
Computes the distribution function ( Floglogistic ) of the log-logistic distribution with parameters.
static double[] getMLE(double[] x, int n)
Estimates the parameters of the log-logistic distribution using the maximum likelihood method,...
LoglogisticDist(double alpha, double beta)
Constructs a log-logistic distribution with parameters and .
String toString()
Returns a String containing information about the current distribution.
static double density(double alpha, double beta, double x)
Computes the density function ( floglogistic ) for a log-logisitic distribution with parameters.
double getBeta()
Returns the parameter of this object.
This class provides miscellaneous functions that are hard to classify.
Definition Misc.java:33
static double quickSelect(double[] A, int n, int k)
Returns the smallest item of the array of size.
Definition Misc.java:47