Class SmallMeanPoissonSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.SmallMeanPoissonSampler
-
- All Implemented Interfaces:
DiscreteSampler,SharedStateDiscreteSampler,SharedStateSampler<SharedStateDiscreteSampler>
public class SmallMeanPoissonSampler extends java.lang.Object implements SharedStateDiscreteSampler
Sampler for the Poisson distribution.-
For small means, a Poisson process is simulated using uniform deviates, as described in
Knuth (1969). Seminumerical Algorithms. The Art of Computer Programming, Volume 2. Chapter 3.4.1.F.3 Important integer-valued distributions: The Poisson distribution. Addison Wesley.
The Poisson process (and hence, the returned value) is bounded by1000 * mean.
This sampler is suitable for
mean < 40. For large means,LargeMeanPoissonSamplershould be used instead.Sampling uses
UniformRandomProvider.nextDouble()and requires on averagemean + 1deviates per sample.- Since:
- 1.1
-
-
Field Summary
Fields Modifier and Type Field Description private intlimitPre-compute1000 * meanas the upper limit of the sample.private doublep0Pre-computeMath.exp(-mean).private UniformRandomProviderrngUnderlying source of randomness.
-
Constructor Summary
Constructors Modifier Constructor Description SmallMeanPoissonSampler(UniformRandomProvider rng, double mean)Create an instance.privateSmallMeanPoissonSampler(UniformRandomProvider rng, double mean, double p0)Instantiates a new small mean poisson sampler.privateSmallMeanPoissonSampler(UniformRandomProvider rng, SmallMeanPoissonSampler source)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static doublecomputeP0(double mean)ComputeMath.exp(-mean).static SharedStateDiscreteSamplerof(UniformRandomProvider rng, double mean)Creates a new sampler for the Poisson distribution.intsample()Creates anintsample.java.lang.StringtoString()SharedStateDiscreteSamplerwithUniformRandomProvider(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
-
p0
private final double p0
Pre-computeMath.exp(-mean). Note: This is the probability of the Poisson sampleP(n=0).
-
limit
private final int limit
Pre-compute1000 * meanas the upper limit of the sample.
-
rng
private final UniformRandomProvider rng
Underlying source of randomness.
-
-
Constructor Detail
-
SmallMeanPoissonSampler
public SmallMeanPoissonSampler(UniformRandomProvider rng, double mean)
Create an instance.- Parameters:
rng- Generator of uniformly distributed random numbers.mean- Mean.- Throws:
java.lang.IllegalArgumentException- ifmean <= 0orMath.exp(-mean) == 0
-
SmallMeanPoissonSampler
private SmallMeanPoissonSampler(UniformRandomProvider rng, double mean, double p0)
Instantiates a new small mean poisson sampler.- Parameters:
rng- Generator of uniformly distributed random numbers.mean- Mean.p0-Math.exp(-mean).
-
SmallMeanPoissonSampler
private SmallMeanPoissonSampler(UniformRandomProvider rng, SmallMeanPoissonSampler source)
- Parameters:
rng- Generator of uniformly distributed random numbers.source- Source to copy.
-
-
Method Detail
-
sample
public int sample()
Creates anintsample.- Specified by:
samplein interfaceDiscreteSampler- Returns:
- a sample.
-
toString
public java.lang.String toString()
- Overrides:
toStringin 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:
withUniformRandomProviderin 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 sampler for the Poisson distribution.- Parameters:
rng- Generator of uniformly distributed random numbers.mean- Mean of the distribution.- Returns:
- the sampler
- Throws:
java.lang.IllegalArgumentException- ifmean <= 0orMath.exp(-mean) == 0.- Since:
- 1.3
-
computeP0
private static double computeP0(double mean)
ComputeMath.exp(-mean).This method exists to raise an exception before invocation of the private constructor; this mitigates Finalizer attacks (see SpotBugs CT_CONSTRUCTOR_THROW).
- Parameters:
mean- Mean.- Returns:
- the mean
- Throws:
java.lang.IllegalArgumentException- ifmean <= 0orMath.exp(-mean) == 0
-
-