Normal mean confidence interval with known variance (source code)

= Normal mean confidence interval with known variance
{title2=$\bar X\pm z_{1-\alpha/2}\sigma/\sqrt n$}

Suppose $X_1,\ldots,X_n$ are <independent and identically distributed> with <normal distribution> $N(\mu,\sigma^2)$, where the <mean> $\mu$ is unknown and the <standard deviation> $\sigma>0$ is known. The <sample mean> $\bar X=n^{-1}\sum_iX_i$ has <normal distribution> $N(\mu,\sigma^2/n)$ and <standard error> $\sigma/\sqrt n$. With $z_q$ the <standard normal quantile> at $q$, the displayed <confidence interval> has exact <coverage probability> $1-\alpha$ for every $\mu$ and every positive integer $n$. No <central limit theorem> approximation is needed.

Take $\sigma=2$, $n=16$ and nominal confidence 95%. The <standard error> is $2/\sqrt{16}=0.5$, and $z_{0.975}=1.9599639845\ldots$. Therefore
$$
C(X)=[\bar X-0.9799819923\ldots,\ \bar X+0.9799819923\ldots].
$$
Rounded for reporting, this is $\bar x\pm0.98$. If $\bar x=1.2$, the realized <confidence interval> is approximately $[0.22,2.18]$. Exact 95% calibration refers to the unrounded <standard normal quantile>; using $1.96$ literally gives <coverage probability> $0.9500042097\ldots$, almost but not exactly 0.95.

The <confidence interval>'s width scales as $n^{-1/2}$: increasing <sample size> from 16 to 64 halves its half-width to $0.4899909961\ldots$. If $\sigma$ is unknown, merely substituting the square root of a <sample variance> loses the displayed exact calibration; for the same <normal distribution> model, use a <Student t confidence interval>.

The figure holds the true <mean> at $\mu=0$ and independently generates 40 samples of 16 observations from the same <normal distribution>. NumPy's PCG64 generator with seed 42 gives 39 covering <confidence intervals> and one miss, at sample 32: observed coverage is $39/40=97.5\%$. Every bar uses the unrounded <standard normal quantile>. This finite collection illustrates the moving endpoints; it does not establish the analytic <coverage probability> or require exactly two misses.

\Image[/confidence-interval-coverage.png]
{title=Forty normal-mean confidence intervals and their exact 95% coverage event}

The beauty of the construction is that a familiar <standard normal distribution> calibrates every possible unknown <mean> at once. For a short proof, the <pivotal quantity>
$$
Z=\frac{\bar X-\mu}{\sigma/\sqrt n}\sim N(0,1)
$$
has a <probability distribution> independent of $\mu$. The event $|Z|\leq z_{1-\alpha/2}$ has <probability> $1-\alpha$; rearranging it gives $\mu\in C(X)$. The right panel shows the equivalent event $|\bar X|\leq0.9799819923\ldots$ when $\mu=0$.