Class Binomial

    • Constructor Summary

      Constructors 
      Constructor Description
      Binomial()
      Constructs a Binomial with n=1, p=0.5
      Binomial​(double[] parameters)
      Constructs a Binomial using the supplied parameters
      Binomial​(double prob, int numTrials)
      Constructs a binomial with p probability of success based on n trials
      Binomial​(double prob, int numTrials, java.lang.String name)
      Constructs a binomial with p probability of success based on n trials
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static double binomialCCDF​(int j, int n, double p)
      Allows static computation of complementary cdf function assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
      static double binomialCCDF​(int j, int n, double p, boolean recursive)
      Allows static computation of complementary cdf function assumes that distribution's range is {0,1, ...,n}
      static double binomialCDF​(int j, int n, double p)
      Allows static computation of the CDF assumes that distribution's range is {0,1, ...,n} Uses the recursive logarithmic algorithm
      static double binomialCDF​(int j, int n, double p, boolean recursive)
      Allows static computation of the CDF assumes that distribution's range is {0,1, ..., n}
      static int binomialInvCDF​(double x, int n, double p)
      Returns the quantile associated with the supplied probability, x assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
      static int binomialInvCDF​(double x, int n, double p, boolean recursive)
      Returns the quantile associated with the supplied probability, x assumes that distribution's range is {0,1, ...,n}
      static double binomialLF1​(double j, int n, double p)
      Allows static computation of 1st order loss function assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
      static double binomialLF1​(double j, int n, double p, boolean recursive)
      Allows static computation of 1st order loss function assumes that distribution's range is {0,1, ...,n}
      static double binomialLF2​(double j, int n, double p)
      Allows static computation of 2nd order loss function assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
      static double binomialLF2​(double j, int n, double p, boolean recursive)
      Allows static computation of 2nd order loss function assumes that distribution's range is {0,1, ...,n}
      static double binomialPMF​(int j, int n, double p)
      Allows static computation of prob mass function assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
      static double binomialPMF​(int j, int n, double p, boolean recursive)
      Allows static computation of prob mass function assumes that distribution's range is {0,1, ..., n}
      static boolean canMatchMoments​(double... moments)  
      double cdf​(double x)
      Returns the F(x) = Pr{X <= x} where F represents the cumulative distribution function
      double cdf​(int x)  
      static Binomial createFromMoments​(double... moments)  
      double firstOrderLossFunction​(double x)
      Computes the first order loss function for the distribution function for given value of x, G1(x) = E[max(X-x,0)]
      double getMean()
      Returns the mean or expected value of a distribution
      double[] getParameters()
      Gets the parameters
      static double[] getParametersFromMoments​(double... moments)  
      double getProb()
      Gets the success probability
      RVariableIfc getRandomVariable​(RNStreamIfc rng)  
      boolean getRecursiveAlgorithmFlag()
      indicates whether or not pmf and cdf calculations are done by recursive (iterative) algorithm based on logarithms or via beta incomplete function and binomial coefficients.
      int getTrials()
      Gets the number of trials
      double getVariance()
      Returns the variance of the distribution if defined
      double invCDF​(double prob)
      Provides the inverse cumulative distribution function for the distribution While closed form solutions for the inverse cdf may not exist, numerical search methods can be used to solve F(X) = U.
      static int invCDFViaNormalApprox​(double x, int n, double p)
      Approximates the quantile of x using a normal distribution
      Binomial newInstance()
      Returns a new instance
      double pmf​(double x)
      If x is not and integer value, then the probability must be zero otherwise pmf(int x) is used to determine the probability
      double pmf​(int x)  
      static double recursiveCDF​(int j, int n, double p)
      Computes the probability mass function at j using a recursive (iterative) algorithm using logarithms
      static double recursivePMF​(int j, int n, double p)
      Computes the probability mass function at j using a recursive (iterative) algorithm using logarithms
      protected static int searchDownCDF​(double x, int n, double p, int start, double cdfAtStart, boolean recursive)  
      protected static int searchUpCDF​(double x, int n, double p, int start, double cdfAtStart, boolean recursive)  
      double secondOrderLossFunction​(double x)
      Computes the 2nd order loss function for the distribution function for given value of x, G2(x) = (1/2)E[max(X-x,0)*max(X-x-1,0)]
      void setParameters​(double[] parameters)
      Sets the parameters
      void setParameters​(double prob, int numTrials)
      Sets the number of trials and success probability
      void setRecursiveAlgorithmFlag​(boolean flag)
      indicates whether or not pmf and cdf calculations are done by recursive (iterative) algorithm based on logarithms or via beta incomplete function and binomial coefficients.
      protected static double sumCCDF_​(double x, int n, double p, boolean recursive)
      Returns the sum of the complementary CDF from 0 up to but not including x
      protected static double sumFirstLoss_​(double x, int n, double p, boolean recursive)
      Sums the first order loss function from 1 up to and including x.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Binomial

        public Binomial()
        Constructs a Binomial with n=1, p=0.5
      • Binomial

        public Binomial​(double[] parameters)
        Constructs a Binomial using the supplied parameters
        Parameters:
        parameters - A array that holds the parameters, parameter[0] should be the probability (p) and parameter[1] should be the number of trials
      • Binomial

        public Binomial​(double prob,
                        int numTrials)
        Constructs a binomial with p probability of success based on n trials
        Parameters:
        prob - The success probability
        numTrials - The number of trials
      • Binomial

        public Binomial​(double prob,
                        int numTrials,
                        java.lang.String name)
        Constructs a binomial with p probability of success based on n trials
        Parameters:
        prob - The success probability
        numTrials - The number of trials
        name - an optional label/name
    • Method Detail

      • getRecursiveAlgorithmFlag

        public final boolean getRecursiveAlgorithmFlag()
        indicates whether or not pmf and cdf calculations are done by recursive (iterative) algorithm based on logarithms or via beta incomplete function and binomial coefficients.
        Returns:
        true if the recursive algo is being used
      • setRecursiveAlgorithmFlag

        public final void setRecursiveAlgorithmFlag​(boolean flag)
        indicates whether or not pmf and cdf calculations are done by recursive (iterative) algorithm based on logarithms or via beta incomplete function and binomial coefficients.
        Parameters:
        flag - true means recursive algorithm is used
      • getProb

        public final double getProb()
        Gets the success probability
        Returns:
        The success probability
      • getTrials

        public final int getTrials()
        Gets the number of trials
        Returns:
        the number of trials
      • setParameters

        public final void setParameters​(double prob,
                                        int numTrials)
        Sets the number of trials and success probability
        Parameters:
        prob - the success probability
        numTrials - the number of trials
      • cdf

        public final double cdf​(double x)
        Description copied from interface: CDFIfc
        Returns the F(x) = Pr{X <= x} where F represents the cumulative distribution function
        Specified by:
        cdf in interface CDFIfc
        Parameters:
        x - a double representing the upper limit
        Returns:
        a double representing the probability
      • cdf

        public final double cdf​(int x)
      • invCDF

        public final double invCDF​(double prob)
        Description copied from interface: InverseCDFIfc
        Provides the inverse cumulative distribution function for the distribution While closed form solutions for the inverse cdf may not exist, numerical search methods can be used to solve F(X) = U.
        Specified by:
        invCDF in interface InverseCDFIfc
        Parameters:
        prob - The probability to be evaluated for the inverse, p must be [0,1] or an IllegalArgumentException is thrown
        Returns:
        The inverse cdf evaluated at the supplied probability
      • getMean

        public final double getMean()
        Description copied from interface: MeanIfc
        Returns the mean or expected value of a distribution
        Specified by:
        getMean in interface MeanIfc
        Returns:
        double the mean or expected value for the distribution
      • getVariance

        public final double getVariance()
        Description copied from interface: VarianceIfc
        Returns the variance of the distribution if defined
        Specified by:
        getVariance in interface VarianceIfc
        Returns:
        double the variance of the random variable
      • pmf

        public final double pmf​(double x)
        If x is not and integer value, then the probability must be zero otherwise pmf(int x) is used to determine the probability
        Specified by:
        pmf in interface PMFIfc
        Parameters:
        x - value to evaluate
        Returns:
        the associated probability
      • pmf

        public final double pmf​(int x)
        Parameters:
        x - value to evaluate
        Returns:
        the associated probability
      • setParameters

        public void setParameters​(double[] parameters)
        Description copied from interface: ParametersIfc
        Sets the parameters
        Specified by:
        setParameters in interface ParametersIfc
        Parameters:
        parameters - A array that holds the parameters, parameter[0] should be the probability (p) and parameter[1] should be the number of trials
      • getParameters

        public double[] getParameters()
        Description copied from interface: ParametersIfc
        Gets the parameters
        Specified by:
        getParameters in interface ParametersIfc
        Returns:
        A array that holds the parameters, parameter[0] should be the probability (p) and parameter[1] should be the number of trials
      • canMatchMoments

        public static boolean canMatchMoments​(double... moments)
        Parameters:
        moments - the mean is in element 0 and the variance is in element 1, must not be null
        Returns:
        true if n and p can be set to match te moments
      • getParametersFromMoments

        public static double[] getParametersFromMoments​(double... moments)
        Parameters:
        moments - the mean is in element 0 and the variance is in element 1, must not be null
        Returns:
        the values of n and p that match the moments with p as element 0 and n as element 1
      • createFromMoments

        public static Binomial createFromMoments​(double... moments)
        Parameters:
        moments - the mean is in element 0 and the variance is in element 1, must not be null
        Returns:
        if the moments can be matched a properly configured Binomial is returned
      • firstOrderLossFunction

        public final double firstOrderLossFunction​(double x)
        Computes the first order loss function for the distribution function for given value of x, G1(x) = E[max(X-x,0)]
        Specified by:
        firstOrderLossFunction in interface FirstOrderLossFunctionIfc
        Parameters:
        x - The value to be evaluated
        Returns:
        The loss function value, E[max(X-x,0)]
      • secondOrderLossFunction

        public final double secondOrderLossFunction​(double x)
        Computes the 2nd order loss function for the distribution function for given value of x, G2(x) = (1/2)E[max(X-x,0)*max(X-x-1,0)]
        Specified by:
        secondOrderLossFunction in interface SecondOrderLossFunctionIfc
        Parameters:
        x - The value to be evaluated
        Returns:
        The loss function value, (1/2)E[max(X-x,0)*max(X-x-1,0)]
      • recursivePMF

        public static double recursivePMF​(int j,
                                          int n,
                                          double p)
        Computes the probability mass function at j using a recursive (iterative) algorithm using logarithms
        Parameters:
        j - the value to evaluate
        n - number of trials
        p - success probability
        Returns:
        probability of j
      • recursiveCDF

        public static double recursiveCDF​(int j,
                                          int n,
                                          double p)
        Computes the probability mass function at j using a recursive (iterative) algorithm using logarithms
        Parameters:
        j - the value to evaluate
        n - number of trials
        p - success probability
        Returns:
        cumulative probability of j
      • binomialPMF

        public static double binomialPMF​(int j,
                                         int n,
                                         double p)
        Allows static computation of prob mass function assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
        Parameters:
        j - value for which prob is needed
        n - num of trials
        p - prob of success
        Returns:
        the probability at j
      • binomialPMF

        public static double binomialPMF​(int j,
                                         int n,
                                         double p,
                                         boolean recursive)
        Allows static computation of prob mass function assumes that distribution's range is {0,1, ..., n}
        Parameters:
        j - value for which prob is needed
        n - num of successes
        p - prob of success
        recursive - true indicates that the recursive logarithmic algorithm should be used
        Returns:
        the probability at j
      • binomialCDF

        public static double binomialCDF​(int j,
                                         int n,
                                         double p)
        Allows static computation of the CDF assumes that distribution's range is {0,1, ...,n} Uses the recursive logarithmic algorithm
        Parameters:
        j - value for which cdf is needed
        n - num of trials
        p - prob of success
        Returns:
        the cumulative probability at j
      • binomialCDF

        public static double binomialCDF​(int j,
                                         int n,
                                         double p,
                                         boolean recursive)
        Allows static computation of the CDF assumes that distribution's range is {0,1, ..., n}
        Parameters:
        j - value for which cdf is needed
        n - num of trials
        p - prob of success
        recursive - true indicates that the recursive logarithmic algorithm should be used
        Returns:
        the cumulative probability at j
      • binomialCCDF

        public static double binomialCCDF​(int j,
                                          int n,
                                          double p)
        Allows static computation of complementary cdf function assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
        Parameters:
        j - value for which ccdf is needed
        n - num of trials
        p - prob of success
        Returns:
        the complementary CDF at j
      • binomialCCDF

        public static double binomialCCDF​(int j,
                                          int n,
                                          double p,
                                          boolean recursive)
        Allows static computation of complementary cdf function assumes that distribution's range is {0,1, ...,n}
        Parameters:
        j - value for which ccdf is needed
        n - num of trials
        p - prob of success
        recursive - true indicates that the recursive logarithmic algorithm should be used
        Returns:
        the complementary CDF at j
      • binomialLF1

        public static double binomialLF1​(double j,
                                         int n,
                                         double p)
        Allows static computation of 1st order loss function assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
        Parameters:
        j - value for which 1st order loss function is needed
        n - num of trial
        p - prob of success
        Returns:
        the first order loss function at j
      • binomialLF1

        public static double binomialLF1​(double j,
                                         int n,
                                         double p,
                                         boolean recursive)
        Allows static computation of 1st order loss function assumes that distribution's range is {0,1, ...,n}
        Parameters:
        j - value for which 1st order loss function is needed
        n - num of trials
        p - prob of success
        recursive - true indicates that the recursive logarithmic algorithm should be used
        Returns:
        the first order loss function at j
      • sumCCDF_

        protected static double sumCCDF_​(double x,
                                         int n,
                                         double p,
                                         boolean recursive)
        Returns the sum of the complementary CDF from 0 up to but not including x
        Parameters:
        x - the value to evaluate
        n - the number of trials
        recursive - the flag to use the recursive algorithm
        p - the probability of success
        Returns:
        the sum of the complementary CDF
      • binomialLF2

        public static double binomialLF2​(double j,
                                         int n,
                                         double p)
        Allows static computation of 2nd order loss function assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
        Parameters:
        j - value for which 2nd order loss function is needed
        n - num of trials
        p - prob of success
        Returns:
        the 2nd order loss function at j
      • binomialLF2

        public static double binomialLF2​(double j,
                                         int n,
                                         double p,
                                         boolean recursive)
        Allows static computation of 2nd order loss function assumes that distribution's range is {0,1, ...,n}
        Parameters:
        j - value for which 2nd order loss function is needed
        n - num of trials
        p - prob of success
        recursive - true indicates that the recursive logarithmic algorithm should be used
        Returns:
        the 2nd order loss function at j
      • sumFirstLoss_

        protected static double sumFirstLoss_​(double x,
                                              int n,
                                              double p,
                                              boolean recursive)
        Sums the first order loss function from 1 up to and including x. x is interpreted as an integer
        Parameters:
        x - the value to evaluate
        n - the number of trials
        p - the probability of success
        recursive - true if recursive algorithm is to be used
        Returns:
        the sum
      • binomialInvCDF

        public static int binomialInvCDF​(double x,
                                         int n,
                                         double p)
        Returns the quantile associated with the supplied probability, x assumes that distribution's range is {0,1, ..., n} Uses the recursive logarithmic algorithm
        Parameters:
        x - The probability that the quantile is needed for
        n - The number of trials
        p - The probability of success, must be in range [0,1)
        Returns:
        the quantile associated with the supplied probability
      • binomialInvCDF

        public static int binomialInvCDF​(double x,
                                         int n,
                                         double p,
                                         boolean recursive)
        Returns the quantile associated with the supplied probability, x assumes that distribution's range is {0,1, ...,n}
        Parameters:
        x - The probability that the quantile is needed for
        n - The number of trials
        p - The probability of success, must be in range [0,1)
        recursive - true indicates that the recursive logarithmic algorithm should be used
        Returns:
        the quantile associated with the supplied probability
      • invCDFViaNormalApprox

        public static int invCDFViaNormalApprox​(double x,
                                                int n,
                                                double p)
        Approximates the quantile of x using a normal distribution
        Parameters:
        x - the value to evaluate
        n - the number of trials
        p - the probability of success
        Returns:
        the approximate inverse CDF value
      • searchUpCDF

        protected static int searchUpCDF​(double x,
                                         int n,
                                         double p,
                                         int start,
                                         double cdfAtStart,
                                         boolean recursive)
      • searchDownCDF

        protected static int searchDownCDF​(double x,
                                           int n,
                                           double p,
                                           int start,
                                           double cdfAtStart,
                                           boolean recursive)