25package umontreal.ssj.probdist;
27import umontreal.ssj.util.Num;
28import umontreal.ssj.functions.MathFunction;
29import umontreal.ssj.util.RootFinder;
51 private double lambda;
52 private double logFactor;
53 private static final double ALIM = 1.0E5;
58 private double empiricalMean;
61 public Function(
int n,
double empiricalMean,
double sumLn) {
63 this.empiricalMean = empiricalMean;
67 public double evaluate(
double x) {
70 return (n * Math.log(empiricalMean / x) + n *
Num.
digamma(x) - sumLn);
77 protected double alp, u;
79 public myFunc(
double alp,
int d,
double u) {
85 public double evaluate(
double x) {
95 setParams(alpha, 1.0, decPrec);
103 setParams(alpha, lambda, decPrec);
112 setParams(alpha, lambda, d);
115 static double mybelog(
double x)
123 return 2.0 * (Math.log(x) - 1.0) / x;
128 if (x < 0.9 || x > 1.1) {
129 double w = (t + x * Math.log(x)) / (t * t);
135 final double EPS = 1.0e-12;
141 term = tpow / (j * (j - 1));
144 }
while (Math.abs(term / sum) > EPS);
151 double z = logFactor + (alpha - 1.0) * Math.log(x) - lambda * x;
158 public double cdf(
double x) {
159 return cdf(alpha, lambda, decPrec, x);
163 return barF(alpha, lambda, decPrec, x);
167 return inverseF(alpha, decPrec, u) / lambda;
175 return GammaDist.getVariance(alpha, lambda);
179 return GammaDist.getStandardDeviation(alpha, lambda);
186 public static double density(
double alpha,
double lambda,
double x) {
188 throw new IllegalArgumentException(
"alpha <= 0");
190 throw new IllegalArgumentException(
"lambda <= 0");
194 double z = alpha * Math.log(lambda * x) - lambda * x -
Num.
lnGamma(alpha);
196 return Math.exp(z) / x;
212 public static double cdf(
double alpha,
double lambda,
int d,
double x) {
213 return cdf(alpha, d, lambda * x);
219 public static double cdf(
double alpha,
int d,
double x) {
221 throw new IllegalArgumentException(
"alpha <= 0");
223 throw new IllegalArgumentException(
"d <= 0");
230 if (x > alpha * 10.0)
238 double d2 = x + 1.0 / 3.0 - alpha - 0.02 / alpha;
239 double S = alpha - 1.0 / 2.0;
240 double w = mybelog(S / x);
241 double y = d2 * Math.sqrt(w / x);
245 if (x <= 1.0 || x < alpha) {
246 double factor, z, rn, term;
247 factor = Math.exp(alpha * Math.log(x) - x -
Num.
lnGamma(alpha));
248 final double EPS = EPSARRAY[d];
256 }
while (term >= EPS * z);
257 return z * factor / alpha;
260 return 1.0 -
barF(alpha, d, x);
266 public static double barF(
double alpha,
double lambda,
int d,
double x) {
267 return barF(alpha, d, lambda * x);
273 public static double barF(
double alpha,
int d,
double x) {
275 throw new IllegalArgumentException(
"alpha <= 0");
277 throw new IllegalArgumentException(
"d <= 0");
284 if (x >= alpha * XBIG)
292 double d2 = x + 1.0 / 3.0 - alpha - 0.02 / alpha;
293 double S = alpha - 1.0 / 2.0;
294 double w = mybelog(S / x);
295 double y = d2 * Math.sqrt(w / x);
299 if (x <= 1.0 || x < alpha)
300 return 1.0 -
cdf(alpha, d, x);
302 double[] V =
new double[6];
303 final double EPS = EPSARRAY[d];
304 final double RENORM = 1.0E100;
307 double factor = Math.exp(alpha * Math.log(x) - x -
Num.
lnGamma(alpha));
309 double A = 1.0 - alpha;
310 double B = A + x + 1.0;
316 double res = V[2] / V[3];
322 V[4] = B * V[2] - A * term * V[0];
323 V[5] = B * V[3] - A * term * V[1];
326 dif = Math.abs(res - R);
331 for (i = 0; i < 4; i++)
333 if (Math.abs(V[4]) >= RENORM) {
334 for (i = 0; i < 4; i++)
343 public static double inverseF(
double alpha,
double lambda,
int d,
double u) {
344 return inverseF(alpha, d, u) / lambda;
350 public static double inverseF(
double alpha,
int d,
double u) {
352 throw new IllegalArgumentException(
"alpha <= 0");
353 if (u > 1.0 || u < 0.0)
354 throw new IllegalArgumentException(
"u not in [0,1]");
358 return Double.POSITIVE_INFINITY;
360 throw new IllegalArgumentException(
"d <= 0");
363 final double EPS = Math.pow(10.0, -d);
365 double sigma =
GammaDist.getStandardDeviation(alpha, 1.0);
374 xmax = alpha + 40.0 * sigma;
375 myFunc f =
new myFunc(alpha, d, u);
377 if (u <= 1.0e-8 || alpha <= 1.5) {
409 public static double[]
getMLE(
double[] x,
int n) {
413 double empiricalMean;
418 parameters =
new double[2];
420 throw new IllegalArgumentException(
"n <= 0");
421 for (
int i = 0; i < n; i++) {
426 sumLn += Math.log(x[i]);
428 empiricalMean = sum / (double) n;
431 for (
int i = 0; i < n; i++) {
432 sum += (x[i] - empiricalMean) * (x[i] - empiricalMean);
435 alphaMME = (empiricalMean * empiricalMean * (double) n) / sum;
436 if ((a = alphaMME - 10.0) <= 0) {
440 Function f =
new Function(n, empiricalMean, sumLn);
442 parameters[1] = parameters[0] / empiricalMean;
457 double parameters[] =
getMLE(x, n);
458 return new GammaDist(parameters[0], parameters[1]);
467 public static double getMean(
double alpha,
double lambda) {
469 throw new IllegalArgumentException(
"alpha <= 0");
471 throw new IllegalArgumentException(
"lambda <= 0");
473 return (alpha / lambda);
486 throw new IllegalArgumentException(
"alpha <= 0");
488 throw new IllegalArgumentException(
"lambda <= 0");
490 return (alpha / (lambda * lambda));
501 throw new IllegalArgumentException(
"alpha <= 0");
503 throw new IllegalArgumentException(
"lambda <= 0");
505 return (Math.sqrt(alpha) / lambda);
522 public void setParams(
double alpha,
double lambda,
int d) {
524 throw new IllegalArgumentException(
"alpha <= 0");
526 throw new IllegalArgumentException(
"lambda <= 0");
529 this.lambda = lambda;
531 logFactor = alpha * Math.log(lambda) -
Num.
lnGamma(alpha);
540 double[] retour = { alpha, lambda };
548 return getClass().getSimpleName() +
" : alpha = " + alpha +
", lambda = " + lambda;
Classes implementing continuous distributions should inherit from this base class.
Extends the class ContinuousDistribution for the exponential distribution tjoh95a (page 494) with me...
double barF(double x)
Returns the complementary distribution function.
double cdf(double x)
Returns the distribution function .
GammaDist(double alpha)
Constructs a GammaDist object with parameters = alpha and .
double inverseF(double u)
Returns the inverse distribution function .
double getMean()
Returns the mean.
double getStandardDeviation()
Returns the standard deviation.
double cdf(double x)
Returns the distribution function .
static GammaDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a gamma distribution with parameters.
static double getVariance(double alpha, double lambda)
Computes and returns the variance of the gamma distribution with parameters.
static double inverseF(double alpha, int d, double u)
Same as inverseF(alpha, 1, d, u).
static double getMean(double alpha, double lambda)
Computes and returns the mean of the gamma distribution with parameters and .
static double cdf(double alpha, int d, double x)
Equivalent to cdf (alpha, 1.0, d, x).
static double inverseF(double alpha, double lambda, int d, double u)
Computes the inverse distribution function.
static double cdf(double alpha, double lambda, int d, double x)
Returns an approximation of the gamma distribution function with parameters = alpha and = lambda,...
String toString()
Returns a String containing information about the current distribution.
static double[] getMLE(double[] x, int n)
Estimates the parameters of the gamma distribution using the maximum likelihood method,...
double density(double x)
Returns , the density evaluated at .
double getAlpha()
Return the parameter for this object.
static double barF(double alpha, double lambda, int d, double x)
Computes the complementary distribution function.
double barF(double x)
Returns the complementary distribution function.
GammaDist(double alpha, double lambda)
Constructs a GammaDist object with parameters = alpha and = lambda.
double getVariance()
Returns the variance.
static double getStandardDeviation(double alpha, double lambda)
Computes and returns the standard deviation of the gamma distribution with parameters and .
static double density(double alpha, double lambda, double x)
Computes the density function ( fgamma ) at .
double getLambda()
Return the parameter for this object.
GammaDist(double alpha, double lambda, int d)
Constructs a GammaDist object with parameters = alpha and = lambda, and approximations of roughly d...
double[] getParams()
Return a table containing the parameters of the current distribution.
static double barF(double alpha, int d, double x)
Same as barF(alpha, 1.0, d, x).
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a (page 80)).
static double cdf01(double x)
Same as cdf(0, 1, x).
static double barF01(double x)
Same as barF(0, 1, x).
double inverseF(double u)
Returns the inverse distribution function .
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 double digamma(double x)
Returns the value of the logarithmic derivative of the Gamma function .
static final double LN2
The values of .
This class provides numerical methods to solve non-linear equations.
static double brentDekker(double a, double b, MathFunction f, double tol)
Computes a root of the function in f using the Brent-Dekker method.
static double bisection(double a, double b, MathFunction f, double tol)
Computes a root of the function in f using the bisection method.
This interface should be implemented by classes which represent univariate mathematical functions.