Normal data, $\sigma$ known. If $n$ observations are randomly sampled from a normal population with unknown $\mu$ and known $\sigma,$ then a 95% confidence interval for $\mu$ is of the form $\bar X \pm 1.96\frac{\sigma}{\sqrt{n}},$ where $\bar X$ is the sample mean of the observations.
Example: Consider $n = 10$ random observations from $\mathsf{Norm}(100, 15),$ as simulated in R and put into the vector x
below. Suppose you know $\sigma=15,$ and estimate $\mu$ by the sample mean $A = \bar X = 98.44.$
set.seed(2020) # for reprodudibility
x = rnorm(10, 100, 15)
summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
58.05 86.79 103.14 98.44 109.52 126.39
Then a 95% CI for $\mu$ is $(89.14, 107.74)$. This happens to be one of the 95% of cases in which the CI 'covers' (contails) the population mean $\mu = 100.$
a = mean(x)
CI = a + c(-1.96, 1.96)*15/sqrt(10); CI
[1] 89.14346 107.73765
Normal data, $\sigma$ estimated by $S.$ If $n$ observations are randomly sampled from a normal population with unknown $\mu$ and unknown $\sigma,$ then a 95% confidence interval for $\mu$ is of the form $\bar X \pm t^*\frac{S}{\sqrt{n}},$ where $\bar X$ and $S$ are, respectively, the sample mean and sample stardard deviation, and $t^*$ cuts probability 0.25 from the upper tail of Student's t distribution with $n-1$ degrees of freedom.
Example: Use the same data as above, but now pretend you don't know $\sigma.$
We estimate $\sigma$ by the sample standard deviation $S = 19.36$ and (from R or a printed table of t distributions) $t^* = 2.262.$ Then the expression above gives
the 95% CI $(84.59, 112.29).$ [I have used the R function t.test
, which prints out the CI.]
s = sd(x); s
[1] 19.3592
qt(.975, 9)
[1] 2.262157
t.test(x)$conf.int
[1] 84.59182 112.28929
attr(,"conf.level")
[1] 0.95
Nonnormal data. If data are not normal or if you are trying something other than the population mean $\mu,$ then different styles of confidence intervals may be appropriate. If the sample is large enough that $\bar X$ is nearly normal then one of the confidence intervals above may be a useful approximation.
Example: Consider $n=500$ observations from an exponential distribution with rate
$\lambda = .01$ and $\mu = \sigma = 100.$ The exponential distribution is severely right-skewed and far from normal. But the average of a large exponential sample is roughly normal.
The 95% t confidence interval provides as approximate 95% CI $(93.07, 111.46)$ in this case.
However, it is best to use the exact 95% CI
$(93.86, 111.86)$
for $\sigma,$ based on a gamma distribution. Because the gamma distribution is not symmetrical, the exact CI is not centered exactly at $\bar Y = 103.17.$ (In this example, both the approximate CI and the
exact CI happen to include the true value $\mu = 100.)$
set.seed(411)
y = rexp(500, .01)
summary(y); a = mean(y)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.2361 32.3471 73.6159 103.1729 143.8860 596.2742
[1] 97.17857
t.test(y)$conf.int
[1] 93.07353 111.45858
attr(,"conf.level")
[1] 0.95
a/qgamma(c(.975,.025), 500, 500)
[1] 93.86247 111.85700