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 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 algorithmstatic 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 algorithmstatic 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 algorithmstatic 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 algorithmstatic 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 algorithmstatic 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 algorithmstatic 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 functiondouble
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 distributiondouble[]
getParameters()
Gets the parametersstatic double[]
getParametersFromMoments(double... moments)
double
getProb()
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.int
getTrials()
Gets the number of trialsdouble
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.static int
invCDFViaNormalApprox(double x, int n, double p)
Approximates the quantile of x using a normal distributionBinomial
newInstance()
Returns a new instancedouble
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 probabilitydouble
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 logarithmsstatic double
recursivePMF(int j, int n, double p)
Computes the probability mass function at j using a recursive (iterative) algorithm using logarithmsprotected 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 parametersvoid
setParameters(double prob, int numTrials)
Sets the number of trials 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.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 xprotected 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 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:NewInstanceIfc
Returns a new instance- Specified by:
newInstance
in interfaceNewInstanceIfc
- Specified by:
newInstance
in 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:CDFIfc
Returns 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: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
-
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)
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:ParametersIfc
Sets the parameters- Specified by:
setParameters
in 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:ParametersIfc
Gets the parameters- Specified by:
getParameters
in 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:
firstOrderLossFunction
in 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:
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)]
-
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:
getRandomVariable
in interfaceGetRVariableIfc
- Overrides:
getRandomVariable
in classDistribution
- Parameters:
rng
- the stream to use- Returns:
- a random variable
-
-