[Stable]

A generalization of the geometric distribution. It is the number of failures in a sequence of i.i.d. Bernoulli trials before a specified number of successes (size) occur. The probability of success in each trial is given by prob.

dist_negative_binomial(size, prob)

Arguments

size

The number of successful trials (target number of successes). Must be a positive number. Also called the dispersion parameter.

prob

The probability of success in each trial. Must be between 0 and 1.

Details

We recommend reading this documentation on pkgdown which renders math nicely. https://pkg.mitchelloharawild.com/distributional/reference/dist_negative_binomial.html

In the following, let \(X\) be a Negative Binomial random variable with success probability prob = \(p\) and the number of successes size = \(r\).

Support: \(\{0, 1, 2, 3, ...\}\)

Mean: \(\frac{r(1-p)}{p}\)

Variance: \(\frac{r(1-p)}{p^2}\)

Probability mass function (p.m.f):

$$ P(X = k) = \binom{k + r - 1}{k} (1-p)^r p^k $$

Cumulative distribution function (c.d.f):

$$ F(k) = \sum_{i=0}^{\lfloor k \rfloor} \binom{i + r - 1}{i} (1-p)^r p^i $$

This can also be expressed in terms of the regularized incomplete beta function, and is computed numerically.

Moment generating function (m.g.f):

$$ E(e^{tX}) = \left(\frac{1-p}{1-pe^t}\right)^r, \quad t < -\log p $$

Skewness:

$$ \gamma_1 = \frac{2-p}{\sqrt{r(1-p)}} $$

Excess Kurtosis:

$$ \gamma_2 = \frac{6}{r} + \frac{p^2}{r(1-p)} $$

Examples

dist <- dist_negative_binomial(size = 10, prob = 0.5)

dist
#> <distribution[1]>
#> [1] NB(10, 0.5)
mean(dist)
#> [1] 10
variance(dist)
#> [1] 20
skewness(dist)
#> [1] 0.6708204
kurtosis(dist)
#> [1] 0.65
support(dist)
#> <support_region[1]>
#> [1] N0

generate(dist, 10)
#> [[1]]
#>  [1]  6  7  7  3 16  3  8  2  8 11
#> 

density(dist, 2)
#> [1] 0.01342773
density(dist, 2, log = TRUE)
#> [1] -4.310433

cdf(dist, 4)
#> [1] 0.08978271

quantile(dist, 0.7)
#> [1] 12