Package org.apache.commons.rng.sampling
Class DiscreteProbabilityCollectionSampler<T>
java.lang.Object
org.apache.commons.rng.sampling.DiscreteProbabilityCollectionSampler<T>
- Type Parameters:
T- Type of items in the collection.
public class DiscreteProbabilityCollectionSampler<T>
extends java.lang.Object
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
-
Constructor Summary
Constructors Constructor Description DiscreteProbabilityCollectionSampler(UniformRandomProvider rng, java.util.List<T> collection, double[] probabilities)Creates a sampler.DiscreteProbabilityCollectionSampler(UniformRandomProvider rng, java.util.Map<T,java.lang.Double> collection)Creates a sampler. -
Method Summary
-
Constructor Details
-
DiscreteProbabilityCollectionSampler
public DiscreteProbabilityCollectionSampler(UniformRandomProvider rng, java.util.Map<T,java.lang.Double> collection)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:
java.lang.IllegalArgumentException- ifcollectionis empty, a probability is negative, infinite orNaN, or the sum of all probabilities is not strictly positive.
-
DiscreteProbabilityCollectionSampler
public DiscreteProbabilityCollectionSampler(UniformRandomProvider rng, java.util.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:
java.lang.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.- Returns:
- a random sample.
-