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 |
Higher confidence → wider interval. Larger n → narrower interval.
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)
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?
- H0 - null hypothesis: no effect / no difference (μ = 30, the new design = the old design).
- H1 - alternative: there is an effect (μ ≠ 30, or μ > 30).
- Choose a significance level α (usually 0.05).
- Compute a test statistic (z or t) from the sample.
- Find the p-value: the probability of getting a result at least this extreme if H0 were true.
- 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#
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:
A: 4.00% B: 4.90% lift: 22.5% z = 2.18, p = 0.0291 Ship button B
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.