python,在中位数附近找到置信区间

标签 python intervals median

如何在python中为我的数据找到中位数附近的置信区间?

说我有数组

a = np.array([24, 38, 61, 22, 16, 57, 31, 29, 35])

我想在中位数附近找到 80% 的置信区间。我如何在 python 中做到这一点?

最佳答案

我对 this procedure 的实现计算中位数周围的置信区间。

对于您的示例,设置 cutoff=0.8 .
这需要 python > 3pandas > 1 .
它假设您将数组作为 pd.Series 传递.

import statistics, math
import pandas as pd 

def median_confidence_interval(dx,cutoff=.95):
    ''' cutoff is the significance level as a decimal between 0 and 1'''
    dx = dx.sort_values(ascending=True, ignore_index=True)
    factor = statistics.NormalDist().inv_cdf((1+cutoff)/2)
    factor *= math.sqrt(len(df)) # avoid doing computation twice

    lix = round(0.5*(len(dx)-factor))
    uix = round(0.5*(1+len(dx)+factor))

    return (dx[lix],dx[uix])

a = np.array([24, 38, 61, 22, 16, 57, 31, 29, 35])
print(median_confidence_interval(df,cutoff=0.8))
# (29,57)

关于python,在中位数附近找到置信区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41102184/

相关文章:

MySQL:使用 "two halves"方法查找中位数

python - 写一个中值函数

python - 为什么不能在 f 字符串中使用 "await"?

python - GIS:Python 中的 line_locate_point()

Python C API unicode 参数

python - Pandas 数据框 : how to fill `nan` with 0s but only nans existing between valid values?

math - 球算术与区间算术

python - 删除第二个数据帧间隔内的数据帧时间戳

ios - 在 Swift 中获取两个 NSDate() 实例之间的时间间隔时遇到问题

c - 编译器 ID 上的错误返回 1 退出状态