One-Sample t-Statistic: Real Gap or Noise?

Probability and StatisticsMedium+15 XP+15

One-Sample t-Statistic: Real Gap or Noise?

Your sample's mean came out at 5.08 and the target was 5.00. Is that a real difference, or just the wobble you'd expect from a handful of measurements?

The t-statistic answers it by measuring the gap in units of "how much wobble this sample size gives you":

t=xˉ−μ0s/nt = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}t=s/n​xˉ−μ0​​
  • xˉ\bar{x}xˉ is the sample mean, μ0\mu_0μ0​ is the value you're testing against, and nnn is how many values you have.
  • sss is the sample standard deviation — the one that divides the squared distances by n - 1.
  • s/ns / \sqrt{n}s/n​ is called the standard error: the typical wobble of a mean built from n values. More values means less wobble, which is why the same gap gets more convincing as n grows.

Task: write one_sample_t(values, mu0) returning [t, dof] — the statistic rounded to 4 decimal places, and the degrees of freedom n - 1 as a plain integer.

  • values holds at least two numbers and is never all-identical, so the standard error is never zero.
  • A negative t is perfectly normal — it just means the sample mean came in below mu0. Don't take an absolute value.

Roughly: |t| above about 2 starts to look like a real difference rather than noise, though the exact cutoff depends on the degrees of freedom.

Examples

values = [5.1, 4.9, 5.3, 5.0, 4.8]
mu0 = 5.0
returns [0.2325, 4]
values = [2, 4, 6, 8]
mu0 = 10
returns [-3.873, 3]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run