Class Gamma

    • Constructor Summary

      Constructors 
      Constructor Description
      Gamma()
      Creates new Gamma with shape 1.0, scale 1.0
      Gamma​(double[] parameters)
      Constructs a gamma distribution with shape = parameters[0] and scale = parameters[1]
      Gamma​(double shape, double scale)
      Constructs a gamma distribution with supplied shape and scale
      Gamma​(double shape, double scale, java.lang.String name)
      Constructs a gamma distribution with supplied shape and scale
    • Field Detail

    • Constructor Detail

      • Gamma

        public Gamma()
        Creates new Gamma with shape 1.0, scale 1.0
      • Gamma

        public Gamma​(double[] parameters)
        Constructs a gamma distribution with shape = parameters[0] and scale = parameters[1]
        Parameters:
        parameters - An array with the shape and scale
      • Gamma

        public Gamma​(double shape,
                     double scale)
        Constructs a gamma distribution with supplied shape and scale
        Parameters:
        shape - The shape parameter of the distribution
        scale - The scale parameter of the distribution
      • Gamma

        public Gamma​(double shape,
                     double scale,
                     java.lang.String name)
        Constructs a gamma distribution with supplied shape and scale
        Parameters:
        shape - The shape parameter of the distribution
        scale - The scale parameter of the distribution
        name - an optional name/label
    • Method Detail

      • setShape

        public final void setShape​(double shape)
        Sets the shape parameter
        Parameters:
        shape - The shape parameter must > 0.0
      • setScale

        public final void setScale​(double scale)
        Sets the scale parameter
        Parameters:
        scale - The scale parameter must be > 0.0
      • getShape

        public final double getShape()
        Gets the shape
        Returns:
        The shape parameter as a double
      • getScale

        public final double getScale()
        Gets the scale parameter
        Returns:
        The scale parameter as a double
      • getMean

        public final double getMean()
        Description copied from interface: MeanIfc
        Returns the mean or expected value of a distribution
        Specified by:
        getMean in interface MeanIfc
        Returns:
        double the mean or expected value for the distribution
      • getMoment2

        public final double getMoment2()
        Returns:
        the 2nd moment
      • getMoment3

        public final double getMoment3()
        Returns:
        the 3rd moment
      • getMoment4

        public final double getMoment4()
        Returns:
        the 4th moment
      • getMoment

        public final double getMoment​(int n)
        Parameters:
        n - the desired moment
        Returns:
        the nth moment
      • getVariance

        public double getVariance()
        Description copied from interface: VarianceIfc
        Returns the variance of the distribution if defined
        Specified by:
        getVariance in interface VarianceIfc
        Returns:
        double the variance of the random variable
      • cdf

        public double cdf​(double x)
        Description copied from interface: CDFIfc
        Returns the F(x) = Pr{X <= x} where F represents the cumulative distribution function
        Specified by:
        cdf in interface CDFIfc
        Parameters:
        x - a double representing the upper limit
        Returns:
        a double representing the probability
      • invCDF

        public double invCDF​(double p)
        Provides the inverse cumulative distribution function for the distribution This is based on a numerical routine that computes the percentage points for the chi-squared distribution
        Specified by:
        invCDF in interface InverseCDFIfc
        Parameters:
        p - The probability to be evaluated for the inverse, p must be [0,1] or an IllegalArgumentException is thrown p = 0.0 returns 0.0 p = 1.0 returns Double.POSITIVE_INFINITY
        Returns:
        The inverse cdf evaluated at p
      • pdf

        public double pdf​(double x)
        Description copied from interface: PDFIfc
        Returns the f(x) where f represents the probability density function for the distribution. Note this is not a probability.
        Specified by:
        pdf in interface PDFIfc
        Parameters:
        x - a double representing the value to be evaluated
        Returns:
        f(x)
      • setNorm

        protected final void setNorm​(double scale,
                                     double shape)
      • getNorm

        protected final double getNorm()
      • getKurtosis

        public final double getKurtosis()
        Gets the kurtosis of the distribution
        Returns:
        the kurtosis
      • getSkewness

        public final double getSkewness()
        Gets the skewness of the distribution
        Returns:
        the skewness
      • invChiSquareDistribution

        public static double invChiSquareDistribution​(double p,
                                                      double v)
        The inverse CDF of the chi-square distribution
        Parameters:
        p - the probability to be evaluated
        v - degrees of freedom
        Returns:
        the inverse value
      • invChiSquareDistribution

        public static double invChiSquareDistribution​(double p,
                                                      double v,
                                                      double g)
        Algorithm AS 91 Appl. Statist. (1975) Vol.24, P.35 To evaluate the percentage points of the chi-squared probability distribution function. The default maximum number of iterations is DEFAULT_MAX_ITERATIONS The default numerical precision is JSLMath.getDefaultNumericalPrecision() logGammaFunction(v/2.0)) Incorporates the suggested changes in AS R85 (vol.40(1), pp.233-5, 1991) Auxiliary routines required: PPND = AS 111 (or AS 241) and GAMMAD = AS 239.
        Parameters:
        p - must lie in the range [0.0,1.0]
        v - must be positive, degrees of freedom
        g - must be supplied and should be equal to natural logarithm of the gamma function evaluated at half of the degrees of freedom
        Returns:
        The percentage point
      • invChiSquareDistribution

        public static double invChiSquareDistribution​(double p,
                                                      double v,
                                                      double g,
                                                      int maxIterations,
                                                      double eps)
        Algorithm AS 91 Appl. Statist. (1975) Vol.24, P.35 To evaluate the percentage points of the chi-squared probability distribution function. logGammaFunction(v/2.0)) Incorporates the suggested changes in AS R85 (vol.40(1), pp.233-5, 1991) Auxiliary routines required: PPND = AS 111 (or AS 241) and GAMMAD = AS 239.
        Parameters:
        p - must lie in the range [0.0,1.0]
        v - must be positive, degrees of freedom
        g - must be supplied and should be equal to natural logarithm of the gamma function evaluated at half of the degrees of freedom
        maxIterations - maximum number of iterations for series/continued fraction evaluation
        eps - the numerical precision for convergence of series/continued fraction evaluation
        Returns:
        The percentage point
      • gammaFunction

        public static double gammaFunction​(double x)
        Computes the gamma function at x
        Parameters:
        x - The value to be evaluated, x must be > 0
        Returns:
        The value of the gamma function evaluated at x
      • logGammaFunction

        public static double logGammaFunction​(double x)
        Computes the natural logarithm of the gamma function at x. Useful when x gets large.
        Parameters:
        x - The value to be evaluated, x must be > 0
        Returns:
        The natural logarithm of the gamma function
      • incompleteGammaFunction

        public static double incompleteGammaFunction​(double x,
                                                     double alpha,
                                                     int maxIterations,
                                                     double eps)
        Computes the incomplete gamma function at x
        Parameters:
        x - The value to be evaluated, must be > 0
        alpha - must be > 0
        maxIterations - maximum number of iterations for series/continued fraction evaluation
        eps - the numerical precision for convergence of series/continued fraction evaluation
        Returns:
        the value of the incomplete gamma function evaluated at x
      • diGammaFunction

        public static double diGammaFunction​(double x)
        Computes the digamma function using AS 103 Reference: Jose Bernardo, Algorithm AS 103: Psi ( Digamma ) Function, Applied Statistics, Volume 25, Number 3, 1976, pages 315-317.
        Parameters:
        x - the value to evaluate
        Returns:
        the function value
      • digamma

        public static double digamma​(double x)
        Computes the digamma function Mark Johnson, 2nd September 2007 Computes the Ψ(x) or digamma function, i.e., the derivative of the log gamma function, using a series expansion. http://www.cog.brown.edu/~mj/code/digamma.c
        Parameters:
        x - the value to evaluate
        Returns:
        the function value
      • getMaxNumIterations

        public final int getMaxNumIterations()
        Gets the maximum number of iterations for the gamma functions
        Returns:
        the maximum number of iterations
      • setMaxNumIterations

        public final void setMaxNumIterations​(int iterations)
        Sets the maximum number of iterations for the gamma functions
        Parameters:
        iterations - the maximum number of iterations
      • getNumericalPrecision

        public final double getNumericalPrecision()
        Gets the numerical precision used in computing the gamma functions
        Returns:
        the numerical precision
      • setNumericalPrecision

        public final void setNumericalPrecision​(double precision)
        Sets the numerical precision used in computing the gamma functions
        Parameters:
        precision - the numerical precision
      • setParameters

        public void setParameters​(double[] parameters)
        Sets the parameters for the distribution with shape = parameters[0] and scale = parameters[1]
        Specified by:
        setParameters in interface ParametersIfc
        Parameters:
        parameters - an array of doubles representing the parameters for the distribution
      • getParameters

        public double[] getParameters()
        Gets the parameters for the distribution
        Specified by:
        getParameters in interface ParametersIfc
        Returns:
        Returns an array of the parameters for the distribution
      • getParametersFromMeanAndVariance

        public static double[] getParametersFromMeanAndVariance​(double mean,
                                                                double var)
        Computes the parameters (shape and scale) by matching to mean and variance element[0] = shape element[1] = scale
        Parameters:
        mean - must be > 0
        var - must be > 0
        Returns:
        the parameter array
      • getParametersFromMoments

        public static double[] getParametersFromMoments​(double... moments)
        Computes the parameters (shape and scale) by matching to mean and variance element[0] = shape element[1] = scale
      • createFromMoments

        public static Gamma createFromMoments​(double... moments)
      • firstOrderLossFunction

        public double firstOrderLossFunction​(double x)
        Description copied from interface: FirstOrderLossFunctionIfc
        Computes the first order loss function for the function for given value of x, G1(x) = E[max(X-x,0)]
        Specified by:
        firstOrderLossFunction in interface FirstOrderLossFunctionIfc
        Parameters:
        x - The value to be evaluated
        Returns:
        The loss function value, E[max(X-x,0)]
      • secondOrderLossFunction

        public double secondOrderLossFunction​(double x)
        Description copied from interface: SecondOrderLossFunctionIfc
        Computes the 2nd order loss function for the distribution function for given value of x, G2(x) = (1/2)E[max(X-x,0)*max(X-x-1,0)]
        Specified by:
        secondOrderLossFunction in interface SecondOrderLossFunctionIfc
        Parameters:
        x - The value to be evaluated
        Returns:
        The loss function value, (1/2)E[max(X-x,0)*max(X-x-1,0)]
      • main

        public static void main​(java.lang.String[] args)