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 classEncapsulate the state of the sampler. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final doubleAlgorithm constant:1 / (8 * lambda).private final doubleAlgorithm constant:Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1)).private final SharedStateContinuousSamplerExponential.private final InternalUtils.FactorialLogLocal class to computelog(n!).private final SharedStateContinuousSamplerGaussian.private final doubleAlgorithm constant:delta / 2.private final doubleAlgorithm constant:Math.floor(mean).private final doubleAlgorithm constant:Math.log(lambda).private final doubleAlgorithm constant:factorialLog((int) lambda).private static final doubleUpper bound to avoid truncation.private static final InternalUtils.FactorialLogClass to computelog(n!).private static final SharedStateDiscreteSamplerUsed when there is no requirement for a small mean Poisson sampler.private final doubleAlgorithm constant:a1 / aSum.private final doubleAlgorithm constant:a2 / aSum.private final UniformRandomProviderUnderlying source of randomness.private final SharedStateDiscreteSamplerThe internal Poisson sampler for the lambda fraction.private final doubleAlgorithm constant:Math.sqrt(lambda + halfDelta).private final doubleAlgorithm constant:2 * lambda + delta. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateLargeMeanPoissonSampler(double mean, UniformRandomProvider rng) privateLargeMeanPoissonSampler(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 doublegetFactorialLog(int n) Compute the natural logarithm of the factorial ofn.(package private) LargeMeanPoissonSampler.LargeMeanPoissonSamplerStategetState()Gets the initialisation state of the sampler.static SharedStateDiscreteSamplerof(UniformRandomProvider rng, double mean) Creates a new Poisson distribution sampler.intsample()Creates anintsample.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, waitMethods 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 < 1ormean > 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 anintsample.- Specified by:
samplein 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:
withUniformRandomProviderin 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 < 1ormean > 0.5 *Integer.MAX_VALUE.- Since:
- 1.3
-
getState
Gets the initialisation state of the sampler.The state is computed using an integer
lambdavalue oflambda = (int)Math.floor(mean).The state will be suitable for reconstructing a new sampler with a mean in the range
lambda <= mean < lambda+1usingLargeMeanPoissonSampler(UniformRandomProvider, LargeMeanPoissonSamplerState, double).- Returns:
- the state
-