SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Pearson6Dist.java
1/*
2 * Class: Pearson6Dist
3 * Description: Pearson type VI 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 optimization.*;
29
54 protected double alpha1;
55 protected double alpha2;
56 protected double beta;
57 protected double logBeta; // Ln (Beta (alpha1, alpha2))
58
59 private static class Optim implements Uncmin_methods {
60 private int n;
61 private double[] x;
62
63 public Optim(double[] x, int n) {
64 this.n = n;
65 this.x = new double[n];
66 System.arraycopy(x, 0, this.x, 0, n);
67 }
68
69 public double f_to_minimize(double[] param) {
70
71 if ((param[1] <= 0.0) || (param[2] <= 0.0) || (param[3] <= 0.0))
72 return 1e200;
73
74 double sumLogY = 0.0;
75 double sumLog1_Y = 0.0;
76 for (int i = 0; i < n; i++) {
77 if (x[i] > 0.0)
78 sumLogY += Math.log(x[i] / param[3]);
79 else
80 sumLogY -= 709.0;
81 sumLog1_Y += Math.log1p(x[i] / param[3]);
82 }
83
84 return (n * (Math.log(param[3]) + Num.lnBeta(param[1], param[2])) - (param[1] - 1.0) * sumLogY
85 + (param[1] + param[2]) * sumLog1_Y);
86 }
87
88 public void gradient(double[] x, double[] g) {
89 }
90
91 public void hessian(double[] x, double[][] h) {
92 }
93 }
94
99 public Pearson6Dist(double alpha1, double alpha2, double beta) {
100 setParam(alpha1, alpha2, beta);
101 }
102
103 public double density(double x) {
104 if (x <= 0.0)
105 return 0.0;
106 return Math.exp((alpha1 - 1.0) * Math.log(x / beta) - (logBeta + (alpha1 + alpha2) * Math.log1p(x / beta)))
107 / beta;
108 }
109
110 public double cdf(double x) {
111 return cdf(alpha1, alpha2, beta, x);
112 }
113
114 public double barF(double x) {
115 return barF(alpha1, alpha2, beta, x);
116 }
117
118 public double inverseF(double u) {
119 return inverseF(alpha1, alpha2, beta, u);
120 }
121
122 public double getMean() {
123 return getMean(alpha1, alpha2, beta);
124 }
125
126 public double getVariance() {
127 return getVariance(alpha1, alpha2, beta);
128 }
129
130 public double getStandardDeviation() {
131 return getStandardDeviation(alpha1, alpha2, beta);
132 }
133
139 public static double density(double alpha1, double alpha2, double beta, double x) {
140 if (alpha1 <= 0.0)
141 throw new IllegalArgumentException("alpha1 <= 0");
142 if (alpha2 <= 0.0)
143 throw new IllegalArgumentException("alpha2 <= 0");
144 if (beta <= 0.0)
145 throw new IllegalArgumentException("beta <= 0");
146 if (x <= 0.0)
147 return 0.0;
148
149 return Math.exp((alpha1 - 1.0) * Math.log(x / beta)
150 - (Num.lnBeta(alpha1, alpha2) + (alpha1 + alpha2) * Math.log1p(x / beta))) / beta;
151 }
152
158 public static double cdf(double alpha1, double alpha2, double beta, double x) {
159 if (alpha1 <= 0.0)
160 throw new IllegalArgumentException("alpha1 <= 0");
161 if (alpha2 <= 0.0)
162 throw new IllegalArgumentException("alpha2 <= 0");
163 if (beta <= 0.0)
164 throw new IllegalArgumentException("beta <= 0");
165 if (x <= 0.0)
166 return 0.0;
167
168 return BetaDist.cdf(alpha1, alpha2, x / (x + beta));
169 }
170
177 public static double barF(double alpha1, double alpha2, double beta, double x) {
178 if (alpha1 <= 0.0)
179 throw new IllegalArgumentException("alpha1 <= 0");
180 if (alpha2 <= 0.0)
181 throw new IllegalArgumentException("alpha2 <= 0");
182 if (beta <= 0.0)
183 throw new IllegalArgumentException("beta <= 0");
184 if (x <= 0.0)
185 return 1.0;
186
187 return BetaDist.barF(alpha1, alpha2, x / (x + beta));
188 }
189
196 public static double inverseF(double alpha1, double alpha2, double beta, double u) {
197 if (alpha1 <= 0.0)
198 throw new IllegalArgumentException("alpha1 <= 0");
199 if (alpha2 <= 0.0)
200 throw new IllegalArgumentException("alpha2 <= 0");
201 if (beta <= 0.0)
202 throw new IllegalArgumentException("beta <= 0");
203
204 double y = BetaDist.inverseF(alpha1, alpha2, u);
205
206 return ((y * beta) / (1.0 - y));
207 }
208
223 public static double[] getMLE(double[] x, int n) {
224 if (n <= 0)
225 throw new IllegalArgumentException("n <= 0");
226
227 double[] parameters = new double[3];
228 double[] xpls = new double[4];
229 double[] param = new double[4];
230 double[] fpls = new double[4];
231 double[] gpls = new double[4];
232 int[] itrcmd = new int[2];
233 double[][] h = new double[4][4];
234 double[] udiag = new double[4];
235
236 Optim system = new Optim(x, n);
237
238 double mean = 0.0;
239 double mean2 = 0.0;
240 double mean3 = 0.0;
241 for (int i = 0; i < n; i++) {
242 mean += x[i];
243 mean2 += x[i] * x[i];
244 mean3 += x[i] * x[i] * x[i];
245 }
246 mean /= (double) n;
247 mean2 /= (double) n;
248 mean3 /= (double) n;
249
250 double r1 = mean2 / (mean * mean);
251 double r2 = mean2 * mean / mean3;
252
253 param[1] = -(2.0 * (-1.0 + r1 * r2)) / (-2.0 + r1 + r1 * r2);
254 if (param[1] <= 0)
255 param[1] = 1;
256 param[2] = (-3.0 - r2 + 4.0 * r1 * r2) / (-1.0 - r2 + 2.0 * r1 * r2);
257 if (param[2] <= 0)
258 param[2] = 1;
259 param[3] = (param[2] - 1.0) * mean / param[1];
260 if (param[3] <= 0)
261 param[3] = 1;
262
263 Uncmin_f77.optif0_f77(3, param, system, xpls, fpls, gpls, itrcmd, h, udiag);
264
265 for (int i = 0; i < 3; i++)
266 parameters[i] = xpls[i + 1];
267
268 return parameters;
269 }
270
280 public static Pearson6Dist getInstanceFromMLE(double[] x, int n) {
281 double parameters[] = getMLE(x, n);
282 return new Pearson6Dist(parameters[0], parameters[1], parameters[2]);
283 }
284
290 public static double getMean(double alpha1, double alpha2, double beta) {
291 if (alpha1 <= 0.0)
292 throw new IllegalArgumentException("alpha1 <= 0");
293 if (alpha2 <= 1.0)
294 throw new IllegalArgumentException("alpha2 <= 1");
295 if (beta <= 0.0)
296 throw new IllegalArgumentException("beta <= 0");
297
298 return ((beta * alpha1) / (alpha2 - 1.0));
299 }
300
307 public static double getVariance(double alpha1, double alpha2, double beta) {
308 if (alpha1 <= 0.0)
309 throw new IllegalArgumentException("alpha1 <= 0");
310 if (alpha2 <= 0.0)
311 throw new IllegalArgumentException("alpha2 <= 2");
312 if (beta <= 0.0)
313 throw new IllegalArgumentException("beta <= 0");
314
315 return (((beta * beta) * alpha1 * (alpha1 + alpha2 - 1.0)) / ((alpha2 - 1.0) * (alpha2 - 1.0) * (alpha2 - 2.0)));
316 }
317
324 public static double getStandardDeviation(double alpha1, double alpha2, double beta) {
325 return Math.sqrt(getVariance(alpha1, alpha2, beta));
326 }
327
331 public double getAlpha1() {
332 return alpha1;
333 }
334
338 public double getAlpha2() {
339 return alpha2;
340 }
341
345 public double getBeta() {
346 return beta;
347 }
348
354 public void setParam(double alpha1, double alpha2, double beta) {
355 if (alpha1 <= 0.0)
356 throw new IllegalArgumentException("alpha1 <= 0");
357 if (alpha2 <= 0.0)
358 throw new IllegalArgumentException("alpha2 <= 0");
359 if (beta <= 0.0)
360 throw new IllegalArgumentException("beta <= 0");
361 supportA = 0.0;
362 this.alpha1 = alpha1;
363 this.alpha2 = alpha2;
364 this.beta = beta;
365 logBeta = Num.lnBeta(alpha1, alpha2);
366 }
367
372 public double[] getParams() {
373 double[] retour = { alpha1, alpha2, beta };
374 return retour;
375 }
376
380 public String toString() {
381 return getClass().getSimpleName() + " : alpha1 = " + alpha1 + ", alpha2 = " + alpha2 + ", beta = " + beta;
382 }
383
384}
Extends the class ContinuousDistribution for the beta distribution.
Definition BetaDist.java:54
double inverseF(double u)
Returns the inverse distribution function .
double barF(double x)
Returns the complementary distribution function.
double cdf(double x)
Returns the distribution function .
Classes implementing continuous distributions should inherit from this base class.
double cdf(double x)
Returns the distribution function .
double[] getParams()
Return a table containing the parameters of the current distribution.
double inverseF(double u)
Returns the inverse distribution function .
double getAlpha2()
Returns the parameter of this object.
Pearson6Dist(double alpha1, double alpha2, double beta)
Constructs a Pearson6Dist object with parameters = alpha1, = alpha2 and = beta.
static double[] getMLE(double[] x, int n)
Estimates the parameters of the Pearson VI distribution using the maximum likelihood method,...
static double density(double alpha1, double alpha2, double beta, double x)
Computes the density function of a Pearson VI distribution with shape parameters and ,...
double getMean()
Returns the mean.
static double getStandardDeviation(double alpha1, double alpha2, double beta)
Computes and returns the standard deviation of a Pearson VI distribution with shape parameters and.
double getAlpha1()
Returns the parameter of this object.
static double getVariance(double alpha1, double alpha2, double beta)
Computes and returns the variance of a Pearson VI distribution with shape parameters and ,...
String toString()
Returns a String containing information about the current distribution.
void setParam(double alpha1, double alpha2, double beta)
Sets the parameters , and.
double getVariance()
Returns the variance.
double barF(double x)
Returns the complementary distribution function.
double getBeta()
Returns the parameter of this object.
static double inverseF(double alpha1, double alpha2, double beta, double u)
Computes the inverse distribution function of a Pearson VI distribution with shape parameters and.
static double cdf(double alpha1, double alpha2, double beta, double x)
Computes the distribution function of a Pearson VI distribution with shape parameters and ,...
double getStandardDeviation()
Returns the standard deviation.
static Pearson6Dist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a Pearson VI distribution with parameters.
double density(double x)
Returns , the density evaluated at .
static double barF(double alpha1, double alpha2, double beta, double x)
Computes the complementary distribution function of a Pearson VI distribution with shape parameters ...
static double getMean(double alpha1, double alpha2, double beta)
Computes and returns the mean of a Pearson VI distribution with shape parameters and ,...
This class provides various constants and methods to compute numerical quantities such as factorials,...
Definition Num.java:35
static double lnBeta(double lam, double nu)
Computes the natural logarithm of the Beta function .
Definition Num.java:475