SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
TriangularDist.java
1/*
2 * Class: TriangularDist
3 * Description: triangular 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 java.util.Arrays;
28
53 private double a;
54 private double b;
55 private double m;
56
62 public TriangularDist() {
63 setParams(0.0, 1.0, 0.5);
64 }
65
71 public TriangularDist(double m) {
72 setParams(0.0, 1.0, m);
73 }
74
80 public TriangularDist(double a, double b, double m) {
81 setParams(a, b, m);
82 }
83
84 public double density(double x) {
85 return density(a, b, m, x);
86 }
87
88 public double cdf(double x) {
89 return cdf(a, b, m, x);
90 }
91
92 public double barF(double x) {
93 return barF(a, b, m, x);
94 }
95
96 public double inverseF(double u) {
97 return inverseF(a, b, m, u);
98 }
99
100 public double getMean() {
101 return TriangularDist.getMean(a, b, m);
102 }
103
104 public double getVariance() {
105 return TriangularDist.getVariance(a, b, m);
106 }
107
108 public double getStandardDeviation() {
109 return TriangularDist.getStandardDeviation(a, b, m);
110 }
111
115 public static double density(double a, double b, double m, double x) {
116 if (m < a || m > b)
117 throw new IllegalArgumentException("m is not in [a,b]");
118 if (x < a || x > b)
119 return 0.0;
120 else if (x <= m && m != a)
121 return 2.0 * (x - a) / ((b - a) * (m - a));
122 else
123 return 2.0 * (b - x) / ((b - a) * (b - m));
124 }
125
129 public static double cdf(double a, double b, double m, double x) {
130 if (m < a || m > b)
131 throw new IllegalArgumentException("m is not in [a,b]");
132 if (x <= a)
133 return 0.0;
134 else if (x <= m && m != a)
135 return (x - a) * (x - a) / ((b - a) * (m - a));
136 else if (x < b)
137 return 1.0 - (b - x) * (b - x) / ((b - a) * (b - m));
138 else
139 return 1.0;
140 }
141
145 public static double barF(double a, double b, double m, double x) {
146 if (m < a || m > b)
147 throw new IllegalArgumentException("m is not in [a,b]");
148 if (x <= a)
149 return 1.0;
150 else if (x <= m && m != a)
151 return 1.0 - (x - a) * (x - a) / ((b - a) * (m - a));
152 else if (x < b)
153 return (b - x) * (b - x) / ((b - a) * (b - m));
154 else
155 return 0.0;
156 }
157
161 public static double inverseF(double a, double b, double m, double u) {
162 if (m < a || m > b)
163 throw new IllegalArgumentException("m is not in [a,b]");
164 if (u < 0.0 || u > 1.0)
165 throw new IllegalArgumentException("u is not in [0,1]");
166 if (u <= 0.0)
167 return a;
168 if (u >= 1.0)
169 return b;
170 // the code is taken and adapted from unuran
171 // file /distributions/c_triangular_gen.c
172 double h = (m - a) / (b - a);
173 return u <= h && m != a ? a + Math.sqrt((b - a) * (m - a) * u) : b - Math.sqrt((b - a) * (b - m) * (1 - u));
174 }
175
189 public static double[] getMLE(double[] x, int n, double a, double b) {
190 if (n <= 0)
191 throw new IllegalArgumentException("n <= 0");
192 double[] Y = new double[n]; // sorted x[i]
193 System.arraycopy(x, 0, Y, 0, n);
194 Arrays.sort(Y);
195
196 int rmax = -1;
197 double prodmax = -1.0e300;
198 final double ba = b - a;
199 double z;
200 int i;
201 for (int r = 0; r < n; r++) {
202 z = (Y[r] - a) / ba;
203 if ((z <= (double) r / n) || (z >= (double) (r + 1) / n))
204 continue; // MLE cannot be there
205 double prod = 1.0;
206 double d = Y[r] - a;
207 for (i = 0; i < r; i++)
208 prod *= (Y[i] - a) / d;
209
210 d = b - Y[r];
211 for (i = r + 1; i < n; i++)
212 prod *= (b - Y[i]) / d;
213
214 if (prod > prodmax) {
215 prodmax = prod;
216 rmax = r;
217 }
218 }
219
220 if (rmax < 0)
221 throw new UnsupportedOperationException(" data cannot fit a triangular distribution");
222
223 double[] param = new double[1];
224 param[0] = Y[rmax];
225 return param;
226 }
227
238 public static TriangularDist getInstanceFromMLE(double[] x, int n, double a, double b) {
239 double param[] = getMLE(x, n, a, b);
240 return new TriangularDist(a, b, param[0]);
241 }
242
249 public static double getMean(double a, double b, double m) {
250 if ((a == 0.0 && b == 1.0) && (m < 0 || m > 1))
251 throw new IllegalArgumentException("m is not in [0,1]");
252 else if (m < a || m > b)
253 throw new IllegalArgumentException("m is not in [a,b]");
254
255 return ((a + b + m) / 3.0);
256 }
257
265 public static double getVariance(double a, double b, double m) {
266 if ((a == 0.0 && b == 1.0) && (m < 0 || m > 1))
267 throw new IllegalArgumentException("m is not in [0,1]");
268 else if (m < a || m > b)
269 throw new IllegalArgumentException("m is not in [a,b]");
270
271 return ((a * a + b * b + m * m - a * b - a * m - b * m) / 18.0);
272 }
273
280 public static double getStandardDeviation(double a, double b, double m) {
281 return Math.sqrt(TriangularDist.getVariance(a, b, m));
282 }
283
287 public double getA() {
288 return a;
289 }
290
294 public double getB() {
295 return b;
296 }
297
301 public double getM() {
302 return m;
303 }
304
309 public void setParams(double a, double b, double m) {
310 if ((a == 0.0 && b == 1.0) && (m < 0 || m > 1))
311 throw new IllegalArgumentException("m is not in [0,1]");
312 else if (a >= b)
313 throw new IllegalArgumentException("a >= b");
314 else if (m < a || m > b)
315 throw new IllegalArgumentException("m is not in [a,b]");
316 this.a = a;
317 this.b = b;
318 this.m = m;
319 supportA = a;
320 supportB = b;
321 }
322
327 public double[] getParams() {
328 double[] retour = { a, b, m };
329 return retour;
330 }
331
335 public String toString() {
336 return getClass().getSimpleName() + " : a = " + a + ", b = " + b + ", m = " + m;
337 }
338
339}
Classes implementing continuous distributions should inherit from this base class.
TriangularDist(double a, double b, double m)
Constructs a TriangularDist object with parameters ,.
double[] getParams()
Return a table containing the parameters of the current distribution.
double cdf(double x)
Returns the distribution function .
static double[] getMLE(double[] x, int n, double a, double b)
Estimates the parameter of the triangular distribution using the maximum likelihood method,...
static double getMean(double a, double b, double m)
Computes and returns the mean of the triangular distribution with parameters , , .
double getB()
Returns the value of for this object.
double getM()
Returns the value of for this object.
static double density(double a, double b, double m, double x)
Computes the density function.
double getVariance()
Returns the variance.
double getA()
Returns the value of for this object.
static double getStandardDeviation(double a, double b, double m)
Computes and returns the standard deviation of the triangular distribution with parameters ,...
String toString()
Returns a String containing information about the current distribution.
double inverseF(double u)
Returns the inverse distribution function .
static double cdf(double a, double b, double m, double x)
Computes the distribution function.
TriangularDist(double m)
Constructs a TriangularDist object with parameters ,.
double density(double x)
Returns , the density evaluated at .
void setParams(double a, double b, double m)
Sets the value of the parameters , and for this object.
static double inverseF(double a, double b, double m, double u)
Computes the inverse distribution function.
static double barF(double a, double b, double m, double x)
Computes the complementary distribution function.
TriangularDist()
Constructs a TriangularDist object with default parameters.
static TriangularDist getInstanceFromMLE(double[] x, int n, double a, double b)
Creates a new instance of a triangular distribution with parameters a and b.
double getStandardDeviation()
Returns the standard deviation.
static double getVariance(double a, double b, double m)
Computes and returns the variance of the triangular distribution with parameters ,...
double barF(double x)
Returns the complementary distribution function.