Class Gamma
- java.lang.Object
-
- com.opengamma.strata.math.impl.cern.Gamma
-
- All Implemented Interfaces:
Serializable
,Cloneable
,DoubleUnaryOperator
,IntUnaryOperator
public class Gamma extends Object
Gamma distribution; math definition, definition of gamma function and animated definition.p(x) = k * x^(alpha-1) * e^(-x/beta) with k = 1/(g(alpha) * b^a)) and g(a) being the gamma function.
Valid parameter ranges: alpha > 0.
Note: For a Gamma distribution to have the mean mean and variance variance, set the parameters as follows:
alpha = mean*mean / variance; lambda = 1 / (variance / mean);
Instance methods operate on a user supplied uniform random number generator; they are unsynchronized.
- Static methods operate on a default uniform random number generator; they are synchronized.
Implementation:
- Method: Acceptance Rejection combined with Acceptance Complement.
- High performance implementation. This is a port of RandGamma used in CLHEP 1.4.0 (C++). CLHEP's implementation, in turn, is based on gds.c from the C-RAND / WIN-RAND library. C-RAND's implementation, in turn, is based upon
J.H. Ahrens, U. Dieter (1974): Computer methods for sampling from gamma, beta, Poisson and binomial distributions, Computing 12, 223-246.
and
J.H. Ahrens, U. Dieter (1982): Generating gamma variates by a modified rejection technique, Communications of the ACM 25, 47-54.
- Version:
- 1.0, 09/24/99
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected double
alpha
protected double
lambda
protected RandomEngine
randomGenerator
protected static Gamma
shared
-
Constructor Summary
Constructors Constructor Description Gamma(double alpha, double lambda, RandomEngine randomGenerator)
Constructs a Gamma distribution.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
applyAsDouble(double dummy)
Equivalent to nextDouble().int
applyAsInt(int dummy)
Equivalent to nextInt().double
cdf(double x)
Returns the cumulative distribution function.Object
clone()
Returns a deep copy of the receiver; the copy will produce identical sequences.protected RandomEngine
getRandomGenerator()
Returns the used uniform random number generator;static RandomEngine
makeDefaultGenerator()
Constructs and returns a new uniform random number generation engine seeded with the current time.double
nextDouble()
Returns a random number from the distribution.double
nextDouble(double alpha, double lambda)
Returns a random number from the distribution; bypasses the internal state.int
nextInt()
Returns a random number from the distribution; returns (int) Math.round(nextDouble()).double
pdf(double x)
Returns the probability distribution function.protected void
setRandomGenerator(RandomEngine randomGenerator)
Sets the uniform random generator internally used.void
setState(double alpha, double lambda)
Sets the mean and variance.static double
staticNextDouble(double alpha, double lambda)
Returns a random number from the distribution.String
toString()
Returns a String representation of the receiver.-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.function.DoubleUnaryOperator
andThen, compose
-
Methods inherited from interface java.util.function.IntUnaryOperator
andThen, compose
-
-
-
-
Field Detail
-
alpha
protected double alpha
-
lambda
protected double lambda
-
shared
protected static Gamma shared
-
randomGenerator
protected RandomEngine randomGenerator
-
-
Constructor Detail
-
Gamma
public Gamma(double alpha, double lambda, RandomEngine randomGenerator)
Constructs a Gamma distribution. Example: alpha=1.0, lambda=1.0.- Parameters:
alpha
- alphalambda
- lambdarandomGenerator
- generator- Throws:
IllegalArgumentException
- if alpha <= 0.0 || lambda <= 0.0.
-
-
Method Detail
-
cdf
public double cdf(double x)
Returns the cumulative distribution function.- Parameters:
x
- x- Returns:
- result
-
nextDouble
public double nextDouble()
Returns a random number from the distribution.- Returns:
- result
-
nextDouble
public double nextDouble(double alpha, double lambda)
Returns a random number from the distribution; bypasses the internal state.- Parameters:
alpha
- alphalambda
- lambda- Returns:
- result
-
pdf
public double pdf(double x)
Returns the probability distribution function.- Parameters:
x
- x- Returns:
- result
-
setState
public void setState(double alpha, double lambda)
Sets the mean and variance.- Parameters:
alpha
- alphalambda
- lambda- Throws:
IllegalArgumentException
- if alpha <= 0.0 || lambda <= 0.0.
-
staticNextDouble
public static double staticNextDouble(double alpha, double lambda)
Returns a random number from the distribution.- Parameters:
alpha
- alphalambda
- lambda- Returns:
- result
- Throws:
IllegalArgumentException
- if alpha <= 0.0 || lambda <= 0.0.
-
toString
public String toString()
Returns a String representation of the receiver.
-
applyAsDouble
public double applyAsDouble(double dummy)
Equivalent to nextDouble(). This has the effect that distributions can now be used as function objects, returning a random number upon function evaluation.- Specified by:
applyAsDouble
in interfaceDoubleUnaryOperator
-
applyAsInt
public int applyAsInt(int dummy)
Equivalent to nextInt(). This has the effect that distributions can now be used as function objects, returning a random number upon function evaluation.- Specified by:
applyAsInt
in interfaceIntUnaryOperator
-
clone
public Object clone()
Returns a deep copy of the receiver; the copy will produce identical sequences. After this call has returned, the copy and the receiver have equal but separate state.- Returns:
- a copy of the receiver.
-
getRandomGenerator
protected RandomEngine getRandomGenerator()
Returns the used uniform random number generator;- Returns:
- result
-
makeDefaultGenerator
public static RandomEngine makeDefaultGenerator()
Constructs and returns a new uniform random number generation engine seeded with the current time. Currently this isMersenneTwister
.- Returns:
- result
-
nextInt
public int nextInt()
Returns a random number from the distribution; returns (int) Math.round(nextDouble()). Override this method if necessary.- Returns:
- result
-
setRandomGenerator
protected void setRandomGenerator(RandomEngine randomGenerator)
Sets the uniform random generator internally used.- Parameters:
randomGenerator
- input
-
-