Package jsl.utilities.distributions
Class NegativeBinomial
- java.lang.Object
-
- jsl.utilities.distributions.Distribution
-
- jsl.utilities.distributions.NegativeBinomial
-
- All Implemented Interfaces:
ControllableIfc
,CDFIfc
,DiscreteDistributionIfc
,DistributionFunctionIfc
,DistributionIfc
,FirstOrderLossFunctionIfc
,InverseCDFIfc
,LossFunctionDistributionIfc
,MeanIfc
,PMFIfc
,SecondOrderLossFunctionIfc
,VarianceIfc
,GetNameIfc
,IdentityIfc
,NewInstanceIfc
,ParametersIfc
,GetRVariableIfc
public class NegativeBinomial extends Distribution implements DiscreteDistributionIfc, LossFunctionDistributionIfc, GetRVariableIfc
The number of failures (=0) before the rth success (=1) in a sequence of independent Bernoulli trials with probability p of success on each trial. The range of this random variable is {0, 1, 2, ....}
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jsl.utilities.distributions.Distribution
Distribution.RandomControls
-
-
Field Summary
-
Fields inherited from class jsl.utilities.distributions.Distribution
myId, myName
-
-
Constructor Summary
Constructors Constructor Description NegativeBinomial()
Constructs a NegativeBinomial with n=1, p=0.5NegativeBinomial(double[] parameter)
Constructs a NegativeBinomial using the supplied parametersNegativeBinomial(double prob, double numSuccess)
Constructs a NegativeBinomial with p probability of success based on n successNegativeBinomial(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 functiondouble
cdf(int j)
Computes the cumulative probability distribution function for given value of failuresstatic 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 successesdouble
getMean()
Returns the mean or expected value of a distributionint
getMode()
Gets the mode of the distributiondouble[]
getParameters()
Gets the parameters as an array parameters[0] is probability of success parameters[1] is number of desired successesstatic double[]
getParametersFromMoments(double... moments)
double
getProbabilityOfSuccess()
Gets the success probabilityRVariableIfc
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 defineddouble
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 algorithmstatic 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 algorithmstatic 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 algorithmstatic 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 algorithmstatic 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 algorithmstatic 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 algorithmstatic 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 instancedouble
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 parameterstatic double
recursiveCDF(int j, double r, double p)
Computes the cdf at j using a recursive (iterative) algorithm using logarithmsstatic double
recursivePMF(int j, double r, double p)
Computes the probability mass function at j using a recursive (iterative) algorithm using logarithmsprotected 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 successesvoid
setParameters(double prob, double numSuccess)
Sets the number of success and success probabilityvoid
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 jsl.utilities.distributions.Distribution
getControls, getId, getName, getStandardDeviation, inverseContinuousCDFViaBisection, inverseContinuousCDFViaBisection, inverseDiscreteCDFViaSearchUp, setControls, setId, setName, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface jsl.utilities.distributions.CDFIfc
cdf, complementaryCDF
-
Methods inherited from interface jsl.utilities.random.rvariable.GetRVariableIfc
getRandomVariable, getRandomVariable
-
Methods inherited from interface jsl.utilities.distributions.VarianceIfc
getStandardDeviation
-
-
-
-
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 successesname
- 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
-
newInstance
public final NegativeBinomial newInstance()
Description copied from interface:NewInstanceIfc
Returns a new instance- Specified by:
newInstance
in interfaceNewInstanceIfc
- Specified by:
newInstance
in classDistribution
- Returns:
- the new instance
-
setParameters
public final void setParameters(double prob, double numSuccess)
Sets the number of success and success probability- Parameters:
prob
- The success probabilitynumSuccess
- 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 interfaceParametersIfc
- 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 interfaceParametersIfc
- 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
-
getVariance
public final double getVariance()
Description copied from interface:VarianceIfc
Returns the variance of the distribution if defined- Specified by:
getVariance
in interfaceVarianceIfc
- 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.
-
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
-
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 interfaceInverseCDFIfc
- 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 evaluatedr
- number of successesp
- 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 evaluatedr
- number of successesp
- 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 neededr
- num of successesp
- 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 neededr
- num of successesp
- 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 neededr
- num of successesp
- 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 neededr
- num of successesp
- 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 neededr
- num of successesp
- 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 neededr
- num of successesp
- 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 neededr
- num of successesp
- 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 neededr
- num of successesp
- 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 neededr
- num of successesp
- 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 neededr
- num of successesp
- prob of successrecursive
- 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 forr
- The number of successes parameterp
- 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 forr
- The number of successes parameterp
- 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 evaluater
- the trial numberp
- the probability of successstart
- the starting place for searchcdfAtStart
- the CDF at the starting placerecursive
- 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 evaluater
- the trial numberp
- the probability of successstart
- the starting place for searchcdfAtStart
- the CDF at the starting placerecursive
- 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 evaluater
- the trial numberp
- 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 interfaceFirstOrderLossFunctionIfc
- 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 interfaceSecondOrderLossFunctionIfc
- Parameters:
x
- The value to be evaluated- Returns:
- The loss function value, (1/2)E[max(X-x,0)*max(X-x-1,0)]
-
getRandomVariable
public final RVariableIfc getRandomVariable(RNStreamIfc rng)
- Specified by:
getRandomVariable
in interfaceGetRVariableIfc
- Overrides:
getRandomVariable
in classDistribution
- Parameters:
rng
- the stream to use- Returns:
- a random variable
-
-