Class GuideTableDiscreteSampler
java.lang.Object
org.apache.commons.rng.sampling.distribution.GuideTableDiscreteSampler
- All Implemented Interfaces:
DiscreteSampler, SharedStateDiscreteSampler, SharedStateSampler<SharedStateDiscreteSampler>
Compute a sample from
n values each with an associated probability. If all unique items
are assigned the same probability it is more efficient to use the DiscreteUniformSampler.
The cumulative probability distribution is searched using a guide table to set an initial start point. This implementation is based on:
Devroye, Luc (1986). Non-Uniform Random Variate Generation. New York: Springer-Verlag. Chapter 3.2.4 "The method of guide tables" p. 96.
The size of the guide table can be controlled using a parameter. A larger guide table will improve performance at the cost of storage space.
Sampling uses UniformRandomProvider.nextDouble().
- Since:
- 1.3
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic SharedStateDiscreteSamplerof(UniformRandomProvider rng, double[] probabilities) Create a new sampler for an enumerated distribution using the givenprobabilities.static SharedStateDiscreteSamplerof(UniformRandomProvider rng, double[] probabilities, double alpha) Create a new sampler for an enumerated distribution using the givenprobabilities.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 Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface DiscreteSampler
samples, samples
-
Method Details
-
sample
Creates anintsample.- Specified by:
samplein interfaceDiscreteSampler- Returns:
- a sample.
-
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
-
of
Create a new sampler for an enumerated distribution using the givenprobabilities. The samples corresponding to each probability are assumed to be a natural sequence starting at zero.The size of the guide table is
probabilities.length.- Parameters:
rng- Generator of uniformly distributed random numbers.probabilities- The probabilities.- Returns:
- the sampler
- Throws:
IllegalArgumentException- ifprobabilitiesis null or empty, a probability is negative, infinite orNaN, or the sum of all probabilities is not strictly positive.
-
of
public static SharedStateDiscreteSampler of(UniformRandomProvider rng, double[] probabilities, double alpha) Create a new sampler for an enumerated distribution using the givenprobabilities. The samples corresponding to each probability are assumed to be a natural sequence starting at zero.The size of the guide table is
alpha * probabilities.length.- Parameters:
rng- Generator of uniformly distributed random numbers.probabilities- The probabilities.alpha- The alpha factor used to set the guide table size.- Returns:
- the sampler
- Throws:
IllegalArgumentException- ifprobabilitiesis null or empty, a probability is negative, infinite orNaN, the sum of all probabilities is not strictly positive, oralphais not strictly positive.
-