# Class BinomialDistribution

java.lang.Object
smile.stat.distribution.DiscreteDistribution
smile.stat.distribution.BinomialDistribution
All Implemented Interfaces:
Serializable, Distribution

public class BinomialDistribution extends DiscreteDistribution
The binomial distribution is the discrete probability distribution of the number of successes in a sequence of n independent yes/no experiments, each of which yields success with probability p. Such a success/failure experiment is also called a Bernoulli experiment or Bernoulli trial. In fact, when n = 1, the binomial distribution is a Bernoulli distribution. The probability of getting exactly k successes in n trials is given by the probability mass function:

Pr(K = k) = nCk pk (1-p)n-k

where nCk is n choose k.

It is frequently used to model number of successes in a sample of size n from a population of size N. Since the samples are not independent (this is sampling without replacement), the resulting distribution is a hypergeometric distribution, not a binomial one. However, for N much larger than n, the binomial distribution is a good approximation, and widely used.

Binomial distribution describes the number of successes for draws with replacement. In contrast, the hypergeometric distribution describes the number of successes for draws without replacement.

Although Binomial distribution belongs to exponential family, we don't implement DiscreteExponentialFamily interface here since it is impossible and meaningless to estimate a mixture of Binomial distributions.

• ## Field Summary

Fields
Modifier and Type
Field
Description
final int
The number of experiments.
final double
The probability of success.
• ## Constructor Summary

Constructors
Constructor
Description
BinomialDistribution(int n, double p)
Constructor.
• ## Method Summary

Modifier and Type
Method
Description
double
cdf(double k)
Cumulative distribution function.
double
Returns Shannon entropy of the distribution.
int
Returns the number of parameters of the distribution.
double
logp(int k)
The probability mass function in log scale.
double
Returns the mean of distribution.
double
p(int k)
The probability mass function.
double
quantile(double p)
The quantile, the probability to the left of quantile is p.
double
This function generates a random variate with the binomial distribution.
double
sd()
Returns the standard deviation of distribution.

double
Returns the variance of distribution.

### Methods inherited from class smile.stat.distribution.DiscreteDistribution

likelihood, logLikelihood, logp, p, quantile, randi, randi

### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

### Methods inherited from interface smile.stat.distribution.Distribution

inverseTransformSampling, likelihood, logLikelihood, quantile, quantile, rand, rejectionSampling
• ## Field Details

• ### p

public final double p
The probability of success.
• ### n

public final int n
The number of experiments.
• ## Constructor Details

• ### BinomialDistribution

public BinomialDistribution(int n, double p)
Constructor.
Parameters:
n - the number of experiments.
p - the probability of success.
• ## Method Details

• ### length

public int length()
Description copied from interface: Distribution
Returns the number of parameters of the distribution. The "length" is in the sense of the minimum description length principle.
Returns:
The number of parameters.
• ### mean

public double mean()
Description copied from interface: Distribution
Returns the mean of distribution.
Returns:
The mean.
• ### variance

public double variance()
Description copied from interface: Distribution
Returns the variance of distribution.
Returns:
The variance.
• ### sd

public double sd()
Description copied from interface: Distribution
Returns the standard deviation of distribution.
Returns:
The standard deviation.
• ### entropy

public double entropy()
Description copied from interface: Distribution
Returns Shannon entropy of the distribution.
Returns:
Shannon entropy.
• ### toString

public String toString()
Overrides:
toString in class Object
• ### p

public double p(int k)
Description copied from class: DiscreteDistribution
The probability mass function.
Specified by:
p in class DiscreteDistribution
Parameters:
k - a real value.
Returns:
the probability.
• ### logp

public double logp(int k)
Description copied from class: DiscreteDistribution
The probability mass function in log scale.
Specified by:
logp in class DiscreteDistribution
Parameters:
k - a real value.
Returns:
the log probability.
• ### cdf

public double cdf(double k)
Description copied from interface: Distribution
Cumulative distribution function. That is the probability to the left of x.
Parameters:
k - a real number.
Returns:
the probability.
• ### quantile

public double quantile(double p)
Description copied from interface: Distribution
The quantile, the probability to the left of quantile is p. It is actually the inverse of cdf.
Parameters:
p - the probability.
Returns:
the quantile.
• ### rand

public double rand()
This function generates a random variate with the binomial distribution.

Uses down/up search from the mode by chop-down technique for n*p < 55, and patchwork rejection method for n*p >= 55.

For n*p < 1E-6 numerical inaccuracy is avoided by poisson approximation.

Returns:
a random number.