Java Utililty Methods Binomial Coefficients

List of utility methods to do Binomial Coefficients

Description

The list of methods to do Binomial Coefficients are organized into topic(s).

Method

longbinomialCoefficient(int n, int k)
binomial Coefficient
if (k > n || k < 0) {
    return 0;
} else {
    return factorial(n) / factorial(k) / factorial(n - k);
longbinomialCoefficient(long n, long k)

Binomial coefficient, also known as "n choose k".

final long m = Math.max(k, n - k);
double temp = 1;
for (long i = n, j = 1; i > m; i--, j++) {
    temp = temp * i / j;
return (long) temp;
doublebinomialCoefficientDouble(final int n, final int k)
Returns a double representation of the Binomial Coefficient, "n choose k", the number of k-element subsets that can be selected from an n-element set.
return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + 0.5);
doublebinomialCoefficientLog(final int n, final int k)
Returns the natural log of the Binomial Coefficient, "n choose k", the number of k-element subsets that can be selected from an n-element set.
if (n < k) {
    throw new IllegalArgumentException("must have n >= k for binomial coefficient (n,k)");
if (n < 0) {
    throw new IllegalArgumentException("must have n >= 0 for binomial coefficient (n,k)");
if ((n == k) || (k == 0)) {
    return 0;
...
longbinomialCoefficients(int n, int k)
binomial Coefficients
long Ank = 1;
if (k < 0 || k > n) {
    return 0;
long i = n - k + 1;
while (i <= n && Ank >= 0) {
    Ank = Ank * i;
    i = i + 1;
...
doublebinomialPmf(int k, int n, double p)
binomial Pmf
if (p == 0.0) {
    return ((k == 0) ? 1.0 : 0.0);
} else if (p == 1.0) {
    return ((k == n) ? 1.0 : 0.0);
} else if ((k < 0) || (k > n)) {
    return 0.0;
} else {
    return (Math.exp(lngamma(n + 1.0) - lngamma(k + 1.0) - lngamma(n - k + 1.0) + k * Math.log(p)
...
intbinomialRand(int n, double pp)
Binomial random generator from Numerical Recipes
int j, k;
double am, em, g, p, sq, t, y;
double pc, plog, pclog, en;
p = (pp <= 0.5) ? pp : 1.0 - pp;
am = n * p;
if (p == 0.0) {
    k = 0;
} else if (p == 1.0) {
...