Class NegativeBinomial

    • Constructor Summary

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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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 j)
      Computes the cumulative probability distribution function for given value of failures
      static NegativeBinomial createFromMoments​(double... moments)  
      double firstOrderLossFunction​(double x)
      Computes the first order loss function for the function for given value of x, G1(x) = E[max(X-x,0)]
      double getDesiredNumberOfSuccesses()
      Gets the desired number of successes
      double getMean()
      Returns the mean or expected value of a distribution
      int getMode()
      Gets the mode of the distribution
      double[] getParameters()
      Gets the parameters as an array parameters[0] is probability of success parameters[1] is number of desired successes
      static double[] getParametersFromMoments​(double... moments)  
      double getProbabilityOfSuccess()
      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.
      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.
      protected static int invCDFViaNormalApprox​(double x, double r, double p)  
      static double negBinomialCCDF​(int j, double r, double p)
      Allows static computation of complementary cdf function assumes that distribution's range is {0,1, ...} Uses the recursive logarithmic algorithm
      static double negBinomialCCDF​(int j, double r, double p, boolean recursive)
      Allows static computation of complementary cdf function assumes that distribution's range is {0,1, ...}
      static double negBinomialCDF​(int j, double r, double p)
      Allows static computation of the CDF assumes that distribution's range is {0,1, ...} Uses the recursive logarithmic algorithm
      static double negBinomialCDF​(int j, double r, double p, boolean recursive)
      Allows static computation of the CDF assumes that distribution's range is {0,1, ...}
      static int negBinomialInvCDF​(double x, double p, double r)
      Returns the quantile associated with the supplied probability, x assumes that distribution's range is {0,1, ...} Uses the recursive logarithmic algorithm
      static int negBinomialInvCDF​(double x, double p, double r, boolean recursive)
      Returns the quantile associated with the supplied probability, x assumes that distribution's range is {0,1, ...}
      static double negBinomialLF1​(int j, double r, double p)
      Allows static computation of 1st order loss function assumes that distribution's range is {0,1, ...} Uses the recursive logarithmic algorithm
      static double negBinomialLF1​(int j, double r, double p, boolean recursive)
      Allows static computation of 1st order loss function assumes that distribution's range is {0,1, ...}
      static double negBinomialLF2​(int j, double r, double p)
      Allows static computation of 2nd order loss function assumes that distribution's range is {0,1, ...} Uses the recursive logarithmic algorithm
      static double negBinomialLF2​(int j, double r, double p, boolean recursive)
      Allows static computation of 2nd order loss function assumes that distribution's range is {0,1, ...}
      static double negBinomialPMF​(int j, double r, double p)
      Allows static computation of prob mass function assumes that distribution's range is {0,1, ...} Uses the recursive logarithmic algorithm
      static double negBinomialPMF​(int j, double r, double p, boolean recursive)
      Allows static computation of prob mass function assumes that distribution's range is {0,1, ...}
      NegativeBinomial newInstance()
      Returns a new instance
      double pmf​(double x)
      Returns the f(x) where f represents the probability mass function for the distribution.
      double pmf​(int j)
      Returns the prob of getting x failures before the rth success where r is the desired number of successes parameter
      static double recursiveCDF​(int j, double r, double p)
      Computes the cdf at j using a recursive (iterative) algorithm using logarithms
      static double recursivePMF​(int j, double r, double p)
      Computes the probability mass function at j using a recursive (iterative) algorithm using logarithms
      protected static int searchDownCDF​(double x, double r, double p, int start, double cdfAtStart, boolean recursive)  
      protected static int searchUpCDF​(double x, double r, 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 as an array parameters[0] is probability of success parameters[1] is number of desired successes
      void setParameters​(double prob, double numSuccess)
      Sets the number of success 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.
      • Methods inherited from class java.lang.Object

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

      • NegativeBinomial

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

        public NegativeBinomial​(double[] parameter)
        Constructs a NegativeBinomial using the supplied parameters
        Parameters:
        parameter - parameter[0] should be the probability (p) and parameter[1] should be the desired number of successes
      • NegativeBinomial

        public NegativeBinomial​(double prob,
                                double numSuccess)
        Constructs a NegativeBinomial with p probability of success based on n success
        Parameters:
        prob - The success probability, must be in range (0,1)
        numSuccess - The desired number of successes
      • NegativeBinomial

        public NegativeBinomial​(double prob,
                                double numSuccess,
                                java.lang.String name)
        Constructs a NegativeBinomial with p probability of success based on n success
        Parameters:
        prob - The success probability, must be in range (0,1)
        numSuccess - The desired number of successes
        name - an optional name/label
    • 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 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
      • setParameters

        public final void setParameters​(double prob,
                                        double numSuccess)
        Sets the number of success and success probability
        Parameters:
        prob - The success probability
        numSuccess - The desired number of successes
      • getMode

        public final int getMode()
        Gets the mode of the distribution
        Returns:
        the mode of the distribution
      • setParameters

        public final void setParameters​(double[] parameters)
        Sets the parameters as an array parameters[0] is probability of success parameters[1] is number of desired successes
        Specified by:
        setParameters in interface ParametersIfc
        Parameters:
        parameters - an array of doubles representing the parameters
      • getParameters

        public final double[] getParameters()
        Gets the parameters as an array parameters[0] is probability of success parameters[1] is number of desired successes
        Specified by:
        getParameters in interface ParametersIfc
        Returns:
        Returns an array of the parameters
      • getProbabilityOfSuccess

        public final double getProbabilityOfSuccess()
        Gets the success probability
        Returns:
        The success probability
      • getDesiredNumberOfSuccesses

        public final double getDesiredNumberOfSuccesses()
        Gets the desired number of successes
        Returns:
        the number of success
      • 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)
        Description copied from interface: PMFIfc
        Returns the f(x) where f represents the probability mass function for the distribution.
        Specified by:
        pmf in interface PMFIfc
        Parameters:
        x - a double representing the value to be evaluated
        Returns:
        f(x) the P(X=x)
      • pmf

        public final double pmf​(int j)
        Returns the prob of getting x failures before the rth success where r is the desired number of successes parameter
        Parameters:
        j - the value to evaluate
        Returns:
        the probability
      • 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 j)
        Computes the cumulative probability distribution function for given value of failures
        Parameters:
        j - The value to be evaluated
        Returns:
        The probability, P{X <=j}
      • 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
      • recursivePMF

        public static double recursivePMF​(int j,
                                          double r,
                                          double p)
        Computes the probability mass function at j using a recursive (iterative) algorithm using logarithms
        Parameters:
        j - the value to be evaluated
        r - number of successes
        p - the probability, must be in range (0,1)
        Returns:
        the probability
      • recursiveCDF

        public static double recursiveCDF​(int j,
                                          double r,
                                          double p)
        Computes the cdf at j using a recursive (iterative) algorithm using logarithms
        Parameters:
        j - the value to be evaluated
        r - number of successes
        p - the probability, must be in range (0,1)
        Returns:
        the probability
      • negBinomialPMF

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

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

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

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

        public static double negBinomialCCDF​(int j,
                                             double r,
                                             double p)
        Allows static computation of complementary cdf function assumes that distribution's range is {0,1, ...} Uses the recursive logarithmic algorithm
        Parameters:
        j - value for which ccdf is needed
        r - num of successes
        p - prob of success, must be in range (0,1)
        Returns:
        the probability
      • negBinomialCCDF

        public static double negBinomialCCDF​(int j,
                                             double r,
                                             double p,
                                             boolean recursive)
        Allows static computation of complementary cdf function assumes that distribution's range is {0,1, ...}
        Parameters:
        j - value for which ccdf is needed
        r - num of successes
        p - prob of success, must be in range (0,1)
        recursive - true indicates that the recursive logarithmic algorithm should be used
        Returns:
        the probability
      • negBinomialLF1

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

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

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

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

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

        public static int negBinomialInvCDF​(double x,
                                            double p,
                                            double r,
                                            boolean recursive)
        Returns the quantile associated with the supplied probability, x assumes that distribution's range is {0,1, ...}
        Parameters:
        x - The probability that the quantile is needed for
        r - The number of successes parameter
        p - The probability of success, must be in range (0,1)
        recursive - true indicates that the recursive logarithmic algorithm should be used
        Returns:
        the inverse CDF value
      • searchUpCDF

        protected static int searchUpCDF​(double x,
                                         double r,
                                         double p,
                                         int start,
                                         double cdfAtStart,
                                         boolean recursive)
        Parameters:
        x - the value to evaluate
        r - the trial number
        p - the probability of success
        start - the starting place for search
        cdfAtStart - the CDF at the starting place
        recursive - true for using recursive algorithm
        Returns:
        the found value
      • searchDownCDF

        protected static int searchDownCDF​(double x,
                                           double r,
                                           double p,
                                           int start,
                                           double cdfAtStart,
                                           boolean recursive)
        Parameters:
        x - the value to evaluate
        r - the trial number
        p - the probability of success
        start - the starting place for search
        cdfAtStart - the CDF at the starting place
        recursive - true for using recursive algorithm
        Returns:
        the found value
      • invCDFViaNormalApprox

        protected static int invCDFViaNormalApprox​(double x,
                                                   double r,
                                                   double p)
        Parameters:
        x - the value to evaluate
        r - the trial number
        p - the probability of success
        Returns:
        the inverse value at the point x
      • canMatchMoments

        public static boolean canMatchMoments​(double... moments)
      • getParametersFromMoments

        public static double[] getParametersFromMoments​(double... moments)
      • createFromMoments

        public static NegativeBinomial createFromMoments​(double... moments)
      • firstOrderLossFunction

        public double firstOrderLossFunction​(double x)
        Description copied from interface: FirstOrderLossFunctionIfc
        Computes the first order loss function for the 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 double secondOrderLossFunction​(double x)
        Description copied from interface: SecondOrderLossFunctionIfc
        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)]