207. Confidence Intervals

Range within which we can be confident that the true value (population parameter) lies, based on the sample data

𝐸[𝑋̄]=𝜇𝜎𝑋̄=𝜎𝑛

Both assume an i.i.d. sample from a normal population; the first is also valid for large 𝑛 by the CLT, and the second is approximately so. Note 𝑡𝛼/2,𝑛1𝑧𝛼/2 as 𝑛.

207.1. Where the confidence interval comes from

What we know. If 𝑋1,,𝑋𝑛 are i.i.d. draws from a population with mean 𝜇 and standard deviation 𝜎, then the sample mean 𝑋̄ has

𝐸[𝑋̄]=𝜇SE𝜎𝑋̄=𝜎𝑛

and its shape is normal — exactly if the population is normal, approximately by the CLT otherwise. We write SE (the standard error) for 𝜎/𝑛 from here on.

The question. How far can 𝑋̄ stray from 𝜇? Pick a distance 𝑏 and ask for the 𝑏 that makes the sample mean land within 𝑏 of the truth 90% of the time:

𝑃(𝜇𝑏𝑋̄𝜇+𝑏)=0.90

Note what is random here: 𝜇 is a fixed (unknown) number and 𝑋̄ is the quantity that varies from sample to sample.

207.1.1. Step 1 — Standardise

We cannot look up probabilities for 𝑋̄ directly, but we can for the standard normal 𝑍. So rewrite the event until the middle term is a 𝑍:

[𝑥̄𝑧𝑠𝑛,𝑥̄+𝑧𝑠𝑛]
Example
Code
from scipy.stats import t, norm
import numpy as np

xbar = 10
s = 5
n = 1000
conf = 0.90

df = n - 1
alpha = 1 - conf
p = 1 - alpha / 2
crit = t.ppf(p, df=df)
se = s / np.sqrt(n)
moe = crit * se

print(f"Degrees of Freedom:     {df}")
print(f"Alpha:                  {alpha:.2f}")
print(f"Cumulative Probability: {p:.3f}")
print(f"Critical Value (t):     {crit:.4f}")
print(f"Critical Value (z):     {norm.ppf(p):.4f}")
print(f"Standard Error:         {se:.4f}")
print(f"Margin of Error:        {moe:.4f}")

lower, upper = xbar - moe, xbar + moe
print(f"Confidence Interval:    [{lower:.2f}, {upper:.2f}]")
print(f"Check:                  {t.interval(conf, df, loc=xbar, scale=se)}")
Code
import numpy as np
from scipy import stats

x = np.array([12.1, 11.8, 13.4, 12.9, 12.2, 13.0, 11.5, 12.7])

stats.t.interval(0.95, len(x) - 1, loc=x.mean(), scale=stats.sem(x))
# (np.float64(12.005...), np.float64(13.019...))