Gep*_*ada 18 python statistics
我需要计算python脚本中大量数据的二项式置信区间.你知道python的任何函数或库可以做到这一点吗?
理想情况下,我希望在python上实现这样的功能http://statpages.org/confint.html.
谢谢你的时间.
Dou*_*gal 25
只是注意到因为它没有在这里发布,所以statsmodels.stats.proportion.proportion_confint可以通过各种方法获得二项式置信区间.但它只做对称间隔.
Cur*_*urt 12
如果你有选择的话,我会说R(或其他统计数据包)可能会更好地为你服务.也就是说,如果您只需要二项式置信区间,则可能不需要整个库.这是我最简单的javascript翻译功能.
def binP(N, p, x1, x2):
p = float(p)
q = p/(1-p)
k = 0.0
v = 1.0
s = 0.0
tot = 0.0
while(k<=N):
tot += v
if(k >= x1 and k <= x2):
s += v
if(tot > 10**30):
s = s/10**30
tot = tot/10**30
v = v/10**30
k += 1
v = v*q*(N+1-k)/k
return s/tot
def calcBin(vx, vN, vCL = 95):
'''
Calculate the exact confidence interval for a binomial proportion
Usage:
>>> calcBin(13,100)
(0.07107391357421874, 0.21204372406005856)
>>> calcBin(4,7)
(0.18405151367187494, 0.9010086059570312)
'''
vx = float(vx)
vN = float(vN)
#Set the confidence bounds
vTU = (100 - float(vCL))/2
vTL = vTU
vP = vx/vN
if(vx==0):
dl = 0.0
else:
v = vP/2
vsL = 0
vsH = vP
p = vTL/100
while((vsH-vsL) > 10**-5):
if(binP(vN, v, vx, vN) > p):
vsH = v
v = (vsL+v)/2
else:
vsL = v
v = (v+vsH)/2
dl = v
if(vx==vN):
ul = 1.0
else:
v = (1+vP)/2
vsL =vP
vsH = 1
p = vTU/100
while((vsH-vsL) > 10**-5):
if(binP(vN, v, 0, vx) < p):
vsH = v
v = (vsL+v)/2
else:
vsL = v
v = (v+vsH)/2
ul = v
return (dl, ul)
Run Code Online (Sandbox Code Playgroud)
我不是统计学专家,但binomtest内置于 SciPy 中并产生与接受的答案相同的结果:
from scipy.stats import binomtest
binomtest(13, 100).proportion_ci()
Out[11]: ConfidenceInterval(low=0.07107304618545972, high=0.21204067708744978)
binomtest(4, 7).proportion_ci()
Out[25]: ConfidenceInterval(low=0.18405156764007, high=0.9010117215575631)
Run Code Online (Sandbox Code Playgroud)
它默认使用Clopper-Pearson 精确方法,该方法与Curt 接受的答案相匹配,该答案给出了这些值,以进行比较:
Usage:
>>> calcBin(13,100)
(0.07107391357421874, 0.21204372406005856)
>>> calcBin(4,7)
(0.18405151367187494, 0.9010086059570312)
Run Code Online (Sandbox Code Playgroud)
它还具有Wilson 方法的选项,带或不带连续性校正,这与TheBamf 的天体答案相匹配:
binomtest(4, 7).proportion_ci(method='wilson')
Out[32]: ConfidenceInterval(low=0.2504583645276572, high=0.8417801447485302)
binom_conf_interval(4, 7, 0.95, interval='wilson')
Out[33]: array([0.25045836, 0.84178014])
Run Code Online (Sandbox Code Playgroud)
根据cxrodgers 的评论,这也匹配 Rbinom.test和:statsmodels.stats.proportion.proportion_confint
对于 60 次试验中的 30 次成功,R 的 binom.test 和 statsmodels.stats.proportion.proportion_confint 使用 Klopper-Pearson 给出 (.37, .63)。
binomtest(30, 60).proportion_ci(method='exact')
Out[34]: ConfidenceInterval(low=0.3680620319424367, high=0.6319379680575633)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11964 次 |
| 最近记录: |