All Downloads are FREE. Search and download functionalities are using the official Maven repository.

umontreal.iro.lecuyer.probdist.InverseGammaDist Maven / Gradle / Ivy

Go to download

SSJ is a Java library for stochastic simulation, developed under the direction of Pierre L'Ecuyer, in the Département d'Informatique et de Recherche Opérationnelle (DIRO), at the Université de Montréal. It provides facilities for generating uniform and nonuniform random variates, computing different measures related to probability distributions, performing goodness-of-fit tests, applying quasi-Monte Carlo methods, collecting (elementary) statistics, and programming discrete-event simulations with both events and processes.

The newest version!


/*
 * Class:        InverseGammaDist
 * Description:  inverse gamma distribution
 * Environment:  Java
 * Software:     SSJ 
 * Copyright (C) 2001  Pierre L'Ecuyer and Université de Montréal
 * Organization: DIRO, Université de Montréal
 * @author       
 * @since

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   GPL licence site.
 */

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; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy