This is a worked-examples deep dive for Confidence intervals . The parent note built the formulas; here we hammer them against every kind of input a confidence-interval problem can throw at you — big samples, tiny samples, proportions, differences of proportions, differences of means, one-sided questions, and the degenerate edges where the formula almost breaks.
Intuition The one picture to keep in your head
A confidence interval is a net of half-width E (the "margin of error") centered on your sample estimate. Different problems only change three dials : how wide the net is per unit of spread (z or t ), how much raw spread there is (σ or s ), and how much the spread shrinks with sample size (n ).
net half-width E = how confident? ( z or t ) × standard error n spread
Every example below is just choosing values for those three dials. Prerequisites live in Central Limit Theorem and Standard Error .
Definition Two words we will use everywhere
Margin of error E = the half-width of the interval. The full interval is x ˉ − E to x ˉ + E , so its total length is 2 E . Never confuse the half-width with the full width.
Degrees of freedom (df ) = how many pieces of independent information are left to estimate spread after you have already spent some estimating the mean. For one sample of size n you spend 1 on x ˉ , leaving df = n − 1 .
Figure s01 (read this first): the black curve is the sampling distribution of the mean; the red bar is the confidence-interval "net" of half-width E thrown around the observed x ˉ (black dot). The true μ (black cross) is fixed — the net is what moves from sample to sample. Every example below just resizes this red bar.
Before working anything, let us enumerate every case class a CI problem can be. Rather than a dense grid, read the decision tree below — it is the same content in visual-first form, and each leaf names the example that fills it.
Figure s02 (the map of this whole page): follow the arrows to land on your case. The red leaf ("small n ⇒ use t ") is the one beginners most often get wrong. Each leaf is tagged with its example number.
Cell D - binomial SE - Ex 4
Cell D-edge - Wilson - Ex 4b
Cell E - add variances - Ex 5
Cell E2 - add variances - Ex 5b
Cell F - all alpha one tail - Ex 6
n equals 1 or zero spread
Cell G - degenerate - Ex 7
The dials for each cell:
Confidence dial: z 0.025 = 1.96 (95%), z 0.005 = 2.576 (99%), z 0.05 = 1.645 (90% two-sided or 95% one-sided).
Spread dial: σ if known, else s (sample std, with the n − 1 from Confidence intervals ).
Shrink dial: n .
Worked example Example 1 — sensor calibration
A temperature sensor is known from the factory to have measurement noise with standard deviation σ = 0. 5 ∘ C. You take n = 25 readings and get x ˉ = 21. 0 ∘ C. Build a 95% CI for the true temperature.
Forecast: With 25 readings and a small known noise, do you expect the interval to be roughly ± 0.02 , ± 0.2 , or ± 2 degrees? Guess before reading on.
Step 1 — pick the tool. σ is known , so use z , not t .
Why this step? The t -distribution only exists to pay for our uncertainty about σ . Here there is none, so z is exact.
Step 2 — standard error.
S E = n σ = 25 0.5 = 5 0.5 = 0.1
Why this step? S E is the spread of the sample mean , not of one reading. Averaging 25 values makes the mean 25 = 5 × tighter than a single reading.
Step 3 — margin of error.
E = z 0.025 ⋅ S E = 1.96 × 0.1 = 0.196
Why this step? z 0.025 = 1.96 is the point of the standard normal that leaves 2.5% in each tail (5% total = the α we allow to miss).
Step 4 — assemble. The interval is x ˉ minus E on the left and x ˉ plus E on the right:
CI 95% = 21.0 ± 0.196 = [ 21.0 − 0.196 , 21.0 + 0.196 ] = [ 20.804 , 21.196 ]
Why this step? E is the half-width ; we lay it off once to the left and once to the right of the point estimate, giving a total length 2 E = 0.392 .
Verify: Half-width 0.196 is between 0.02 and 2 — matches the middle forecast. Full length = 2 × 0.196 = 0.392 , and 21.196 − 20.804 = 0.392 ✓. Units are °C throughout ✓. Sanity: 1.96 SEs is a hair under 2 SEs, so ≈ 2 × 0.1 = 0.2 ✓.
Worked example Example 2 — model latency, big log
You log the inference latency of a model on n = 400 requests. Sample mean x ˉ = 52 ms, sample std s = 10 ms. 95% CI for the true mean latency?
Forecast: With 400 samples, will you use t or z ? Will the interval be wider or narrower than Example 1's ± 0.196 ?
Step 1 — tool choice. σ unknown, but n = 400 ≥ 30 , so s ≈ σ and we may use z .
Why this step? For large n , t 399 is visually indistinguishable from N ( 0 , 1 ) (heavier tails vanish). Using z costs almost nothing.
Step 2 — standard error.
S E = n s = 400 10 = 20 10 = 0.5 ms
Why this step? We swap the unknown σ for the sample std s and, exactly as in Example 1, divide by n to get the spread of the mean rather than of a single request — 400 requests make the mean 400 = 20 × tighter than one request.
Step 3 — margin & interval.
E = 1.96 × 0.5 = 0.98 , CI 95% = 52 ± 0.98 = [ 51.02 , 52.98 ] ms
Why this step? Same recipe as Cell A — we multiply the 95% critical value 1.96 by the standard error S E = 0.5 to turn "spread of the mean" into a half-width, then lay that half-width off to the left and right of x ˉ = 52 . Multiplying by 1.96 (not, say, 1) is what pins the coverage at exactly 95%; centering on x ˉ is what makes the interval an honest statement about the unknown μ .
Verify: t 0.025 , 399 ≈ 1.966 — replacing 1.96 by 1.966 changes E from 0.980 to 0.983 , a 0.3% difference. Negligible, confirming the "use z " decision ✓. Units ms ✓.
Worked example Example 3 — five A/B experiment lifts
A cautious team ran the same experiment n = 5 times, getting conversion lifts (in %): 2.0 , 3.5 , 1.0 , 4.0 , 2.5 . Build a 90% CI for the true lift.
Forecast: With only 5 points, will t be much bigger than z = 1.645 ? Guess whether the interval spans zero.
Step 1 — sample mean.
x ˉ = 5 2.0 + 3.5 + 1.0 + 4.0 + 2.5 = 5 13.0 = 2.6
Step 2 — sample std with Bessel's n − 1 . Deviations from 2.6: − 0.6 , 0.9 , − 1.6 , 1.4 , − 0.1 . Squares: 0.36 , 0.81 , 2.56 , 1.96 , 0.01 , summing to 5.70 .
s = 5 − 1 5.70 = 1.425 = 1.1937
Why n − 1 ? We already spent one degree of freedom (see the [!definition] above) pinning x ˉ ; dividing by df = 4 (not 5) un-biases s 2 . See Confidence intervals .
Step 3 — standard error.
S E = 5 s = 2.2361 1.1937 = 0.5338
Step 4 — critical value. With df = n − 1 = 4 and 90% two-sided ⇒ t 0.05 , 4 = 2.132 .
Why t and why so big? n < 30 and σ unknown. With only 4 degrees of freedom the tails are fat, so 2.132 ≫ 1.645 — the interval must be wider to stay honest.
Step 5 — interval.
E = 2.132 × 0.5338 = 1.138 , CI 90% = 2.6 ± 1.138 = [ 1.462 , 3.738 ]
Verify: Interval does not include 0 ⇒ lift is significant at 90%. Compare: had we wrongly used z = 1.645 , E = 0.878 , giving [ 1.72 , 3.48 ] — falsely narrower . The t correction widened it by ≈ 30% ✓.
Figure s03 (why small n demands t ): the black curve is the standard normal; the red curve is the t -distribution with df = 4 . Notice the red curve's fatter tails — that is why its 90% cutoff (red dashed, at 2.132) sits further out than the normal's (black dashed, at 1.96). Fatter tails = wider net = honest extra caution for tiny samples. This is the picture behind Example 3, Step 4.
Worked example Example 4 — classifier accuracy
A classifier is right on 87 of 100 test cases. 95% CI for true accuracy?
Forecast: Will the half-width be near ± 0.03 , ± 0.07 , or ± 0.15 ?
Step 1 — point estimate. p ^ = 87/100 = 0.87 .
Step 2 — proportion standard error.
S E = n p ^ ( 1 − p ^ ) = 100 0.87 × 0.13 = 0.0011310 = 0.033630
Why this SE? A "correct/incorrect" count is binomial with variance n p ( 1 − p ) ; a proportion divides by n twice, giving variance p ( 1 − p ) / n . See Standard Error .
Step 3 — interval.
E = 1.96 × 0.033630 = 0.065915 , CI 95% = 0.87 ± 0.0659 = [ 0.8041 , 0.9359 ]
Why this step? Identical recipe to the mean cases — the only thing that changed is which S E formula we fed in. The 1.96 (95%) and the "lay off half-width both sides" logic are unchanged.
Verify: Half-width 0.066 ≈ the ± 0.07 forecast ✓. Both endpoints lie in [ 0 , 1 ] (valid probabilities) ✓. Rule-of-thumb check n p = 87 ≥ 5 and n ( 1 − p ) = 13 ≥ 5 , so the normal approximation is legitimate ✓.
Worked example Example 4b — the extreme-proportion edge (Wilson / Jeffreys)
A model is right on all 10 of 10 test cases: p ^ = 10/10 = 1.0 . Build a 95% CI.
Forecast: The naïve formula gives S E = 1 ⋅ 0/10 = 0 , so it claims the interval is [ 1 , 1 ] — "we are certain accuracy is exactly 100%". Does 10-for-10 really prove perfection?
Step 1 — spot why the normal formula collapses. When p ^ = 0 or p ^ = 1 , p ^ ( 1 − p ^ ) = 0 , so S E = 0 and the interval degenerates to a point.
Why this step? The normal approximation needs n p ^ ≥ 5 and n ( 1 − p ^ ) ≥ 5 ; here n ( 1 − p ^ ) = 0 , so the approximation is simply invalid , not exact.
Step 2 — use the Wilson interval instead. The Wilson score interval never gives zero width at the boundary and always stays inside [ 0 , 1 ] :
center = 1 + n z 2 p ^ + 2 n z 2 , half-width = 1 + n z 2 z n p ^ ( 1 − p ^ ) + 4 n 2 z 2
With p ^ = 1 , n = 10 , z = 1.96 (so z 2 = 3.8416 ): center = 1 + 3.8416/10 1 + 3.8416/20 = 1.38416 1.19208 = 0.8613 , and half-width = 1.38416 1.96 0 + 400 3.8416 = 1.41603 × 0.098 = 0.1388 , giving [ 0.7224 , 0.9999 ] .
Why this step? Wilson adds an imaginary z 2 /2 successes and z 2 /2 failures, nudging the estimate off the boundary so uncertainty is honestly reported.
Verify: Wilson lower bound 0.7224 < 1 — it correctly refuses to claim certainty from 10 trials ✓; upper bound 0.9999 ≤ 1 (can't exceed 100%) ✓. (The Jeffreys interval, based on a Beta prior, behaves similarly at the edges — mention it as the Bayesian cousin, see Bayesian Credible Intervals .)
Worked example Example 5 — is Model B really better?
Model A: 82% correct, reported margin ± 3% (95%). Model B: 85% correct, margin ± 4% . Is B significantly better?
Forecast: Their intervals overlap. Does overlap alone prove "no difference"? (Trap!)
Step 1 — recover each SE from its margin. A 95% margin is 1.96 × S E , so S E = E /1.96 .
S E A = 1.96 0.03 = 0.015306 , S E B = 1.96 0.04 = 0.020408
Why this step? The problem hands us margins, not standard errors. Since a margin is built from the SE by multiplying by 1.96, we divide by 1.96 to run that construction backwards and recover the raw uncertainty of each model.
Step 2 — SE of the difference (add variances). For independent estimates, variances add :
S E diff = S E A 2 + S E B 2 = 0.01530 6 2 + 0.02040 8 2 = 0.00065598 = 0.025612
Why add variances? Uncertainty in a difference is the Pythagorean sum of the two independent uncertainties, never the plain sum. (This is a variance-sum SE for the difference — do not call it a "pooled SE," which is a different construction that assumes a common variance.) See A/B Testing .
Step 3 — CI of the difference. p ^ B − p ^ A = 0.85 − 0.82 = 0.03 .
E = 1.96 × 0.025612 = 0.050200 , CI 95% = 0.03 ± 0.0502 = [ − 0.0202 , 0.0802 ]
Why this step? Same universal recipe once more: point estimate (here the difference 0.03 ) ± (critical value × the difference's SE). We center on the difference because that is the quantity the question asks about.
Verify: The interval contains 0 , so the difference is not significant — despite B's point estimate being higher. Lesson: overlapping individual CIs is a weak test; the difference CI is the correct one, and here it confirms "not conclusive" ✓. Related: Hypothesis Testing .
Worked example Example 5b — did the new optimizer lower loss?
Old optimizer: n 1 = 36 runs, mean loss x ˉ 1 = 0.50 , sample std s 1 = 0.12 . New optimizer: n 2 = 36 runs, mean loss x ˉ 2 = 0.44 , sample std s 2 = 0.09 . 95% CI for the drop in mean loss μ 1 − μ 2 ?
Forecast: The point drop is 0.06 . Will the CI clear 0 (real improvement) or straddle it?
Step 1 — SE of each mean. Large n (≥ 30 ), σ unknown ⇒ Cell B logic, use z with s .
S E 1 = 36 0.12 = 0.02 , S E 2 = 36 0.09 = 0.015
Why this step? Each mean has its own standard error, computed exactly as in Example 2.
Step 2 — SE of the difference (add variances).
S E diff = S E 1 2 + S E 2 2 = 0.0 2 2 + 0.01 5 2 = 0.000625 = 0.025
Why the same rule as Ex 5? Two independent means differ with a Pythagorean-sum uncertainty, identical in spirit to the two-proportion case — the SE formula fed in just came from means, not proportions.
Step 3 — CI of the difference.
E = 1.96 × 0.025 = 0.049 , CI 95% = 0.06 ± 0.049 = [ 0.011 , 0.109 ]
Why this step? Center on the observed drop x ˉ 1 − x ˉ 2 = 0.06 ; lay off the half-width 0.049 both sides.
Verify: Interval [ 0.011 , 0.109 ] excludes 0 , so the loss drop is significant at 95% ✓ — unlike Example 5, this difference clears zero. Units are loss units throughout ✓.
Worked example Example 6 — a floor on accuracy
Same classifier as Ex 4 (p ^ = 0.87 , S E = 0.033630 , n = 100 ). Your deployment gate only asks: "Are we at least 95% confident accuracy exceeds some floor L ?" Find L .
Forecast: Should the one-sided critical value be larger or smaller than 1.96?
Step 1 — put all α in one tail. For a one-sided 95% lower bound, all 5% goes to the left tail, so the critical value is z 0.05 = 1.645 , not 1.96.
Why smaller? We only guard against being wrong on one side, so we can afford a less extreme cutoff — the one-sided bound is tighter than each side of a two-sided interval.
Step 2 — lower confidence bound.
L = p ^ − 1.645 × S E = 0.87 − 1.645 × 0.033630 = 0.87 − 0.055322 = 0.814678
Why this step? A one-sided lower bound only subtracts the half-width once (there is no upper cap — the interval runs from L up to 1). We use 1.645 because that is the cutoff leaving exactly the 5% we're willing to risk entirely below L .
Verify: L = 0.8147 > 0.8041 (the two-sided lower endpoint from Ex 4) — the one-sided floor is indeed higher/tighter, matching the "smaller critical value" forecast ✓. Statement: "We are 95% confident accuracy exceeds 81.5%" ✓.
Worked example Example 7 — the edges: n=1 and zero spread
Two broken inputs a formula must survive:
(a) You have a single observation and unknown σ .
(b) All observations are identical, e.g. three runs each scoring exactly 0.90 .
Forecast: What width should each interval have — zero, finite, or infinite?
Case (a): n = 1 , σ unknown.
s needs n − 1 = 0 in its denominator → undefined . Degrees of freedom df = 0 .
The t 0 distribution has no finite spread, so the honest CI is ( − ∞ , ∞ ) .
Why? One point tells you a location but nothing about spread. A single sample cannot estimate its own uncertainty.
Case (b): zero sample variance. x ˉ = 0.90 , and every deviation is 0, so s = 0 .
S E = n s = 3 0 = 0 ⇒ E = t × 0 = 0 , CI = [ 0.90 , 0.90 ]
Why a zero-width interval is a red flag, not a triumph: identical values usually mean the metric is quantized or capped (e.g. all runs hit an accuracy ceiling), so the s = 0 is an artifact. Prefer Bootstrap Methods here.
Verify (b): s = ( 0 + 0 + 0 ) /2 = 0 ; S E = 0 ; interval collapses to the point ✓. Verify (a): n − 1 = 0 makes s formula divide by zero ✓ — the degenerate case is real, not a rounding issue.
Worked example Example 8 — how many samples for ±2%?
You want a 95% CI on a metric with σ = 0.1 , and you demand margin E ≤ 0.02 . Minimum n ?
Forecast: Halving the margin costs how many times more samples — 2 × , 4 × , or 8 × ?
Step 1 — invert the margin formula. From E = z σ / n :
n = ( E z σ ) 2
Why square? Because S E ∝ 1/ n , shrinking the interval by a factor k needs k 2 times the data. Precision is expensive.
Step 2 — plug in.
n = ( 0.02 1.96 × 0.1 ) 2 = ( 0.02 0.196 ) 2 = 9. 8 2 = 96.04
Why this step? We substitute the demanded confidence (1.96), the assumed spread (0.1) and the target margin (0.02) into the inverted formula; the result is the exact real-valued sample count that hits E = 0.02 dead on.
Step 3 — round up. Always ceil : n = 97 .
Why round up, never down? n = 96 would give a margin slightly larger than 0.02, violating the requirement. Only rounding up guarantees E ≤ 0.02 .
Verify: At n = 97 , E = 1.96 × 0.1/ 97 = 0.019899 ≤ 0.02 ✓; at n = 96 , E = 0.020003 > 0.02 ✗ — so 97 is genuinely the minimum. Forecast answer: halving E quadruples n (the square) ✓.
Worked example Example 9 — from 95% to 99%, same data
Reusing Example 1 (S E = 0.1 ), what happens to the interval width when you demand 99% instead of 95% confidence?
Forecast: More confidence with the same data — wider or narrower? By roughly what ratio?
Step 1 — swap the critical value. z 0.025 = 1.96 → z 0.005 = 2.576 .
Why bigger? To catch μ 99 times out of 100 (instead of 95), the net must extend farther into the tails.
Step 2 — new margin & ratio.
E 99 = 2.576 × 0.1 = 0.2576 , E 95 E 99 = 0.196 0.2576 = 1.3143
Why this step? S E and n are untouched — only the confidence dial moved — so the width scales purely by the ratio of critical values, 2.576/1.96 . Nothing about the data changed; we simply demanded a bigger net.
Verify: The 99% interval is ≈ 31% wider than the 95% one with identical data ✓ — more confidence is bought with width, not with new information. This is the core trade-off behind Bayesian Credible Intervals and Cross-validation reporting.
Recall Which cell: 400 latency samples, σ unknown?
Cell B — σ unknown but n ≥ 30 , so s ≈ σ and we use z . ::: Use z because large n makes t n − 1 ≈ N ( 0 , 1 ) .
Recall Two 95% CIs overlap. Are the estimates significantly different?
Not necessarily — overlap is a weak test. Compute the CI of the difference (Ex 5). ::: If that interval excludes 0, they differ significantly.
Recall Your test accuracy is 10-for-10. Why not report [1.0, 1.0]?
Because p ^ = 1 makes the normal S E = 0 , an artifact. Use the Wilson interval, giving about [ 0.722 , 1.0 ] . ::: Extreme proportions break the normal approximation.
Recall To halve a margin of error, multiply
n by ___?
4 — because S E ∝ 1/ n , so precision costs the square of the improvement factor.
Mnemonic Choosing z vs t vs bootstrap vs Wilson
σ known → z. σ unknown & big n → z. σ unknown & small n → t. Extreme/tiny proportion → Wilson. Skewed/capped data → Bootstrap Methods .