DiscreteSampler, SharedStateDiscreteSampler, SharedStateSampler<SharedStateDiscreteSampler>public final class GuideTableDiscreteSampler extends java.lang.Object implements SharedStateDiscreteSampler
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().
| Modifier and Type | Method | Description |
|---|---|---|
static SharedStateDiscreteSampler |
of(org.apache.commons.rng.UniformRandomProvider rng,
double[] probabilities) |
Create a new sampler for an enumerated distribution using the given
probabilities. |
static SharedStateDiscreteSampler |
of(org.apache.commons.rng.UniformRandomProvider rng,
double[] probabilities,
double alpha) |
Create a new sampler for an enumerated distribution using the given
probabilities. |
int |
sample() |
Creates a sample.
|
java.lang.String |
toString() |
|
SharedStateDiscreteSampler |
withUniformRandomProvider(org.apache.commons.rng.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.
|
public int sample()
sample in interface DiscreteSamplerpublic java.lang.String toString()
toString in class java.lang.Objectpublic SharedStateDiscreteSampler withUniformRandomProvider(org.apache.commons.rng.UniformRandomProvider rng)
withUniformRandomProvider in interface SharedStateSampler<SharedStateDiscreteSampler>rng - Generator of uniformly distributed random numbers.public static SharedStateDiscreteSampler of(org.apache.commons.rng.UniformRandomProvider rng, double[] probabilities)
probabilities.
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.
rng - Generator of uniformly distributed random numbers.probabilities - The probabilities.java.lang.IllegalArgumentException - if probabilities is null or empty, a
probability is negative, infinite or NaN, or the sum of all
probabilities is not strictly positive.public static SharedStateDiscreteSampler of(org.apache.commons.rng.UniformRandomProvider rng, double[] probabilities, double alpha)
probabilities.
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.
rng - Generator of uniformly distributed random numbers.probabilities - The probabilities.alpha - The alpha factor used to set the guide table size.java.lang.IllegalArgumentException - if probabilities is null or empty, a
probability is negative, infinite or NaN, the sum of all
probabilities is not strictly positive, or alpha is not strictly positive.Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.