Class InverseTransformDiscreteSampler

  • All Implemented Interfaces:
    DiscreteSampler, SharedStateDiscreteSampler, SharedStateSampler<SharedStateDiscreteSampler>

    public class InverseTransformDiscreteSampler
    extends SamplerBase
    implements SharedStateDiscreteSampler
    Distribution sampler that uses the inversion method. It can be used to sample any distribution that provides access to its inverse cumulative probability function.

    Sampling uses UniformRandomProvider.nextDouble().

    Example:

    
     import org.apache.commons.math3.distribution.IntegerDistribution;
     import org.apache.commons.math3.distribution.BinomialDistribution;
    
     import org.apache.commons.rng.simple.RandomSource;
     import org.apache.commons.rng.sampling.distribution.DiscreteSampler;
     import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler;
     import org.apache.commons.rng.sampling.distribution.DiscreteInverseCumulativeProbabilityFunction;
    
     // Distribution to sample.
     final IntegerDistribution dist = new BinomialDistribution(11, 0.56);
     // Create the sampler.
     final DiscreteSampler binomialSampler =
         InverseTransformDiscreteSampler.of(RandomSource.XO_RO_SHI_RO_128_PP.create(),
                                            new DiscreteInverseCumulativeProbabilityFunction() {
                                                public int inverseCumulativeProbability(double p) {
                                                    return dist.inverseCumulativeProbability(p);
                                                }
                                            });
    
     // Generate random deviate.
     int random = binomialSampler.sample();
     
    Since:
    1.0