Code
n df X90. X95. X99.
1 Inf Inf 1.645 1.960 2.576
2 5 4 2.132 2.776 4.604
3 10 9 1.833 2.262 3.250
4 20 19 1.729 2.093 2.861
5 50 49 1.677 2.010 2.680
6 100 99 1.660 1.984 2.626

Zhaoxia Yu
Suppose we want to know something about a large population. Examples:
Note
The quantity we want to know is called a population parameter.
Unfortunately, we usually do not have data on the entire population.
Instead of measuring everyone,
we collect a sample.
Use the sample to estimate the population parameter.
Because different samples produce different estimates,
uncertainty is unavoidable.
Suppose \(X_1,\ldots,X_n \overset{iid}{\sim} N(\mu,\sigma^2),\) where
Goal
Estimate the unknown population mean \(\mu\).
A natural estimate of the population mean is the sample mean
\[ \bar X=\frac1n\sum_{i=1}^n X_i. \]
It can be shown that \(E(\bar X)=\mu.\)
Thus, on average, the sample mean equals the population mean.
We call \(\boxed{\bar X}\) the point estimate of \(\mu\).
Since \(X_i\sim N(\mu,\sigma^2),\)
the sample mean also follows a normal distribution:
\[ \boxed{ \bar X\sim N\left( \mu,\, \frac{\sigma^2}{n} \right) } \]
Notice that
Subtract the mean and divide by the standard deviation:
\[ Z= \frac{\bar X-\mu} {\sigma/\sqrt n}. \]
Then \[Z\sim N(0,1).\]
\[ P(-2<Z<2)\approx0.95. \]
\[ P(-1.96<Z<1.96)\approx0.95. \]
\[z_{0.975} = 1.96.\]
qnorm(0.975).Rearranging the inequality gives
\[ P\left( \bar X- z_{0.975}\frac{\sigma}{\sqrt n} < \mu < \bar X+ z_{0.975}\frac{\sigma}{\sqrt n} \right) =0.95. \]
Thus, a 95% confidence interval for \(\mu\) is
\[ \boxed{ \bar X \pm z_{0.975}\frac{\sigma}{\sqrt n} } \]
\[\bar X \pm z_{1-\alpha/2}\frac{\sigma}{\sqrt n}.\]
Because the population standard deviation \(\sigma\) is usually unknown, we need to
The resulting 95% confidence interval is
\[ \boxed{ \bar X \pm t_{0.975,n-1} \frac{s}{\sqrt n} } \]
\[ s.e.(\bar X)=\frac{s}{\sqrt n}. \]
\[ ME=t_{0.975,n-1}\frac{s}{\sqrt n} =t_{0.975,n-1}s.e.(\bar X). \]
The critical value depends on the confidence level, and the degrees of freedom (\(df=n-1\)) for the \(t\)-distribution.
| Sample Size \(n\) | \(df\) | 90% | 95% | 99% |
|---|---|---|---|---|
| Normal (\(z\)) | \(\infty\) | 1.645 | 1.960 | 2.576 |
| 5 | 4 | 2.132 | 2.776 | 4.604 |
| 10 | 9 | 1.833 | 2.262 | 3.250 |
| 20 | 19 | 1.729 | 2.093 | 2.861 |
| 50 | 49 | 1.677 | 2.010 | 2.680 |
| 100 | 99 | 1.660 | 1.984 | 2.626 |
n df X90. X95. X99.
1 Inf Inf 1.645 1.960 2.576
2 5 4 2.132 2.776 4.604
3 10 9 1.833 2.262 3.250
4 20 19 1.729 2.093 2.861
5 50 49 1.677 2.010 2.680
6 100 99 1.660 1.984 2.626
Higher confidence levels require larger critical values, resulting in wider confidence intervals.
For the same confidence level, \(t\)-critical values are always larger than the corresponding \(z\)-critical values.
As the sample size increases, the \(t\)-critical values get closer to the \(z\)-critical values.

When \(\sigma\) is known, we use the critical value from the standard normal distribution \(N(0,1)\).
When \(\sigma\) is unknown,
Suppose he population follows \(X\sim N(2,5).\)
\(\bar X_n\) also follows a normal distribution. Compare \(\bar X_{20}\) and \(\bar X_{100}\).
set.seed(20260709)
B <- 10000
# Population
x <- rnorm(100000, mean = 2, sd = sqrt(5))
# Sampling distributions
xbar20 <- replicate(B, mean(rnorm(20, mean = 2, sd = sqrt(5))))
xbar100 <- replicate(B, mean(rnorm(100, mean = 2, sd = sqrt(5))))
par(mfrow = c(1,3))
hist(x,
probability = TRUE,
breaks = 40,
main = "Population",
xlab = "X",
xlim = c(-6,10))
hist(xbar20,
probability = TRUE,
breaks = 40,
main = expression(bar(X)[20]),
xlab = expression(bar(X)),
xlim = c(-6,10))
hist(xbar100,
probability = TRUE,
breaks = 40,
main = expression(bar(X)[100]),
xlab = expression(bar(X)),
xlim = c(-6,10))In practice, \(\sigma\) is unknown and needs to replaced by \(s\). This substitution introduces extra uncertainty.
Therefore, t distribution is a better choice.
In Example 0, we assume that the population distribution is normal.
In practice, we often do not know the population distribution.
In some situations, based on histograms or other visualizations, we may feel that the distribution is approximately normal.
library(tidyverse)
alzheimer_data <- read.csv("https://raw.githubusercontent.com/COSMOS-DataScience/slides/refs/heads/main/data/alzheimer_data.csv", head=T)%>%
select(id, diagnosis, age, educ, female, height, weight) %>%
mutate(diagnosis = as.factor(diagnosis), female = as.factor(female))
# Heights of female participants
f_height = alzheimer_data %>%
filter(female == 1) %>%
pull(height)
# histogram height of female
hist(f_height)t.test[1] 63.4776
[1] 63.33941 63.61579
attr(,"conf.level")
[1] 0.95
What is the estimated mean height? 63.4776
What is the margin of error? 0.1381925
Is the confidence interval symmetric about the point estimate? (63.33941 63.61579)
We typically round the results. For example,
Using the Alzheimer’s data set, we found that
\[(63.3, 63.6) \text{ in}\]
We are 95% confident that the true mean height of all female participants lies between 63.3 in and 63.6 in.
If we repeatedly collected random samples of the same size and constructed a 95% confidence interval from each sample,
The confidence level refers to the method, not to this particular interval. This will be clear when we look at the next example.
Suppose the population is not normal, e.g., skewed.
Although the population is not normal, the sampling distribution of \(\bar X_n\) becomes approximately normal as the sample size increases.
set.seed(20260709)
B <- 10000
# Population
x <- rgamma(100000, shape = 2, scale = 1)
# Sampling distributions
xbar20 <- replicate(B, mean(rgamma(20, shape = 2, scale = 1)))
xbar100 <- replicate(B, mean(rgamma(100, shape = 2, scale = 1)))
par(mfrow = c(1,3))
hist(x,
probability = TRUE,
breaks = 40,
main = "Population",
xlab = "X")
hist(xbar20,
probability = TRUE,
breaks = 40,
main = expression(bar(X)[20]),
xlab = expression(bar(X)))
hist(xbar100,
probability = TRUE,
breaks = 40,
main = expression(bar(X)[100]),
xlab = expression(bar(X)))Suppose everyone is studying the same coin.
\[\hat p = \frac{1}{n}\sum_{i=1}^{n}X_i\]
Each student will “flip” (in silicon) a coin 10 times and record the number of heads by running following code.
Report what the value of p_hat is.
29 experiments were conducted, each with a random seed to generate a sample.
# replace 111111 with your birth date
set.seed(111111)
x <- rbinom(n=10, 1, p)
p_hat_sim1 <- mean(x)
p_hat_sim1
n <- 10
# Estimated standard errors
se <- sqrt(p_hat_sim1 * (1 - p_hat_sim1) / n)
# Margin of error
ME <- 1.96 * se
# Confidence intervals
lower <- p_hat_sim1 - ME
upper <- p_hat_sim1 + ME
lower
upperset.seed(20260706)
p <- runif(1)
p_hat_all_sim1=rep(NA, 29)
for(i in 1:29){
set.seed(i)
x <- rbinom(n=10, 1, p)
p_hat_all_sim1[i] <- mean(x)
#print("Student ID: ", i, " p_hat: ", p_hat)
}
n=10
hist(p_hat_all_sim1, main="Distribution of p_hat for n=10", xlab="p_hat",
probability = TRUE, col="lightblue", border="black",
breaks = seq(-0.5/n, max(p_hat_all_sim1) + 0.5/n, by = 1/n))# replace 111111 with your birth date
set.seed(111111)
x <- rbinom(n=100, 1, p)
p_hat_sim2 <- mean(x)
p_hat_sim2
n <- 100
# Estimated standard errors
se <- sqrt(p_hat_sim2 * (1 - p_hat_sim2) / n)
# Margin of error
ME <- 1.96 * se
# Confidence intervals
lower <- p_hat_sim2 - ME
upper <- p_hat_sim2 + ME
lower
upperset.seed(20260706)
p <- runif(1)
p_hat_all_sim2=rep(NA, 29)
for(i in 1:29){
set.seed(i)
x <- rbinom(n=100, 1, p)
p_hat_all_sim2[i] <- mean(x)
#print("Student ID: ", i, " p_hat: ", p_hat)
}
n=100
hist(p_hat_all_sim2, main="Distribution of p_hat for n=100", xlab="p_hat", probability = TRUE, col="lightblue", border="black",
breaks = seq(-0.5/n, max(p_hat_all_sim2) + 0.5/n, by = 1/n))par(mfrow=c(1,2))
n=10
hist(p_hat_all_sim1, main="Distribution of p_hat for n=10", xlab="p_hat", probability = TRUE, col="lightblue", border="black", xlim=c(0,1),
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n))
n=100
hist(p_hat_all_sim2, main="Distribution of p_hat for n=100", xlab="p_hat", probability = TRUE, col="lightblue", border="black", xlim=c(0,1),
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n))# Number of simulated students
B <- 10000
# True probability (hidden)
set.seed(20260706)
p <- runif(1)
# Repeat the experiment B times
# for a large B, for loop is slow
p_hat_all_sim1 <- replicate(B,
mean(rbinom(10, size = 1, prob = p)))
p_hat_all_sim2 <- replicate(B,
mean(rbinom(100, size = 1, prob = p)))
par(mfrow=c(1,2))
n <- 10
hist(
p_hat_all_sim1,
probability = TRUE,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1)
)
n <- 100
hist(
p_hat_all_sim2,
probability = TRUE,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1)
)# Number of simulated students
B <- 10000
# True probability (hidden)
set.seed(20260706)
p <- runif(1)
# Repeat the experiment B times
# for a large B, for loop is slow
p_hat_all_sim10 <- replicate(B,
mean(rbinom(10, size = 1, prob = p)))
p_hat_all_sim20 <- replicate(B,
mean(rbinom(20, size = 1, prob = p)))
p_hat_all_sim50 <- replicate(B,
mean(rbinom(50, size = 1, prob = p)))
p_hat_all_sim100 <- replicate(B,
mean(rbinom(100, size = 1, prob = p)))
par(mfrow=c(2,2))
n <- 10
hist(
p_hat_all_sim10,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
probability = TRUE,
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1))
n <- 20
hist(
p_hat_all_sim20,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
probability = TRUE,
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1))
n <- 50
hist(
p_hat_all_sim50,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
probability = TRUE,
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1))
n <- 100
hist(
p_hat_all_sim100,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
probability = TRUE,
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1))par(mfrow=c(2,2))
n <- 10
hist(
p_hat_all_sim10,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
probability = TRUE,
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1))
curve(
dnorm(x, mean = p, sd = sqrt(p * (1 - p) / n)),
add = TRUE,
col = "red",
lwd = 2)
n <- 20
hist(
p_hat_all_sim20,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
probability = TRUE,
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1))
curve(
dnorm(x, mean = p, sd = sqrt(p * (1 - p) / n)),
add = TRUE,
col = "red",
lwd = 2)
n <- 50
hist(
p_hat_all_sim50,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
probability = TRUE,
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1))
curve(
dnorm(x, mean = p, sd = sqrt(p * (1 - p) / n)),
add = TRUE,
col = "red",
lwd = 2)
n <- 100
hist(
p_hat_all_sim100,
breaks = seq(-0.5/n, 1 + 0.5/n, by = 1/n),
probability = TRUE,
main = paste("Distribution of p_hat for n =", n),
xlab = expression(hat(p)),
col = "lightblue",
border = "black",
xlim = c(0, 1))
curve(
dnorm(x, mean = p, sd = sqrt(p * (1 - p) / n)),
add = TRUE,
col = "red",
lwd = 2)Does the center of the distribution change? If yes, how?
Does the spread of the distribution change? If yes, how?
\[E[\hat p] = p.\]
\[Var[\hat p]=\frac{p(1-p)}{n}.\]
\[s.e.(\hat p) = \sqrt{\frac{\hat p(1-\hat p)}{n}}.\]
\[Z = \frac{\hat p - p}{\sqrt{p(1- p)/n}}.\]
\[Z\overset{approx}{\sim} N(0,1).\]
Since
\[ Z= \frac{\hat p-p} {\sqrt{p(1-p)/n}} \overset{\text{approx}}{\sim}N(0,1), \]
the 68–95–99.7 rule tells us that \[ P\left( -2< \frac{\hat p-p}{\sqrt{p(1-p)/n}} <2 \right) \approx0.95. \]
By rearranging the inequality,
\[ P\left( -2< \frac{\hat p-p}{\sqrt{p(1-p)/n}} <2 \right) \approx0.95, \]
we obtain
\[ P\left( -2\sqrt{\frac{p(1-p)}{n}} < \hat p-p < 2\sqrt{\frac{p(1-p)}{n}} \right) \approx0.95, \]
Therefore, an approximate 95% confidence interval for \(p\) is
\[ \hat p \pm 2\sqrt{\frac{p(1-p)}{n}} \]
Since the true probability \(p\) is unknown, we replace it with its estimate \(\hat p\).
Recall that estimated standard deviation of \(\hat p\) is called the standard error:
\[ s.e.(\hat p) = \sqrt{\frac{\hat p(1-\hat p)}{n}}. \]
Thus, an approximate 95% confidence interval for the population proportion is
\[ \boxed{ \hat p \pm 2\times s.e.(\hat p) = \hat p \pm 2\sqrt{\frac{\hat p(1-\hat p)}{n}} } \]
\[ \boxed{ \hat p \pm 1.96\times s.e.(\hat p) = \hat p \pm 1.96\sqrt{\frac{\hat p(1-\hat p)}{n}} } \]
If we want to construct a 90% confidence interval, we can use \(z_{0.95} = 1.645\) as the critical value.
If we want to construct a 99% confidence interval, we can use \(z_{0.995} = 2.576\) as the critical value.
Is the way we constructed CI for \(p\) good when \(n=50\)?
We will
Recall that a 95% confidence interval is
\[ \hat p \pm 1.96\sqrt{\frac{\hat p(1-\hat p)}{n}}. \]
Does this method really produce intervals that contain the true probability about 95% of the time?
Let’s find out by simulation.
We will
[1] 0.9404
The value above is the proportion of confidence intervals that contain the true probability.
Each horizontal line represents one confidence interval.
B_show <- 20
plot(
NA,
xlim = c(0, 1),
ylim = c(1, B_show),
xlab = "Probability",
ylab = "Simulation",
yaxt = "n",
main="n=50"
)
abline(v = p, col = "darkgreen", lwd = 2)
for(i in 1:B_show){
col <- ifelse(cover[i], "steelblue", "red")
segments(lower[i], i, upper[i], i,
col = col, lwd = 2)
points(p_hat_all_sim50[i], i,
pch = 16,
col = col)
}Again, we will
[1] 0.9291
Compare the estimated coverage probability with the nominal confidence level of 95%.
Is the coverage closer to 95% than when \(n=50\)?
B_show <- 20
plot(
NA,
xlim = c(0, 1),
ylim = c(1, B_show),
xlab = "Probability",
ylab = "Simulation",
yaxt = "n",
main="n=20"
)
abline(v = p, col = "darkgreen", lwd = 2)
for(i in 1:B_show){
col <- ifelse(cover[i], "steelblue", "red")
segments(lower[i], i, upper[i], i,
col = col, lwd = 2)
points(p_hat_all_sim20[i], i,
pch = 16,
col = col)
}What differences do you notice?
Take-home message
As the sample size increases,
This is exactly what “95% confidence” means.
As the sample size increases,
Suppose he population follows \(X\sim N(2,5).\)
\(\bar X_n\) also follows a normal distribution. Compare \(\bar X_{20}\) and \(\bar X_{100}\).
set.seed(20260709)
B <- 10000
# Population
x <- rnorm(100000, mean = 2, sd = sqrt(5))
# Sampling distributions
xbar20 <- replicate(B, mean(rnorm(20, mean = 2, sd = sqrt(5))))
xbar100 <- replicate(B, mean(rnorm(100, mean = 2, sd = sqrt(5))))
par(mfrow = c(1,3))
hist(x,
probability = TRUE,
breaks = 40,
main = "Population",
xlab = "X",
xlim = c(-6,10))
hist(xbar20,
probability = TRUE,
breaks = 40,
main = expression(bar(X)[20]),
xlab = expression(bar(X)),
xlim = c(-6,10))
hist(xbar100,
probability = TRUE,
breaks = 40,
main = expression(bar(X)[100]),
xlab = expression(bar(X)),
xlim = c(-6,10))Suppose the population is not normal, e.g., skewed.
Although the population is not normal, the sampling distribution of \(\bar X_n\) becomes approximately normal as the sample size increases.
set.seed(20260709)
B <- 10000
# Population
x <- rgamma(100000, shape = 2, scale = 1)
# Sampling distributions
xbar20 <- replicate(B, mean(rgamma(20, shape = 2, scale = 1)))
xbar100 <- replicate(B, mean(rgamma(100, shape = 2, scale = 1)))
par(mfrow = c(1,3))
hist(x,
probability = TRUE,
breaks = 40,
main = "Population",
xlab = "X")
hist(xbar20,
probability = TRUE,
breaks = 40,
main = expression(bar(X)[20]),
xlab = expression(bar(X)))
hist(xbar100,
probability = TRUE,
breaks = 40,
main = expression(bar(X)[100]),
xlab = expression(bar(X)))Suppose \(X\sim\text{Bernoulli}(p).\)
The population has only two possible values, but the sampling distribution of \(\bar X_n\) becomes approximately normal.
set.seed(20260709)
B <- 10000
# Population
x <- rbinom(100000, 1, p)
# Sampling distributions
xbar20 <- replicate(B, mean(rbinom(20, 1, p)))
xbar100 <- replicate(B, mean(rbinom(100, 1, p)))
par(mfrow = c(1,3))
hist(x,
probability = TRUE,
breaks = seq(-0.5,1.5,1),
main = "Population",
xlab = "X")
hist(xbar20,
probability = TRUE,
breaks = seq(-1/(2*20),1+1/(2*20),by=1/20),
main = expression(bar(X)[20]),
xlab = expression(bar(X)))
hist(xbar100,
probability = TRUE,
breaks = seq(-1/(2*100),1+1/(2*100),by=1/100),
main = expression(bar(X)[100]),
xlab = expression(bar(X)))Suppose \(X_1, X_2, \ldots, X_n\) are independent and identically distributed (i.i.d.) random variables with mean \(\mu\) and variance \(\sigma^2\).
Here we did not require the distribution of \(X_i\) to be normal.
It can be shown that \(E[\bar X_n] = \mu, \quad Var[\bar X_n] = \frac{\sigma^2}{n}.\)
equivalently, we can say that
\[\bar X_n \sim (\mu, \sigma^2/n).\]
\[\bar X_n \overset{approx}\sim N(\mu, \sigma^2/n)\]
This is known as the Central Limit Theorem (CLT).
This approximation is good enough for \(n>30\) in general, but it can be used for smaller \(n\) if the underlying distribution is not too skewed.
The Central Limit Theorem (CLT) is one of the most important results in statistics. It was first formulated by Abraham de Moivre in 1733, and later generalized by Pierre-Simon Laplace in 1810.
The CLT allows us to make inferences about population parameters even when the underlying distribution is not normal, as long as the sample size is large enough.
Here we used CLT can to construct confidence intervals.
In tomorrow’s lecture, we will see how CLT can be used to perform hypothesis testing.
