Lesson 9 of 18

One-Sample t-Test

Hypothesis Testing

The one-sample t-test checks whether the mean of your data is significantly different from a hypothesized value.

Hypotheses:

  • H0H_0 (null): mu=mu0mu = mu_0 (the mean equals the hypothesized value)
  • H1H_1 (alternative): mueqmu0mu eq mu_0
import math, statistics

data = [2.1, 2.5, 2.3, 2.8, 2.4]
mu_0 = 2.0   # hypothesized mean
n = len(data)

t_stat = (statistics.fmean(data) - mu_0) / (statistics.stdev(data) / math.sqrt(n))
print(round(t_stat, 4))   # 4.0

The t-Statistic

t = rac{ar{x} - mu_0}{s / sqrt{n}}

A large t|t| means the sample mean is far from mu0mu_0 relative to the data's variability.

The p-value

The pp-value is the probability of observing a tt-statistic this extreme (or more) if H0H_0 were true.

  • p<0.05p < 0.05 → statistically significant (reject H0H_0 at 5% significance level)
  • pgeq0.05p geq 0.05 → insufficient evidence to reject H0H_0

Special Case: Testing Against the Sample Mean

When mu0mu_0 equals the sample mean exactly, t=0t = 0 and p=1.0p = 1.0.

Your Task

Implement ttest_one_sample(data, mu) that prints the tt-statistic (rounded to 4 decimal places) and whether the result is significant (p<0.05p < 0.05).

Pyodide loading...
Loading...
Click "Run" to execute your code.