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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Class to sample from the Gamma distribution when0 < alpha < 1
.private static class
Base class for a sampler from the Gamma distribution.private static final class
Class to sample from the Gamma distribution when thealpha >= 1
. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final SharedStateContinuousSampler
The appropriate gamma sampler for the parameters. -
Constructor Summary
ConstructorsConstructorDescriptionAhrensDieterMarsagliaTsangGammaSampler
(UniformRandomProvider rng, double alpha, double theta) This instance delegates sampling. -
Method Summary
Modifier and TypeMethodDescriptionstatic SharedStateContinuousSampler
of
(UniformRandomProvider rng, double alpha, double theta) Creates a new gamma distribution sampler.double
sample()
Creates adouble
sample.toString()
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
-
Field Details
-
delegate
The appropriate gamma sampler for the parameters.
-
-
Constructor Details
-
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 Details
-
sample
public double sample()Creates adouble
sample.- Specified by:
sample
in interfaceContinuousSampler
- Returns:
- a sample.
-
toString
- Overrides:
toString
in classSamplerBase
-
withUniformRandomProvider
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
-