Class AhrensDieterMarsagliaTsangGammaSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.SamplerBase
-
- org.apache.commons.rng.sampling.distribution.AhrensDieterMarsagliaTsangGammaSampler
-
- All Implemented Interfaces:
ContinuousSampler
,SharedStateContinuousSampler
,SharedStateSampler<SharedStateContinuousSampler>
public class AhrensDieterMarsagliaTsangGammaSampler extends SamplerBase implements SharedStateContinuousSampler
Sampling from the gamma distribution.-
For
0 < alpha < 1
:Ahrens, J. H. and Dieter, U., Computer methods for sampling from gamma, beta, Poisson and binomial distributions, Computing, 12, 223-246, 1974.
-
For
alpha >= 1
:Marsaglia and Tsang, A Simple Method for Generating Gamma Variables. ACM Transactions on Mathematical Software, Volume 26 Issue 3, September, 2000.
Sampling uses:
UniformRandomProvider.nextDouble()
(both algorithms)UniformRandomProvider.nextLong()
(only foralpha >= 1
)
- Since:
- 1.0
- See Also:
- MathWorld Gamma distribution, Wikipedia Gamma distribution
-
-
Constructor Summary
Constructors Constructor Description AhrensDieterMarsagliaTsangGammaSampler(UniformRandomProvider rng, double alpha, double theta)
This instance delegates sampling.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SharedStateContinuousSampler
of(UniformRandomProvider rng, double alpha, double theta)
Creates a new gamma distribution sampler.double
sample()
Creates adouble
sample.String
toString()
SharedStateContinuousSampler
withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.-
Methods inherited from class org.apache.commons.rng.sampling.distribution.SamplerBase
nextDouble, nextInt, nextInt, nextLong
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.rng.sampling.distribution.ContinuousSampler
samples, samples
-
-
-
-
Constructor Detail
-
AhrensDieterMarsagliaTsangGammaSampler
public AhrensDieterMarsagliaTsangGammaSampler(UniformRandomProvider rng, double alpha, double theta)
This instance delegates sampling. Use the factory methodof(UniformRandomProvider, double, double)
to create an optimal sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.alpha
- Alpha parameter of the distribution (this is a shape parameter).theta
- Theta parameter of the distribution (this is a scale parameter).- Throws:
IllegalArgumentException
- ifalpha <= 0
ortheta <= 0
-
-
Method Detail
-
sample
public double sample()
Creates adouble
sample.- Specified by:
sample
in interfaceContinuousSampler
- Returns:
- a sample.
-
toString
public String toString()
- Overrides:
toString
in classSamplerBase
-
withUniformRandomProvider
public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<SharedStateContinuousSampler>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
of
public static SharedStateContinuousSampler of(UniformRandomProvider rng, double alpha, double theta)
Creates a new gamma distribution sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.alpha
- Alpha parameter of the distribution (this is a shape parameter).theta
- Theta parameter of the distribution (this is a scale parameter).- Returns:
- the sampler
- Throws:
IllegalArgumentException
- ifalpha <= 0
ortheta <= 0
- Since:
- 1.3
-
-