25package umontreal.ssj.probdist;
27import java.util.Arrays;
88 public double cdf(
double x) {
89 return cdf(a, b, m, x);
92 public double barF(
double x) {
93 return barF(a, b, m, x);
115 public static double density(
double a,
double b,
double m,
double x) {
117 throw new IllegalArgumentException(
"m is not in [a,b]");
120 else if (x <= m && m != a)
121 return 2.0 * (x - a) / ((b - a) * (m - a));
123 return 2.0 * (b - x) / ((b - a) * (b - m));
129 public static double cdf(
double a,
double b,
double m,
double x) {
131 throw new IllegalArgumentException(
"m is not in [a,b]");
134 else if (x <= m && m != a)
135 return (x - a) * (x - a) / ((b - a) * (m - a));
137 return 1.0 - (b - x) * (b - x) / ((b - a) * (b - m));
145 public static double barF(
double a,
double b,
double m,
double x) {
147 throw new IllegalArgumentException(
"m is not in [a,b]");
150 else if (x <= m && m != a)
151 return 1.0 - (x - a) * (x - a) / ((b - a) * (m - a));
153 return (b - x) * (b - x) / ((b - a) * (b - m));
161 public static double inverseF(
double a,
double b,
double m,
double u) {
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]");
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));
189 public static double[]
getMLE(
double[] x,
int n,
double a,
double b) {
191 throw new IllegalArgumentException(
"n <= 0");
192 double[] Y =
new double[n];
193 System.arraycopy(x, 0, Y, 0, n);
197 double prodmax = -1.0e300;
198 final double ba = b - a;
201 for (
int r = 0; r < n; r++) {
203 if ((z <= (
double) r / n) || (z >= (
double) (r + 1) / n))
207 for (i = 0; i < r; i++)
208 prod *= (Y[i] - a) / d;
211 for (i = r + 1; i < n; i++)
212 prod *= (b - Y[i]) / d;
214 if (prod > prodmax) {
221 throw new UnsupportedOperationException(
" data cannot fit a triangular distribution");
223 double[] param =
new double[1];
239 double param[] =
getMLE(x, n, a, b);
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]");
255 return ((a + b + m) / 3.0);
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]");
271 return ((a * a + b * b + m * m - a * b - a * m - b * m) / 18.0);
310 if ((a == 0.0 && b == 1.0) && (m < 0 || m > 1))
311 throw new IllegalArgumentException(
"m is not in [0,1]");
313 throw new IllegalArgumentException(
"a >= b");
314 else if (m < a || m > b)
315 throw new IllegalArgumentException(
"m is not in [a,b]");
328 double[] retour = { a, b, m };
336 return getClass().getSimpleName() +
" : a = " + a +
", b = " + b +
", m = " + m;
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.
double getMean()
Returns the mean.
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.