Section 3 · Lesson 2
Standard error and confidence intervals
Quantify the uncertainty of an estimate with the standard error, build confidence intervals for a mean and a proportion, and plan sample sizes.
A point estimate on its own (“variant B converts at 11.7%”) hides the most important question: how far off could that be? The standard error answers it, and a confidence interval turns it into a range you can put on a slide.
Standard deviation vs standard error
- The standard deviation describes individuals: how much one order differs from another.
- The standard error describes an estimate: how much the sample mean (or rate) would bounce around if you repeated the study. It is the SD of the sampling distribution.
SE of a mean = s / sqrt(n) # s = sample SD (ddof=1)
SE of a proportion = sqrt(p_hat * (1 - p_hat) / n)
Both shrink with sqrt(n), exactly as the CLT predicted.
Building a confidence interval
estimate ± critical value × SE
For 95% confidence with a normal sampling distribution, the critical value is
z* = norm.ppf(0.975) ≈ 1.96: it leaves 2.5% in each tail. The part after the ± is the
margin of error.
Means: use t. When you estimate a mean, you also estimate the SD from the same sample,
which adds uncertainty. The t distribution, with n - 1 degrees of freedom, accounts for
that by having fatter tails. Its critical value approaches 1.96 as n grows: about 2.26 at
n = 10, 2.05 at n = 30, and 1.963 at n = 768.
Proportions: the normal approximation (the “Wald” interval) works well when you have at
least about 10 successes and 10 failures. With tiny counts (3 conversions out of 40), use
the Wilson interval instead:
scipy.stats.binomtest(k, n).proportion_ci(method="wilson").
What “95% confident” means
If you repeated the experiment many times and built an interval each time, about 95% of those intervals would contain the true value. It does not mean 95% of the data falls in the interval, and strictly it isn’t “a 95% probability the truth is in this particular interval”. The truth is fixed; it’s the intervals that vary.
In Python
import numpy as np
import pandas as pd
from scipy import stats
t = pd.read_csv("ab_test.csv")["time_on_page_sec"]
n, mean = len(t), t.mean()
se = t.std() / np.sqrt(n) # same as stats.sem(t), which uses ddof=1
t_star = stats.t.ppf(0.975, df=n - 1)
(mean - t_star * se, mean + t_star * se)
stats.t.interval(0.95, df=n - 1, loc=mean, scale=se) # same interval in one call
Planning sample size
Flip the margin-of-error formula around to find how much data you need before you start:
n = z*² × p × (1 - p) / MOE²
Because MOE sits squared in the denominator, halving the margin quadruples the sample size.
In the real world
- Report intervals, not just point estimates: “B converts at 11.7% (95% CI 10.3% to 13.1%).” Decision makers react very differently to ±0.3 points than to ±3 points.
- Two variants’ intervals can overlap even when the difference between them is statistically significant. To compare variants, build an interval for the difference (next section) instead of eyeballing two separate intervals.
- Sample-size math tells you whether a test is even worth launching. If a campaign gets 500 visitors a week and you need 3,500 per variant, that’s a 14-week test.
Practice
Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.
Confidence interval for average order value
DoneUsing the amount of completed orders in orders.csv, build a 95% confidence interval for the mean order value with the t distribution:
n: the number of completed ordersse: the standard error, sample SD (ddof=1) / sqrt(n), rounded to 2 decimalsci_low,ci_high: mean ∓ t* × SE, wheret* = stats.t.ppf(0.975, df=n - 1),
each rounded to 2 decimals
Round only the final values (compute the bounds from the unrounded SE).
Need a hint?
amount.std()is the sample SD (ddof=1).scipy.stats.sem(amount)gives the standard error directly.stats.t.ppf(0.975, df=n - 1)is about 1.963 here; 0.975 leaves 2.5% in the upper tail.stats.t.interval(0.95, df=n - 1, loc=mean, scale=se)returns both bounds at once.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import numpy as np
import pandas as pd
from scipy import stats
orders = pd.read_csv("orders.csv")
amount = orders.loc[orders["status"] == "completed", "amount"]
n = len(amount)
se = None
ci_low = None
ci_high = None
print(n, se, ci_low, ci_high)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)Confidence interval for a conversion rate
DoneIn ab_test.csv, build a 95% confidence interval for variant B's conversion rate using the normal approximation:
p_hat: B's conversion ratese_p: sqrt(p_hat × (1 - p_hat) / n)ci_low,ci_high: p_hat ∓ z* × se_p withz* = stats.norm.ppf(0.975)
Round all four to 4 decimals at the end.
Need a hint?
- Filter to B first:
b = ab[ab["variant"] == "B"], thenn = len(b). stats.norm.ppf(0.975)≈ 1.96.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import numpy as np
import pandas as pd
from scipy import stats
ab = pd.read_csv("ab_test.csv")
b = ab[ab["variant"] == "B"]
p_hat = None
se_p = None
ci_low = None
ci_high = None
print(p_hat, se_p, ci_low, ci_high)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)How many users do we need?
DoneBefore the next test, your manager wants each variant's conversion rate (expected to be about 10%) estimated within a margin of error of ±1 percentage point at 95% confidence. Solve the margin-of-error formula for n:
n = z*² × p × (1 - p) / MOE²with z* = stats.norm.ppf(0.975), p = 0.10.
n_needed: users needed for MOE = 0.01, rounded up to a whole number withmath.ceiln_needed_half: users needed for MOE = 0.005 (half the margin), also rounded up
Need a hint?
- One percentage point is 0.01 when rates are written as fractions.
- Halving the margin of error multiplies the required sample size by 4.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import math
from scipy import stats
p = 0.10
z_star = stats.norm.ppf(0.975)
n_needed = None
n_needed_half = None
print(n_needed, n_needed_half)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)