Skip to contents

Create a ddf object for the negative binomial distribution with the given parameters.

Usage

negative_bin(r, p, eps = 1e-10, normalize = TRUE)

Source

In order to calculate a fitting cutoff, the quantile function stats::qnbinom() is used. stats::dnbinom() is then employed to calculate the distribution.

Arguments

r

A positive number, the number of successes until the experiment is stopped.

p

A number between 0 and 1, the success probability in each experiment.

eps

A positive number, how close the distribution is approximated. See ‘Details.’ (default: 1e-10)

normalize

Logical, whether to normalize the approximated distribution. (default: TRUE)

Value

A ddf distribution as described above.

Details

The negative binomial distribution models the number of failures in a sequence of iid Bernoulli trials (see also bernoulli()) with common success probability \(p\), before a specified number \(r\) of successes occurs.

It has support \(\mathbb{N}_0\) on which its probability mass function is given by $$p(k) = \binom{k+r-1}{k} (1-p)^k p^r.$$ Note how the second parameter \(r\) can be generalized to arbitrary positive reals which the currently documented function also does.

As the negative binomial distribution has countably infinite support and this package only works with discrete distributions with finite support, the resulting ddf object can only approximate the negative binomial distribution.

For this, the support is cut off at a large enough integer such that the overall probability is still close to 1. The cutoff is controlled via the eps argument which specifies how close the sum of all probabilities has to be to 1. The default value is 1e-10 since this is also the minimum accuracy required for creating valid ddf objects.

By default, i.e. unless normalize is set to FALSE, the specified accuracy won't raise any problems even when being larger than 1e-10 as the approximation is normalized at the end (that is, the approximating probabilities are divided by their sum). This ensures that the returned object is an actual distribution with its overall probability being precisely one.

See also

Examples

# 'r' does not have to be an integer
negative_bin(2.25, 0.95)
#> (Approximation of a) negative binomial distribution with parameters r = 2.25 and p = 0.95 
#> 
#> Support:
#> [1] 0 1 2 3 4 5 6 7 8
#> 
#> Probabilities:
#> [1] 8.910008e-01 1.002376e-01 8.144305e-03 5.768882e-04 3.785829e-05
#> [6] 2.366143e-06 1.429545e-07 8.424103e-09 4.870185e-10
# A more accurate approximation of the same distribution
negative_bin(2.25, 0.95, 1e-12)
#> (Approximation of a) negative binomial distribution with parameters r = 2.25 and p = 0.95 
#> 
#> Support:
#>  [1]  0  1  2  3  4  5  6  7  8  9 10
#> 
#> Probabilities:
#>  [1] 8.910008e-01 1.002376e-01 8.144305e-03 5.768882e-04 3.785829e-05
#>  [6] 2.366143e-06 1.429545e-07 8.424103e-09 4.870185e-10 2.773300e-11
#> [11] 1.559981e-12