Class DiscreteProbabilityCollectionSampler<T>
java.lang.Object
org.apache.commons.rng.sampling.DiscreteProbabilityCollectionSampler<T>
- Type Parameters:
T- Type of items in the collection.
- All Implemented Interfaces:
ObjectSampler<T>, SharedStateObjectSampler<T>, SharedStateSampler<SharedStateObjectSampler<T>>
public class DiscreteProbabilityCollectionSampler<T>
extends Object
implements SharedStateObjectSampler<T>
Sampling from a collection of items with user-defined
probabilities.
Note that if all unique items are assigned the same probability,
it is much more efficient to use
CollectionSampler.
Sampling uses UniformRandomProvider.nextDouble().
- Since:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThe error message for an empty collection.Collection to be sampled from.private final SharedStateDiscreteSamplerSampler for the probabilities. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateDiscreteProbabilityCollectionSampler(List<T> items, SharedStateDiscreteSampler sampler) DiscreteProbabilityCollectionSampler(UniformRandomProvider rng, List<T> collection, double[] probabilities) Creates a sampler.DiscreteProbabilityCollectionSampler(UniformRandomProvider rng, Map<T, Double> collection) Creates a sampler. -
Method Summary
Modifier and TypeMethodDescriptionprivate static <T> List<T> Create a (shallow) copy of the collection.private static SharedStateDiscreteSamplercreateSampler(UniformRandomProvider rng, double[] probabilities) Creates the sampler of the enumerated probability distribution.private static <T> SharedStateDiscreteSamplercreateSampler(UniformRandomProvider rng, List<T> collection, double[] probabilities) Creates the sampler of the enumerated probability distribution.sample()Picks one of the items from the collection passed to the constructor.private static <T> List<T> Extract the items.private static <T> double[]toProbabilities(Map<T, Double> collection) Extract the probabilities.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, toString, wait, wait, waitMethods inherited from interface ObjectSampler
samples, samples
-
Field Details
-
EMPTY_COLLECTION
-
items
-
sampler
Sampler for the probabilities.
-
-
Constructor Details
-
DiscreteProbabilityCollectionSampler
Creates a sampler.- Parameters:
rng- Generator of uniformly distributed random numbers.collection- Collection to be sampled, with the probabilities associated to each of its items. A (shallow) copy of the items will be stored in the created instance. The probabilities must be non-negative, but zero values are allowed and their sum does not have to equal one (input will be normalized to make the probabilities sum to one).- Throws:
IllegalArgumentException- ifcollectionis empty, a probability is negative, infinite orNaN, or the sum of all probabilities is not strictly positive.
-
DiscreteProbabilityCollectionSampler
public DiscreteProbabilityCollectionSampler(UniformRandomProvider rng, List<T> collection, double[] probabilities) Creates a sampler.- Parameters:
rng- Generator of uniformly distributed random numbers.collection- Collection to be sampled. A (shallow) copy of the items will be stored in the created instance.probabilities- Probability associated to each item of thecollection. The probabilities must be non-negative, but zero values are allowed and their sum does not have to equal one (input will be normalized to make the probabilities sum to one).- Throws:
IllegalArgumentException- ifcollectionis empty or a probability is negative, infinite orNaN, or if the number of items in thecollectionis not equal to the number of providedprobabilities.
-
-
Method Details
-
sample
Picks one of the items from the collection passed to the constructor.- Specified by:
samplein interfaceObjectSampler<T>- Returns:
- a random sample.
-
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<T>- Parameters:
rng- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
createSampler
private static SharedStateDiscreteSampler createSampler(UniformRandomProvider rng, double[] probabilities) Creates the sampler of the enumerated probability distribution.- Parameters:
rng- Generator of uniformly distributed random numbers.probabilities- Probability associated to each item.- Returns:
- the sampler
-
createSampler
private static <T> SharedStateDiscreteSampler createSampler(UniformRandomProvider rng, List<T> collection, double[] probabilities) Creates the sampler of the enumerated probability distribution.- Type Parameters:
T- Type of items in the collection.- Parameters:
rng- Generator of uniformly distributed random numbers.collection- Collection to be sampled.probabilities- Probability associated to each item.- Returns:
- the sampler
- Throws:
IllegalArgumentException- if the number of items in thecollectionis not equal to the number of providedprobabilities.
-
toList
Extract the items.- Type Parameters:
T- Type of items in the collection.- Parameters:
collection- Collection.- Returns:
- the items
- Throws:
IllegalArgumentException- ifcollectionis empty.
-
toProbabilities
-
copyList
Create a (shallow) copy of the collection.- Type Parameters:
T- Type of items in the collection.- Parameters:
collection- Collection.- Returns:
- the copy
- Throws:
IllegalArgumentException- ifcollectionis empty.
-