Class MarsagliaTsangWangDiscreteSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.MarsagliaTsangWangDiscreteSampler
-
public final class MarsagliaTsangWangDiscreteSampler extends java.lang.Object
Sampler for a discrete distribution using an optimised look-up table.- The method requires 30-bit integer probabilities that sum to 230 as described in George Marsaglia, Wai Wan Tsang, Jingbo Wang (2004) Fast Generation of Discrete Random Variables. Journal of Statistical Software. Vol. 11, Issue. 3, pp. 1-11.
Sampling uses 1 call to
UniformRandomProvider.nextInt()
.Memory requirements depend on the maximum number of possible sample values,
n
, and the values for the probabilities. Storage is optimised forn
. The worst case scenario is a uniform distribution of the maximum sample size. This is capped at 0.06MB forn <=
28, 17.0MB forn <=
216, and 4.3GB forn <=
230. Realistic requirements will be in the kB range.The sampler supports the following distributions:
- Enumerated distribution (probabilities must be provided for each sample)
- Poisson distribution up to
mean = 1024
- Binomial distribution up to
trials = 65535
- Since:
- 1.3
- See Also:
- Margsglia, et al (2004) JSS Vol. 11, Issue 3
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
MarsagliaTsangWangDiscreteSampler.AbstractMarsagliaTsangWangDiscreteSampler
The base class for Marsaglia-Tsang-Wang samplers.static class
MarsagliaTsangWangDiscreteSampler.Binomial
Create a sampler for the Binomial distribution.static class
MarsagliaTsangWangDiscreteSampler.Enumerated
Create a sampler for an enumerated distribution ofn
values each with an associated probability.private static class
MarsagliaTsangWangDiscreteSampler.MarsagliaTsangWangBase64Int16DiscreteSampler
An implementation for the sample algorithm based on the decomposition of the index in the range[0,2^30)
into 5 base-64 digits with 16-bit backing storage.private static class
MarsagliaTsangWangDiscreteSampler.MarsagliaTsangWangBase64Int32DiscreteSampler
An implementation for the sample algorithm based on the decomposition of the index in the range[0,2^30)
into 5 base-64 digits with 32-bit backing storage.private static class
MarsagliaTsangWangDiscreteSampler.MarsagliaTsangWangBase64Int8DiscreteSampler
An implementation for the sample algorithm based on the decomposition of the index in the range[0,2^30)
into 5 base-64 digits with 8-bit backing storage.static class
MarsagliaTsangWangDiscreteSampler.Poisson
Create a sampler for the Poisson distribution.
-
Constructor Summary
Constructors Modifier Constructor Description private
MarsagliaTsangWangDiscreteSampler()
Class contains only static methods.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static SharedStateDiscreteSampler
createSampler(UniformRandomProvider rng, java.lang.String distributionName, int[] prob, int offset)
Create a new instance for probabilitiesp(i)
where the sample valuex
isi + offset
.private static int
getBase64Digit(int m, int k)
Gets the kth base 64 digit ofm
.private static int
toUnsignedInt30(double p)
Convert the probability to an integer in the range [0,2^30].
-
-
-
Field Detail
-
INT_8
private static final int INT_8
The value 28 as anint
.- See Also:
- Constant Field Values
-
INT_16
private static final int INT_16
The value 216 as anint
.- See Also:
- Constant Field Values
-
INT_30
private static final int INT_30
The value 230 as anint
.- See Also:
- Constant Field Values
-
DOUBLE_31
private static final double DOUBLE_31
The value 231 as adouble
.- See Also:
- Constant Field Values
-
-
Method Detail
-
getBase64Digit
private static int getBase64Digit(int m, int k)
Gets the kth base 64 digit ofm
.- Parameters:
m
- the value m.k
- the digit.- Returns:
- the base 64 digit
-
toUnsignedInt30
private static int toUnsignedInt30(double p)
Convert the probability to an integer in the range [0,2^30]. This is the numerator of a fraction with assumed denominator 230.- Parameters:
p
- Probability.- Returns:
- the fraction numerator
-
createSampler
private static SharedStateDiscreteSampler createSampler(UniformRandomProvider rng, java.lang.String distributionName, int[] prob, int offset)
Create a new instance for probabilitiesp(i)
where the sample valuex
isi + offset
.The sum of the probabilities must be
>=
230. Only the values for cumulative probability up to 230 will be sampled.- Parameters:
rng
- Generator of uniformly distributed random numbers.distributionName
- Distribution name.prob
- The probabilities.offset
- The offset (must be positive).- Returns:
- Sampler.
-
-