⚡0 XP🔥0

Confidence Intervals and Hypothesis Testing

Lesson 4 of 5 ⏱ 5 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Python

Point and interval estimates, 95% confidence intervals for means and proportions, null and alternative hypotheses, z-tests and t-tests, p-values, significance level, Type I and II errors, and an A/B test in Python.

From a sample to the population#

A sample mean x̄ is a point estimate of μ - a single best guess. But a different sample would give a slightly different number. A confidence interval gives a range that shows how precise the estimate is.

Confidence interval for a mean#

For a large sample (or known σ):

x̄ ± z* × σ/√n

Confidence z*
90% 1.645
95% 1.96
99% 2.576
▶ Try it YourselfPython
Ctrl + Enter to run

Higher confidence → wider interval. Larger n → narrower interval.

Common Mistake

A 95% CI does not mean "95% of customers spend in this range". It is about the average bill, not individual bills.

For small samples: the t distribution#

When σ is unknown and n is small, use t* from the t distribution with n − 1 degrees of freedom instead of z*. It's a bit larger, making the interval wider:

df (n − 1) 4 9 19 29 99 ∞
t* for 95% 2.776 2.262 2.093 2.045 1.984 1.960

Confidence interval for a proportion#

p̂ ± z* × √(p̂(1 − p̂)/n)

▶ Try it YourselfPython
Ctrl + Enter to run
Output
45.0% ± 2.8%  ->  42.2% to 47.8%

That "± 2.8%" is the margin of error you see in news opinion polls.

Hypothesis testing#

A hypothesis test asks: is the pattern in my sample real, or could it be just chance?

  1. H0 - null hypothesis: no effect / no difference (μ = 30, the new design = the old design).
  2. H1 - alternative: there is an effect (μ ≠ 30, or μ > 30).
  3. Choose a significance level α (usually 0.05).
  4. Compute a test statistic (z or t) from the sample.
  5. Find the p-value: the probability of getting a result at least this extreme if H0 were true.
  6. If p < α, reject H0 ("statistically significant"); otherwise fail to reject H0.
हिंदी में समझें

Hypothesis testing अदालत जैसा है। H0 = "आरोपी निर्दोष है" - जब तक पक्के सबूत न मिलें, हम इसे मानते हैं। p-value बताता है कि अगर H0 सच होता तो ऐसा सबूत मिलना कितना मुश्किल था। अगर p-value बहुत छोटा (0.05 से कम) है, तो हम H0 को खारिज करते हैं। ध्यान दें - "खारिज न कर पाना" का मतलब "निर्दोष साबित" होना नहीं है।

Errors#

H0 is true H0 is false
reject H0 Type I error (false alarm), probability α correct (power)
don't reject H0 correct Type II error (missed effect), probability β

z-test and t-test#

▶ Try it YourselfPython
Ctrl + Enter to run

In real projects, SciPy does the calculation and gives the exact p-value:

from scipy import stats
stats.ttest_1samp(new, popmean=62)            # one sample vs a known value
stats.ttest_ind(group_a, group_b, equal_var=False)   # two independent groups (Welch's t-test)
stats.ttest_rel(before, after)                # paired: same students before/after

A/B testing - two proportions#

An online store tests two "Buy now" buttons:

▶ Try it YourselfPython
Ctrl + Enter to run
Output
A: 4.00%  B: 4.90%  lift: 22.5%
z = 2.18, p = 0.0291
Ship button B
Tip

Statistically significant is not the same as important. With millions of users, a 0.01% difference can be "significant" but worthless. Always report the effect size (the lift) and a confidence interval, not only the p-value.

Summary#

  • CI for a mean: x̄ ± z*·s/√n (use t* for small samples); for a proportion: p̂ ± z*·√(p̂(1−p̂)/n).
  • Tests: state H0/H1, pick α, compute z or t, get the p-value, reject H0 if p < α.
  • Type I = false alarm (α); Type II = missed effect (β).
  • A/B tests compare two proportions; report the lift as well as the p-value.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Juice bottle confidence interval

A machine fills juice bottles. A sample of n = 64 bottles has mean 480 ml and standard deviation 16 ml. Compute the 95% confidence interval for the mean fill using z = 1.96, store the ends in low and high, and print:

95% CI: 476.08 to 483.92

Expected output:
95% CI: 476.08 to 483.92
2

Is delivery slower than promised?

An app promises an average delivery time of 30 minutes (H0: μ = 30). A sample of n = 49 deliveries has mean 32.1 minutes; the known SD is 7. Run a two-sided z-test: compute z, the p-value p = 2 × (1 − Φ(|z|)) with Φ(z) = 0.5(1 + erf(z/√2)), and decide at α = 0.05. Print:

z = 2.10
p = 0.0357
Reject H0
Expected output:
z = 2.10
p = 0.0357
Reject H0

Quick Quiz

Q1.

What does a 95% confidence interval of (476, 484) ml mean?

Q2.

Which change makes a confidence interval narrower?

Q3.

A test gives p = 0.03 with significance level α = 0.05. The conclusion is...

Q4.

A Type I error is...

Q5.

When should you use a t-test instead of a z-test for a mean?