alternative parametrization of the negative binomial in scipy
Asked Answered
C

2

9

In scipy the negative binomial distribution is defined as:

nbinom.pmf(k) = choose(k+n-1, n-1) * p**n * (1-p)**k

This is the common definition, see also wikipedia: https://en.wikipedia.org/wiki/Negative_binomial_distribution

However, there exists a different parametrization where the negative Binomial is defined by the mean mu and the dispersion parameter.

In R this is easy, as the negbin can be defined by both parametrizations:

dnbinom(x, size, prob, mu, log = FALSE)

How can I use the mean/dispersion parametrization in scipy ?

edit:

straight from the R help:

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

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

An alternative parametrization (often used in ecology) is by the mean mu (see above), and size, the dispersion parameter, where prob = size/(size+mu). The variance is mu + mu^2/size in this parametrization.

It is also describe here in more detail:

https://en.wikipedia.org/wiki/Negative_binomial_distribution#Alternative_formulations

Cornejo answered 28/11, 2016 at 14:44 Comment(2)
Can you give us a probability density function or distribution function?Testy
I have just posted a question in here related to this post.Catercousin
T
10
from scipy.stats import nbinom


def convert_params(mu, theta):
    """
    Convert mean/dispersion parameterization of a negative binomial to the ones scipy supports

    See https://en.wikipedia.org/wiki/Negative_binomial_distribution#Alternative_formulations
    """
    r = theta
    var = mu + 1 / r * mu ** 2
    p = (var - mu) / var
    return r, 1 - p


def pmf(counts, mu, theta):
    """
    >>> import numpy as np
    >>> from scipy.stats import poisson
    >>> np.isclose(pmf(10, 10, 10000), poisson.pmf(10, 10), atol=1e-3)
    True
    """
    return nbinom.pmf(counts, *convert_params(mu, theta))


def logpmf(counts, mu, theta):
    return nbinom.logpmf(counts, *convert_params(mu, theta))


def cdf(counts, mu, theta):
    return nbinom.cdf(counts, *convert_params(mu, theta))


def sf(counts, mu, theta):
    return nbinom.sf(counts, *convert_params(mu, theta))
Trivia answered 21/11, 2017 at 6:16 Comment(2)
excellent implementation should be natively supported. cheersJenjena
Quick comment: convert_params would be a bit clearer if it were in the same notation as scipy, which uses n and p. The wikipedia page uses r and p. But r_wikipedia = n_scipy and p_wikipedia = 1-p_scipy. So the use of r makes me concerned it's the other parameterization (that wikipedia uses).Bridlewise
S
2

The Wikipedia page you linked given a precise formula for p and r in terms of mu and sigma, see the very last bullet item in the Alternative parametrization section,https://en.m.wikipedia.org/wiki/Negative_binomial_distribution#Alternative_formulations

Selenaselenate answered 28/11, 2016 at 23:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.