python,找到中值附近的置信区间

python,找到中值附近的置信区间,python,intervals,median,Python,Intervals,Median,如何在python中找到数据中位数周围的置信区间 假设我有一个数组 a = np.array([24, 38, 61, 22, 16, 57, 31, 29, 35]) 我想找出中位数周围80%的置信区间。如何在python中实现它?我的实现用于计算中值周围的置信区间 例如,设置cutoff=0.8。 这需要python>3和pandas>1 它假定您将数组作为pd.Series传递 import statistics, math import pandas as pd def media

如何在python中找到数据中位数周围的置信区间

假设我有一个数组

a = np.array([24, 38, 61, 22, 16, 57, 31, 29, 35])
我想找出中位数周围80%的置信区间。如何在python中实现它?

我的实现用于计算中值周围的置信区间

例如,设置
cutoff=0.8
。 这需要
python>3
pandas>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)
您可以使用引导: