4  Statistical Inference for the Mean

4.1 From a probability interval to a confidence interval

In Chapter 3, we started with a parent distribution whose mean was known.

Recall that

\[ \mu \]

is the mean of the parent distribution.

For sufficiently large \(n\), the Central Limit Theorem tells us that

\[ \bar X \approx N\left(\mu,\frac{\sigma^2}{n}\right). \]

From this distribution, we constructed a 95% probability interval for the sample mean:

\[ P\left( \mu-1.96\sqrt{\frac{\sigma^2}{n}} < \bar X < \mu+1.96\sqrt{\frac{\sigma^2}{n}} \right) \approx0.95. \]

Here, \(\mu\) is fixed and the sample mean \(\bar X\) is random.

That is the probability question from Chapter 3:

If we know the parent mean \(\mu\), where is the sample mean \(\bar X\) likely to fall?

Turning the question around

Statistical inference usually starts from the opposite situation.

We observe a sample,

\[ X_1,\ldots,X_n, \]

and calculate its sample mean,

\[ \bar X_1. \]

But we do not know

\[ \mu, \]

the mean of the parent distribution.

Now our question is:

Given the sample mean we observed, what can we learn about the unknown parent mean \(\mu\)?

We can answer this by rearranging the probability interval from Chapter 3.

Start with

\[ \mu-1.96\sqrt{\frac{\sigma^2}{n}} < \bar X < \mu+1.96\sqrt{\frac{\sigma^2}{n}}. \]

Rearrange the inequalities so that \(\mu\) is in the middle:

\[ \bar X-1.96\sqrt{\frac{\sigma^2}{n}} < \mu < \bar X+1.96\sqrt{\frac{\sigma^2}{n}}. \]

This gives the 95% confidence interval

\[ \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 sample
sig2 <- 0.75                           # Variance of the parent distribution
n <- 30                                # Sample size

lower <- 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 sample
R <- 10                                # Number of Monte Carlo repetitions

mu <- 3.5                              # True mean of the parent distribution
sig2 <- 0.75                           # Variance of the parent distribution
se <- sqrt(sig2 / n)                   # Standard error of the sample mean

for (i in 1: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 <- 0

  if ((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 <- 10000
success <- 0

for (i in 1: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

  if ((lower < mu) && (mu < upper)) {
    success <- success + 1              # Count intervals containing mu
  }
}

p.hat <- success / R

cat("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:

\[ \boxed{ \frac{\text{confidence intervals containing }\mu} {\text{number of repeated samples}} \approx0.95 }. \]

Complete Monte Carlo Algorithm

1. Theoretical Prediction

A 95% confidence interval for the parent mean \(\mu\) is

\[ \left[ \bar X-1.96\sqrt{\frac{\sigma^2}{n}}, \; \bar X+1.96\sqrt{\frac{\sigma^2}{n}} \right]. \]

Across repeated samples, approximately 95% of these intervals should contain the true parent mean \(\mu\).

2. Numerical Simulation

  1. Generate a sample of \(n\) observations from the parent distribution.
  2. Calculate \(\bar X\).
  3. Construct the 95% confidence interval around \(\bar X\).
  4. Count a success if the interval contains the true parent mean \(\mu\).
  5. Repeat steps 1–4 \(R\) times.
  6. Calculate

\[ \hat p = \frac{\text{number of intervals containing }\mu}{R}. \]

3. Comparison

Compare

\[ \hat p \]

with

\[ 0.95. \]

4.3 The t-statistic

So far, we have used the sample mean

\[ \bar X \]

to learn about

\[ \mu, \]

the unknown mean of the parent distribution.

Suppose we want to consider a particular value for that parent mean:

\[ H_0:\mu=\mu_0. \]

Here,

\[ \mu \]

is the true, unknown mean of the parent distribution, while

\[ \mu_0 \]

is the particular value we assume for \(\mu\) under the null hypothesis.

Choosing the null value

In real applications, we usually do not know \(\mu\). That is why we do statistical inference in the first place.

Instead, we consider a particular value,

\[ \mu_0, \]

and ask whether the data are consistent with

\[ H_0:\mu=\mu_0. \]

The value \(\mu_0\) is not something we are trying to estimate. It is the value for the parent mean that we specify under the null hypothesis.

In our Monte Carlo example, however, we know that

\[ X\sim U(2,5) \]

has true parent mean

\[ \mu=3.5. \]

Because we know the truth in a simulation, it is convenient to choose

\[ \mu_0=3.5. \]

Then

\[ H_0:\mu=3.5 \]

is actually true.

This allows us to ask:

If the null hypothesis really is true, what does the distribution of the t-statistic look like?

How far is the sample mean from the null value?

Suppose our observed sample gives

\[ \bar X_1=3.8. \]

The difference between the sample mean and the value predicted by the null hypothesis is

\[ \bar X_1-\mu_0 = 3.8-3.5 = 0.3. \]

But is \(0.3\) a large difference?

That depends on how much sample means normally vary from sample to sample.

From Chapter 3,

\[ \operatorname{Var}(\bar X) = \frac{\sigma^2}{n}. \]

Therefore, the standard deviation of the distribution of sample means is

\[ \sqrt{\frac{\sigma^2}{n}}. \]

This quantity is called the standard error of the sample mean.

For our example,

\[ \sigma^2=0.75 \]

and

\[ n=30, \]

so

\[ \sqrt{\frac{0.75}{30}} \approx0.158. \]

The sample mean \(3.8\) is therefore about

\[ \frac{0.3}{0.158} \approx1.90 \]

standard errors above the null value.

That is what the t-statistic measures.

The t-statistic

We define

\[ \boxed{ t= \frac{\bar X-\mu_0} {\sqrt{\sigma^2/n}} }. \]

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.

For the observed sample,

\[ t_1 = \frac{\bar X_1-\mu_0} {\sqrt{\sigma^2/n}}. \]

Using

\[ \bar X_1=3.8, \qquad \mu_0=3.5, \qquad \sigma^2=0.75, \qquad n=30, \]

we get

\[ t_1 = \frac{3.8-3.5} {\sqrt{0.75/30}} \approx1.90. \]

In R:

x.bar.1 <- 3.8                         # Mean from the observed sample
mu.0 <- 3.5                            # Parent mean assumed under H0
sig2 <- 0.75                           # Parent variance
n <- 30                                # Sample size

se <- sqrt(sig2 / n)                   # Standard error of the sample mean

t.1 <- (x.bar.1 - mu.0) / se           # t-statistic for the observed sample

cat("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.

Dividing by

\[ \sqrt{\frac{\sigma^2}{n}} \]

puts the spread into standard-error units.

Therefore,

\[ \boxed{ t = \frac{\bar X-\mu_0} {\sqrt{\sigma^2/n}} \approx N(0,1) } \]

when the null hypothesis is true.

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 <- 10

for (i in 1: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 mean

  cat("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 <- 10000
t <- rep(0, R)

for (i in 1: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.

hist(t,
     probability = TRUE,
     breaks = 35,
     xlab = "t-statistic",
     main = "")

curve(dnorm(x, mean = 0, sd = 1),
      add = TRUE,
      lwd = 2)
Figure 4.2: Monte Carlo distribution of the t-statistic when the null hypothesis is true, with the theoretical N(0,1) distribution.

If the null hypothesis is true, the histogram should be close to the theoretical

\[ N(0,1) \]

curve.

The comparison also gives us a visual interpretation of the t-statistic:

  • values near \(0\) are common;
  • values far above or below \(0\) are increasingly unusual;
  • the distribution is centered at \(0\);
  • its variance is approximately \(1\).

Standardization has not created a new source of randomness.

It has simply expressed the same sample mean in a different unit:

\[ \boxed{ \bar X \quad\longrightarrow\quad \text{number of standard errors from }\mu_0 }. \]

Complete Monte Carlo Algorithm

1. Theoretical Prediction

Under

\[ H_0:\mu=\mu_0, \]

for sufficiently large \(n\),

\[ t= \frac{\bar X-\mu_0} {\sqrt{\sigma^2/n}} \approx N(0,1). \]

2. Numerical Simulation

  1. Generate a sample of \(n\) observations under \(H_0\).
  2. Calculate \(\bar X\).
  3. Calculate

\[ t= \frac{\bar X-\mu_0} {\sqrt{\sigma^2/n}}. \]

  1. Save \(t\).
  2. Repeat steps 1–4 \(R\) times.
  3. Examine the distribution of the saved t-statistics.

3. Comparison

Compare the simulated distribution of \(t\) with

\[ N(0,1). \]

4.4 The p-value

Suppose again that we are considering the null hypothesis

\[ H_0:\mu=3.5. \]

Recall that \(\mu\) is the true mean of the parent distribution. Under the null hypothesis, we are assuming that this mean equals

\[ \mu_0=3.5. \]

Suppose our observed sample gives

\[ \bar X_1=3.8. \]

From the previous section,

\[ t_1 = \frac{\bar X_1-\mu_0} {\sqrt{\sigma^2/n}} = \frac{3.8-3.5}{\sqrt{0.75/30}} \approx1.90. \]

So our observed sample mean is about 1.90 standard errors above the value predicted by the null hypothesis.

The question now is:

If the true parent mean is 3.5, how unusual would it be to obtain a sample mean as far from 3.5 as 3.8?

Equivalently:

If the true parent mean is 3.5, how unusual would it be to obtain a t-statistic as far from zero as 1.90?

That is what the p-value measures.

The p-value using the t-statistic

Under the null hypothesis,

\[ t\approx N(0,1). \]

Our observed statistic is

\[ t_1\approx1.90. \]

For a two-sided test, results at least as extreme as \(1.90\) can occur on either side of zero:

\[ t\ge1.90 \]

or

\[ t\le-1.90. \]

Therefore, the p-value is

\[ \boxed{ p = P(t\ge1.90\text{ or }t\le-1.90\mid H_0) }. \]

In R, we can use pnorm() to calculate probabilities from a normal distribution. For a standard normal random variable \(Z\),

\[ \texttt{pnorm(a)} = P(Z\le a). \]

For a two-sided p-value, we calculate one tail probability and multiply it by 2.

t.1 <- (3.8 - 3.5) / sqrt(0.75 / 30)   # Calculate the observed t-statistic

left.tail <- pnorm(-abs(t.1))            # Probability below -|t.1| under N(0,1)

p.value.t <- 2 * left.tail               # Two-sided p-value: count both tails

cat("t.1 =", round(t.1, 3), "\n")
t.1 = 1.897 
cat("one tail =", round(left.tail, 3), "\n")
one tail = 0.029 
cat("p-value =", round(p.value.t, 3), "\n")
p-value = 0.058 

The figure below shows the same p-value as the two tail areas beyond \(-1.90\) and \(1.90\).

xx <- seq(-4, 4, length.out = 800)
yy <- dnorm(xx)

plot(xx, yy,
     type = "l",
     lwd = 2,
     xlab = "t-statistic",
     ylab = "Density",
     main = "")

left <- xx <= -abs(t.1)
right <- xx >= abs(t.1)

polygon(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 = c(-abs(t.1), abs(t.1)), lty = 2)
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.5
sig2 <- 0.75
n <- 30

se <- sqrt(sig2 / n)

p.upper <- 1 - pnorm(3.8, mean = mu.0, sd = se)  # Probability X-bar is at least 3.8
p.lower <- pnorm(3.2, mean = mu.0, sd = se)      # Probability X-bar is at most 3.2

p.value.xbar <- p.lower + p.upper                 # Add the two tail probabilities

cat("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.2
right <- xx >= 3.8

polygon(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.

Two scales, one p-value

These are not two different p-values.

On the t-statistic scale, we calculate

\[ P(t\le-1.90\text{ or }t\ge1.90). \]

On the sample-mean scale, we calculate

\[ P(\bar X\le3.2\text{ or }\bar X\ge3.8). \]

The t-statistic simply transforms

\[ \bar X \]

into the number of standard errors it lies from

\[ \mu_0. \]

So

\[ \boxed{ \bar X=3.8 \quad\longleftrightarrow\quad t\approx1.90 } \]

and

\[ \boxed{ \bar X=3.2 \quad\longleftrightarrow\quad t\approx-1.90 }. \]

The shaded probability is the same in both pictures.

Verify the same p-value with Monte Carlo

Monte Carlo gives us a numerical estimate of the same tail probability shown in the figures.

The observed sample above gives us the benchmark we want to evaluate. It is not one of the Monte Carlo repetitions.

Assume the null hypothesis is true and generate new samples from the parent distribution with

\[ \mu=3.5. \]

For each simulated sample:

  1. calculate \(\bar X\);
  2. calculate its t-statistic;
  3. check whether the simulated result is at least as extreme as the observed result.

We can use either success rule:

\[ |t|\ge |t_1| \]

or

\[ \bar X\le3.2 \quad\text{or}\quad \bar X\ge3.8. \]

They are equivalent.

One Monte Carlo repetition: \(R=1\)

First generate one new sample under \(H_0\).

x <- runif(n, min = 2, max = 5)      # Draw one simulated sample under H0

x.bar <- mean(x)                      # Calculate its sample mean
t.sim <- (x.bar - mu.0) / se          # Calculate its t-statistic

ext.t <- abs(t.sim) >= abs(t.1)
ext.xbar <- (x.bar <= 3.2) || (x.bar >= 3.8)

cat("x.bar =", round(x.bar, 3), "\n")
x.bar = 3.441 
cat("t =", round(t.sim, 3), "\n")
t = -0.372 
cat("ext.t =", ext.t, "\n")
ext.t = FALSE 
cat("ext.xbar =", ext.xbar, "\n")
ext.xbar = FALSE 

This is one simulated sample under the null. The two success rules give the same answer.

Watch 10 simulated samples

Now use 10 repetitions so we can see the equivalence repeatedly.

R <- 10

for (i in 1:R) {

  x <- runif(n, min = 2, max = 5)      # Draw one sample under H0

  x.bar <- mean(x)                      # Calculate sample mean
  t.sim <- (x.bar - mu.0) / se          # Calculate t-statistic

  ext.t <- abs(t.sim) >= abs(t.1)

  ext.xbar <- (x.bar <= 3.2) || (x.bar >= 3.8)

  cat("rep =", i,
      "x.bar =", round(x.bar, 3),
      "t =", round(t.sim, 3),
      "ext.t =", ext.t,
      "ext.xbar =", ext.xbar, "\n")
}
rep = 1 x.bar = 3.324 t = -1.115 ext.t = FALSE ext.xbar = FALSE 
rep = 2 x.bar = 3.659 t = 1.007 ext.t = FALSE ext.xbar = FALSE 
rep = 3 x.bar = 3.33 t = -1.073 ext.t = FALSE ext.xbar = FALSE 
rep = 4 x.bar = 3.673 t = 1.096 ext.t = FALSE ext.xbar = FALSE 
rep = 5 x.bar = 3.41 t = -0.571 ext.t = FALSE ext.xbar = FALSE 
rep = 6 x.bar = 3.642 t = 0.895 ext.t = FALSE ext.xbar = FALSE 
rep = 7 x.bar = 3.496 t = -0.027 ext.t = FALSE ext.xbar = FALSE 
rep = 8 x.bar = 3.582 t = 0.516 ext.t = FALSE ext.xbar = FALSE 
rep = 9 x.bar = 3.57 t = 0.442 ext.t = FALSE ext.xbar = FALSE 
rep = 10 x.bar = 3.568 t = 0.429 ext.t = FALSE ext.xbar = FALSE 

For every simulated sample, ext.t and ext.xbar should give the same answer.

Now increase to

\[ R=10{,}000. \]

R <- 10000
extreme.total <- 0

for (i in 1:R) {

  x <- runif(n, min = 2, max = 5)      # Draw one sample under H0

  x.bar <- mean(x)                      # Calculate sample mean

  if ((x.bar <= 3.2) || (x.bar >= 3.8)) {
    extreme.total <- extreme.total + 1
  }
}

p.hat <- extreme.total / R

cat("Monte Carlo p-value =", round(p.hat, 3), "\n")
Monte Carlo p-value = 0.059 
cat("Theoretical p-value =", round(p.value.t, 3), "\n")
Theoretical p-value = 0.058 

The Monte Carlo estimate should be close to the theoretical p-value.

Complete Monte Carlo Algorithm

1. Theoretical Prediction

Under

\[ H_0:\mu=\mu_0, \]

calculate the observed t-statistic

\[ t_1 = \frac{\bar X_1-\mu_0} {\sqrt{\sigma^2/n}}. \]

For a two-sided test, the p-value is

\[ p = P(|t|\ge |t_1|\mid H_0). \]

Equivalently, the same probability can be expressed using the distribution of \(\bar X\) under the null hypothesis.

2. Numerical Simulation

  1. Generate a sample of \(n\) observations under \(H_0\).
  2. Calculate \(\bar X\).
  3. Calculate the simulated t-statistic.
  4. Count a success if the simulated result is at least as extreme as the observed result.
  5. Repeat steps 1–4 \(R\) times.
  6. Calculate

\[ \hat p = \frac{\text{number of simulated samples at least as extreme as the observed result}}{R}. \]

3. Comparison

Compare

\[ \hat p \]

with the theoretical p-value.

4.5 Confidence intervals, t-statistics, and p-values are connected

We have now looked at the same sample in three different ways.

Suppose again that

\[ H_0:\mu=3.5, \]

where \(\mu\) is the unknown mean of the parent distribution, and the observed sample gives

\[ \bar X_1=3.8. \]

We also have

\[ \sigma^2=0.75 \qquad\text{and}\qquad n=30. \]

The confidence interval

From the observed sample, the 95% confidence interval is approximately

\[ [3.49,4.11]. \]

The null value

\[ \mu_0=3.5 \]

is inside this interval.

So the sample is consistent with

\[ H_0:\mu=3.5 \]

at the 5% significance level.

The t-statistic

The same sample gives

\[ t_1\approx1.90. \]

For a two-sided 5% test, the usual cutoff is approximately

\[ 1.96. \]

Since the absolute t-statistic is smaller than \(1.96\), the t-statistic is not far enough from zero to reject the null hypothesis at the 5% level.

The p-value

The same sample gives

\[ p\approx0.058. \]

Since

\[ 0.058>0.05, \]

we again do not reject

\[ H_0:\mu=3.5. \]

All three methods give the same conclusion:

Method Our example
95% confidence interval \(3.5\) is inside \([3.49,4.11]\)
t-statistic Absolute t-statistic: \(1.90 < 1.96\)
p-value \(0.058 > 0.05\)
Conclusion Do not reject \(H_0\) at 5%

Why are they connected?

For a two-sided test at the 5% significance level,

\[ H_0:\mu=\mu_0, \]

the following statements are equivalent:

\[ \boxed{ \mu_0\text{ is outside the 95\% confidence interval} } \]

\[ \Longleftrightarrow \]

\[ \boxed{ \text{absolute t-statistic}>1.96 } \]

\[ \Longleftrightarrow \]

\[ \boxed{ p<0.05 }. \]

Likewise, if \(\mu_0\) is inside the 95% confidence interval, the absolute t-statistic is less than \(1.96\) and the p-value is greater than \(0.05\).

The confidence interval asks:

Which values of the parent mean \(\mu\) are reasonably consistent with our sample?

The t-statistic asks:

How many standard errors is our sample mean from the value proposed under \(H_0\)?

The p-value asks:

If \(H_0\) is true, how often would repeated samples produce a result at least this extreme?

They emphasize different aspects of the problem, but they are closely connected.

4.6 One sample, many possible samples

In most statistical applications, and often in textbooks, we observe only one sample.

From that one sample we calculate quantities such as

\[ \bar X, \]

a confidence interval, a t-statistic, and a p-value.

But the theory behind statistical inference asks us to think about what would happen if we could draw many samples from the same parent distribution.

Monte Carlo simulation lets us actually see those repeated samples:

\[ \text{Simulated Sample 1},\text{ Simulated Sample 2},\ldots,\text{ Simulated Sample }R. \]

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.