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.

40 min 3 practice tasks
Confidence Intervals: Crash Course Statistics #20 by CrashCourse on YouTube

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.

1

Confidence interval for average order value

Using 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 orders
  • se: the standard error, sample SD (ddof=1) / sqrt(n), rounded to 2 decimals
  • ci_low, ci_high: mean ∓ t* × SE, where t* = 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?
  1. amount.std() is the sample SD (ddof=1). scipy.stats.sem(amount) gives the standard error directly.
  2. stats.t.ppf(0.975, df=n - 1) is about 1.963 here; 0.975 leaves 2.5% in the upper tail.
  3. stats.t.interval(0.95, df=n - 1, loc=mean, scale=se) returns both bounds at once.
Python 3
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)
user_id, variant, device, time_on_page_sec, converted, revenue
daily_stats.csv (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
orders.csv (900 rows)
id, customer_id, campaign_id, order_date, amount, status
campaigns.csv (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
2

Confidence interval for a conversion rate

In ab_test.csv, build a 95% confidence interval for variant B's conversion rate using the normal approximation:

  • p_hat: B's conversion rate
  • se_p: sqrt(p_hat × (1 - p_hat) / n)
  • ci_low, ci_high: p_hat ∓ z* × se_p with z* = stats.norm.ppf(0.975)

Round all four to 4 decimals at the end.

Need a hint?
  1. Filter to B first: b = ab[ab["variant"] == "B"], then n = len(b).
  2. stats.norm.ppf(0.975) ≈ 1.96.
Python 3
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)
user_id, variant, device, time_on_page_sec, converted, revenue
daily_stats.csv (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
orders.csv (900 rows)
id, customer_id, campaign_id, order_date, amount, status
campaigns.csv (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
3

How many users do we need?

Before 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 with math.ceil
  • n_needed_half: users needed for MOE = 0.005 (half the margin), also rounded up
Need a hint?
  1. One percentage point is 0.01 when rates are written as fractions.
  2. Halving the margin of error multiplies the required sample size by 4.
Python 3
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)
user_id, variant, device, time_on_page_sec, converted, revenue
daily_stats.csv (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
orders.csv (900 rows)
id, customer_id, campaign_id, order_date, amount, status
campaigns.csv (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status