Database Reference
In-Depth Information
Where the
Arithmetic.binomial
function from the Colt numerical
libraryimplementsthecombinationfunction“nchoosek”fromtheprevious
section. This is commonly known as the
binomial coefficient
.
In addition to describing the probability of drawing i balls of a particular
color in n draws when the balls are replaced, it is also used to model other
processes where the probability doesn't change from trial to trial. This
includes the number of heads in n flips of a coin, or the sum of the faces of a
number of dice after each roll. In general, it is the probability of i successes
out of n tries when the probability of success is p.
If the balls are not returned after each draw, it is called a
hypergeometric
distribution
. If K is the total number of red balls and N is the total number
of balls, its mass function is implemented as the following:
public static double dhypergeom(long k,long n,long
N,long K) {
return (Arithmetic.
binomial
(K, k)
*Arithmetic.
binomial
(N-K, n-k))
/Arithmetic.
binomial
(N, n);
}
Geometric and Negative Binomial Distributions
The distribution of the number of non-red balls drawn with replacement
before the first red ball is called the
geometric distribution
(sometimes also
called the
first success distribution
):
public static
double
dgeometric(long i,double p) {
return Math.pow(1-p, i-1)*p;
}
Extending this to the distribution of the number of draws with replacement
before the r
th
red ball is drawn is called the
negative binomial distribution
.
The geometric distribution is actually a special case of the negative binomial
(the case where r=1), so it is no surprise that they have very similar
implementations:
public static
double
dnegbinom(long i,long r,double p)
{
[[OPEN-LW-CODE80]]
return Arithmetic.binomial(i-1,