mitchelloharawild / distributional

Vectorised distributions for R
https://pkg.mitchelloharawild.com/distributional
GNU General Public License v3.0
94 stars 15 forks source link

negative binomial distribution parametrization #46

Closed Fuco1 closed 3 years ago

Fuco1 commented 3 years ago

The stats::*nbinom functions are parametrized as follows (emph. mine)

The negative binomial distribution with size = n and ‘prob’ = p has density

                  Gamma(x+n)/(Gamma(n) x!) p^n (1-p)^x              

for x = 0, 1, 2, ..., n > 0 and 0 < p <= 1.

This represents the number of failures which occur in a sequence of Bernoulli trials before a target number of successes is reached. The mean is mu = n(1-p)/p and variance n(1-p)/p^2.

In this package, it is described as:

A generalization of the geometric distribution. It is the number of successes in a sequence of i.i.d. Bernoulli trials before a specified number (r) of failures occurs.

Both use p for the probability of success.

I believe in your documentation r is the size parameter (it would be great if this was made explicit in the documentation).

However looking at the source I see you are simply calling the stats functions which have the roles reversed.

Now it might be very well possible I just don't get something, but it seems to me this will give "inverse" results? The formulas for mean/variance are also different.

Edit: indeed the variance and mean formulas are equivalent if we make a p' = 1 - p substitution. So something is definitely odd here.

mitchelloharawild commented 3 years ago

Thanks, I think you're right here. The documentation is copied from the distributions3 package, so the changes will also need to be applied there.

Could you briefly review the changes in #47 to check that it is now correct?

Fuco1 commented 3 years ago

Thanks! I left you a review, there's just one typo I'm not sure about :+1:

mitchelloharawild commented 3 years ago

Fixed, thanks for spotting this and for your review of the changes.