Class MarsagliaTsangWangDiscreteSampler.Binomial
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.MarsagliaTsangWangDiscreteSampler.Binomial
-
- Enclosing class:
- MarsagliaTsangWangDiscreteSampler
public static final class MarsagliaTsangWangDiscreteSampler.Binomial extends java.lang.Object
Create a sampler for the Binomial distribution.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
MarsagliaTsangWangDiscreteSampler.Binomial.MarsagliaTsangWangFixedResultBinomialSampler
Return a fixed result for the Binomial distribution.private static class
MarsagliaTsangWangDiscreteSampler.Binomial.MarsagliaTsangWangInversionBinomialSampler
Return an inversion result for the Binomial distribution.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
BINOMIAL_NAME
The name of the Binomial distribution.
-
Constructor Summary
Constructors Modifier Constructor Description private
Binomial()
Class contains only static methods.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static SharedStateDiscreteSampler
createBinomialDistributionSampler(UniformRandomProvider rng, int trials, double probabilityOfSuccess)
Creates the Binomial distribution sampler.private static SharedStateDiscreteSampler
createBinomialDistributionSamplerFromRange(UniformRandomProvider rng, int trials, double p, boolean useInversion, double p0, int begin, int end)
Creates the Binomial distribution sampler using only the probability values forX
between the begin and the end (inclusive).static SharedStateDiscreteSampler
of(UniformRandomProvider rng, int trials, double probabilityOfSuccess)
Creates a sampler for the Binomial distribution.private static void
validateBinomialDistributionParameters(int trials, double probabilityOfSuccess)
Validate the Binomial distribution parameters.
-
-
-
Field Detail
-
BINOMIAL_NAME
private static final java.lang.String BINOMIAL_NAME
The name of the Binomial distribution.- See Also:
- Constant Field Values
-
-
Method Detail
-
of
public static SharedStateDiscreteSampler of(UniformRandomProvider rng, int trials, double probabilityOfSuccess)
Creates a sampler for the Binomial distribution.Any probability less than 2-31 will not be observed in samples.
Storage requirements depend on the tabulated probability values. Example storage requirements are listed below (in kB).
p trials 0.5 0.1 0.01 0.001 4 0.06 0.63 0.44 0.44 16 0.69 1.14 0.76 0.44 64 4.73 2.40 1.14 0.51 256 8.63 5.17 1.89 0.82 1024 31.12 9.45 3.34 0.89
The method requires that the Binomial distribution probability at
x=0
can be computed. This will fail when(1 - p)^trials == 0
which requirestrials
to be large and/orp
to be small. In this case an exception is raised.- Parameters:
rng
- Generator of uniformly distributed random numbers.trials
- Number of trials.probabilityOfSuccess
- Probability of success (p).- Returns:
- Sampler.
- Throws:
java.lang.IllegalArgumentException
- iftrials < 0
ortrials >= 2^16
,p
is not in the range[0-1]
, or the probability distribution cannot be computed.
-
validateBinomialDistributionParameters
private static void validateBinomialDistributionParameters(int trials, double probabilityOfSuccess)
Validate the Binomial distribution parameters.- Parameters:
trials
- Number of trials.probabilityOfSuccess
- Probability of success (p).- Throws:
java.lang.IllegalArgumentException
- iftrials < 0
orp
is not in the range[0-1]
-
createBinomialDistributionSampler
private static SharedStateDiscreteSampler createBinomialDistributionSampler(UniformRandomProvider rng, int trials, double probabilityOfSuccess)
Creates the Binomial distribution sampler.This assumes the parameters for the distribution are valid. The method will only fail if the initial probability for
X=0
is zero.- Parameters:
rng
- Generator of uniformly distributed random numbers.trials
- Number of trials.probabilityOfSuccess
- Probability of success (p).- Returns:
- Sampler.
- Throws:
java.lang.IllegalArgumentException
- if the probability distribution cannot be computed.
-
createBinomialDistributionSamplerFromRange
private static SharedStateDiscreteSampler createBinomialDistributionSamplerFromRange(UniformRandomProvider rng, int trials, double p, boolean useInversion, double p0, int begin, int end)
Creates the Binomial distribution sampler using only the probability values forX
between the begin and the end (inclusive).- Parameters:
rng
- Generator of uniformly distributed random numbers.trials
- Number of trials.p
- Probability of success (p).useInversion
- Set totrue
if the probability was inverted.p0
- Probability atX=0
begin
- Begin valueX
for the distribution.end
- End valueX
for the distribution.- Returns:
- Sampler.
-
-