Class GeometricSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.GeometricSampler
-
- All Implemented Interfaces:
DiscreteSampler
public class GeometricSampler extends java.lang.Object implements DiscreteSampler
Sampling from a geometric distribution.This distribution samples the number of failures before the first success taking values in the set
[0, 1, 2, ...].The sample is computed using a related exponential distribution. If \( X \) is an exponentially distributed random variable with parameter \( \lambda \), then \( Y = \left \lfloor X \right \rfloor \) is a geometrically distributed random variable with parameter \( p = 1 − e^\lambda \), with \( p \) the probability of success.
This sampler outperforms using the
InverseTransformDiscreteSamplerwith an appropriate Geometric inverse cumulative probability function.Usage note: As the probability of success (\( p \)) tends towards zero the mean of the distribution (\( \frac{1-p}{p} \)) tends towards infinity and due to the use of
intfor the sample this can result in truncation of the distribution.Sampling uses
UniformRandomProvider.nextDouble().- Since:
- 1.3
- See Also:
- Geometric distribution - related distributions
-
-
Constructor Summary
Constructors Constructor Description GeometricSampler(UniformRandomProvider rng, double probabilityOfSuccess)Creates a new geometric distribution sampler.
-
-
-
Constructor Detail
-
GeometricSampler
public GeometricSampler(UniformRandomProvider rng, double probabilityOfSuccess)
Creates a new geometric distribution sampler. The samples will be provided in the setk=[0, 1, 2, ...]wherekindicates the number of failures before the first success.- Parameters:
rng- Generator of uniformly distributed random numbersprobabilityOfSuccess- The probability of success- Throws:
java.lang.IllegalArgumentException- ifprobabilityOfSuccessis not in the range[0 < probabilityOfSuccess <= 1])
-
-
Method Detail
-
sample
public int sample()
Create a sample from a geometric distribution.The sample will take the values in the set
[0, 1, 2, ...], equivalent to the number of failures before the first success.- Specified by:
samplein interfaceDiscreteSampler- Returns:
- a sample.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-