\[
\boxed{
\bar X
\pm
1.96\sqrt{\frac{\sigma^2}{n}}
}.
\]
Example
Continue with the same parent distribution we used in Chapter 3:
\[
X\sim U(2,5).
\]
Its variance is
\[
\sigma^2=0.75.
\]
Suppose our sample size is
\[
n=30.
\]
Now suppose the sample we actually observe gives
\[
\bar X_1=3.8.
\]
Remember:
\[
\bar X_1
\]
is the mean calculated from this particular sample.
The population mean
\[
\mu
\]
is the fixed but unknown mean of the parent distribution.
The 95% confidence interval is
\[
3.8
\pm
1.96\sqrt{\frac{0.75}{30}}.
\]
In R:
x.bar.1<-3.8# Mean from the observed samplesig2 <-0.75# Variance of the parent distributionn <-30# Sample sizelower <- x.bar.1-1.96*sqrt(sig2 / n)upper <- x.bar.1+1.96*sqrt(sig2 / n)cat("95% confidence interval =",round(lower, 3), "to", round(upper, 3), "\n")
95% confidence interval = 3.49 to 4.11
Probability interval versus confidence interval
The two intervals look very similar algebraically, but they answer different questions.
In Chapter 3, the probability interval was
\[
\mu
\pm
1.96\sqrt{\frac{\sigma^2}{n}}.
\]
Here, \(\mu\) is known and fixed, and the distribution of \(\bar X\) is known. So this gives one probability interval for the distribution of the sample mean.
Different samples produce different values of \(\bar X\), but the probability interval itself does not change.
Now consider the confidence interval:
\[
\bar X
\pm
1.96\sqrt{\frac{\sigma^2}{n}}.
\]
Here, \(\mu\) is fixed but unknown, while \(\bar X\) changes from sample to sample.
So the confidence interval also changes from sample to sample.
The interval we calculate from our observed sample,
\[
\bar X_1
\pm
1.96\sqrt{\frac{\sigma^2}{n}},
\]
is therefore just one of many possible confidence intervals we could have obtained.
If we drew another sample, we would usually get a different sample mean,
\[
\bar X_2,
\]
and therefore a different confidence interval.
The figure below summarizes the contrast.
Figure 4.1: A probability interval is one fixed interval for the distribution of the sample mean. A confidence interval is one of many possible intervals produced by repeated samples.
So the difference is:
\[
\boxed{\text{Probability interval: one fixed interval}}
\]
versus
\[
\boxed{\text{Confidence interval: one interval from our sample, but one of many possible intervals}}
\]
What does 95% confidence mean?
The value of
\[
\mu
\]
does not change from sample to sample. It is the fixed mean of the parent distribution.
What changes are
\[
\bar X_1,\bar X_2,\ldots
\]
and therefore the confidence intervals constructed around them.
So the interval from our observed sample is only one member of a larger collection of possible intervals.
The observed sample is not a Monte Carlo repetition. In the simulation below, we generate new samples from the parent distribution to see how the confidence-interval procedure behaves across repeated samples.
If we repeatedly draw samples and construct a 95% confidence interval from each sample, about 95% of those intervals will contain the true value of \(\mu\).
We can see that repeated-sampling interpretation directly with Monte Carlo simulation.
4.2 Seeing confidence intervals with Monte Carlo
In the previous section, we calculated one confidence interval from the observed sample.
But that interval was only one of many possible confidence intervals we could have obtained.
Return to the same parent distribution:
\[
X\sim U(2,5).
\]
Recall that
\[
\mu=3.5
\]
is the true mean of the parent distribution, and
\[
\sigma^2=0.75.
\]
We will repeatedly draw samples of size
\[
n=30.
\]
Every sample comes from this same parent distribution. The value of \(\mu=3.5\) therefore stays fixed.
What changes from sample to sample are the sample means
\[
\bar X_1,\bar X_2,\ldots
\]
and the confidence intervals constructed from those sample means.
For Sample \(i\), the 95% confidence interval is
\[
\bar X_i
\pm
1.96\sqrt{\frac{\sigma^2}{n}}.
\]
Watch 10 confidence intervals
As before, start with only
\[
R=10
\]
so we can see each repetition.
n <-30# Number of observations in each sampleR <-10# Number of Monte Carlo repetitionsmu <-3.5# True mean of the parent distributionsig2 <-0.75# Variance of the parent distributionse <-sqrt(sig2 / n) # Standard error of the sample meanfor (i in1:R) { x <-runif(n, min =2, max =5) # Draw one sample from the parent distribution x.bar <-mean(x) # Calculate its sample mean lower <- x.bar -1.96* se upper <- x.bar +1.96* se contains.mu <-0if ((lower < mu) && (mu < upper)) { contains.mu <-1# Does this interval contain the true parent mean? }cat("sample =", i,"x.bar =", round(x.bar, 3),"CI =", round(lower, 3), "to", round(upper, 3),"contains mu =", contains.mu, "\n")}
sample = 1 x.bar = 3.809 CI = 3.499 to 4.119 contains mu = 1
sample = 2 x.bar = 3.375 CI = 3.065 to 3.685 contains mu = 1
sample = 3 x.bar = 3.532 CI = 3.222 to 3.842 contains mu = 1
sample = 4 x.bar = 3.453 CI = 3.143 to 3.763 contains mu = 1
sample = 5 x.bar = 3.476 CI = 3.166 to 3.786 contains mu = 1
sample = 6 x.bar = 3.438 CI = 3.128 to 3.748 contains mu = 1
sample = 7 x.bar = 3.173 CI = 2.863 to 3.483 contains mu = 0
sample = 8 x.bar = 3.624 CI = 3.314 to 3.934 contains mu = 1
sample = 9 x.bar = 3.528 CI = 3.218 to 3.838 contains mu = 1
sample = 10 x.bar = 3.437 CI = 3.127 to 3.747 contains mu = 1
Each sample produces a different sample mean.
Therefore, each sample also produces a different confidence interval.
But throughout the experiment,
\[
\boxed{\mu=3.5}
\]
does not change. It remains the fixed mean of the parent distribution.
What the 10 repetitions show
The 10 repetitions above already show the main idea: each new sample produces a new sample mean and therefore a new confidence interval.
With only 10 samples, we may or may not happen to see an interval that misses \(\mu\). The theoretical claim is not that exactly 95% of every small group of intervals will contain \(\mu\). It is a long-run statement about the confidence-interval procedure.
The value of
\[
\mu
\]
is fixed. The samples change, so
\[
\bar X_i
\]
and the confidence intervals around the sample means change.
Now use many repetitions to check the 95% coverage directly.
Check the 95% coverage with Monte Carlo
Now increase the number of repetitions to
\[
R=10{,}000.
\]
R <-10000success <-0for (i in1:R) { x <-runif(n, min =2, max =5) # Draw one sample from the parent distribution x.bar <-mean(x) # Calculate its sample mean lower <- x.bar -1.96* se upper <- x.bar +1.96* seif ((lower < mu) && (mu < upper)) { success <- success +1# Count intervals containing mu }}p.hat <- success / Rcat("fraction containing mu =", round(p.hat, 3), "\n")
fraction containing mu = 0.949
cat("theoretical coverage = 0.950\n")
theoretical coverage = 0.950
The Monte Carlo simulation shows what 95% confidence means:
The t-statistic tells us how many standard errors the sample mean is from the value of the parent mean assumed under the null hypothesis.
When \(\sigma^2\) is known, many textbooks call this a z-statistic. We use “t-statistic” as the general term in these notes. In Chapter 5, estimating the error variance leads to the Student \(t\) distribution.
x.bar.1<-3.8# Mean from the observed samplemu.0<-3.5# Parent mean assumed under H0sig2 <-0.75# Parent variancen <-30# Sample sizese <-sqrt(sig2 / n) # Standard error of the sample meant.1<- (x.bar.1- mu.0) / se # t-statistic for the observed samplecat("standard error =", round(se, 3), "\n")
standard error = 0.158
cat("t.1 =", round(t.1, 3), "\n")
t.1 = 1.897
So the observed sample produced a sample mean that is about 1.9 standard errors above the value predicted by the null hypothesis.
What should t look like if the null hypothesis is true?
Under
\[
H_0:\mu=\mu_0,
\]
the Central Limit Theorem tells us that
\[
\bar X
\approx
N\left(\mu_0,\frac{\sigma^2}{n}\right).
\]
Subtracting \(\mu_0\) centers this distribution at zero.
This gives the t-statistic a simple interpretation:
\[
t=0
\]
means the sample mean is exactly at the value predicted by \(H_0\).
\[
t=1
\]
means it is one standard error above that value.
\[
t=-2
\]
means it is two standard errors below that value.
Watch 10 t-statistics
We can see this with Monte Carlo.
Under the null hypothesis,
\[
\mu_0=3.5.
\]
Our parent distribution
\[
X\sim U(2,5)
\]
has exactly this mean, so it gives us a convenient way to simulate samples when the null hypothesis is true.
R <-10for (i in1:R) { x <-runif(n, min =2, max =5) # Draw one sample under H0 x.bar <-mean(x) # Calculate its sample mean t <- (x.bar - mu.0) / se # Standardize the sample meancat("sample =", i,"x.bar =", round(x.bar, 3),"t =", round(t, 3), "\n")}
sample = 1 x.bar = 3.438 t = -0.395
sample = 2 x.bar = 3.106 t = -2.492
sample = 3 x.bar = 3.547 t = 0.295
sample = 4 x.bar = 3.515 t = 0.093
sample = 5 x.bar = 3.665 t = 1.046
sample = 6 x.bar = 3.616 t = 0.732
sample = 7 x.bar = 3.287 t = -1.35
sample = 8 x.bar = 3.647 t = 0.931
sample = 9 x.bar = 3.682 t = 1.151
sample = 10 x.bar = 3.591 t = 0.573
Some t-statistics will be positive, some negative, and most will be reasonably close to zero.
Many samples
Now increase to
\[
R=10{,}000.
\]
R <-10000t <-rep(0, R)for (i in1:R) { x <-runif(n, min =2, max =5) # Draw one sample under H0 x.bar <-mean(x) # Calculate its sample mean t[i] <- (x.bar - mu.0) / se # Save the t-statistic}cat("mean of t =", round(mean(t), 3), "\n")
mean of t = -0.016
cat("variance of t =", round(var(t), 3), "\n")
variance of t = 0.992
We should obtain a mean close to
\[
0
\]
and a variance close to
\[
1.
\]
Comparison
The theoretical prediction is
\[
t\approx N(0,1).
\]
The Monte Carlo simulation gives us 10,000 values,
\[
t_1,t_2,\ldots,t_{10000}.
\]
We can compare their simulated distribution with the theoretical standard normal distribution.
Figure 4.3: The two-sided p-value on the t-statistic scale. The shaded tail areas are outcomes at least as extreme as the observed t-statistic.
So, if the null hypothesis is true, a t-statistic at least this far from zero would occur about 5.8% of the time.
The same p-value using the distribution of the sample mean
We can calculate exactly the same p-value without using the t-statistic.
Under
\[
H_0:\mu=3.5,
\]
the Central Limit Theorem gives
\[
\bar X
\approx
N\left(3.5,\frac{0.75}{30}\right).
\]
Our observed sample mean is
\[
\bar X_1=3.8.
\]
It is
\[
3.8-3.5=0.3
\]
above the parent mean assumed under the null hypothesis.
For a two-sided test, we also count sample means that are at least \(0.3\) below the null value.
That corresponding value is
\[
3.5-0.3=3.2.
\]
Therefore, the same p-value is
\[
\boxed{
p
=
P(\bar X\ge3.8
\text{ or }
\bar X\le3.2
\mid H_0)
}.
\]
In R:
mu.0<-3.5sig2 <-0.75n <-30se <-sqrt(sig2 / n)p.upper <-1-pnorm(3.8, mean = mu.0, sd = se) # Probability X-bar is at least 3.8p.lower <-pnorm(3.2, mean = mu.0, sd = se) # Probability X-bar is at most 3.2p.value.xbar <- p.lower + p.upper # Add the two tail probabilitiescat("p-value =", round(p.value.xbar, 3), "\n")
p-value = 0.058
The figure below shows the same two tail areas, now on the sample-mean scale.
xx <-seq(mu.0-4* se, mu.0+4* se, length.out =800)yy <-dnorm(xx, mean = mu.0, sd = se)plot(xx, yy,type ="l",lwd =2,xlab ="Sample mean",ylab ="Density",main ="")left <- xx <=3.2right <- xx >=3.8polygon(c(min(xx[left]), xx[left], max(xx[left])),c(0, yy[left], 0),density =15,border =NA)polygon(c(min(xx[right]), xx[right], max(xx[right])),c(0, yy[right], 0),density =15,border =NA)lines(xx, yy, lwd =2)abline(v = mu.0, lty =3)abline(v =c(3.2, 3.8), lty =2)text(mu.0, max(yy) *0.92, expression(mu[0] ==3.5))
Figure 4.4: The same two-sided p-value on the sample-mean scale. Under the null hypothesis, the distribution of X-bar is centered at mu0 = 3.5.
Recall that
\[
\mu_0=3.5
\]
is the value assumed for the mean of the parent distribution under the null hypothesis.
From each simulated sample we can calculate a new \(\bar X\), confidence interval, or t-statistic. As a result, Monte Carlo also lets us see the sampling distributions these repeated samples generate.
So Monte Carlo makes the repeated-sampling logic behind statistical inference visible rather than purely theoretical.
The main lesson in this chapter is how we use one observed sample to learn about an unknown feature of the parent distribution, while using repeated sampling—through theory and Monte Carlo—to understand how reliable that inference is.
The big picture is:
\[
\boxed{\text{applications usually give us one sample}}
\]
\[
\boxed{\text{theory considers many possible samples}}
\]
\[
\boxed{\text{Monte Carlo lets us see those samples and the distributions they generate}}
\]
4.7 Where we go next
So far, our parameter of interest has been the mean of one parent distribution:
\[
\mu.
\]
But often we want to understand the relationship between two variables.
For example, suppose we observe pairs
\[
(X_1,Y_1),\ldots,(X_n,Y_n).
\]
We may want to ask whether changes in \(X\) are associated with changes in \(Y\).
A simple way to describe that relationship is
\[
Y_i=\beta_0+\beta_1X_i+u_i.
\]
Now the unknown quantities of interest are
\[
\beta_0
\qquad\text{and}\qquad
\beta_1.
\]
Just as different samples produced different values of \(\bar X\), different samples will produce different estimates of the regression coefficients:
\[
\hat\beta_0,
\qquad
\hat\beta_1.
\]
And once again we can use Monte Carlo simulation to ask:
What does the distribution of an estimator look like across repeated samples?
That takes us from inference about a sample mean to regression and OLS.