alternative parametrization of the negative binomial in scipy

给你一囗甜甜゛ 提交于 2020-06-25 00:39:07

问题


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


回答1:


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))



回答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



来源:https://stackoverflow.com/questions/40846992/alternative-parametrization-of-the-negative-binomial-in-scipy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!