Package jsl.utilities.distributions
Class Binomial
- java.lang.Object
-
- jsl.utilities.distributions.Distribution
-
- jsl.utilities.distributions.Binomial
-
- All Implemented Interfaces:
ControllableIfc,CDFIfc,DiscreteDistributionIfc,DistributionFunctionIfc,DistributionIfc,FirstOrderLossFunctionIfc,InverseCDFIfc,LossFunctionDistributionIfc,MeanIfc,PMFIfc,SecondOrderLossFunctionIfc,VarianceIfc,GetNameIfc,IdentityIfc,NewInstanceIfc,ParametersIfc,GetRVariableIfc
public class Binomial extends Distribution implements DiscreteDistributionIfc, LossFunctionDistributionIfc, GetRVariableIfc
Represents a Binomial distribution. A binomial random variable represents the number of successes out of n trials with each trial having a probability (p) of success. The pmf and cdf are computed using an iterative algorithm that relies on logarithms to avoid the large factorial associated with binomial coefficients. Thus it is suitable for reasonably large values for the number of trials. This appears to give about 14 decimal places of accuracy when compared to the statistical package R. The inverse CDF uses a normal approximation with a Cornish-Fisher expansion to approximate the quantile and then a search via the inverse transform method.
-
-
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 Binomial()Constructs a Binomial with n=1, p=0.5Binomial(double[] parameters)Constructs a Binomial using the supplied parametersBinomial(double prob, int numTrials)Constructs a binomial with p probability of success based on n trialsBinomial(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 doublebinomialCCDF(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 algorithmstatic doublebinomialCCDF(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 doublebinomialCDF(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 algorithmstatic doublebinomialCDF(int j, int n, double p, boolean recursive)Allows static computation of the CDF assumes that distribution's range is {0,1, ..., n}static intbinomialInvCDF(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 algorithmstatic intbinomialInvCDF(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 doublebinomialLF1(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 algorithmstatic doublebinomialLF1(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 doublebinomialLF2(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 algorithmstatic doublebinomialLF2(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 doublebinomialPMF(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 algorithmstatic doublebinomialPMF(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 booleancanMatchMoments(double... moments)doublecdf(double x)Returns the F(x) = Pr{X <= x} where F represents the cumulative distribution functiondoublecdf(int x)static BinomialcreateFromMoments(double... moments)doublefirstOrderLossFunction(double x)Computes the first order loss function for the distribution function for given value of x, G1(x) = E[max(X-x,0)]doublegetMean()Returns the mean or expected value of a distributiondouble[]getParameters()Gets the parametersstatic double[]getParametersFromMoments(double... moments)doublegetProb()Gets the success probabilityRVariableIfcgetRandomVariable(RNStreamIfc rng)booleangetRecursiveAlgorithmFlag()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.intgetTrials()Gets the number of trialsdoublegetVariance()Returns the variance of the distribution if defineddoubleinvCDF(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 intinvCDFViaNormalApprox(double x, int n, double p)Approximates the quantile of x using a normal distributionBinomialnewInstance()Returns a new instancedoublepmf(double x)If x is not and integer value, then the probability must be zero otherwise pmf(int x) is used to determine the probabilitydoublepmf(int x)static doublerecursiveCDF(int j, int n, double p)Computes the probability mass function at j using a recursive (iterative) algorithm using logarithmsstatic doublerecursivePMF(int j, int n, double p)Computes the probability mass function at j using a recursive (iterative) algorithm using logarithmsprotected static intsearchDownCDF(double x, int n, double p, int start, double cdfAtStart, boolean recursive)protected static intsearchUpCDF(double x, int n, double p, int start, double cdfAtStart, boolean recursive)doublesecondOrderLossFunction(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)]voidsetParameters(double[] parameters)Sets the parametersvoidsetParameters(double prob, int numTrials)Sets the number of trials and success probabilityvoidsetRecursiveAlgorithmFlag(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 doublesumCCDF_(double x, int n, double p, boolean recursive)Returns the sum of the complementary CDF from 0 up to but not including xprotected static doublesumFirstLoss_(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 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
-
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 probabilitynumTrials- 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 probabilitynumTrials- The number of trialsname- 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
-
newInstance
public final Binomial newInstance()
Description copied from interface:NewInstanceIfcReturns a new instance- Specified by:
newInstancein interfaceNewInstanceIfc- Specified by:
newInstancein classDistribution- Returns:
- the new instance
-
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 probabilitynumTrials- the number of trials
-
cdf
public final double cdf(double x)
Description copied from interface:CDFIfcReturns the F(x) = Pr{X <= x} where F represents the cumulative distribution function
-
cdf
public final double cdf(int x)
-
invCDF
public final double invCDF(double prob)
Description copied from interface:InverseCDFIfcProvides 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:
invCDFin 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
-
getMean
public final double getMean()
Description copied from interface:MeanIfcReturns the mean or expected value of a distribution
-
getVariance
public final double getVariance()
Description copied from interface:VarianceIfcReturns the variance of the distribution if defined- Specified by:
getVariancein interfaceVarianceIfc- 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
-
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:ParametersIfcSets the parameters- Specified by:
setParametersin interfaceParametersIfc- 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:ParametersIfcGets the parameters- Specified by:
getParametersin interfaceParametersIfc- 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:
firstOrderLossFunctionin interfaceFirstOrderLossFunctionIfc- 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:
secondOrderLossFunctionin 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)]
-
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 evaluaten- number of trialsp- 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 evaluaten- number of trialsp- 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 neededn- num of trialsp- 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 neededn- num of successesp- prob of successrecursive- 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 neededn- num of trialsp- 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 neededn- num of trialsp- prob of successrecursive- 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 neededn- num of trialsp- 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 neededn- num of trialsp- prob of successrecursive- 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 neededn- num of trialp- 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 neededn- num of trialsp- prob of successrecursive- 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 evaluaten- the number of trialsrecursive- the flag to use the recursive algorithmp- 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 neededn- num of trialsp- 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 neededn- num of trialsp- prob of successrecursive- 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 evaluaten- the number of trialsp- the probability of successrecursive- 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 forn- The number of trialsp- 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 forn- The number of trialsp- 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 evaluaten- the number of trialsp- 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)
-
getRandomVariable
public final RVariableIfc getRandomVariable(RNStreamIfc rng)
- Specified by:
getRandomVariablein interfaceGetRVariableIfc- Overrides:
getRandomVariablein classDistribution- Parameters:
rng- the stream to use- Returns:
- a random variable
-
-