【问题标题】:python, find confidence interval around medianpython,找到中位数周围的置信区间
【发布时间】:2017-04-27 09:32:26
【问题描述】:

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

假设我有数组

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

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

【问题讨论】:

标签: python intervals median


【解决方案1】:

我实现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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2020-10-06
    • 2018-07-13
    • 1970-01-01
    • 2019-04-30
    • 2015-09-15
    • 1970-01-01
    相关资源
    最近更新 更多