Class LargeMeanPoissonSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.LargeMeanPoissonSampler
-
- All Implemented Interfaces:
DiscreteSampler
,SharedStateDiscreteSampler
,SharedStateSampler<SharedStateDiscreteSampler>
public class LargeMeanPoissonSampler extends java.lang.Object implements 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 Classes Modifier and Type Class Description (package private) static class
LargeMeanPoissonSampler.LargeMeanPoissonSamplerState
Encapsulate the state of the sampler.
-
Field Summary
Fields Modifier and Type Field Description private double
c1
Algorithm constant:1 / (8 * lambda)
.private double
delta
Algorithm constant:Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1))
.private SharedStateContinuousSampler
exponential
Exponential.private InternalUtils.FactorialLog
factorialLog
Local class to computelog(n!)
.private SharedStateContinuousSampler
gaussian
Gaussian.private double
halfDelta
Algorithm constant:delta / 2
.private double
lambda
Algorithm constant:Math.floor(mean)
.private double
logLambda
Algorithm constant:Math.log(lambda)
.private double
logLambdaFactorial
Algorithm constant:factorialLog((int) lambda)
.private static double
MAX_MEAN
Upper bound to avoid truncation.private static InternalUtils.FactorialLog
NO_CACHE_FACTORIAL_LOG
Class to computelog(n!)
.private static SharedStateDiscreteSampler
NO_SMALL_MEAN_POISSON_SAMPLER
Used when there is no requirement for a small mean Poisson sampler.private double
p1
Algorithm constant:a1 / aSum
.private double
p2
Algorithm constant:a2 / aSum
.private UniformRandomProvider
rng
Underlying source of randomness.private SharedStateDiscreteSampler
smallMeanPoissonSampler
The internal Poisson sampler for the lambda fraction.private double
sqrtLambdaPlusHalfDelta
Algorithm constant:Math.sqrt(lambda + halfDelta)
.private double
twolpd
Algorithm constant:2 * lambda + delta
.
-
Constructor Summary
Constructors Modifier Constructor Description LargeMeanPoissonSampler(UniformRandomProvider rng, double mean)
private
LargeMeanPoissonSampler(UniformRandomProvider rng, LargeMeanPoissonSampler source)
(package private)
LargeMeanPoissonSampler(UniformRandomProvider rng, LargeMeanPoissonSampler.LargeMeanPoissonSamplerState state, double lambdaFractional)
Instantiates a sampler using a precomputed state.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private 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.java.lang.String
toString()
SharedStateDiscreteSampler
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 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 Detail
-
MAX_MEAN
private static final double MAX_MEAN
Upper bound to avoid truncation.- See Also:
- Constant Field Values
-
NO_CACHE_FACTORIAL_LOG
private static final InternalUtils.FactorialLog NO_CACHE_FACTORIAL_LOG
Class to computelog(n!)
. This has no cached values.
-
NO_SMALL_MEAN_POISSON_SAMPLER
private static final SharedStateDiscreteSampler NO_SMALL_MEAN_POISSON_SAMPLER
Used when there is no requirement for a small mean Poisson sampler.
-
rng
private final UniformRandomProvider rng
Underlying source of randomness.
-
exponential
private final SharedStateContinuousSampler exponential
Exponential.
-
gaussian
private final SharedStateContinuousSampler gaussian
Gaussian.
-
factorialLog
private final InternalUtils.FactorialLog factorialLog
Local class to computelog(n!)
. This may have cached values.
-
lambda
private final double lambda
Algorithm constant:Math.floor(mean)
.
-
logLambda
private final double logLambda
Algorithm constant:Math.log(lambda)
.
-
logLambdaFactorial
private final double logLambdaFactorial
Algorithm constant:factorialLog((int) lambda)
.
-
delta
private final double delta
Algorithm constant:Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1))
.
-
halfDelta
private final double halfDelta
Algorithm constant:delta / 2
.
-
sqrtLambdaPlusHalfDelta
private final double sqrtLambdaPlusHalfDelta
Algorithm constant:Math.sqrt(lambda + halfDelta)
.
-
twolpd
private final double twolpd
Algorithm constant:2 * lambda + delta
.
-
p1
private final double p1
Algorithm constant:a1 / aSum
.a1 = Math.sqrt(Math.PI * twolpd) * Math.exp(c1)
aSum = a1 + a2 + 1
-
p2
private final double p2
Algorithm constant:a2 / aSum
.a2 = (twolpd / delta) * Math.exp(-delta * (1 + delta) / twolpd)
aSum = a1 + a2 + 1
-
c1
private final double c1
Algorithm constant:1 / (8 * lambda)
.
-
smallMeanPoissonSampler
private final SharedStateDiscreteSampler smallMeanPoissonSampler
The internal Poisson sampler for the lambda fraction.
-
-
Constructor Detail
-
LargeMeanPoissonSampler
public LargeMeanPoissonSampler(UniformRandomProvider rng, double mean)
- Parameters:
rng
- Generator of uniformly distributed random numbers.mean
- Mean.- Throws:
java.lang.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:
java.lang.IllegalArgumentException
- iflambdaFractional < 0 || lambdaFractional >= 1
.
-
LargeMeanPoissonSampler
private LargeMeanPoissonSampler(UniformRandomProvider rng, LargeMeanPoissonSampler source)
- Parameters:
rng
- Generator of uniformly distributed random numbers.source
- Source to copy.
-
-
Method Detail
-
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:
java.lang.IllegalArgumentException
- ifn < 0
.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
withUniformRandomProvider
public SharedStateDiscreteSampler 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<SharedStateDiscreteSampler>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
of
public static SharedStateDiscreteSampler of(UniformRandomProvider rng, double mean)
Creates a new Poisson distribution sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.mean
- Mean.- Returns:
- the sampler
- Throws:
java.lang.IllegalArgumentException
- ifmean < 1
ormean > 0.5 *
Integer.MAX_VALUE
.- Since:
- 1.3
-
getState
LargeMeanPoissonSampler.LargeMeanPoissonSamplerState 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
-
-