001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.rng.sampling.distribution;
018
019import org.apache.commons.rng.UniformRandomProvider;
020
021/**
022 * Distribution sampler that uses the
023 * <a href="https://en.wikipedia.org/wiki/Inverse_transform_sampling">
024 * inversion method</a>.
025 *
026 * It can be used to sample any distribution that provides access to its
027 * <em>inverse cumulative probabilty function</em>.
028 *
029 * <p>Example:</p>
030 * <pre><code>
031 * import org.apache.commons.math3.distribution.IntegerDistribution;
032 * import org.apache.commons.math3.distribution.BinomialDistribution;
033 *
034 * import org.apache.commons.rng.simple.RandomSource;
035 * import org.apache.commons.rng.sampling.distribution.DiscreteSampler;
036 * import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler;
037 * import org.apache.commons.rng.sampling.distribution.DiscreteInverseCumulativeProbabilityFunction;
038 *
039 * // Distribution to sample.
040 * final IntegerDistribution dist = new BinomialDistribution(11, 0.56);
041 * // Create the sampler.
042 * final DiscreteSampler binomialSampler =
043 *     new InverseTransformDiscreteSampler(RandomSource.create(RandomSource.MT),
044 *                                         new DiscreteInverseCumulativeProbabilityFunction() {
045 *                                             public int inverseCumulativeProbability(double p) {
046 *                                                 return dist.inverseCumulativeProbability(p);
047 *                                             }
048 *                                         });
049 *
050 * // Generate random deviate.
051 * int random = binomialSampler.sample();
052 * </code></pre>
053 *
054 * @since 1.0
055 */
056public class InverseTransformDiscreteSampler
057    extends SamplerBase
058    implements DiscreteSampler {
059    /** Inverse cumulative probability function. */
060    private final DiscreteInverseCumulativeProbabilityFunction function;
061    /** Underlying source of randomness. */
062    private final UniformRandomProvider rng;
063
064    /**
065     * @param rng Generator of uniformly distributed random numbers.
066     * @param function Inverse cumulative probability function.
067     */
068    public InverseTransformDiscreteSampler(UniformRandomProvider rng,
069                                           DiscreteInverseCumulativeProbabilityFunction function) {
070        super(null);
071        this.rng = rng;
072        this.function = function;
073    }
074
075    /** {@inheritDoc} */
076    @Override
077    public int sample() {
078        return function.inverseCumulativeProbability(rng.nextDouble());
079    }
080
081    /** {@inheritDoc} */
082    @Override
083    public String toString() {
084        return function.toString() + " (inverse method) [" + rng.toString() + "]";
085    }
086}