t-test

The t-test is a statistical hypothesis test used to determine whether there is a significant difference between the means of two groups. It is widely used when the sample size is small and the population standard deviation is unknown.

1. What is a t-test?

A t-test compares the means of one or two samples to determine whether the observed difference is statistically significant or likely due to random chance.

It is especially useful when:

  • The sample size is small
  • The population standard deviation is unknown
  • The data is approximately normally distributed

  • One-sample t-test: Compares a sample mean to a known value.
  • Independent two-sample t-test: Compares means of two independent groups.
  • Paired t-test: Compares means from the same group at two different times.

3. Types of t-Tests: Mathematical Explanation

The t-test is used to determine whether there is a statistically significant difference between means. Depending on the structure of the data and the research question, three main types of t-tests are used.


1. One-Sample t-Test

The one-sample t-test is used when we want to compare the mean of a single sample to a known or hypothesized population mean.

Hypotheses

H₀: μ = μ₀    (sample mean equals population mean)
H₁: μ ≠ μ₀    (sample mean is different from population mean)

Test Statistic

The t-statistic is computed as:

$$t= \frac{\bar{x}-\mu_0}{s/\sqrt{n}}$$

where:

  • x̄ = sample mean
  • μ₀ = hypothesized population mean
  • s = sample standard deviation
  • n = sample size

This statistic measures how many standard errors the sample mean is away from the hypothesized population mean.


2. Independent (Two-Sample) t-Test

The independent t-test compares the means of two independent groups to determine whether their population means differ significantly.

Hypotheses

H₀: μ₁ = μ₂
H₁: μ₁ ≠ μ₂

Test Statistic (Equal Variance Assumption)

When the population variances are assumed equal, a pooled variance is used.

$$ t= \frac{\bar{x}_1-\bar{x}_2}{s_p\sqrt{\frac{1}{n_1}+\frac{1}{n_2}}} $$

where the pooled standard deviation is:

$$ s_p = \sqrt{\frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}} $$

Degrees of Freedom

df = n₁ + n₂ − 2

Unequal Variance Case (Welch’s t-test)

When variances are not equal, Welch’s t-test is preferred:

$$ t=\frac{\bar{x}_1-\bar{x}_2}{\sqrt{\frac{s_1^2}{n_1}+\frac{s_2^2}{n_2}}} $$

Degrees of freedom are approximated using the Welch–Satterthwaite equation.


3. Paired Sample t-Test

The paired t-test is used when observations come in pairs — for example, before-and-after measurements on the same subjects.

Hypotheses

H₀: μd = 0    (mean difference is zero)
H₁: μd ≠ 0

Test Statistic

First compute the difference for each pair:

di = x1i − x2i

Then compute:

$$ t = \frac{\bar{d}}{s_d/\sqrt{n}} $$

where:

  • \(\bar{d}\) = mean of the differences
  • sd = standard deviation of the differences
  • n = number of paired observations

Summary Table

Test Type Used When Key Assumption
One-sample t-test Compare sample mean to known value Normal population
Independent t-test Compare two independent groups Independent samples, normality
Paired t-test Compare before–after or matched data Differences are normally distributed

Key Takeaway

The t-test family provides a powerful framework for comparing means under different experimental conditions. Choosing the correct version depends on the structure of the data and the relationship between observations.

Interpretation

After calculating the t-value and degrees of freedom, the p-value can be obtained from the t-distribution table. If the p-value is less than the significance level, then the null hypothesis is rejected, indicating that there is a significant difference between the means of the two groups. If the p-value is greater than the significance level, then the null hypothesis cannot be rejected, indicating that there is not a significant difference between the means of the two groups.

t-test – Python Examples