scipy.stats.bootstrap¶
- scipy.stats.bootstrap(data, statistic, *, vectorized=True, paired=False, axis=0, confidence_level=0.95, n_resamples=9999, batch=None, method='BCa', random_state=None)[源代码]¶
- 计算统计量的双边Bootstrap置信区间。 - 什么时候 method 是 - 'percentile'引导置信区间是根据以下过程计算的。- 重新采样数据:对于中的每个样本 data 并且对于每个 n_resamples ,随机抽取与原始样本大小相同的原始样本(带替换)。 
- 计算统计量的自举分布:对于每组重采样,计算测试统计量。 
- 确定置信区间:找出引导分布的区间,即 - 关于中位数和中位数的对称 
- 包含 confidence_level 重新抽样的统计值。 
 
 - 在此期间, - 'percentile'方法是最直观的,在实践中很少使用。还有两种更常见的方法可用,- 'basic'(‘反向百分位数’)和- 'BCa'(“偏差校正和加速”);它们在步骤3的执行方式上有所不同。- 如果里面的样品 data 是从它们各自的分布中随机抽取的 \(n\) 次,返回的置信区间 - bootstrap将包含这些分布的统计值的真实值 confidence_level :MATH:`,Times,n`次。- 参数
- data类阵列序列
- 每个数据元素都是来自底层分布的样本。 
- statistic可调用
- 要计算置信区间的统计信息。 statistic 必须是接受 - len(data)将样本作为单独的参数进行采样,并返回结果统计信息。如果 vectorized 已设置- True, statistic 还必须接受关键字参数 axis 并被矢量化以沿提供的 axis 。
- 矢量化 :Bool,默认值: True布尔默认值:
- 如果 vectorized 已设置 - False, statistic 不会传递关键字参数 axis ,并假定仅计算一维样本的统计量。
- 成对的 :Bool,默认值: False布尔默认值:
- 统计信息是否处理中样本的相应元素 data 就像配对一样。 
- axis :int,默认值: 0int,默认值:
- 中的样本轴 data 沿着这条路 statistic 是经过计算的。 
- confidence_level :浮动,默认值: 0.95浮点,默认值:
- 置信区间的置信级别。 
- n_resamples :int,默认值: 9999int,默认值:
- 为形成统计数据的引导分布而执行的重采样次数。 
- batch整型,可选
- 在每个向量化调用中要处理的重采样数 statistic 。内存使用率为O (batch * - n),其中- n是样本大小。默认值为- None,在这种情况下- batch = n_resamples(或- batch = max(n_resamples, n)为- method='BCa')。
- 方法 :{‘Percententile’,‘Basic’,‘BCA’},默认: 'BCa'{‘Percententile’,‘Basic’,‘BCA’},默认值:
- 是否返回‘PERCENTAL’引导置信区间 ( - 'percentile')、‘反向’或经偏差校正和加速的引导置信区间 (- 'BCa')。请注意,只有- 'percentile'和- 'basic'目前支持多样本统计。
- random_state :{无,整型, numpy.random.Generator,{无,整型,
- 用于生成重采样的伪随机数生成器状态。 - 如果 seed 是 - None(或 np.random )、- numpy.random.RandomState使用的是Singleton。如果 seed 是一个整型、一个新的- RandomState实例,其种子设定为 seed 。如果 seed 已经是一个- Generator或- RandomState实例,则使用该实例。
 
- 退货
- resBootstrapResult
- 具有属性的对象: - confidence_intervalConfidenceInterval
- 作为实例的引导置信区间 - collections.namedtuple具有属性 low 和 high 。
- standard_error浮动或ndarray
- Bootstrap标准误差,即Bootstrap分布的样本标准差 
 
 
 - 参考文献 - 1
- B·埃夫隆和R·J·提布希拉尼,“引导导论”,查普曼和霍尔/CRC,博卡拉顿,美国佛罗里达州(1993)。 
- 2
- Nathaniel E.Helwig,“Bootstrap置信区间”,http://users.stat.umn.edu/~helwig/notes/bootci-Notes.pdf 
- 3
- 引导(统计)、维基百科、https://en.wikipedia.org/wiki/Bootstrapping_%28statistics%29 
 - 示例 - 假设我们从未知分布中抽样了数据。 - >>> import numpy as np >>> rng = np.random.default_rng() >>> from scipy.stats import norm >>> dist = norm(loc=2, scale=4) # our "unknown" distribution >>> data = dist.rvs(size=100, random_state=rng) - 我们对分布的标准差感兴趣。 - >>> std_true = dist.std() # the true value of the statistic >>> print(std_true) 4.0 >>> std_sample = np.std(data) # the sample statistic >>> print(std_sample) 3.9460644295563863 - 我们可以使用以下命令计算统计数据的90%置信区间 - bootstrap。- >>> from scipy.stats import bootstrap >>> data = (data,) # samples must be in a sequence >>> res = bootstrap(data, np.std, confidence_level=0.9, ... random_state=rng) >>> print(res.confidence_interval) ConfidenceInterval(low=3.57655333533867, high=4.382043696342881) - 如果我们从分布中抽样1000次,并为每个样本形成引导置信区间,则置信区间包含统计量的真实值大约900次。 - >>> n_trials = 1000 >>> ci_contains_true_std = 0 >>> for i in range(n_trials): ... data = (dist.rvs(size=100, random_state=rng),) ... ci = bootstrap(data, np.std, confidence_level=0.9, n_resamples=1000, ... random_state=rng).confidence_interval ... if ci[0] < std_true < ci[1]: ... ci_contains_true_std += 1 >>> print(ci_contains_true_std) 875 - 我们还可以一次确定所有1000个样本的置信区间,而不是编写循环。 - >>> data = (dist.rvs(size=(n_trials, 100), random_state=rng),) >>> res = bootstrap(data, np.std, axis=-1, confidence_level=0.9, ... n_resamples=1000, random_state=rng) >>> ci_l, ci_u = res.confidence_interval - 这里, ci_l 和 ci_u 包含每个对象的置信区间 - n_trials = 1000样本。- >>> print(ci_l[995:]) [3.77729695 3.75090233 3.45829131 3.34078217 3.48072829] >>> print(ci_u[995:]) [4.88316666 4.86924034 4.32032996 4.2822427 4.59360598] - 同样,大约90%包含真实值, - std_true = 4。- >>> print(np.sum((ci_l < std_true) & (std_true < ci_u))) 900 - bootstrap也可用于估计多样本统计量的置信区间,包括通过假设检验计算的置信区间。- scipy.stats.moodPerform的Mod‘s test for equalscale参数,它返回两个输出:一个统计数据和一个p值。为了获得测试统计数据的置信区间,我们首先包装- scipy.stats.mood在接受两个示例参数的函数中,接受 axis 关键字参数,并且仅返回统计信息。- >>> from scipy.stats import mood >>> def my_statistic(sample1, sample2, axis): ... statistic, _ = mood(sample1, sample2, axis=-1) ... return statistic - 这里,我们使用“百分位数”方法,默认置信度为95%。 - >>> sample1 = norm.rvs(scale=1, size=100, random_state=rng) >>> sample2 = norm.rvs(scale=2, size=100, random_state=rng) >>> data = (sample1, sample2) >>> res = bootstrap(data, my_statistic, method='basic', random_state=rng) >>> print(mood(sample1, sample2)[0]) # element 0 is the statistic -5.521109549096542 >>> print(res.confidence_interval) ConfidenceInterval(low=-7.255994487314675, high=-4.016202624747605) - 标准误差的自举估计也是可用的。 - >>> print(res.standard_error) 0.8344963846318795 - 配对样本统计也有效。例如,考虑皮尔逊相关系数。 - >>> from scipy.stats import pearsonr >>> n = 100 >>> x = np.linspace(0, 10, n) >>> y = x + rng.uniform(size=n) >>> print(pearsonr(x, y)[0]) # element 0 is the statistic 0.9962357936065914 - 我们把它包起来 - pearsonr因此它只返回统计数据。- >>> def my_statistic(x, y): ... return pearsonr(x, y)[0] - 我们打电话给 - bootstrap使用- paired=True。另外,因为- my_statistic未被矢量化以沿给定轴计算统计数据,则我们传入- vectorized=False。- >>> res = bootstrap((x, y), my_statistic, vectorized=False, paired=True, ... random_state=rng) >>> print(res.confidence_interval) ConfidenceInterval(low=0.9950085825848624, high=0.9971212407917498)