/* *
* 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 inverse gamma distribution with shape parameter
*
* α > 0 and scale parameter β > 0.
* The density function is given by
*
*
*
* f (x) = (βαexp-β/x)/(xα+1Γ(α)) for x > 0,
*
* and f (x) = 0 otherwise,
* where Γ is the gamma function.
* The distribution function is given by
*
*
*
* F(x) = 1 - FG(1/x), for x > 0,
*
* and F(x) = 0 otherwise, where FG(x) is the distribution function
* of a gamma
* distribution with shape parameter α and scale parameter β.
*
*/
public class InverseGammaDist extends ContinuousDistribution {
protected double alpha;
protected double beta;
protected double logam; // Ln (Gamma(alpha))
/**
* Constructs an InverseGammaDist object with parameters
* α = alpha and β = beta.
*
*/
public InverseGammaDist (double alpha, double beta) {
setParam (alpha, beta);
}
public double density (double x) {
if (x <= 0.0)
return 0.0;
return Math.exp (alpha * Math.log (beta/x) - (beta / x) - logam) / x;
}
public double cdf (double x) {
return cdf (alpha, beta, x);
}
public double barF (double x) {
return barF (alpha, beta, x);
}
public double inverseF (double u) {
return inverseF (alpha, beta, u);
}
public double getMean () {
return getMean (alpha, beta);
}
public double getVariance () {
return getVariance (alpha, beta);
}
public double getStandardDeviation () {
return getStandardDeviation (alpha, beta);
}
/**
* Computes the density function of the inverse gamma distribution with shape
* parameter α and scale parameter β.
*
*/
public static double density (double alpha, double beta, double x) {
if (alpha <= 0.0)
throw new IllegalArgumentException("alpha <= 0");
if (beta <= 0.0)
throw new IllegalArgumentException("beta <= 0");
if (x <= 0.0)
return 0.0;
return Math.exp (alpha * Math.log (beta/x) - (beta / x) - Num.lnGamma (alpha)) / x;
}
/**
* Computes the cumulative probability function of the inverse gamma distribution
* with shape parameter α and scale parameter β.
*
*/
public static double cdf (double alpha, double beta, double x) {
if (alpha <= 0.0)
throw new IllegalArgumentException("alpha <= 0");
if (beta <= 0.0)
throw new IllegalArgumentException("beta <= 0");
if (x <= 0.0)
return 0.0;
return GammaDist.barF (alpha, beta, 15, 1.0 / x);
}
/**
* Computes the complementary distribution function of the inverse gamma distribution
* with shape parameter α and scale parameter β.
*
*/
public static double barF (double alpha, double beta, double x) {
if (alpha <= 0.0)
throw new IllegalArgumentException("alpha <= 0");
if (beta <= 0.0)
throw new IllegalArgumentException("beta <= 0");
if (x <= 0.0)
return 1.0;
return GammaDist.cdf (alpha, beta, 15, 1.0 / x);
}
/**
* Computes the inverse distribution function of the inverse gamma distribution
* with shape parameter α and scale parameter β.
*
*/
public static double inverseF (double alpha, double beta, double u) {
if (alpha <= 0.0)
throw new IllegalArgumentException("alpha <= 0");
if (beta <= 0.0)
throw new IllegalArgumentException("beta <= 0");
return 1.0 / GammaDist.inverseF (alpha, beta, 15, 1 - u);
}
/**
* Estimates the parameters
* (α, β) of the inverse gamma distribution
* using the maximum likelihood method, from the n observations
* x[i],
* i = 0, 1,…, n - 1. The estimates are returned in a two-element
* array, in regular order: [α, β].
*
* @param x the list of observations to use to evaluate parameters
*
* @param n the number of observations to use to evaluate parameters
*
* @return returns the parameters [
* hat(α), hat(β)]
*
*/
public static double[] getMLE (double[] x, int n) {
double[] y = new double[n];
for (int i = 0; i < n; i++) {
if(x[i] > 0)
y[i] = 1.0 / x[i];
else
y[i] = 1.0E100;
}
return GammaDist.getMLE (y, n);
}
/**
* Creates a new instance of the inverse gamma distribution with parameters α
* and β 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 InverseGammaDist getInstanceFromMLE (double[] x, int n) {
double parameters[] = getMLE (x, n);
return new InverseGammaDist (parameters[0], parameters[1]);
}
/**
* Returns the mean
* E[X] = β/(α - 1) of the inverse gamma
* distribution with shape parameter α and scale parameter β.
*
*/
public static double getMean (double alpha, double beta) {
if (alpha <= 0.0)
throw new IllegalArgumentException("alpha <= 0");
if (beta <= 0.0)
throw new IllegalArgumentException("beta <= 0");
return (beta / (alpha - 1.0));
}
/**
* Returns the variance
* Var[X] = β2/((α -1)2(α - 2))
* of the inverse gamma distribution with shape parameter α and scale
* parameter β.
*
*/
public static double getVariance (double alpha, double beta) {
if (alpha <= 0.0)
throw new IllegalArgumentException("alpha <= 0");
if (beta <= 0.0)
throw new IllegalArgumentException("beta <= 0");
return ((beta * beta) / ((alpha - 1.0) * (alpha - 1.0) * (alpha - 2.0)));
}
/**
* Returns the standard deviation of the inverse gamma distribution with
* shape parameter α and scale parameter β.
*
*/
public static double getStandardDeviation (double alpha, double beta) {
return Math.sqrt (getVariance (alpha, beta));
}
/**
* Returns the α parameter of this object.
*
*/
public double getAlpha() {
return alpha;
}
/**
* Returns the β parameter of this object.
*
*/
public double getBeta() {
return beta;
}
/**
* Sets the parameters α and β of this object.
*
*/
public void setParam (double alpha, double beta) {
if (alpha <= 0.0)
throw new IllegalArgumentException("alpha <= 0");
if (beta <= 0.0)
throw new IllegalArgumentException("beta <= 0");
supportA = 0.0;
this.alpha = alpha;
this.beta = beta;
logam = Num.lnGamma (alpha);
}
/**
* Returns a table containing the parameters of the current distribution.
* This table is put in regular order: [α, β].
*
*
*/
public double[] getParams () {
double[] retour = {alpha, beta};
return retour;
}
public String toString () {
return getClass().getSimpleName() + " : alpha = " + alpha + ", beta = " + beta;
}
}
Ads help maintain this website.