JSci.maths.statistics.BinomialDistribution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsci Show documentation
Show all versions of jsci Show documentation
JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software.
It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ...
Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).
The newest version!
package JSci.maths.statistics;
import JSci.maths.ExtraMath;
/**
* The BinomialDistribution class provides an object for encapsulating binomial distributions.
* @version 0.1
* @author Mark Hale
*/
public final class BinomialDistribution extends ProbabilityDistribution {
private int n;
private double p;
/**
* Constructs a binomial distribution.
* @param trials the number of trials.
* @param prob the probability.
*/
public BinomialDistribution(int trials,double prob) {
if(trials<=0)
throw new OutOfRangeException("The number of trials should be (strictly) positive.");
n=trials;
if(prob<0.0 || prob>1.0)
throw new OutOfRangeException("The probability should be between 0 and 1.");
p=prob;
}
/**
* Returns the number of trials.
*/
public int getTrialsParameter() {
return n;
}
/**
* Returns the probability.
*/
public double getProbabilityParameter() {
return p;
}
/**
* Returns the mean.
*/
public double getMean() {
return n*p;
}
/**
* Returns the variance.
*/
public double getVariance() {
return n*p*(1.0-p);
}
/**
* Probability density function of a binomial distribution.
* @param X should be integer-valued.
* @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
*/
public double probability(double X) {
checkRange(X,0.0,n);
return ExtraMath.binomial(n,X)*Math.pow(p,X)*Math.pow(1.0-p,n-X);
}
/**
* Cumulative binomial distribution function.
* @param X should be integer-valued.
* @return the probability that a stochastic variable x is less than or equal to X, i.e. P(x<=X).
*/
public double cumulative(double X) {
checkRange(X,0.0,n);
double sum=0.0;
for(double i=0.0;i<=X;i++)
sum+=probability(i);
return sum;
}
/**
* Inverse of the cumulative binomial distribution function.
* @return the value X for which P(x<=X).
*/
public double inverse(double probability) {
checkRange(probability);
return Math.floor(findRoot(probability,n/2.0,0.0,n));
}
}