蟒蛇,求中位数的置信区间。

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

如何在python中找到数据的置信区间?

假设我有一个数组

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

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

python intervals median
1个回答
0
投票

我的实现是 此程序 来计算中位数周围的置信区间。

在您的例子中,设置 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)
© www.soinside.com 2019 - 2024. All rights reserved.