【问题标题】:alternative parametrization of the negative binomial in scipyscipy中负二项式的替代参数化
【发布时间】:2017-04-12 08:01:07
【问题描述】:

在 scipy 中,负二项分布定义为:

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

这是通用定义,另见维基百科: https://en.wikipedia.org/wiki/Negative_binomial_distribution

但是,存在不同的参数化,其中负二项式由平均值 mu 和分散参数定义。

在 R 中这很容易,因为 negbin 可以由两个参数化定义:

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

如何在 scipy 中使用均值/色散参数化?

编辑:

直接来自 R 帮助:

size = n 且 prob = p 的负二项分布有密度

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

另一种参数化(通常在生态学中使用)是通过均值 mu(见上文)和 size(分散参数),其中 prob = size/(size+mu)。此参数化的方差为 mu + mu^2/size。

这里也有更详细的描述:

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

【问题讨论】:

  • 你能给我们一个概率密度函数或分布函数吗?
  • 我刚刚在here 发布了一个与此帖子相关的问题。

标签: python numpy scipy distribution


【解决方案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))

【讨论】:

  • 优秀的实现应该是原生支持的。欢呼
  • 快速评论:convert_params 如果使用与使用np 的scipy 相同的符号,会更清晰一些。维基百科页面使用rp。但是r_wikipedia = n_scipy 和p_wikipedia = 1-p_scipy。所以r 的使用让我担心它是另一种参数化(维基百科使用的)。
【解决方案2】:

您链接的维基百科页面给出了 p 和 r 在 mu 和 sigma 方面的精确公式,请参阅替代参数化部分中的最后一个项目符号,https://en.m.wikipedia.org/wiki/Negative_binomial_distribution#Alternative_formulations

【讨论】:

    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 2021-06-30
    • 2014-12-03
    • 2016-09-07
    • 2014-08-19
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多