如何用 scipy 计算单边公差区间

Gig*_*igi 5 python statistics reliability scipy confidence-interval

我想根据给定已知 N(样本大小)、标准差和平均值的数据集的正态分布计算单侧公差范围。

如果间隔是两侧的,我会执行以下操作:

conf_int = stats.norm.interval(alpha, loc=mean, scale=sigma)

在我的情况下,我正在引导样本,但如果我不是,我会参考 stackoverflow 上的这篇文章:Cure way to getting CI scipy并使用以下内容:conf_int = stats.norm.interval(0.68, loc=mean, scale=sigma / np.sqrt(len(a)))

您会如何做同样的事情,但将其计算为单边边界(95% 的值高于或低于 x<--bound)?

小智 6

我假设您有兴趣使用正态分布计算一侧容差界限(基于您提到该scipy.stats.norm.interval函数作为您需要的两侧等价物的事实)。

那么好消息是,根据维基百科页面的容差区间

单侧正态公差区间具有基于非中心 t 分布的样本均值和样本方差的精确解。

(仅供参考:不幸的是,双面设置并非如此)

这一论断是基于这篇论文。此外,第 4.8 段(第 23 页)提供了公式。

坏消息是,我认为没有一个现成的scipy功能可以让您安全地调整和使用以达到您的目的。

但您可以自己轻松计算。您可以在 Github 存储库上找到包含此类计算器的信息,您可以从中找到灵感,例如我构建了以下说明性示例的计算器:

import numpy as np
from scipy.stats import norm, nct

# sample size
n=1000

# Percentile for the TI to estimate
p=0.9
# confidence level
g = 0.95

# a demo sample
x = np.array([np.random.normal(100) for k in range(n)])

# mean estimate based on the sample
mu_est = x.mean()

# standard deviation estimated based on the sample
sigma_est = x.std(ddof=1)

# (100*p)th percentile of the standard normal distribution
zp = norm.ppf(p)

# gth quantile of a non-central t distribution
# with n-1 degrees of freedom and non-centrality parameter np.sqrt(n)*zp
t = nct.ppf(g, df=n-1., nc=np.sqrt(n)*zp)

# k factor from Young et al paper
k = t / np.sqrt(n)

# One-sided tolerance upper bound
conf_upper_bound = mu_est + (k*sigma_est)
Run Code Online (Sandbox Code Playgroud)

  • @Greta,如果它解决了您的问题,您可以考虑接受答案。https://stackoverflow.com/help/someone-answers。评论可能会被删除,如果有人遇到类似问题,这会有所帮助 (2认同)