Class DiscreteProbabilityCollectionSampler<T>

    • Field Detail

      • EMPTY_COLLECTION

        private static final java.lang.String EMPTY_COLLECTION
        The error message for an empty collection.
        See Also:
        Constant Field Values
      • items

        private final java.util.List<T> items
        Collection to be sampled from.
    • Constructor Detail

      • 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 - if collection is empty, a probability is negative, infinite or NaN, 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 the collection. 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 - if collection is empty or a probability is negative, infinite or NaN, or if the number of items in the collection is not equal to the number of provided probabilities.
    • Method Detail

      • sample

        public T sample()
        Picks one of the items from the collection passed to the constructor.
        Specified by:
        sample in interface ObjectSampler<T>
        Returns:
        a random sample.
      • 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