Confidence Interval Calculator
Calculate confidence intervals for means and proportions.
Lower Bound
46.2718
Upper Bound
53.7282
Margin of Error
3.7282
Critical Value
2.0420
We are 95% confident that the true mean lies between 46.2718 and 53.7282.
Using t(29)-distribution · SE = 1.8257
Frequently Asked Questions
Code Implementation
import math
import scipy.stats as stats
import numpy as np
# --- Manual confidence interval for mean (known sigma) ---
def ci_mean_known_sigma(mean, sigma, n, confidence=0.95):
z = stats.norm.ppf((1 + confidence) / 2)
margin = z * (sigma / math.sqrt(n))
return mean - margin, mean + margin
lower, upper = ci_mean_known_sigma(50, 10, 100, 0.95)
print(f"95% CI: [{lower:.4f}, {upper:.4f}]")
# 95% CI: [48.0400, 51.9600]
# --- t-distribution CI (unknown sigma, from sample) ---
data = [52, 48, 55, 47, 50, 53, 49, 51, 46, 54]
mean = np.mean(data)
sem = stats.sem(data) # standard error of mean
ci = stats.t.interval(0.95, df=len(data)-1, loc=mean, scale=sem)
print(f"Mean: {mean:.2f}, 95% CI: [{ci[0]:.4f}, {ci[1]:.4f}]")
# --- Proportion confidence interval (Wilson score) ---
def ci_proportion(successes, n, confidence=0.95):
p = successes / n
z = stats.norm.ppf((1 + confidence) / 2)
center = (p + z**2/(2*n)) / (1 + z**2/n)
spread = z * math.sqrt(p*(1-p)/n + z**2/(4*n**2)) / (1 + z**2/n)
return center - spread, center + spread
lo, hi = ci_proportion(45, 100, 0.95)
print(f"Proportion CI: [{lo:.4f}, {hi:.4f}]")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.