Python Central HubSign in

Data Analytics with Python / Phase 8: Statistics for Data Analytics

Confidence Intervals (CI)

Build and interpret confidence intervals for means and proportions (with practical examples).

What a confidence interval means

A 95% confidence interval is not:

  • “There is a 95% probability the true mean is in this interval.”

It is:

  • “If we repeat this sampling process many times, ~95% of such intervals will contain the true mean.”

CI for a mean (using t-distribution)

Use when:

  • Population std is unknown (common)
  • Sample size is small-ish
Python
import numpy as np
from scipy import stats
 
x = np.array([12, 13, 10, 9, 11, 12, 14, 8, 13, 11])
 
n = len(x)
mean = x.mean()
se = x.std(ddof=1) / np.sqrt(n)
 
alpha = 0.05
 
t_crit = stats.t.ppf(1 - alpha/2, df=n-1)
ci = (mean - t_crit * se, mean + t_crit * se)
 
print("mean:", mean)
print("95% CI:", ci)

CI for a proportion

Example: conversion rate.

Python
import numpy as np
 
# 60 conversions out of 1000
x = 60
n = 1000
p_hat = x / n
 
alpha = 0.05
z = 1.96
 
se = np.sqrt(p_hat * (1 - p_hat) / n)
ci = (p_hat - z * se, p_hat + z * se)
 
print("p_hat:", p_hat)
print("95% CI:", ci)

Practical notes

  • CI width depends on sample size: larger n → tighter CI.
  • CI helps decision-making: overlap/non-overlap gives a quick signal (not a full test).

Building a CI, step by step

diagram How a confidence interval is built mermaid
Starting from the sample mean and standard error, a critical value stretches the interval to a chosen confidence level.

Visualize it

Widening the sample size shrinks the confidence interval around the mean. Watch the amber band tighten around the estimate as n grows — the true mean (dashed line) stays fixed, only your certainty about it improves:

sketch Confidence interval shrinks as n grows p5.js
A wider sample size produces a narrower band around the estimated mean.

Check your understanding

quizCheck yourself
  1. What is the correct reading of a 95% confidence interval?

    Show answer

    B — If you repeat the sampling many times, about 95% of such intervals contain the true mean — The true mean is fixed and the interval is the random part, so the 95% describes the procedure.

  2. Why does the lesson use stats.t.ppf for the mean CI?

    Show answer

    A — Population std is unknown and the sample is small-ish — The t-distribution with n - 1 degrees of freedom is used when the population std is unknown.

  3. For 60 conversions out of 1000, what is p_hat and roughly what is the 95% CI?

    Show answer

    B — 0.06, from about 0.045 to 0.075 — p_hat = 0.06 and se = sqrt(0.06 times 0.94 / 1000) is about 0.0075, so 0.06 plus or minus 1.96 times that.

  4. What happens to CI width when the sample size n grows?

    Show answer

    C — It tightens — The standard error shrinks with the square root of n, so larger samples give tighter intervals.

🧪 Try It Yourself

Exercise 1 – 95% CI for a mean

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – CI for a conversion rate

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Sample size shrinks the interval

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Next

Continue to Hypothesis Testing to turn "is the true value in this range" into a formal yes/no decision with p-values.

Was this page helpful?

Let us know how we did