Python, 寻找中位数置信区间

7

我该如何在Python中找到我的数据中位数的置信区间?

假设我有一个数组

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

我想在中位数周围找到80%的置信区间。如何在Python中实现?


你可以使用Bootstrap:https://dev59.com/questions/_6Lia4cB1Zd3GeqPh1to#66008548 - Marco Cerliani
1个回答

2

我实现了这个流程来计算中位数周围的置信区间。

对于您的示例,请设置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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接