Class Binomial

  • All Implemented Interfaces:
    DoubleFunction, IntFunction, java.io.Serializable, java.lang.Cloneable

    public class Binomial
    extends AbstractDiscreteDistribution
    Binomial distribution; See the math definition and animated definition.

    p(x) = k * p^k * (1-p)^(n-k) with k = n! / (k! * (n-k)!).

    Instance methods operate on a user supplied uniform random number generator; they are unsynchronized.

    Static methods operate on a default uniform random number generator; they are synchronized.

    Implementation: High performance implementation. Acceptance Rejection/Inversion method. This is a port of RandBinomial used in CLHEP 1.4.0 (C++). CLHEP's implementation is, in turn, based on

    V. Kachitvichyanukul, B.W. Schmeiser (1988): Binomial random variate generation, Communications of the ACM 31, 216-222.

    Version:
    1.0, 09/24/99
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int b  
      private double c  
      private double ch  
      private double ll  
      private double log_n  
      private double log_p  
      private double log_q  
      private double lr  
      private int m  
      protected int n  
      private int n_last  
      private int n_prev  
      private int nm  
      private double np  
      protected double p  
      private double p_last  
      private double p_prev  
      private double p0  
      private double p1  
      private double p2  
      private double p3  
      private double p4  
      private double par  
      private double pq  
      private double q  
      private double rc  
      protected static Binomial shared  
      private double ss  
      private double xl  
      private double xm  
      private double xr  
    • Constructor Summary

      Constructors 
      Constructor Description
      Binomial​(int n, double p, RandomEngine randomGenerator)
      Constructs a binomial distribution.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double cdf​(int k)
      Returns the cumulative distribution function.
      private double cdfSlow​(int k)
      Returns the cumulative distribution function.
      protected int generateBinomial​(int n, double p)
      * Binomial-Distribution - Acceptance Rejection/Inversion * * * Acceptance Rejection method combined with Inversion for * generating Binomial random numbers with parameters * n (number of trials) and p (probability of success).
      int nextInt()
      Returns a random number from the distribution.
      int nextInt​(int n, double p)
      Returns a random number from the distribution with the given parameters n and p; bypasses the internal state.
      double pdf​(int k)
      Returns the probability distribution function.
      void setNandP​(int n, double p)
      Sets the parameters number of trials and the probability of success.
      static int staticNextInt​(int n, double p)
      Returns a random number from the distribution with the given parameters n and p.
      java.lang.String toString()
      Returns a String representation of the receiver.
      private static void xstaticSetRandomGenerator​(RandomEngine randomGenerator)
      Sets the uniform random number generated shared by all static methods.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • n

        protected int n
      • p

        protected double p
      • n_last

        private int n_last
      • n_prev

        private int n_prev
      • par

        private double par
      • np

        private double np
      • p0

        private double p0
      • q

        private double q
      • p_last

        private double p_last
      • p_prev

        private double p_prev
      • b

        private int b
      • m

        private int m
      • nm

        private int nm
      • pq

        private double pq
      • rc

        private double rc
      • ss

        private double ss
      • xm

        private double xm
      • xl

        private double xl
      • xr

        private double xr
      • ll

        private double ll
      • lr

        private double lr
      • c

        private double c
      • p1

        private double p1
      • p2

        private double p2
      • p3

        private double p3
      • p4

        private double p4
      • ch

        private double ch
      • log_p

        private double log_p
      • log_q

        private double log_q
      • log_n

        private double log_n
      • shared

        protected static Binomial shared
    • Constructor Detail

      • Binomial

        public Binomial​(int n,
                        double p,
                        RandomEngine randomGenerator)
        Constructs a binomial distribution. Example: n=1, p=0.5.
        Parameters:
        n - the number of trials (also known as sample size).
        p - the probability of success.
        randomGenerator - a uniform random number generator.
        Throws:
        java.lang.IllegalArgumentException - if n*Math.min(p,1-p) <= 0.0
    • Method Detail

      • cdf

        public double cdf​(int k)
        Returns the cumulative distribution function.
      • cdfSlow

        private double cdfSlow​(int k)
        Returns the cumulative distribution function.
      • generateBinomial

        protected int generateBinomial​(int n,
                                       double p)
        * Binomial-Distribution - Acceptance Rejection/Inversion * * * Acceptance Rejection method combined with Inversion for * generating Binomial random numbers with parameters * n (number of trials) and p (probability of success). * For min(n*p,n*(1-p)) < 10 the Inversion method is applied: * The random numbers are generated via sequential search, * starting at the lowest index k=0. The cumulative probabilities * are avoided by using the technique of chop-down. * For min(n*p,n*(1-p)) >= 10 Acceptance Rejection is used: * The algorithm is based on a hat-function which is uniform in * the centre region and exponential in the tails. * A triangular immediate acceptance region in the centre speeds * up the generation of binomial variates. * If candidate k is near the mode, f(k) is computed recursively * starting at the mode m. * The acceptance test by Stirling's formula is modified * according to W. Hoermann (1992): The generation of binomial * random variates, to appear in J. Statist. Comput. Simul. * If p < .5 the algorithm is applied to parameters n, p. * Otherwise p is replaced by 1-p, and k is replaced by n - k. * * * FUNCTION: - samples a random number from the binomial * distribution with parameters n and p and is * valid for n*min(p,1-p) > 0. * REFERENCE: - V. Kachitvichyanukul, B.W. Schmeiser (1988): * Binomial random variate generation, * Communications of the ACM 31, 216-222. * SUBPROGRAMS: - StirlingCorrection() * ... Correction term of the Stirling * approximation for log(k!) * (series in 1/k or table values * for small k) with long int k * - randomGenerator ... (0,1)-Uniform engine * *
      • nextInt

        public int nextInt​(int n,
                           double p)
        Returns a random number from the distribution with the given parameters n and p; bypasses the internal state.
        Parameters:
        n - the number of trials
        p - the probability of success.
        Throws:
        java.lang.IllegalArgumentException - if n*Math.min(p,1-p) <= 0.0
      • pdf

        public double pdf​(int k)
        Returns the probability distribution function.
      • setNandP

        public void setNandP​(int n,
                             double p)
        Sets the parameters number of trials and the probability of success.
        Parameters:
        n - the number of trials
        p - the probability of success.
        Throws:
        java.lang.IllegalArgumentException - if n*Math.min(p,1-p) <= 0.0
      • staticNextInt

        public static int staticNextInt​(int n,
                                        double p)
        Returns a random number from the distribution with the given parameters n and p.
        Parameters:
        n - the number of trials
        p - the probability of success.
        Throws:
        java.lang.IllegalArgumentException - if n*Math.min(p,1-p) <= 0.0
      • toString

        public java.lang.String toString()
        Returns a String representation of the receiver.
        Overrides:
        toString in class java.lang.Object
      • xstaticSetRandomGenerator

        private static void xstaticSetRandomGenerator​(RandomEngine randomGenerator)
        Sets the uniform random number generated shared by all static methods.
        Parameters:
        randomGenerator - the new uniform random number generator to be shared.