SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
LogisticDist.java
1/*
2 * Class: LogisticDist
3 * Description: 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 optimization.*;
28
49 private double alpha;
50 private double lambda;
51
52 private static class Optim implements Lmder_fcn {
53 protected double[] xi;
54 protected int n;
55
56 public Optim(double[] x, int n) {
57 this.n = n;
58 this.xi = new double[n];
59 System.arraycopy(x, 0, this.xi, 0, n);
60 }
61
62 public void fcn(int m, int n, double[] x, double[] fvec, double[][] fjac, int iflag[]) {
63 if (x[2] <= 0.0) {
64 final double BIG = 1.0e100;
65 fvec[1] = BIG;
66 fvec[2] = BIG;
67 fjac[1][1] = BIG;
68 fjac[1][2] = 0.0;
69 fjac[2][1] = 0.0;
70 fjac[2][2] = BIG;
71 return;
72 }
73
74 double sum;
75 double prod;
76
77 if (iflag[1] == 1) {
78 sum = 0.0;
79 for (int i = 0; i < n; i++)
80 sum += (1.0 / (1.0 + Math.exp(x[2] * (xi[i] - x[1]))));
81 fvec[1] = sum - n / 2.0;
82
83 sum = 0.0;
84 for (int i = 0; i < n; i++) {
85 prod = x[2] * (xi[i] - x[1]);
86 sum -= prod * Math.tanh(prod / 2.0);
87 }
88 fvec[2] = sum - n;
89 } else if (iflag[1] == 2) {
90 sum = 0.0;
91 for (int i = 0; i < n; i++) {
92 prod = Math.exp(x[2] * (xi[i] - x[1]));
93 sum -= x[2] * prod / ((1 + prod) * (1 + prod));
94 }
95 fjac[1][1] = sum;
96
97 sum = 0.0;
98 for (int i = 0; i < n; i++) {
99 prod = Math.exp(x[2] * (xi[i] - x[1]));
100 sum -= (xi[i] - x[1]) * prod / ((1 + prod) * (1 + prod));
101 }
102 fjac[1][2] = sum;
103
104 sum = 0.0;
105 for (int i = 0; i < n; i++) {
106 prod = Math.exp(x[2] * (xi[i] - x[1]));
107 sum -= (x[2] * ((-1.0 + prod) * (1.0 + prod) - (2.0 * (x[2] * (xi[i] - x[1])) * prod)))
108 / ((1.0 + prod) * (1.0 + prod));
109 }
110 fjac[2][1] = sum;
111
112 sum = 0.0;
113 for (int i = 0; i < n; i++) {
114 prod = Math.exp(x[2] * (xi[i] - x[1]));
115 sum -= ((x[1] - xi[1]) * ((-1.0 + prod) * (1.0 + prod) - (2.0 * (x[2] * (xi[i] - x[1])) * prod)))
116 / ((1.0 + prod) * (1.0 + prod));
117 }
118 fjac[2][2] = sum;
119 }
120 }
121 }
122
128 public LogisticDist() {
129 setParams(0.0, 1.0);
130 }
131
136 public LogisticDist(double alpha, double lambda) {
137 setParams(alpha, lambda);
138 }
139
140 public double density(double x) {
141 return density(alpha, lambda, x);
142 }
143
144 public double cdf(double x) {
145 return cdf(alpha, lambda, x);
146 }
147
148 public double barF(double x) {
149 return barF(alpha, lambda, x);
150 }
151
152 public double inverseF(double u) {
153 return inverseF(alpha, lambda, u);
154 }
155
156 public double getMean() {
157 return LogisticDist.getMean(alpha, lambda);
158 }
159
160 public double getVariance() {
161 return LogisticDist.getVariance(alpha, lambda);
162 }
163
164 public double getStandardDeviation() {
165 return LogisticDist.getStandardDeviation(alpha, lambda);
166 }
167
171 public static double density(double alpha, double lambda, double x) {
172 if (lambda <= 0)
173 throw new IllegalArgumentException("lambda <= 0");
174 double z = lambda * (x - alpha);
175 if (z >= -100.0) {
176 double v = Math.exp(-z);
177 return lambda * v / ((1.0 + v) * (1.0 + v));
178 }
179 return lambda * Math.exp(z);
180 }
181
185 public static double cdf(double alpha, double lambda, double x) {
186 if (lambda <= 0)
187 throw new IllegalArgumentException("lambda <= 0");
188 double z = lambda * (x - alpha);
189 if (z >= -100.0)
190 return 1.0 / (1.0 + Math.exp(-z));
191 return Math.exp(z);
192 }
193
197 public static double barF(double alpha, double lambda, double x) {
198 if (lambda <= 0)
199 throw new IllegalArgumentException("lambda <= 0");
200 double z = lambda * (x - alpha);
201 if (z <= 100.0)
202 return 1.0 / (1.0 + Math.exp(z));
203 return Math.exp(-z);
204 }
205
209 public static double inverseF(double alpha, double lambda, double u) {
210 if (lambda <= 0)
211 throw new IllegalArgumentException("lambda <= 0");
212 if (u < 0.0 || u > 1.0)
213 throw new IllegalArgumentException("u not in [0, 1]");
214 if (u >= 1.0)
215 return Double.POSITIVE_INFINITY;
216 if (u <= 0.0)
217 return Double.NEGATIVE_INFINITY;
218
219 return Math.log(u / (1.0 - u)) / lambda + alpha;
220 }
221
240 public static double[] getMLE(double[] x, int n) {
241 if (n <= 0)
242 throw new IllegalArgumentException("n <= 0");
243
244 double sum = 0.0;
245 for (int i = 0; i < n; i++)
246 sum += x[i];
247
248 double[] param = new double[3];
249 param[1] = sum / (double) n;
250
251 sum = 0.0;
252 for (int i = 0; i < n; i++)
253 sum += ((x[i] - param[1]) * (x[i] - param[1]));
254
255 param[2] = Math.sqrt(Math.PI * Math.PI * n / (3.0 * sum));
256
257 double[] fvec = new double[3];
258 double[][] fjac = new double[3][3];
259 int[] iflag = new int[2];
260 int[] info = new int[2];
261 int[] ipvt = new int[3];
262 Optim system = new Optim(x, n);
263
264 Minpack_f77.lmder1_f77(system, 2, 2, param, fvec, fjac, 1e-5, info, ipvt);
265
266 double parameters[] = new double[2];
267 parameters[0] = param[1];
268 parameters[1] = param[2];
269
270 return parameters;
271 }
272
282 public static LogisticDist getInstanceFromMLE(double[] x, int n) {
283 double parameters[] = getMLE(x, n);
284 return new LogisticDist(parameters[0], parameters[1]);
285 }
286
293 public static double getMean(double alpha, double lambda) {
294 if (lambda <= 0.0)
295 throw new IllegalArgumentException("lambda <= 0");
296
297 return alpha;
298 }
299
308 public static double getVariance(double alpha, double lambda) {
309 if (lambda <= 0.0)
310 throw new IllegalArgumentException("lambda <= 0");
311
312 return ((Math.PI * Math.PI / 3) * (1 / (lambda * lambda)));
313 }
314
321 public static double getStandardDeviation(double alpha, double lambda) {
322 if (lambda <= 0.0)
323 throw new IllegalArgumentException("lambda <= 0");
324
325 return (Math.sqrt(1.0 / 3.0) * Math.PI / lambda);
326 }
327
331 public double getAlpha() {
332 return alpha;
333 }
334
338 public double getLambda() {
339 return lambda;
340 }
341
345 public void setParams(double alpha, double lambda) {
346 if (lambda <= 0)
347 throw new IllegalArgumentException("lambda <= 0");
348 this.alpha = alpha;
349 this.lambda = lambda;
350 }
351
356 public double[] getParams() {
357 double[] retour = { alpha, lambda };
358 return retour;
359 }
360
364 public String toString() {
365 return getClass().getSimpleName() + " : alpha = " + alpha + ", lambda = " + lambda;
366 }
367
368}
Classes implementing continuous distributions should inherit from this base class.
double getVariance()
Returns the variance.
static double getMean(double alpha, double lambda)
Computes and returns the mean of the logistic distribution with parameters and .
static double[] getMLE(double[] x, int n)
Estimates the parameters of the logistic distribution using the maximum likelihood method,...
double[] getParams()
Return a table containing the parameters of the current distribution.
static double density(double alpha, double lambda, double x)
Computes the density function .
double cdf(double x)
Returns the distribution function .
LogisticDist()
Constructs a LogisticDist object with default parameters.
static double getStandardDeviation(double alpha, double lambda)
Computes and returns the standard deviation of the logistic distribution with parameters and .
LogisticDist(double alpha, double lambda)
Constructs a LogisticDist object with parameters = alpha and = lambda.
double density(double x)
Returns , the density evaluated at .
static double barF(double alpha, double lambda, double x)
Computes the complementary distribution function .
double getStandardDeviation()
Returns the standard deviation.
double getLambda()
Returns the parameter of this object.
double barF(double x)
Returns the complementary distribution function.
double getMean()
Returns the mean.
static double getVariance(double alpha, double lambda)
Computes and returns the variance of the logistic distribution with parameters.
static LogisticDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a logistic distribution with parameters.
double inverseF(double u)
Returns the inverse distribution function .
void setParams(double alpha, double lambda)
Sets the parameters and of this object.
static double inverseF(double alpha, double lambda, double u)
Computes the inverse distribution function .
String toString()
Returns a String containing information about the current distribution.
double getAlpha()
Return the parameter of this object.
static double cdf(double alpha, double lambda, double x)
Computes the distribution function .