public class BinomialDistribution extends AbstractDiscreteDistribution
BinomialDistribution is a discrete distribution over
the number of successes given a fixed number of Bernoulli trials.
A binomial distribution is constructed from a specified Bernoulli
distribution which determines the success probability. The minimum
outcome is 0 and the maximum outcome is the number of
trials. This class also defines a constant method log2BinomialCoefficient(long,long) for computing binomial
coefficients.
The method z(int) returns the z-score statistic for a
specified number of outcomes.
As of LingPipe 3.2.0, the dependency on Jakarta Commons Math was removed. As a result, we removed the two methods that computed p-values. Here's their implementation in case you need the functionality (you may need to increas the text size):
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.NormalDistribution;
import org.apache.commons.math.distribution.NormalDistributionImpl;
static final NormalDistribution Z_DISTRIBUTION
= new NormalDistributionImpl();
/**
* Returns the two-sided p-value computed from the z-score for
* this distribution for the specified number of successes.
...
double pValue(int numSuccesses) throws MathException {
return pValue(bernoulliDistribution().successProbability(),
numSuccesses,
numTrials());
}
/**
* Returns the one-sided p-value computed from the z-score for
* this distribution for the specified number of successes.
...
double pValueLess(int numSuccesses) throws MathException {
return pValueLess(bernoulliDistribution().successProbability(),
numSuccesses,
mNumTrials());
}
/**
* Returns the two-sided p-value for the z-score statistic on the
* specified number of successes out of the specified number of
* trials for the specified success probability.
...
static double pValue(double successProbability,
int numSuccesses,
int numTrials) throws MathException {
double z = z(successProbability,numSuccesses,numTrials);
return 2.0 * Z_DISTRIBUTION.cumulativeProbability(Math.min(-z,z));
}
/**
* Returns the one-sided (lower) p-value for the z-score statistic
* on the specified number of successes out of the specified
* number of trials for the specified success probability.
...
static double pValueLess(double successProbability,
int numSuccesses,
int numTrials) throws MathException {
double z = z(successProbability,numSuccesses,numTrials);
return 1.0 - Z_DISTRIBUTION.cumulativeProbability(z);
}
For more information, see:
| Constructor and Description |
|---|
BinomialDistribution(BernoulliDistribution distribution,
int numTrials)
Construct a binomial distribution that samples from the
specified Bernoulli distribution the specified number of times.
|
| Modifier and Type | Method and Description |
|---|---|
BernoulliDistribution |
bernoulliDistribution()
Returns the underlying Bernoulli (two outcome) distribution
underlying this binomial distribution.
|
static double |
log2BinomialCoefficient(long n,
long m)
Returns the log (base 2) of the binomial coefficient of the
specified arguments.
|
double |
log2Probability(long outcome)
Returns the log (base 2) probability of the specified outcome.
|
long |
maxOutcome()
Returns the maximum non-zero probability outcome, which is the
number of trials for this distribution.
|
long |
minOutcome()
Returns zero, the minimum outcome for a binomial distribution.
|
long |
numTrials()
Returns the number of trials for this binomial distribution.
|
double |
probability(long outcome)
Returns the probability of the specified outcome.
|
double |
variance()
Returns the variance of this binomial distribution.
|
static double |
z(double successProbability,
int numSuccesses,
int numTrials)
Returns the z score for the specified number of successes out
of the specified number of trials given the specified success
probability.
|
double |
z(int numSuccesses)
Returns the z-score for the specified number of successes given
this distribution's success probability and number of trials.
|
cumulativeProbability, cumulativeProbabilityGreater, cumulativeProbabilityLess, entropy, meanpublic BinomialDistribution(BernoulliDistribution distribution, int numTrials)
The Bernoulli distribution is stored and any change to it will affect the constructed binomial distribution.
distribution - Underlying Bernoulli distribution.public BernoulliDistribution bernoulliDistribution()
public long minOutcome()
minOutcome in interface DiscreteDistributionminOutcome in class AbstractDiscreteDistributionpublic long maxOutcome()
maxOutcome in interface DiscreteDistributionmaxOutcome in class AbstractDiscreteDistributionpublic long numTrials()
maxOutcome().public double probability(long outcome)
The probability for a specified number of outcomes is:
P(numSuccesses)
= binomialCoefficient(numTrials,numSuccesses)
* P(success)n
* (1 - P(success))numTrials - numSuccesses
where numTrials is the number of trials for this
binomial distribution and P(success) is the
success probability of the Bernoulli distribution underlying
this binomial distribution.probability in interface DiscreteDistributionprobability in class AbstractDiscreteDistributionoutcome - Number of successes.public double log2Probability(long outcome)
probability(long) for an exact definition.log2Probability in interface DiscreteDistributionlog2Probability in class AbstractDiscreteDistributionoutcome - Number of successes.public double z(int numSuccesses)
The formula for z-scores is provided in the documentation
for the static method z(double,int,int).
numSuccesses - Number of successes in sample.IllegalArgumentException - If the number of successes is less
than 0 or more than the number of trials for this distribution.public double variance()
variance = numTrials * P(success) * (1 - P(success))
variance in interface DiscreteDistributionvariance in class AbstractDiscreteDistributionpublic static double z(double successProbability,
int numSuccesses,
int numTrials)
The z-score for binomial distributions is defined by:
z = (numSuccesses - expectedSuccesses)
/ (numTrials * P(success) * (1-P(success)))1/2
where
expectedSuccesses = P(success) * numTrials
Thus numerator is the difference between observed and expected
values for the number of successes and the denominator is the
standard deviation for the Bernoulli trial iterated over the
specified number of trials.successProbability - Probability of success.numSuccesses - Number of successes.numTrials - Number of trials.IllegalArgumentException - If the success probability is
not between 0 and 1 or if the number of successes is less than
zero or greater than the number of trials.public static double log2BinomialCoefficient(long n,
long m)
m from a
set of n objects, which is pronounced "n choose
m", and is given by:
binomialCoefficient(n,m) = n! / ( m! * (n-m)!)
log2 choose(n,m)
= log2 n - log2 m
- log2 (n-m)
Copyright © 2016 Alias-i, Inc.. All rights reserved.