1.3.21Probability & Statistics

Confidence intervals

2,688 words12 min readdifficulty · medium6 backlinks

What Problem Does This Solve?

Point estimates lie. You calculate sample mean xˉ=170\bar{x} = 170 cm, but that's just one realization from a random process. The confidence interval quantifies how much we should trust this estimate.

Why do we need this in ML?

  • Reporting model accuracy: "Test accuracy is 87% ± 2%"
  • A/B testing: "Treatment increased conversion by 3.2% [2.1%, 4.3%]"
  • Hyperparameter tuning: quantify uncertainty in cross-validation scores

Derivation from First Principles

Step 1: The Sampling Distribution

Suppose we have a population with true mean μ\mu (unknown) and variance σ2\sigma^2. We take a sample of size nn, compute sample mean:

Xˉ=1ni=1nXi\bar{X} = \frac{1}{n} \sum_{i=1}^{n} X_i

Why this step? Xˉ\bar{X} is our estimator for μ\mu. But Xˉ\bar{X} itself is a random variable because different samples give different values.

By the Central Limit Theorem, for large nn:

XˉN(μ,σ2n)\bar{X} \sim N\left(\mu, \frac{\sigma^2}{n}\right)

Why? Each XiX_i has mean μ\mu and variance σ2\sigma^2. The sum has variance nσ2n\sigma^2 (independent samples). Dividing by nn gives variance σ2/n\sigma^2/n. The CLT says the distribution becomes normal.

Step 2: Standardize to Control Probability

We want to find an interval [Xˉmargin,Xˉ+margin][\bar{X} - \text{margin}, \bar{X} + \text{margin}] that captures μ\mu with probability 1α1-\alpha (e.g., 95%).

Standardize Xˉ\bar{X}:

Z=Xˉμσ/nN(0,1)Z = \frac{\bar{X} - \mu}{\sigma/\sqrt{n}} \sim N(0, 1)

Why this step? Standardizing converts our problem to the standard normal, where we know quantiles exactly.

For a (1α)(1-\alpha) confidence level, we want:

P(zα/2Xˉμσ/nzα/2)=1αP\left(-z_{\alpha/2} \leq \frac{\bar{X} - \mu}{\sigma/\sqrt{n}} \leq z_{\alpha/2}\right) = 1 - \alpha

where zα/2z_{\alpha/2} is the critical value (e.g., z0.025=1.96z_{0.025} = 1.96 for 95%).

Why α/2\alpha/2? We split the tail probability equally on both sides (two-tailed test).

Step 3: Rearange to Get the Interval

Multiply through by σ/n\sigma/\sqrt{n}:

P(zα/2σnXˉμzα/2σn)=1αP\left(-z_{\alpha/2} \cdot \frac{\sigma}{\sqrt{n}} \leq \bar{X} - \mu \leq z_{\alpha/2} \cdot \frac{\sigma}{\sqrt{n}}\right) = 1 - \alpha

Subtract Xˉ\bar{X} from all parts:

P(Xˉzα/2σnμXˉ+zα/2σn)=1αP\left(-\bar{X} - z_{\alpha/2} \cdot \frac{\sigma}{\sqrt{n}} \leq -\mu \leq -\bar{X} + z_{\alpha/2} \cdot \frac{\sigma}{\sqrt{n}}\right) = 1 - \alpha

Multiply by -1 (flips inequalities):

P(Xˉzα/2σnμXˉ+zα/2σn)=1αP\left(\bar{X} - z_{\alpha/2} \cdot \frac{\sigma}{\sqrt{n}} \leq \mu \leq \bar{X} + z_{\alpha/2} \cdot \frac{\sigma}{\sqrt{n}}\right) = 1 - \alpha

Why this step? We've isolated μ\mu in the middle. Now the interval depends on observed xˉ\bar{x} (lowercase = realized value).

Step 4: When σ is Unknown (The t-Distribution)

In practice, we don't know σ\sigma. Replace with sample standard deviation ss:

s=1n1i=1n(XiXˉ)2s = \sqrt{\frac{1}{n-1} \sum_{i=1}^{n} (X_i - \bar{X})^2}

Why n1n-1? Bessel's correction makes s2s^2 an unbiased estimator of σ2\sigma^2 (we "used up" one degree of freedom estimating μ\mu).

Now the standardized statistic follows a t-distribution with df=n1df = n-1 degrees of freedom:

T=Xˉμs/ntn1T = \frac{\bar{X} - \mu}{s/\sqrt{n}} \sim t_{n-1}

Why not normal? Because ss is random too. The t-distribution has heavier tails to account for this extra uncertainty. As nn \to \infty, tn1N(0,1)t_{n-1} \to N(0,1).


Worked Examples


Common Mistakes (Steel-man Your Errors)


How to Use This in Practice

Sample Size Planning

Rearange the margin of error formula to find required nn:

E=zα/2σn    n=(zα/2σE)2E = z_{\alpha/2} \cdot \frac{\sigma}{\sqrt{n}} \implies n = \left(\frac{z_{\alpha/2} \cdot \sigma}{E}\right)^2

Why this matters: To get margin of error ±2% with 95% confidence, assuming σ=0.1\sigma = 0.1:

n=(1.960.10.02)2=96.0497n = \left(\frac{1.96 \cdot 0.1}{0.02}\right)^2 = 96.04 \approx 97

80/20 insight: Doubling precision (halving EE) requires 4× the sample size because of the square.

One-sided vs Two-sided

  • Two-sided: μ\mu could be higher or lower (most common)
  • One-sided: only care if μ>\mu > some value (e.g., "is accuracy above 80%?")

For one-sided 95% CI: use z0.05=1.645z_{0.05} = 1.645 instead of z0.025=1.96z_{0.025} = 1.96.


Recall Explain to a 12-year-old

Imagine you want to know the average height of all students in your school, but you can only measure 30 kids.

You measure them and get 155 cm. But you know if you measured a different 30 kids, you'd get maybe 153 cm or 157 cm. It keeps changing!

A confidence interval is like saying: "I'm pretty sure (95% sure) the real average of the whole school is somewhere between 152 cm and 158 cm."

It's NOT saying "there's a 95% chance the real average is in this range" – the real average is one fixed number, we just don't know it. It's saying "my method of making these ranges is good enough that if I used it 100 times, about 95 of my ranges would catch the real answer."

The more kids you measure, the narrower your range gets, because you're more certain.


Connections Central Limit Theorem – why Xˉ\bar{X} is approximately normal

  • Standard Error – why we divide by n\sqrt{n}
  • Hypothesis Testing – CIs and p-values are two sides of the same coin
  • Bootstrap Methods – non-parametric alternative when assumptions fail
  • Bayesian Credible Intervals – the "95% chance" interpretation you wished you could use
  • A/B Testing – confidence intervals for treatment effects
  • Cross-validation – uncertainty in model performance estimates

Active Recall Flashcards

#flashcards/ai-ml

What is a confidence interval?
An interval estimate that, if we repeated the sampling procedure many times, would contain the true population parameter in (1α)100%(1-\alpha) \cdot 100\% of cases.
Why is "95% chance the true value is in the interval" wrong?
The true value is fixed (not random). The interval is random. Correct interpretation is about the long-run frequency of the procedure capturing the true value.
Formula for95% CI when σ is known?
xˉ±1.96σn\bar{x} \pm 1.96 \cdot \frac{\sigma}{\sqrt{n}}
Why use t-distribution instead of z?
When population standard deviation σ\sigma is unknown and we use sample standard deviation ss, the extra uncertainty requires the heavier-tailed t-distribution, especially for small samples (n<30n < 30).
What happens to margin of error if you double the sample size?
It decreases by a factor of 21.41\sqrt{2} \approx 1.41, because the margin of error is proportional to 1/n1/\sqrt{n} — so multiplying nn by 2 divides the margin by 2\sqrt{2}.
How do you find required sample size for a given margin of error E?
n=(zα/2σE)2n = \left(\frac{z_{\alpha/2} \cdot \sigma}{E}\right)^2
Why do confidence intervals get wider for higher confidence levels?
Higher confidence (e.g., 99% vs 95%) requires larger critical values (z0.005=2.576z_{0.005} = 2.576 vs z0.025=1.96z_{0.025} = 1.96), which increases the margin of error.
What are the key assumptions for a valid confidence interval?
1) Random sampling (IID observations), 2) Approximate normality of the sampling distribution (or large nn for CLT), 3) No extreme outliers or violations of the model.
If two95% CIs overlap, what can you conclude?
You cannot conclude the parameters are equal. You need to compute the confidence interval for the difference to test for significance.
What is Bessel's correction and why use n-1?
Using n1n-1 instead of nn in sample variance makes s2s^2 an unbiased estimator of σ2\sigma^2, accounting for one degree of freedom lost in estimating the mean.

Concept Map

is one realization of

needs

solved by

gives

standardized to

use critical value

controls tail

rearrange for mu

width set by

means

applied in ML

Point estimate x-bar

Random variable

Uncertainty quantification

Confidence interval

Central Limit Theorem

Sampling distribution N mu sigma2 over n

Standard normal Z

z alpha over 2 e.g. 1.96

Split alpha over 2 two-tailed

Margin z times sigma over root n

95 pct of intervals capture true mu

Accuracy A B testing CV scores

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, confidence interval ka core idea ye samajhna zaroori hai. Jab tum kisi population ka average nikalna chahte ho, jaise ki 100 logon ki average height, toh tumhe ek single number milta hai jaise 170 cm. Lekin problem ye hai ki agar tum dobara koi aur 100 log sample karo, toh ye number thoda change ho jayega. Matlab tumhara point estimate (170 cm) hamesha thoda "jhoot" bolta hai, kyunki wo bas ek random sample ka result hai. Confidence interval isko fix karta hai — wo tumhe ek range deta hai, jaise 167 se 173 cm, aur kehta hai "main 95% confident hoon ki asli population average is range mein hai." Ek important baat: iska matlab ye NAHI hai ki true value ke 95% chance hai is range mein hone ke — true value toh fixed hai. Iska matlab ye hai ki agar tum ye poora sampling procedure 100 baar repeat karo, toh lagbhag 95 baar tumhara interval sahi value ko capture kar lega.

Ab thodi si maths samajh lo. Central Limit Theorem kehta hai ki sample mean Xˉ\bar{X} approximately normal distribution follow karta hai, jiska mean μ\mu hai aur variance σ2/n\sigma^2/n. Isko standardize karke hum Z-score bana lete hain, aur phir jo critical value hoti hai (jaise 95% ke liye 1.96), usse multiply karke margin of error nikalte hain. Formula simple hai: xˉ±zα/2σ/n\bar{x} \pm z_{\alpha/2} \cdot \sigma/\sqrt{n}. Notice karo ki jitna bada nn hoga, utna chota interval hoga — matlab zyada data se zyada confidence. Aur jab humein population ki actual σ\sigma nahi pata (jo real life mein zyadatar hota hai), tab hum sample se estimate kiya hua ss use karte hain, aur normal ki jagah t-distribution use karte hain jiske tails thode heavy hote hain — kyunki ss khud bhi ek random quantity hai, toh extra uncertainty ka dhyan rakhna padta hai.

Ye cheez ML mein kyun matter karti hai? Kyunki jab tum kisi model ki accuracy report karte ho jaise "87%", toh wo bhi bas ek estimate hai. Behtar hai kehna "87% ± 2%" — isse pata chalta hai ki tumhara result kitna reliable hai. A/B testing mein bhi, agar tum bolo "conversion 3.2% badh gaya", toh confidence interval [2.1%, 4.3%] batata hai ki ye result genuine hai ya bas random noise. Aur hyperparameter tuning mein cross-validation scores ki uncertainty samajhne ke liye bhi yahi concept kaam aata hai. Basically, confidence interval tumhe honest banata hai — bas ek number pe blindly bharosa karne ke bajaye tum uncertainty ko bhi acknowledge karte ho, jo ki proper data science ka foundation hai.

Go deeper — visual, from zero

Test yourself — Probability & Statistics

Connections