Package jsl.utilities.distributions
Class Gamma
- java.lang.Object
-
- jsl.utilities.distributions.Distribution
-
- jsl.utilities.distributions.Gamma
-
- All Implemented Interfaces:
ControllableIfc
,CDFIfc
,ContinuousDistributionIfc
,DistributionFunctionIfc
,DistributionIfc
,DomainIfc
,FirstOrderLossFunctionIfc
,InverseCDFIfc
,LossFunctionDistributionIfc
,MeanIfc
,PDFIfc
,SecondOrderLossFunctionIfc
,VarianceIfc
,GetNameIfc
,IdentityIfc
,NewInstanceIfc
,ParametersIfc
,GetRVariableIfc
public class Gamma extends Distribution implements ContinuousDistributionIfc, LossFunctionDistributionIfc, InverseCDFIfc, GetRVariableIfc
Models random variables that have gamma distribution For more information on the gamma distribution and its related functions, see "Object-Oriented Numerical Methods" by D. Besset
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jsl.utilities.distributions.Distribution
Distribution.RandomControls
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_MAX_ITERATIONS
-
Fields inherited from class jsl.utilities.distributions.Distribution
myId, myName
-
-
Constructor Summary
Constructors Constructor Description Gamma()
Creates new Gamma with shape 1.0, scale 1.0Gamma(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 scaleGamma(double shape, double scale, java.lang.String name)
Constructs a gamma distribution with supplied shape and scale
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
cdf(double x)
Returns the F(x) = Pr{X <= x} where F represents the cumulative distribution functionstatic Gamma
createFromMoments(double... moments)
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.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.double
firstOrderLossFunction(double x)
Computes the first order loss function for the function for given value of x, G1(x) = E[max(X-x,0)]static double
gammaFunction(double x)
Computes the gamma function at xInterval
getDomain()
double
getKurtosis()
Gets the kurtosis of the distributionint
getMaxNumIterations()
Gets the maximum number of iterations for the gamma functionsdouble
getMean()
Returns the mean or expected value of a distributiondouble
getMoment(int n)
double
getMoment2()
double
getMoment3()
double
getMoment4()
protected double
getNorm()
double
getNumericalPrecision()
Gets the numerical precision used in computing the gamma functionsdouble[]
getParameters()
Gets the parameters for the distributionstatic double[]
getParametersFromMeanAndVariance(double mean, double var)
Computes the parameters (shape and scale) by matching to mean and variance element[0] = shape element[1] = scalestatic double[]
getParametersFromMoments(double... moments)
Computes the parameters (shape and scale) by matching to mean and variance element[0] = shape element[1] = scaleRVariableIfc
getRandomVariable(RNStreamIfc rng)
double
getScale()
Gets the scale parameterdouble
getShape()
Gets the shapedouble
getSkewness()
Gets the skewness of the distributiondouble
getVariance()
Returns the variance of the distribution if definedstatic double
incompleteGammaFunction(double x, double alpha, int maxIterations, double eps)
Computes the incomplete gamma function at xdouble
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 distributionstatic double
invChiSquareDistribution(double p, double v)
The inverse CDF of the chi-square distributionstatic double
invChiSquareDistribution(double p, double v, double g)
Algorithm AS 91 Appl.static double
invChiSquareDistribution(double p, double v, double g, int maxIterations, double eps)
Algorithm AS 91 Appl.static double
logGammaFunction(double x)
Computes the natural logarithm of the gamma function at x.static void
main(java.lang.String[] args)
Gamma
newInstance()
Returns a new instancedouble
pdf(double x)
Returns the f(x) where f represents the probability density function for the distribution.double
secondOrderLossFunction(double x)
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)]void
setMaxNumIterations(int iterations)
Sets the maximum number of iterations for the gamma functionsprotected void
setNorm(double scale, double shape)
void
setNumericalPrecision(double precision)
Sets the numerical precision used in computing the gamma functionsvoid
setParameters(double[] parameters)
Sets the parameters for the distribution with shape = parameters[0] and scale = parameters[1]void
setScale(double scale)
Sets the scale parametervoid
setShape(double shape)
Sets the shape parameter-
Methods inherited from class jsl.utilities.distributions.Distribution
getControls, getId, getName, getStandardDeviation, inverseContinuousCDFViaBisection, inverseContinuousCDFViaBisection, inverseDiscreteCDFViaSearchUp, setControls, setId, setName, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface jsl.utilities.distributions.CDFIfc
cdf, complementaryCDF
-
Methods inherited from interface jsl.utilities.random.rvariable.GetRVariableIfc
getRandomVariable, getRandomVariable
-
Methods inherited from interface jsl.utilities.distributions.VarianceIfc
getStandardDeviation
-
-
-
-
Field Detail
-
DEFAULT_MAX_ITERATIONS
public static final int DEFAULT_MAX_ITERATIONS
- See Also:
- Constant Field Values
-
-
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 distributionscale
- 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 distributionscale
- The scale parameter of the distributionname
- an optional name/label
-
-
Method Detail
-
newInstance
public final Gamma newInstance()
Description copied from interface:NewInstanceIfc
Returns a new instance- Specified by:
newInstance
in interfaceNewInstanceIfc
- Specified by:
newInstance
in classDistribution
- Returns:
- the new instance
-
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
-
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 interfaceVarianceIfc
- 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
-
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 interfaceInverseCDFIfc
- 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.
-
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 evaluatedv
- 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 freedomg
- 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 freedomg
- must be supplied and should be equal to natural logarithm of the gamma function evaluated at half of the degrees of freedommaxIterations
- maximum number of iterations for series/continued fraction evaluationeps
- 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 > 0alpha
- must be > 0maxIterations
- maximum number of iterations for series/continued fraction evaluationeps
- 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 interfaceParametersIfc
- 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 interfaceParametersIfc
- 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 > 0var
- 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 interfaceFirstOrderLossFunctionIfc
- 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 interfaceSecondOrderLossFunctionIfc
- Parameters:
x
- The value to be evaluated- Returns:
- The loss function value, (1/2)E[max(X-x,0)*max(X-x-1,0)]
-
getRandomVariable
public final RVariableIfc getRandomVariable(RNStreamIfc rng)
- Specified by:
getRandomVariable
in interfaceGetRVariableIfc
- Overrides:
getRandomVariable
in classDistribution
- Parameters:
rng
- the stream to use- Returns:
- a random variable
-
main
public static void main(java.lang.String[] args)
-
-