LargeMeanPoissonSampler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.rng.sampling.distribution;

  18. import org.apache.commons.rng.UniformRandomProvider;
  19. import org.apache.commons.rng.sampling.distribution.InternalUtils.FactorialLog;

  20. /**
  21.  * Sampler for the <a href="http://mathworld.wolfram.com/PoissonDistribution.html">Poisson distribution</a>.
  22.  *
  23.  * <ul>
  24.  *  <li>
  25.  *   For large means, we use the rejection algorithm described in
  26.  *   <blockquote>
  27.  *    Devroye, Luc. (1981).<i>The Computer Generation of Poisson Random Variables</i><br>
  28.  *    <strong>Computing</strong> vol. 26 pp. 197-207.
  29.  *   </blockquote>
  30.  *  </li>
  31.  * </ul>
  32.  *
  33.  * <p>This sampler is suitable for {@code mean >= 40}.</p>
  34.  *
  35.  * <p>Sampling uses:</p>
  36.  *
  37.  * <ul>
  38.  *   <li>{@link UniformRandomProvider#nextLong()}
  39.  *   <li>{@link UniformRandomProvider#nextDouble()}
  40.  * </ul>
  41.  *
  42.  * @since 1.1
  43.  */
  44. public class LargeMeanPoissonSampler
  45.     implements SharedStateDiscreteSampler {
  46.     /** Upper bound to avoid truncation. */
  47.     private static final double MAX_MEAN = 0.5 * Integer.MAX_VALUE;
  48.     /** Class to compute {@code log(n!)}. This has no cached values. */
  49.     private static final InternalUtils.FactorialLog NO_CACHE_FACTORIAL_LOG;
  50.     /** Used when there is no requirement for a small mean Poisson sampler. */
  51.     private static final SharedStateDiscreteSampler NO_SMALL_MEAN_POISSON_SAMPLER =
  52.         new SharedStateDiscreteSampler() {
  53.             @Override
  54.             public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) {
  55.                 // No requirement for RNG
  56.                 return this;
  57.             }

  58.             @Override
  59.             public int sample() {
  60.                 // No Poisson sample
  61.                 return 0;
  62.             }
  63.         };

  64.     static {
  65.         // Create without a cache.
  66.         NO_CACHE_FACTORIAL_LOG = FactorialLog.create();
  67.     }

  68.     /** Underlying source of randomness. */
  69.     private final UniformRandomProvider rng;
  70.     /** Exponential. */
  71.     private final SharedStateContinuousSampler exponential;
  72.     /** Gaussian. */
  73.     private final SharedStateContinuousSampler gaussian;
  74.     /** Local class to compute {@code log(n!)}. This may have cached values. */
  75.     private final InternalUtils.FactorialLog factorialLog;

  76.     // Working values

  77.     /** Algorithm constant: {@code Math.floor(mean)}. */
  78.     private final double lambda;
  79.     /** Algorithm constant: {@code Math.log(lambda)}. */
  80.     private final double logLambda;
  81.     /** Algorithm constant: {@code factorialLog((int) lambda)}. */
  82.     private final double logLambdaFactorial;
  83.     /** Algorithm constant: {@code Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1))}. */
  84.     private final double delta;
  85.     /** Algorithm constant: {@code delta / 2}. */
  86.     private final double halfDelta;
  87.     /** Algorithm constant: {@code Math.sqrt(lambda + halfDelta)}. */
  88.     private final double sqrtLambdaPlusHalfDelta;
  89.     /** Algorithm constant: {@code 2 * lambda + delta}. */
  90.     private final double twolpd;
  91.     /**
  92.      * Algorithm constant: {@code a1 / aSum}.
  93.      * <ul>
  94.      *  <li>{@code a1 = Math.sqrt(Math.PI * twolpd) * Math.exp(c1)}</li>
  95.      *  <li>{@code aSum = a1 + a2 + 1}</li>
  96.      * </ul>
  97.      */
  98.     private final double p1;
  99.     /**
  100.      * Algorithm constant: {@code a2 / aSum}.
  101.      * <ul>
  102.      *  <li>{@code a2 = (twolpd / delta) * Math.exp(-delta * (1 + delta) / twolpd)}</li>
  103.      *  <li>{@code aSum = a1 + a2 + 1}</li>
  104.      * </ul>
  105.      */
  106.     private final double p2;
  107.     /** Algorithm constant: {@code 1 / (8 * lambda)}. */
  108.     private final double c1;

  109.     /** The internal Poisson sampler for the lambda fraction. */
  110.     private final SharedStateDiscreteSampler smallMeanPoissonSampler;


  111.     /**
  112.      * Create an instance.
  113.      *
  114.      * @param rng Generator of uniformly distributed random numbers.
  115.      * @param mean Mean.
  116.      * @throws IllegalArgumentException if {@code mean < 1} or
  117.      * {@code mean > 0.5 *} {@link Integer#MAX_VALUE}.
  118.      */
  119.     public LargeMeanPoissonSampler(UniformRandomProvider rng,
  120.                                    double mean) {
  121.         // Validation before java.lang.Object constructor exits prevents partially initialized object
  122.         this(InternalUtils.requireRangeClosed(1, MAX_MEAN, mean, "mean"), rng);
  123.     }

  124.     /**
  125.      * Instantiates a sampler using a precomputed state.
  126.      *
  127.      * @param rng              Generator of uniformly distributed random numbers.
  128.      * @param state            The state for {@code lambda = (int)Math.floor(mean)}.
  129.      * @param lambdaFractional The lambda fractional value
  130.      *                         ({@code mean - (int)Math.floor(mean))}.
  131.      * @throws IllegalArgumentException
  132.      *                         if {@code lambdaFractional < 0 || lambdaFractional >= 1}.
  133.      */
  134.     LargeMeanPoissonSampler(UniformRandomProvider rng,
  135.                             LargeMeanPoissonSamplerState state,
  136.                             double lambdaFractional) {
  137.         // Validation before java.lang.Object constructor exits prevents partially initialized object
  138.         this(state, InternalUtils.requireRange(0, 1, lambdaFractional, "lambdaFractional"), rng);
  139.     }

  140.     /**
  141.      * @param mean Mean.
  142.      * @param rng Generator of uniformly distributed random numbers.
  143.      */
  144.     private LargeMeanPoissonSampler(double mean,
  145.                                     UniformRandomProvider rng) {
  146.         this.rng = rng;

  147.         gaussian = ZigguratSampler.NormalizedGaussian.of(rng);
  148.         exponential = ZigguratSampler.Exponential.of(rng);
  149.         // Plain constructor uses the uncached function.
  150.         factorialLog = NO_CACHE_FACTORIAL_LOG;

  151.         // Cache values used in the algorithm
  152.         lambda = Math.floor(mean);
  153.         logLambda = Math.log(lambda);
  154.         logLambdaFactorial = getFactorialLog((int) lambda);
  155.         delta = Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1));
  156.         halfDelta = delta / 2;
  157.         sqrtLambdaPlusHalfDelta = Math.sqrt(lambda + halfDelta);
  158.         twolpd = 2 * lambda + delta;
  159.         c1 = 1 / (8 * lambda);
  160.         final double a1 = Math.sqrt(Math.PI * twolpd) * Math.exp(c1);
  161.         final double a2 = (twolpd / delta) * Math.exp(-delta * (1 + delta) / twolpd);
  162.         final double aSum = a1 + a2 + 1;
  163.         p1 = a1 / aSum;
  164.         p2 = a2 / aSum;

  165.         // The algorithm requires a Poisson sample from the remaining lambda fraction.
  166.         final double lambdaFractional = mean - lambda;
  167.         smallMeanPoissonSampler = (lambdaFractional < Double.MIN_VALUE) ?
  168.             NO_SMALL_MEAN_POISSON_SAMPLER : // Not used.
  169.             KempSmallMeanPoissonSampler.of(rng, lambdaFractional);
  170.     }

  171.     /**
  172.      * Instantiates a sampler using a precomputed state.
  173.      *
  174.      * @param state            The state for {@code lambda = (int)Math.floor(mean)}.
  175.      * @param lambdaFractional The lambda fractional value
  176.      *                         ({@code mean - (int)Math.floor(mean))}.
  177.      * @param rng              Generator of uniformly distributed random numbers.
  178.      */
  179.     private LargeMeanPoissonSampler(LargeMeanPoissonSamplerState state,
  180.                                     double lambdaFractional,
  181.                                     UniformRandomProvider rng) {
  182.         this.rng = rng;

  183.         gaussian = ZigguratSampler.NormalizedGaussian.of(rng);
  184.         exponential = ZigguratSampler.Exponential.of(rng);
  185.         // Plain constructor uses the uncached function.
  186.         factorialLog = NO_CACHE_FACTORIAL_LOG;

  187.         // Use the state to initialize the algorithm
  188.         lambda = state.getLambdaRaw();
  189.         logLambda = state.getLogLambda();
  190.         logLambdaFactorial = state.getLogLambdaFactorial();
  191.         delta = state.getDelta();
  192.         halfDelta = state.getHalfDelta();
  193.         sqrtLambdaPlusHalfDelta = state.getSqrtLambdaPlusHalfDelta();
  194.         twolpd = state.getTwolpd();
  195.         p1 = state.getP1();
  196.         p2 = state.getP2();
  197.         c1 = state.getC1();

  198.         // The algorithm requires a Poisson sample from the remaining lambda fraction.
  199.         smallMeanPoissonSampler = (lambdaFractional < Double.MIN_VALUE) ?
  200.             NO_SMALL_MEAN_POISSON_SAMPLER : // Not used.
  201.             KempSmallMeanPoissonSampler.of(rng, lambdaFractional);
  202.     }

  203.     /**
  204.      * @param rng Generator of uniformly distributed random numbers.
  205.      * @param source Source to copy.
  206.      */
  207.     private LargeMeanPoissonSampler(UniformRandomProvider rng,
  208.                                     LargeMeanPoissonSampler source) {
  209.         this.rng = rng;

  210.         gaussian = source.gaussian.withUniformRandomProvider(rng);
  211.         exponential = source.exponential.withUniformRandomProvider(rng);
  212.         // Reuse the cache
  213.         factorialLog = source.factorialLog;

  214.         lambda = source.lambda;
  215.         logLambda = source.logLambda;
  216.         logLambdaFactorial = source.logLambdaFactorial;
  217.         delta = source.delta;
  218.         halfDelta = source.halfDelta;
  219.         sqrtLambdaPlusHalfDelta = source.sqrtLambdaPlusHalfDelta;
  220.         twolpd = source.twolpd;
  221.         p1 = source.p1;
  222.         p2 = source.p2;
  223.         c1 = source.c1;

  224.         // Share the state of the small sampler
  225.         smallMeanPoissonSampler = source.smallMeanPoissonSampler.withUniformRandomProvider(rng);
  226.     }

  227.     /** {@inheritDoc} */
  228.     @Override
  229.     public int sample() {
  230.         // This will never be null. It may be a no-op delegate that returns zero.
  231.         final int y2 = smallMeanPoissonSampler.sample();

  232.         double x;
  233.         double y;
  234.         double v;
  235.         int a;
  236.         double t;
  237.         double qr;
  238.         double qa;
  239.         while (true) {
  240.             // Step 1:
  241.             final double u = rng.nextDouble();
  242.             if (u <= p1) {
  243.                 // Step 2:
  244.                 final double n = gaussian.sample();
  245.                 x = n * sqrtLambdaPlusHalfDelta - 0.5d;
  246.                 if (x > delta || x < -lambda) {
  247.                     continue;
  248.                 }
  249.                 y = x < 0 ? Math.floor(x) : Math.ceil(x);
  250.                 final double e = exponential.sample();
  251.                 v = -e - 0.5 * n * n + c1;
  252.             } else {
  253.                 // Step 3:
  254.                 if (u > p1 + p2) {
  255.                     y = lambda;
  256.                     break;
  257.                 }
  258.                 x = delta + (twolpd / delta) * exponential.sample();
  259.                 y = Math.ceil(x);
  260.                 v = -exponential.sample() - delta * (x + 1) / twolpd;
  261.             }
  262.             // The Squeeze Principle
  263.             // Step 4.1:
  264.             a = x < 0 ? 1 : 0;
  265.             t = y * (y + 1) / (2 * lambda);
  266.             // Step 4.2
  267.             if (v < -t && a == 0) {
  268.                 y = lambda + y;
  269.                 break;
  270.             }
  271.             // Step 4.3:
  272.             qr = t * ((2 * y + 1) / (6 * lambda) - 1);
  273.             qa = qr - (t * t) / (3 * (lambda + a * (y + 1)));
  274.             // Step 4.4:
  275.             if (v < qa) {
  276.                 y = lambda + y;
  277.                 break;
  278.             }
  279.             // Step 4.5:
  280.             if (v > qr) {
  281.                 continue;
  282.             }
  283.             // Step 4.6:
  284.             if (v < y * logLambda - getFactorialLog((int) (y + lambda)) + logLambdaFactorial) {
  285.                 y = lambda + y;
  286.                 break;
  287.             }
  288.         }

  289.         return (int) Math.min(y2 + (long) y, Integer.MAX_VALUE);
  290.     }

  291.     /**
  292.      * Compute the natural logarithm of the factorial of {@code n}.
  293.      *
  294.      * @param n Argument.
  295.      * @return {@code log(n!)}
  296.      * @throws IllegalArgumentException if {@code n < 0}.
  297.      */
  298.     private double getFactorialLog(int n) {
  299.         return factorialLog.value(n);
  300.     }

  301.     /** {@inheritDoc} */
  302.     @Override
  303.     public String toString() {
  304.         return "Large Mean Poisson deviate [" + rng.toString() + "]";
  305.     }

  306.     /**
  307.      * {@inheritDoc}
  308.      *
  309.      * @since 1.3
  310.      */
  311.     @Override
  312.     public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) {
  313.         return new LargeMeanPoissonSampler(rng, this);
  314.     }

  315.     /**
  316.      * Creates a new Poisson distribution sampler.
  317.      *
  318.      * @param rng Generator of uniformly distributed random numbers.
  319.      * @param mean Mean.
  320.      * @return the sampler
  321.      * @throws IllegalArgumentException if {@code mean < 1} or {@code mean > 0.5 *}
  322.      * {@link Integer#MAX_VALUE}.
  323.      * @since 1.3
  324.      */
  325.     public static SharedStateDiscreteSampler of(UniformRandomProvider rng,
  326.                                                 double mean) {
  327.         return new LargeMeanPoissonSampler(rng, mean);
  328.     }

  329.     /**
  330.      * Gets the initialisation state of the sampler.
  331.      *
  332.      * <p>The state is computed using an integer {@code lambda} value of
  333.      * {@code lambda = (int)Math.floor(mean)}.
  334.      *
  335.      * <p>The state will be suitable for reconstructing a new sampler with a mean
  336.      * in the range {@code lambda <= mean < lambda+1} using
  337.      * {@link #LargeMeanPoissonSampler(UniformRandomProvider, LargeMeanPoissonSamplerState, double)}.
  338.      *
  339.      * @return the state
  340.      */
  341.     LargeMeanPoissonSamplerState getState() {
  342.         return new LargeMeanPoissonSamplerState(lambda, logLambda, logLambdaFactorial,
  343.                 delta, halfDelta, sqrtLambdaPlusHalfDelta, twolpd, p1, p2, c1);
  344.     }

  345.     /**
  346.      * Encapsulate the state of the sampler. The state is valid for construction of
  347.      * a sampler in the range {@code lambda <= mean < lambda+1}.
  348.      *
  349.      * <p>This class is immutable.
  350.      *
  351.      * @see #getLambda()
  352.      */
  353.     static final class LargeMeanPoissonSamplerState {
  354.         /** Algorithm constant {@code lambda}. */
  355.         private final double lambda;
  356.         /** Algorithm constant {@code logLambda}. */
  357.         private final double logLambda;
  358.         /** Algorithm constant {@code logLambdaFactorial}. */
  359.         private final double logLambdaFactorial;
  360.         /** Algorithm constant {@code delta}. */
  361.         private final double delta;
  362.         /** Algorithm constant {@code halfDelta}. */
  363.         private final double halfDelta;
  364.         /** Algorithm constant {@code sqrtLambdaPlusHalfDelta}. */
  365.         private final double sqrtLambdaPlusHalfDelta;
  366.         /** Algorithm constant {@code twolpd}. */
  367.         private final double twolpd;
  368.         /** Algorithm constant {@code p1}. */
  369.         private final double p1;
  370.         /** Algorithm constant {@code p2}. */
  371.         private final double p2;
  372.         /** Algorithm constant {@code c1}. */
  373.         private final double c1;

  374.         /**
  375.          * Creates the state.
  376.          *
  377.          * <p>The state is valid for construction of a sampler in the range
  378.          * {@code lambda <= mean < lambda+1} where {@code lambda} is an integer.
  379.          *
  380.          * @param lambda the lambda
  381.          * @param logLambda the log lambda
  382.          * @param logLambdaFactorial the log lambda factorial
  383.          * @param delta the delta
  384.          * @param halfDelta the half delta
  385.          * @param sqrtLambdaPlusHalfDelta the sqrt(lambda+half delta)
  386.          * @param twolpd the two lambda plus delta
  387.          * @param p1 the p1 constant
  388.          * @param p2 the p2 constant
  389.          * @param c1 the c1 constant
  390.          */
  391.         LargeMeanPoissonSamplerState(double lambda, double logLambda,
  392.                 double logLambdaFactorial, double delta, double halfDelta,
  393.                 double sqrtLambdaPlusHalfDelta, double twolpd,
  394.                 double p1, double p2, double c1) {
  395.             this.lambda = lambda;
  396.             this.logLambda = logLambda;
  397.             this.logLambdaFactorial = logLambdaFactorial;
  398.             this.delta = delta;
  399.             this.halfDelta = halfDelta;
  400.             this.sqrtLambdaPlusHalfDelta = sqrtLambdaPlusHalfDelta;
  401.             this.twolpd = twolpd;
  402.             this.p1 = p1;
  403.             this.p2 = p2;
  404.             this.c1 = c1;
  405.         }

  406.         /**
  407.          * Get the lambda value for the state.
  408.          *
  409.          * <p>Equal to {@code floor(mean)} for a Poisson sampler.
  410.          * @return the lambda value
  411.          */
  412.         int getLambda() {
  413.             return (int) getLambdaRaw();
  414.         }

  415.         /**
  416.          * @return algorithm constant {@code lambda}
  417.          */
  418.         double getLambdaRaw() {
  419.             return lambda;
  420.         }

  421.         /**
  422.          * @return algorithm constant {@code logLambda}
  423.          */
  424.         double getLogLambda() {
  425.             return logLambda;
  426.         }

  427.         /**
  428.          * @return algorithm constant {@code logLambdaFactorial}
  429.          */
  430.         double getLogLambdaFactorial() {
  431.             return logLambdaFactorial;
  432.         }

  433.         /**
  434.          * @return algorithm constant {@code delta}
  435.          */
  436.         double getDelta() {
  437.             return delta;
  438.         }

  439.         /**
  440.          * @return algorithm constant {@code halfDelta}
  441.          */
  442.         double getHalfDelta() {
  443.             return halfDelta;
  444.         }

  445.         /**
  446.          * @return algorithm constant {@code sqrtLambdaPlusHalfDelta}
  447.          */
  448.         double getSqrtLambdaPlusHalfDelta() {
  449.             return sqrtLambdaPlusHalfDelta;
  450.         }

  451.         /**
  452.          * @return algorithm constant {@code twolpd}
  453.          */
  454.         double getTwolpd() {
  455.             return twolpd;
  456.         }

  457.         /**
  458.          * @return algorithm constant {@code p1}
  459.          */
  460.         double getP1() {
  461.             return p1;
  462.         }

  463.         /**
  464.          * @return algorithm constant {@code p2}
  465.          */
  466.         double getP2() {
  467.             return p2;
  468.         }

  469.         /**
  470.          * @return algorithm constant {@code c1}
  471.          */
  472.         double getC1() {
  473.             return c1;
  474.         }
  475.     }
  476. }