25package umontreal.ssj.probdist;
27import umontreal.ssj.util.Num;
28import umontreal.ssj.util.RootFinder;
29import umontreal.ssj.functions.MathFunction;
51 private double lambda;
57 private double lnXi[];
58 private double sumLnXi = 0.0;
61 public Function(
double x[],
int n) {
63 this.xi =
new double[n];
64 this.lnXi =
new double[n];
66 for (
int i = 0; i < n; i++) {
69 this.lnXi[i] = Math.log(x[i]);
71 this.lnXi[i] = LN_EPS;
72 sumLnXi += this.lnXi[i];
76 public double evaluate(
double x) {
79 double sumXiLnXi = 0.0;
83 for (
int i = 0; i < n; i++) {
84 xalpha = Math.pow(this.xi[i], x);
85 sumXiLnXi += xalpha * lnXi[i];
89 return (x * (n * sumXiLnXi - sumLnXi * sumXi) - n * sumXi);
110 return density(alpha, lambda, delta, x);
113 public double cdf(
double x) {
114 return cdf(alpha, lambda, delta, x);
118 return barF(alpha, lambda, delta, x);
122 return inverseF(alpha, lambda, delta, u);
130 return WeibullDist.getVariance(alpha, lambda, delta);
134 return WeibullDist.getStandardDeviation(alpha, lambda, delta);
140 public static double density(
double alpha,
double lambda,
double delta,
double x) {
142 throw new IllegalArgumentException(
"alpha <= 0");
144 throw new IllegalArgumentException(
"lambda <= 0");
147 double y = Math.log(lambda * (x - delta)) * alpha;
152 return alpha * (y / (x - delta)) * Math.exp(-y);
158 public static double density(
double alpha,
double x) {
159 return density(alpha, 1.0, 0.0, x);
165 public static double cdf(
double alpha,
double lambda,
double delta,
double x) {
167 throw new IllegalArgumentException(
"alpha <= 0");
169 throw new IllegalArgumentException(
"lambda <= 0");
172 if ((lambda * (x - delta) >= XBIG) && (alpha >= 1.0))
174 double y = Math.log(lambda * (x - delta)) * alpha;
178 return -Math.expm1(-y);
184 public static double cdf(
double alpha,
double x) {
185 return cdf(alpha, 1.0, 0.0, x);
191 public static double barF(
double alpha,
double lambda,
double delta,
double x) {
193 throw new IllegalArgumentException(
"alpha <= 0");
195 throw new IllegalArgumentException(
"lambda <= 0");
201 double temp = Math.log(lambda * (x - delta)) * alpha;
204 temp = Math.exp(temp);
205 return Math.exp(-temp);
211 public static double barF(
double alpha,
double x) {
212 return barF(alpha, 1.0, 0.0, x);
218 public static double inverseF(
double alpha,
double lambda,
double delta,
double u) {
221 throw new IllegalArgumentException(
"alpha <= 0");
223 throw new IllegalArgumentException(
"lambda <= 0");
225 if (u < 0.0 || u > 1.0)
226 throw new IllegalArgumentException(
"u not in [0, 1]");
230 return Double.POSITIVE_INFINITY;
234 throw new ArithmeticException(
"inverse function cannot be positive infinity");
236 return Math.pow(t, 1.0 / alpha) / lambda + delta;
242 public static double inverseF(
double alpha,
double x) {
243 return inverseF(alpha, 1.0, 0.0, x);
246 private static double[] getMaximumLikelihoodEstimate(
double[] x,
int n,
double delta) {
248 throw new IllegalArgumentException(
"n <= 0");
250 throw new IllegalArgumentException(
"delta must be equal to 0");
257 for (
int i = 0; i < n; i++) {
261 lnxi = Math.log(x[i]);
263 sumLn2 += lnxi * lnxi;
266 double alpha0 = Math.sqrt((
double) n / ((6.0 / (Math.PI * Math.PI)) * (sumLn2 - sumLn * sumLn / (
double) n)));
267 double a = alpha0 - 20.0;
271 double param[] =
new double[3];
273 Function f =
new Function(x, n);
274 param[0] = RootFinder.brentDekker(a, alpha0 + 20.0, f, 1e-5);
276 double sumXalpha = 0.0;
277 for (
int i = 0; i < n; i++)
278 sumXalpha += Math.pow(x[i], param[0]);
279 param[1] = Math.pow((
double) n / sumXalpha, 1.0 / param[0]);
303 public static double[]
getMLE(
double[] x,
int n) {
304 return getMaximumLikelihoodEstimate(x, n, 0.0);
317 double param[] =
getMLE(x, n);
318 return new WeibullDist(param[0], param[1], param[2]);
330 public static double getMean(
double alpha,
double lambda,
double delta) {
332 throw new IllegalArgumentException(
"alpha <= 0");
334 throw new IllegalArgumentException(
"lambda <= 0");
336 return (delta + Math.exp(
Num.
lnGamma(1.0 + 1.0 / alpha)) / lambda);
349 public static double getVariance(
double alpha,
double lambda,
double delta) {
353 throw new IllegalArgumentException(
"alpha <= 0");
355 throw new IllegalArgumentException(
"lambda <= 0");
357 gAlpha = Math.exp(
Num.
lnGamma(1.0 / alpha + 1.0));
359 return (Math.abs(Math.exp(
Num.
lnGamma(2 / alpha + 1)) - gAlpha * gAlpha) / (lambda * lambda));
369 return Math.sqrt(
WeibullDist.getVariance(alpha, lambda, delta));
397 public void setParams(
double alpha,
double lambda,
double delta) {
399 throw new IllegalArgumentException(
"alpha <= 0");
401 throw new IllegalArgumentException(
"lambda <= 0");
404 this.lambda = lambda;
414 double[] retour = { alpha, lambda, delta };
422 return getClass().getSimpleName() +
" : alpha = " + alpha +
", lambda = " + lambda +
", delta = " + delta;
Classes implementing continuous distributions should inherit from this base class.
WeibullDist(double alpha)
Constructs a WeibullDist object with parameters = alpha, = 1, and = 0.
double getStandardDeviation()
Returns the standard deviation.
String toString()
Returns a String containing information about the current distribution.
static double getMean(double alpha, double lambda, double delta)
Computes and returns the mean of the Weibull distribution with parameters ,.
double getMean()
Returns the mean.
static double barF(double alpha, double x)
Same as barF (alpha, 1, 0, x).
WeibullDist(double alpha, double lambda, double delta)
Constructs a WeibullDist object with parameters alpha, = lambda, and = delta.
static double inverseF(double alpha, double lambda, double delta, double u)
Computes the inverse of the distribution function.
static WeibullDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a Weibull distribution with parameters.
static double barF(double alpha, double lambda, double delta, double x)
Computes the complementary distribution function.
double inverseF(double u)
Returns the inverse distribution function .
double getAlpha()
Returns the parameter .
static double getVariance(double alpha, double lambda, double delta)
Computes and returns the variance.
static double inverseF(double alpha, double x)
Same as inverseF (alpha, 1, 0, x).
double density(double x)
Returns , the density evaluated at .
double getVariance()
Returns the variance.
static double getStandardDeviation(double alpha, double lambda, double delta)
Computes and returns the standard deviation of the Weibull distribution with parameters ,...
static double[] getMLE(double[] x, int n)
Estimates the parameters of the Weibull distribution, assuming that , using the maximum likelihood m...
static double density(double alpha, double x)
Same as density (alpha, 1, 0, x).
double getDelta()
Returns the parameter .
double barF(double x)
Returns the complementary distribution function.
static double density(double alpha, double lambda, double delta, double x)
Computes the density function.
double getLambda()
Returns the parameter .
double cdf(double x)
Returns the distribution function .
double[] getParams()
Return a table containing the parameters of the current distribution.
static double cdf(double alpha, double x)
Same as cdf (alpha, 1, 0, x).
static double cdf(double alpha, double lambda, double delta, double x)
Computes the distribution function.
void setParams(double alpha, double lambda, double delta)
Sets the parameters , and for this object.
This class provides various constants and methods to compute numerical quantities such as factorials,...
static final double LN_DBL_MIN
Natural logarithm of DBL_MIN.
static double lnGamma(double x)
Returns the natural logarithm of the gamma function evaluated at x.
static final int DBL_MAX_10_EXP
Largest int such that is representable (approximately) as a double.
static final int DBL_MAX_EXP
Largest int such that is representable (approximately) as a double.
static final double LN2
The values of .
This interface should be implemented by classes which represent univariate mathematical functions.