通过均值和标准差对scipy中的负二项式进行参数化 [英] parameterization of the negative binomial in scipy via mean and std
问题描述
我正在尝试使用 Python 中的软件包 scipy 将数据拟合为负二项分布。但是,我的验证似乎失败了。
I am trying to fit my data to a Negative Binomial Distribution with the package scipy in Python. However, my validation seems to fail.
这些是我的步骤:
- 我有一些需求数据,描述如下根据统计数据:
mu = 1.4
std = 1.59
print(mu, std)
- 我使用下面的参数化函数,取自此帖子,以计算两个 NB 参数。
- I use the parameterization function below, taken from this post to compute the two NB parameters.
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
我通过了(希望是正确的...)我的两个统计信息-不同来源之间的命名约定相当混乱此时 p
, r
, k
I pass (hopefully correctly...) my two statistics - the naming convention between different sources is rather confusing at this point p
, r
, k
firstParam, secondParam = convert_params(mu, std)
- 然后我将使用以下两个参数来拟合分布:
from scipy.stats import nbinom
rv = nbinom(firstParam, secondParam)
然后我用百分比点函数 <$来计算值 R
c $ c> .ppf(0.95)。在我的问题中,值 R
是重新订购点。
Then I calculate a value R
with the Percent Point Function .ppf(0.95)
. The value R
in the context of my problem is a Reorder Point.
R = rv.ppf(0.95)
- 现在是我希望验证前面的步骤的时候了,但是我无法获取我的原始统计信息
mu
,std
分别具有平均值
和math.sqrt(var)
- Now is when I expect to validate the previous steps, but I do not manage to retrieve my original statistics
mu
andstd
withmean
andmath.sqrt(var)
respectively.
import math
mean, var = nbinom.stats(firstParam, secondParam, moments='mv')
print(mean, math.sqrt(var))
我想念什么?在 Scipy
中实现的有关参数化的任何反馈?
What am I missing? Any feedback about the parameterization implemented in Scipy
?
推荐答案
转换代码是错误,我相信,SciPy没有使用Wiki约定,但使用了Mathematica约定
Conversion code is wrong, I believe, SciPy is NOT using Wiki convention, but Mathematica convention
#%%
import numpy as np
from scipy.stats import nbinom
def convert_params(mean, std):
"""
Convert mean/dispersion parameterization of a negative binomial to the ones scipy supports
See https://mathworld.wolfram.com/NegativeBinomialDistribution.html
"""
p = mean/std**2
n = mean*p/(1.0 - p)
return n, p
mean = 1.4
std = 1.59
n, p = convert_params(mean, std)
print((n, p))
#%%
m, v = nbinom.stats(n, p, moments='mv')
print(m, np.sqrt(v))
代码回显1.4、1.59对
Code prints back 1.4, 1.59 pair
,重新排序点计算为
rv = nbinom(n, p)
print("reorder point:", rv.ppf(0.95))
输出 5
这篇关于通过均值和标准差对scipy中的负二项式进行参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!