Top Description Fields Constructors Methods
org.apache.commons.math3.distribution

public Class GammaDistribution

extends AbstractRealDistribution
Class Inheritance
Imports
org.apache.commons.math3.exception.NotStrictlyPositiveException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.random.RandomGenerator, .Well19937c, org.apache.commons.math3.special.Gamma, org.apache.commons.math3.util.FastMath

Implementation of the Gamma distribution.
See Also
Gamma distribution (Wikipedia), Gamma distribution (MathWorld)

Field Summary

Modifier and TypeField and Description
public static final double
DEFAULT_INVERSE_ABSOLUTE_ACCURACY

Default inverse cumulative probability accuracy.

private final double
densityPrefactor1

The constant value of shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape), where L(shape) is the Lanczos approximation returned by Gamma#lanczos(double).

private final double
densityPrefactor2

The constant value of shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape), where L(shape) is the Lanczos approximation returned by Gamma#lanczos(double).

private final double
logDensityPrefactor1

The constant value of log(shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)), where L(shape) is the Lanczos approximation returned by Gamma#lanczos(double).

private final double
logDensityPrefactor2

The constant value of log(shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)), where L(shape) is the Lanczos approximation returned by Gamma#lanczos(double).

private final double
maxLogY

Upper bound on log(y) (y = x / scale) for the selection of the computation method in density(double).

private final double
minY

Lower bound on y = x / scale for the selection of the computation method in density(double).

private final double
scale

The scale parameter.

private static final long
private final double
shape

The shape parameter.

private final double
shiftedShape

The constant value of shape + g + 0.5, where g is the Lanczos constant Gamma#LANCZOS_G.

private final double
Inherited from org.apache.commons.math3.distribution.AbstractRealDistribution:
randomrandomDataSOLVER_DEFAULT_ABSOLUTE_ACCURACY

Constructor Summary

AccessConstructor and Description
public
GammaDistribution(double
the shape parameter
shape
,
double
the scale parameter
scale
)

Creates a new gamma distribution with specified values of the shape and scale parameters.

public
GammaDistribution(double
the shape parameter
shape
,
double
the scale parameter
scale
,
double
the maximum absolute error in inverse cumulative probability estimates (defaults to DEFAULT_INVERSE_ABSOLUTE_ACCURACY).
inverseCumAccuracy
)

Creates a new gamma distribution with specified values of the shape and scale parameters.

public
GammaDistribution(RandomGenerator
Random number generator.
rng
,
double
the shape parameter
shape
,
double
the scale parameter
scale
)

Creates a Gamma distribution.

public
GammaDistribution(RandomGenerator
Random number generator.
rng
,
double
the shape parameter
shape
,
double
the scale parameter
scale
,
double
the maximum absolute error in inverse cumulative probability estimates (defaults to DEFAULT_INVERSE_ABSOLUTE_ACCURACY).
inverseCumAccuracy
)

Creates a Gamma distribution.

Method Summary

Modifier and TypeMethod and Description
public double
cumulativeProbability(double
the point at which the CDF is evaluated
x
)

Implements org.apache.commons.math3.distribution.RealDistribution.cumulativeProbability.

For a random variable X whose values are distributed according to this distribution, this method returns P(X <= x).

public double
density(double
the point at which the PDF is evaluated
x
)

Implements org.apache.commons.math3.distribution.RealDistribution.density.

Returns the probability density function (PDF) of this distribution evaluated at the specified point x.

public double

Returns:

the shape parameter
getAlpha
()
Deprecated as of version 3.1, getShape() should be preferred. This method will be removed in version 4.0.

Returns the shape parameter of this distribution.

public double

Returns:

the scale parameter
getBeta
()
Deprecated as of version 3.1, getScale() should be preferred. This method will be removed in version 4.0.

Returns the scale parameter of this distribution.

public double
getNumericalMean()

Implements org.apache.commons.math3.distribution.RealDistribution.getNumericalMean.

Use this method to get the numerical value of the mean of this distribution.

public double

Returns:

the variance (possibly Double.POSITIVE_INFINITY as for certain cases in TDistribution) or Double.NaN if it is not defined
getNumericalVariance
()

Implements org.apache.commons.math3.distribution.RealDistribution.getNumericalVariance.

Use this method to get the numerical value of the variance of this distribution.

public double

Returns:

the scale parameter
getScale
()

Returns the scale parameter of this distribution.

public double

Returns:

the shape parameter
getShape
()

Returns the shape parameter of this distribution.

protected double
getSolverAbsoluteAccuracy()

Overrides org.apache.commons.math3.distribution.AbstractRealDistribution.getSolverAbsoluteAccuracy.

Returns the solver absolute accuracy for inverse cumulative computation.

public double

Returns:

lower bound of the support (always 0)
getSupportLowerBound
()

Implements org.apache.commons.math3.distribution.RealDistribution.getSupportLowerBound.

Access the lower bound of the support.

public double

Returns:

upper bound of the support (always Double.POSITIVE_INFINITY)
getSupportUpperBound
()

Implements org.apache.commons.math3.distribution.RealDistribution.getSupportUpperBound.

Access the upper bound of the support.

public boolean

Returns:

true
isSupportConnected
()

Implements org.apache.commons.math3.distribution.RealDistribution.isSupportConnected.

Use this method to get information about whether the support is connected, i.e. whether all values between the lower and upper bound of the support are included in the support.

public boolean
isSupportLowerBoundInclusive()

Implements org.apache.commons.math3.distribution.RealDistribution.isSupportLowerBoundInclusive.

Whether or not the lower bound of support is in the domain of the density function.

public boolean
isSupportUpperBoundInclusive()

Implements org.apache.commons.math3.distribution.RealDistribution.isSupportUpperBoundInclusive.

Whether or not the upper bound of support is in the domain of the density function.

public double
logDensity(double
the point at which the PDF is evaluated
x
)

Overrides org.apache.commons.math3.distribution.AbstractRealDistribution.logDensity.

Returns the natural logarithm of the probability density function (PDF) of this distribution evaluated at the specified point x.

public double

Returns:

random value sampled from the Gamma(shape, scale) distribution
sample
()

Overrides org.apache.commons.math3.distribution.AbstractRealDistribution.sample.

Implements org.apache.commons.math3.distribution.RealDistribution.sample.

This implementation uses the following algorithms:

For 0 < shape < 1: Ahrens, J.

Inherited from org.apache.commons.math3.distribution.AbstractRealDistribution:
cumulativeProbabilityinverseCumulativeProbabilityprobabilityprobabilityreseedRandomGeneratorsample

Field Detail

DEFAULT_INVERSE_ABSOLUTE_ACCURACYback to summary
public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY

Default inverse cumulative probability accuracy.

Since
2.1
densityPrefactor1back to summary
private final double densityPrefactor1

The constant value of shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape), where L(shape) is the Lanczos approximation returned by Gamma#lanczos(double). This prefactor is used in density(double), when no overflow occurs with the natural calculation.

densityPrefactor2back to summary
private final double densityPrefactor2

The constant value of shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape), where L(shape) is the Lanczos approximation returned by Gamma#lanczos(double). This prefactor is used in density(double), when overflow occurs with the natural calculation.

logDensityPrefactor1back to summary
private final double logDensityPrefactor1

The constant value of log(shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)), where L(shape) is the Lanczos approximation returned by Gamma#lanczos(double). This prefactor is used in logDensity(double), when no overflow occurs with the natural calculation.

logDensityPrefactor2back to summary
private final double logDensityPrefactor2

The constant value of log(shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)), where L(shape) is the Lanczos approximation returned by Gamma#lanczos(double). This prefactor is used in logDensity(double), when overflow occurs with the natural calculation.

maxLogYback to summary
private final double maxLogY

Upper bound on log(y) (y = x / scale) for the selection of the computation method in density(double). For log(y) >= maxLogY, the natural calculation overflows.

minYback to summary
private final double minY

Lower bound on y = x / scale for the selection of the computation method in density(double). For y <= minY, the natural calculation overflows.

scaleback to summary
private final double scale

The scale parameter.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.commons.math3.distribution.AbstractRealDistribution.serialVersionUID.

Serializable version identifier.

shapeback to summary
private final double shape

The shape parameter.

shiftedShapeback to summary
private final double shiftedShape

The constant value of shape + g + 0.5, where g is the Lanczos constant Gamma#LANCZOS_G.

solverAbsoluteAccuracyback to summary
private final double solverAbsoluteAccuracy

Hides org.apache.commons.math3.distribution.AbstractRealDistribution.solverAbsoluteAccuracy.

Inverse cumulative probability accuracy.

Constructor Detail

GammaDistributionback to summary
public GammaDistribution(double shape, double scale) throws NotStrictlyPositiveException

Creates a new gamma distribution with specified values of the shape and scale parameters.

Note

this constructor will implicitly create an instance of Well19937c as random generator to be used for sampling only (see sample() and sample(int)). In case no sampling is needed for the created distribution, it is advised to pass null as random generator via the appropriate constructors to avoid the additional initialisation overhead.

Parameters
shape:double

the shape parameter

scale:double

the scale parameter

Exceptions
NotStrictlyPositiveException:
if shape <= 0 or scale <= 0.
GammaDistributionback to summary
public GammaDistribution(double shape, double scale, double inverseCumAccuracy) throws NotStrictlyPositiveException

Creates a new gamma distribution with specified values of the shape and scale parameters.

Note

this constructor will implicitly create an instance of Well19937c as random generator to be used for sampling only (see sample() and sample(int)). In case no sampling is needed for the created distribution, it is advised to pass null as random generator via the appropriate constructors to avoid the additional initialisation overhead.

Parameters
shape:double

the shape parameter

scale:double

the scale parameter

inverseCumAccuracy:double

the maximum absolute error in inverse cumulative probability estimates (defaults to DEFAULT_INVERSE_ABSOLUTE_ACCURACY).

Exceptions
NotStrictlyPositiveException:
if shape <= 0 or scale <= 0.
Since
2.1
GammaDistributionback to summary
public GammaDistribution(RandomGenerator rng, double shape, double scale) throws NotStrictlyPositiveException

Creates a Gamma distribution.

Parameters
rng:RandomGenerator

Random number generator.

shape:double

the shape parameter

scale:double

the scale parameter

Exceptions
NotStrictlyPositiveException:
if shape <= 0 or scale <= 0.
Since
3.3
GammaDistributionback to summary
public GammaDistribution(RandomGenerator rng, double shape, double scale, double inverseCumAccuracy) throws NotStrictlyPositiveException

Creates a Gamma distribution.

Parameters
rng:RandomGenerator

Random number generator.

shape:double

the shape parameter

scale:double

the scale parameter

inverseCumAccuracy:double

the maximum absolute error in inverse cumulative probability estimates (defaults to DEFAULT_INVERSE_ABSOLUTE_ACCURACY).

Exceptions
NotStrictlyPositiveException:
if shape <= 0 or scale <= 0.
Since
3.1

Method Detail

cumulativeProbabilityback to summary
public double cumulativeProbability(double x)

Implements org.apache.commons.math3.distribution.RealDistribution.cumulativeProbability.

Doc from org.apache.commons.math3.distribution.RealDistribution.cumulativeProbability.

For a random variable X whose values are distributed according to this distribution, this method returns P(X <= x). In other words, this method represents the (cumulative) distribution function (CDF) for this distribution. The implementation of this method is based on:

  • Chi-Squared Distribution, equation (9).
  • Casella, G., & Berger, R. (1990). Statistical Inference. Belmont, CA: Duxbury Press.
Parameters
x:double

the point at which the CDF is evaluated

Returns:double

the probability that a random variable with this distribution takes a value less than or equal to x

densityback to summary
public double density(double x)

Implements org.apache.commons.math3.distribution.RealDistribution.density.

Doc from org.apache.commons.math3.distribution.RealDistribution.density.

Returns the probability density function (PDF) of this distribution evaluated at the specified point x. In general, the PDF is the derivative of the CDF. If the derivative does not exist at x, then an appropriate replacement should be returned, e.g. Double.POSITIVE_INFINITY, Double.NaN, or the limit inferior or limit superior of the difference quotient.

Parameters
x:double

the point at which the PDF is evaluated

Returns:double

the value of the probability density function at point x

getAlphaback to summary
public double getAlpha()

Deprecated

as of version 3.1, getShape() should be preferred. This method will be removed in version 4.0.

Returns the shape parameter of this distribution.

Returns:double

the shape parameter

Annotations
@Deprecated
getBetaback to summary
public double getBeta()

Deprecated

as of version 3.1, getScale() should be preferred. This method will be removed in version 4.0.

Returns the scale parameter of this distribution.

Returns:double

the scale parameter

Annotations
@Deprecated
getNumericalMeanback to summary
public double getNumericalMean()

Implements org.apache.commons.math3.distribution.RealDistribution.getNumericalMean.

Doc from org.apache.commons.math3.distribution.RealDistribution.getNumericalMean.

Use this method to get the numerical value of the mean of this distribution. For shape parameter alpha and scale parameter beta, the mean is alpha * beta.

Returns:double

the mean or Double.NaN if it is not defined

getNumericalVarianceback to summary
public double getNumericalVariance()

Implements org.apache.commons.math3.distribution.RealDistribution.getNumericalVariance.

Doc from org.apache.commons.math3.distribution.RealDistribution.getNumericalVariance.

Use this method to get the numerical value of the variance of this distribution. For shape parameter alpha and scale parameter beta, the variance is alpha * beta^2.

Returns:double

the variance (possibly Double.POSITIVE_INFINITY as for certain cases in TDistribution) or Double.NaN if it is not defined

getScaleback to summary
public double getScale()

Returns the scale parameter of this distribution.

Returns:double

the scale parameter

Since
3.1
getShapeback to summary
public double getShape()

Returns the shape parameter of this distribution.

Returns:double

the shape parameter

Since
3.1
getSolverAbsoluteAccuracyback to summary
protected double getSolverAbsoluteAccuracy()

Overrides org.apache.commons.math3.distribution.AbstractRealDistribution.getSolverAbsoluteAccuracy.

Doc from org.apache.commons.math3.distribution.AbstractRealDistribution.getSolverAbsoluteAccuracy.

Returns the solver absolute accuracy for inverse cumulative computation. You can override this method in order to use a Brent solver with an absolute accuracy different from the default.

Returns:double

the maximum absolute error in inverse cumulative probability estimates

Annotations
@Override
getSupportLowerBoundback to summary
public double getSupportLowerBound()

Implements org.apache.commons.math3.distribution.RealDistribution.getSupportLowerBound.

Doc from org.apache.commons.math3.distribution.RealDistribution.getSupportLowerBound.

Access the lower bound of the support. This method must return the same value as inverseCumulativeProbability(0). In other words, this method must return

inf {x in R | P(X <= x) > 0}.

The lower bound of the support is always 0 no matter the parameters.
Returns:double

lower bound of the support (always 0)

getSupportUpperBoundback to summary
public double getSupportUpperBound()

Implements org.apache.commons.math3.distribution.RealDistribution.getSupportUpperBound.

Doc from org.apache.commons.math3.distribution.RealDistribution.getSupportUpperBound.

Access the upper bound of the support. This method must return the same value as inverseCumulativeProbability(1). In other words, this method must return

inf {x in R | P(X <= x) = 1}.

The upper bound of the support is always positive infinity no matter the parameters.
Returns:double

upper bound of the support (always Double.POSITIVE_INFINITY)

isSupportConnectedback to summary
public boolean isSupportConnected()

Implements org.apache.commons.math3.distribution.RealDistribution.isSupportConnected.

Doc from org.apache.commons.math3.distribution.RealDistribution.isSupportConnected.

Use this method to get information about whether the support is connected, i.e. whether all values between the lower and upper bound of the support are included in the support. The support of this distribution is connected.

Returns:boolean

true

isSupportLowerBoundInclusiveback to summary
public boolean isSupportLowerBoundInclusive()

Implements org.apache.commons.math3.distribution.RealDistribution.isSupportLowerBoundInclusive.

Doc from org.apache.commons.math3.distribution.RealDistribution.isSupportLowerBoundInclusive.

Whether or not the lower bound of support is in the domain of the density function. Returns true iff getSupporLowerBound() is finite and density(getSupportLowerBound()) returns a non-NaN, non-infinite value.

Returns:boolean

true if the lower bound of support is finite and the density function returns a non-NaN, non-infinite value there

isSupportUpperBoundInclusiveback to summary
public boolean isSupportUpperBoundInclusive()

Implements org.apache.commons.math3.distribution.RealDistribution.isSupportUpperBoundInclusive.

Doc from org.apache.commons.math3.distribution.RealDistribution.isSupportUpperBoundInclusive.

Whether or not the upper bound of support is in the domain of the density function. Returns true iff getSupportUpperBound() is finite and density(getSupportUpperBound()) returns a non-NaN, non-infinite value.

Returns:boolean

true if the upper bound of support is finite and the density function returns a non-NaN, non-infinite value there

logDensityback to summary
public double logDensity(double x)

Overrides org.apache.commons.math3.distribution.AbstractRealDistribution.logDensity.

Doc from org.apache.commons.math3.distribution.AbstractRealDistribution.logDensity.

Returns the natural logarithm of the probability density function (PDF) of this distribution evaluated at the specified point x. In general, the PDF is the derivative of the CDF. If the derivative does not exist at x, then an appropriate replacement should be returned, e.g. Double.POSITIVE_INFINITY, Double.NaN, or the limit inferior or limit superior of the difference quotient. Note that due to the floating point precision and under/overflow issues, this method will for some distributions be more precise and faster than computing the logarithm of density(double). The default implementation simply computes the logarithm of density(x).

Parameters
x:double

the point at which the PDF is evaluated

Returns:double

the logarithm of the value of the probability density function at point x

Annotations
@Override
sampleback to summary
public double sample()

Overrides org.apache.commons.math3.distribution.AbstractRealDistribution.sample.

Implements org.apache.commons.math3.distribution.RealDistribution.sample.

This implementation uses the following algorithms:

For 0 < shape < 1:
Ahrens, J. H. and Dieter, U., Computer methods for sampling from gamma, beta, Poisson and binomial distributions. Computing, 12, 223-246, 1974.

For shape >= 1:
Marsaglia and Tsang, A Simple Method for Generating Gamma Variables. ACM Transactions on Mathematical Software, Volume 26 Issue 3, September, 2000.

Returns:double

random value sampled from the Gamma(shape, scale) distribution

Annotations
@Override