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?
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)(.
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).
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With