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 * Sampler for the <a href="http://mathworld.wolfram.com/PoissonDistribution.html">Poisson distribution</a>.
023 *
024 * <ul>
025 *  <li>
026 *   For small means, a Poisson process is simulated using uniform deviates, as
027 *   described <a href="http://mathaa.epfl.ch/cours/PMMI2001/interactive/rng7.htm">here</a>.
028 *   The Poisson process (and hence, the returned value) is bounded by 1000 * mean.
029 *  </li>
030 *  <li>
031 *   For large means, we use the rejection algorithm described in
032 *   <blockquote>
033 *    Devroye, Luc. (1981). <i>The Computer Generation of Poisson Random Variables</i><br>
034 *    <strong>Computing</strong> vol. 26 pp. 197-207.
035 *   </blockquote>
036 *  </li>
037 * </ul>
038 *
039 * @since 1.0
040 */
041public class PoissonSampler
042    extends SamplerBase
043    implements DiscreteSampler {
044
045    /**
046     * Value for switching sampling algorithm.
047     *
048     * <p>Package scope for the {@link PoissonSamplerCache}.
049     */
050    static final double PIVOT = 40;
051    /** The internal Poisson sampler. */
052    private final DiscreteSampler poissonSampler;
053
054    /**
055     * @param rng Generator of uniformly distributed random numbers.
056     * @param mean Mean.
057     * @throws IllegalArgumentException if {@code mean <= 0} or
058     * {@code mean >} {@link Integer#MAX_VALUE}.
059     */
060    public PoissonSampler(UniformRandomProvider rng,
061                          double mean) {
062        super(null);
063
064        // Delegate all work to specialised samplers.
065        // These should check the input arguments.
066        poissonSampler = mean < PIVOT ?
067            new SmallMeanPoissonSampler(rng, mean) :
068            new LargeMeanPoissonSampler(rng, mean);
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public int sample() {
074        return poissonSampler.sample();
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    public String toString() {
080        return poissonSampler.toString();
081    }
082}