/* *
 * Title:        

* Description:

* Copyright: Copyright (c)

* Company:

* @author * @version 1.0 */ package umontreal.iro.lecuyer.probdist; import umontreal.iro.lecuyer.util.Num; /** * Extends the class {@link ContinuousDistribution} for * the exponential distribution * with mean 1/λ where * λ > 0. * Its density is *

*

*
* f (x) = λe-λx        for x >= 0, *

* its distribution function is *

*

*
* F(x) = 1 - e-λx,        for x >= 0, *

* and its inverse distribution function is *

*

*
* F-1(u) = - ln(1 - u)/λ,        for 0 < u < 1. *

* */ public class ExponentialDist extends ContinuousDistribution { private double lambda; /** * Constructs an ExponentialDist object with parameter λ = 1. * */ public ExponentialDist() { setLambda (1.0); } /** * Constructs an ExponentialDist object with parameter λ = * lambda. * */ public ExponentialDist (double lambda) { setLambda (lambda); } public double density (double x) { return density (lambda, x); } public double cdf (double x) { return cdf (lambda, x); } public double barF (double x) { return barF (lambda, x); } public double inverseF (double u) { return inverseF (lambda, u); } public double getMean() { return ExponentialDist.getMean (lambda); } public double getVariance() { return ExponentialDist.getVariance (lambda); } public double getStandardDeviation() { return ExponentialDist.getStandardDeviation (lambda); } /** * Computes the density function. * */ public static double density (double lambda, double x) { if (lambda <= 0) throw new IllegalArgumentException ("lambda <= 0"); return x < 0 ? 0 : lambda*Math.exp (-lambda*x); } /** * Computes the distribution function. * */ public static double cdf (double lambda, double x) { if (lambda <= 0) throw new IllegalArgumentException ("lambda <= 0"); if (x <= 0.0) return 0.0; double y = lambda * x; if (y >= XBIG) return 1.0; return -Math.expm1 (-y); } /** * Computes the complementary distribution function. * */ public static double barF (double lambda, double x) { if (lambda <= 0) throw new IllegalArgumentException ("lambda <= 0"); if (x <= 0.0) return 1.0; if (lambda*x >= XBIGM) return 0.0; return Math.exp (-lambda*x); } /** * Computes the inverse distribution function. * */ public static double inverseF (double lambda, double u) { if (lambda <= 0) throw new IllegalArgumentException ("lambda <= 0"); if (u < 0.0 || u > 1.0) throw new IllegalArgumentException ("u not in [0,1]"); if (u >= 1.0) return Double.POSITIVE_INFINITY; if (u <= 0.0) return 0.0; return -Math.log1p (-u)/lambda; } /** * Estimates the parameter λ of the exponential distribution * using the maximum likelihood method, from the n observations * x[i], * i = 0, 1,…, n - 1. The estimate is returned in a one-element * array, as element 0. * * @param x the list of observations used to evaluate parameters * * @param n the number of observations used to evaluate parameters * * @return returns the parameter [ * hat(λ)] * */ public static double[] getMLE (double[] x, int n) { if (n <= 0) throw new IllegalArgumentException ("n <= 0"); double parameters[]; double sum = 0.0; parameters = new double[1]; for (int i = 0; i < n; i++) sum+= x[i]; parameters[0] = (double) n / sum; return parameters; } /** * Creates a new instance of an exponential distribution with parameter * λ estimated using * the maximum likelihood method based on the n observations x[i], * * i = 0, 1,…, n - 1. * * @param x the list of observations to use to evaluate parameters * * @param n the number of observations to use to evaluate parameters * * */ public static ExponentialDist getInstanceFromMLE (double[] x, int n) { double parameters[] = getMLE (x, n); return new ExponentialDist (parameters[0]); } /** * Computes and returns the mean, * E[X] = 1/λ, * of the exponential distribution with parameter λ. * * @return the mean of the exponential distribution * E[X] = 1/λ * */ public static double getMean (double lambda) { if (lambda <= 0.0) throw new IllegalArgumentException ("lambda <= 0"); return (1 / lambda); } /** * Computes and returns the variance, * Var[X] = 1/λ2, * of the exponential distribution with parameter λ. * * @return the variance of the Exponential distribution * Var[X] = 1/λ2 * */ public static double getVariance (double lambda) { if (lambda <= 0.0) throw new IllegalArgumentException ("lambda <= 0"); return (1 / (lambda * lambda)); } /** * Computes and returns the standard deviation of the * exponential distribution with parameter λ. * * @return the standard deviation of the exponential distribution * */ public static double getStandardDeviation (double lambda) { if (lambda <= 0.0) throw new IllegalArgumentException ("lambda <= 0"); return (1 / lambda); } /** * Returns the value of λ for this object. * */ public double getLambda() { return lambda; } /** * Sets the value of λ for this object. * */ public void setLambda (double lambda) { if (lambda <= 0) throw new IllegalArgumentException ("lambda <= 0"); this.lambda = lambda; supportA = 0.0; } /** * Return a table containing the parameters of the current distribution. * * */ public double[] getParams () { double[] retour = {lambda}; return retour; } public String toString () { return getClass().getSimpleName() + " : lambda = " + lambda; } }

Ads help maintain this website.