Class LargeMeanPoissonSampler
java.lang.Object
org.apache.commons.rng.sampling.distribution.LargeMeanPoissonSampler
- All Implemented Interfaces:
DiscreteSampler
,SharedStateDiscreteSampler
,SharedStateSampler<SharedStateDiscreteSampler>
Sampler for the Poisson distribution.
-
For large means, we use the rejection algorithm described in
Devroye, Luc. (1981).The Computer Generation of Poisson Random Variables
Computing vol. 26 pp. 197-207.
This sampler is suitable for mean >= 40
.
Sampling uses:
- Since:
- 1.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
Encapsulate the state of the sampler. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final double
Algorithm constant:1 / (8 * lambda)
.private final double
Algorithm constant:Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1))
.private final SharedStateContinuousSampler
Exponential.private final InternalUtils.FactorialLog
Local class to computelog(n!)
.private final SharedStateContinuousSampler
Gaussian.private final double
Algorithm constant:delta / 2
.private final double
Algorithm constant:Math.floor(mean)
.private final double
Algorithm constant:Math.log(lambda)
.private final double
Algorithm constant:factorialLog((int) lambda)
.private static final double
Upper bound to avoid truncation.private static final InternalUtils.FactorialLog
Class to computelog(n!)
.private static final SharedStateDiscreteSampler
Used when there is no requirement for a small mean Poisson sampler.private final double
Algorithm constant:a1 / aSum
.private final double
Algorithm constant:a2 / aSum
.private final UniformRandomProvider
Underlying source of randomness.private final SharedStateDiscreteSampler
The internal Poisson sampler for the lambda fraction.private final double
Algorithm constant:Math.sqrt(lambda + halfDelta)
.private final double
Algorithm constant:2 * lambda + delta
. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
LargeMeanPoissonSampler
(double mean, UniformRandomProvider rng) private
LargeMeanPoissonSampler
(LargeMeanPoissonSampler.LargeMeanPoissonSamplerState state, double lambdaFractional, UniformRandomProvider rng) Instantiates a sampler using a precomputed state.LargeMeanPoissonSampler
(UniformRandomProvider rng, double mean) Create an instance.private
(package private)
LargeMeanPoissonSampler
(UniformRandomProvider rng, LargeMeanPoissonSampler.LargeMeanPoissonSamplerState state, double lambdaFractional) Instantiates a sampler using a precomputed state. -
Method Summary
Modifier and TypeMethodDescriptionprivate double
getFactorialLog
(int n) Compute the natural logarithm of the factorial ofn
.(package private) LargeMeanPoissonSampler.LargeMeanPoissonSamplerState
getState()
Gets the initialisation state of the sampler.static SharedStateDiscreteSampler
of
(UniformRandomProvider rng, double mean) Creates a new Poisson distribution sampler.int
sample()
Creates anint
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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.commons.rng.sampling.distribution.DiscreteSampler
samples, samples
-
Field Details
-
MAX_MEAN
private static final double MAX_MEANUpper bound to avoid truncation.- See Also:
-
NO_CACHE_FACTORIAL_LOG
Class to computelog(n!)
. This has no cached values. -
NO_SMALL_MEAN_POISSON_SAMPLER
Used when there is no requirement for a small mean Poisson sampler. -
rng
Underlying source of randomness. -
exponential
Exponential. -
gaussian
Gaussian. -
factorialLog
Local class to computelog(n!)
. This may have cached values. -
lambda
private final double lambdaAlgorithm constant:Math.floor(mean)
. -
logLambda
private final double logLambdaAlgorithm constant:Math.log(lambda)
. -
logLambdaFactorial
private final double logLambdaFactorialAlgorithm constant:factorialLog((int) lambda)
. -
delta
private final double deltaAlgorithm constant:Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1))
. -
halfDelta
private final double halfDeltaAlgorithm constant:delta / 2
. -
sqrtLambdaPlusHalfDelta
private final double sqrtLambdaPlusHalfDeltaAlgorithm constant:Math.sqrt(lambda + halfDelta)
. -
twolpd
private final double twolpdAlgorithm constant:2 * lambda + delta
. -
p1
private final double p1Algorithm constant:a1 / aSum
.a1 = Math.sqrt(Math.PI * twolpd) * Math.exp(c1)
aSum = a1 + a2 + 1
-
p2
private final double p2Algorithm constant:a2 / aSum
.a2 = (twolpd / delta) * Math.exp(-delta * (1 + delta) / twolpd)
aSum = a1 + a2 + 1
-
c1
private final double c1Algorithm constant:1 / (8 * lambda)
. -
smallMeanPoissonSampler
The internal Poisson sampler for the lambda fraction.
-
-
Constructor Details
-
LargeMeanPoissonSampler
Create an instance.- Parameters:
rng
- Generator of uniformly distributed random numbers.mean
- Mean.- Throws:
IllegalArgumentException
- ifmean < 1
ormean > 0.5 *
Integer.MAX_VALUE
.
-
LargeMeanPoissonSampler
LargeMeanPoissonSampler(UniformRandomProvider rng, LargeMeanPoissonSampler.LargeMeanPoissonSamplerState state, double lambdaFractional) Instantiates a sampler using a precomputed state.- Parameters:
rng
- Generator of uniformly distributed random numbers.state
- The state forlambda = (int)Math.floor(mean)
.lambdaFractional
- The lambda fractional value (mean - (int)Math.floor(mean))
.- Throws:
IllegalArgumentException
- iflambdaFractional < 0 || lambdaFractional >= 1
.
-
LargeMeanPoissonSampler
- Parameters:
mean
- Mean.rng
- Generator of uniformly distributed random numbers.
-
LargeMeanPoissonSampler
private LargeMeanPoissonSampler(LargeMeanPoissonSampler.LargeMeanPoissonSamplerState state, double lambdaFractional, UniformRandomProvider rng) Instantiates a sampler using a precomputed state.- Parameters:
state
- The state forlambda = (int)Math.floor(mean)
.lambdaFractional
- The lambda fractional value (mean - (int)Math.floor(mean))
.rng
- Generator of uniformly distributed random numbers.
-
LargeMeanPoissonSampler
- Parameters:
rng
- Generator of uniformly distributed random numbers.source
- Source to copy.
-
-
Method Details
-
sample
public int sample()Creates anint
sample.- Specified by:
sample
in interfaceDiscreteSampler
- Returns:
- a sample.
-
getFactorialLog
private double getFactorialLog(int n) Compute the natural logarithm of the factorial ofn
.- Parameters:
n
- Argument.- Returns:
log(n!)
- Throws:
IllegalArgumentException
- ifn < 0
.
-
toString
-
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<SharedStateDiscreteSampler>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
of
Creates a new Poisson distribution sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.mean
- Mean.- Returns:
- the sampler
- Throws:
IllegalArgumentException
- ifmean < 1
ormean > 0.5 *
Integer.MAX_VALUE
.- Since:
- 1.3
-
getState
Gets the initialisation state of the sampler.The state is computed using an integer
lambda
value oflambda = (int)Math.floor(mean)
.The state will be suitable for reconstructing a new sampler with a mean in the range
lambda <= mean < lambda+1
usingLargeMeanPoissonSampler(UniformRandomProvider, LargeMeanPoissonSamplerState, double)
.- Returns:
- the state
-