Python 中是否支持计算 Wilcoxon 检验的置信区间?

问题描述 投票:0回答:1

简要免责声明:我已从 Overleaf 中的 LaTEX 编辑器导入我的提交内容,因此提交内容完全包含在图像附件中。

enter image description here

python confidence-interval scipy.stats
1个回答
0
投票

如果您对 Wilcoxon 检验感兴趣,我认为您正在寻找有关中位数的置信区间,而不是平均值。

作为一个病态的例子,假设你的数据是利维分布的(在

scipy.stats.levy
的意义上)。中位数略高于 2,PDF 看起来无害: enter image description here 但分布是重尾的,并且分布的均值是无限的,因此我们不能指望均值的置信区间非常有用。

import numpy as np
from scipy import stats
rng = np.random.default_rng(39891409235034)
dist = stats.levy()
# draw 1000 samples from Levy, each with 100 observations
x = dist.rvs(size=(1000, 100))

这是样本均值的直方图:

import matplotlib.pyplot as plt
plt.plot(np.mean(x, axis=-1))

enter image description here

它们遍布地图上。 (查看 y 轴上的刻度。)只是为了确认平均值的置信区间在这里没有任何用处:

res = stats.ttest_1samp(x, popmean=0, axis=-1)
l, h = res.confidence_interval()
np.mean(h-l), np.std(h-l), np.median(h-l)
# (85403.02332485246, 807469.2708232998, 595.9180866193315)

但是,

scipy.stats.bootstrap
可用于计算关于中位数的 95% 置信区间。

res = stats.bootstrap((x,), np.median, axis=-1, batch=100)

我们可以确认置信区间包含大约 95% 的样本的真实中位数:

l, h = res.confidence_interval
median = dist.median()
np.sum((l < median) & (h > median))
# 943
© www.soinside.com 2019 - 2024. All rights reserved.