# GRANDBINOMIAL

## Purpose:

Generates a binomial distributed random series.

## Syntax:

GRANDBINOMIAL(length, spacing, n, p)

 length - An integer, the length of the output series. spacing - A real, the spacing (delta x) between points. n - Optional. An integer, the number of trials where 0 ≤ n. Defaults to 1. p - Optional. A real, the probability of success where 0 ≤ p ≤ 1. Defaults to 0.5.

A series.

## Example:

grandbinomial(100, 0.01, 200, 0.2)

creates a 100 point binomal distributed random series of values from a population where the number of trials is 200 and the probability of success is 0.2.

## Example:

k = 200..0.1..380;

n = 1000;

p = 0.3;

W1: grandbinomial(50000, 1, n, p);label("Random Binomial")

W2: hist(W1, 40, "pdf");label("Histogram")

W3: exp(gammaln(n+1)-gammaln(k+1)-gammaln(n-k+1)+k*log(p)+(n-k)*log1p(-p));lines;overp(W2, lred);label("Binomial Distribution")

W1 contains 50000 samples of binomial distributed random values with 1000 trials and a probability of success of 0.3.

W2 contains a 40 sample normalized histogram of W1.

W3 compares the distribution of the generated binomial random series to the analytic distribution with n = 1000 and p = 0.3.

## Remarks:

GRANDBINOMIAL uses a transformation-rejection method due to Kachitvichyanukul and Schmeiser [1] to transform uniformly distributed random values to binomial distributed values.

The probability mass function, f(k), for binomial distributed random values is:

where n is the number of trails and p is the probability of success. For example, the probability of obtaining 4 heads out of 6 coin tosses of a fair coin is:

The cumulative distribution function, F(k) is:

where Ix is the regularized incomplete beta function implemented by BETAINC.

The mean of a binomial distribution is np and the variance is np(1-p).

GRANDBINOMIAL returns NaN for p < 0 or p > 1 or n < 0 or n not an integer.

BINCOEFF

GNORMAL

GRANDGAMMA

GRANDOM

GRANDPOISSON

NCHOOSEK

SEEDRAND

## References:

[1]  Voratas Kachitvichyanukul, Bruce Schmeiser

Binomial Random Variate Generation

Communications of the ACM

Volume 31, Number 2, February 1988, pages 216-222.