Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, find confidence interval around median

How do I find confidence interval around median for my data in python?

Say I have array

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

I would like to find 80% confidence interval around median. How do I do it in python?

like image 666
Kasia Kowalska Avatar asked Dec 12 '16 13:12

Kasia Kowalska


People also ask

Can you calculate confidence interval for median?

Suppose we would like to find a 95% confidence interval for the population median. To do so, we need to first find j and k: j: nq – z√nq(1-q) = (15)(. 5) – 1.96√(15)(.

Which Python function will give the 95% confidence interval?

Confidence Intervals Using the t Distribution interval() function from the scipy. stats library to calculate a confidence interval for a population mean. What is this? The 95% confidence interval for the true population mean height is (16.758, 24.042).

Which Python function will give the 90% confidence interval?

In this example, we will be using the data set of size(n=20) and will be calculating the 90% confidence Intervals using the t Distribution using the t. interval() function and passing the alpha parameter to 0.90 in the python.


1 Answers

My implementation of this procedure to calculate the confidence interval around the median.

For your example, set cutoff=0.8. This requires python > 3 and pandas > 1.
It assumes that you pass the array as a 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)
like image 133
mac389 Avatar answered Oct 04 '22 06:10

mac389