Foundations to Frontier
Tests
A. Mathematical Foundations › A5 Statistics › Descriptive and inferential statistics

t-tests, Chi-square Tests and ANOVA

The everyday toolkit for comparing measurements and counts. One-sample, paired and Welch two-sample t-tests compare means; χ² tests check whether counts fit a model or whether two categorical variables are related; ANOVA compares several groups at once by splitting variation into between and within parts. You will compute every p-value from scratch (incomplete beta and gamma functions), see why pairing or blocking on shared conditions can turn an invisible effect into a decisive one, and learn the assumptions each test needs.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Comparing fairly

Two robot controllers are timed on a course. Controller B is faster by 1 second on average, but times vary by ±5 seconds from course to course. Is B really better, or did it get lucky?

Test both on the same courses and compare course by course. The course-to-course variation cancels, and even a small advantage stands out. That’s the idea behind the paired t-test. The family of tests on this page answers “is this difference real?” for means (t-tests), for several groups at once (ANOVA), and for counts (χ² tests).

Same courses, two controllers: paired or unpaired test?
controller Acontroller B
mean difference 1.28 s · unpaired Welch t = 0.65, p = 0.5187 · paired t = 2.93, p = 0.0109 · B faster on 12/15 courses
Each line joins one course's times: green when B was faster. Course difficulty (share ρ of the variance) affects both controllers equally. The unpaired test sees two wide overlapping clouds. The paired test looks only at within-course differences, where the course effect cancels. With ρ near 0 the advantage disappears (and the paired test loses a little power from halving its degrees of freedom).
Why engineers care

Comparing a new algorithm with the baseline, checking whether failure types depend on the test site, or deciding whether three suppliers’ parts differ: these are the most common statistical questions in engineering, and these tests answer them.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Testing both controllers on the same courses lets you…
  2. 2. A test for 'do these counts fit the expected pattern?' is a…
  3. 3. To compare the means of 4 groups at once, use…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

t-tests

  • One-sample: t=xˉ−μ0s/nt = \frac{\bar x - \mu_0}{s/\sqrt n} on n−1n - 1 df.
  • Paired: take the differences di=ai−bid_i = a_i - b_i and run a one-sample test of dˉ=0\bar d = 0.
  • Two-sample (Welch): t=aˉ−bˉsa2/na+sb2/nbt = \frac{\bar a - \bar b}{\sqrt{s_a^2/n_a + s_b^2/n_b}}, with Welch–Satterthwaite df ν=(va+vb)2va2/(na−1)+vb2/(nb−1)\nu = \frac{(v_a + v_b)^2}{v_a^2/(n_a - 1) + v_b^2/(n_b - 1)}. Prefer Welch over the “equal-variance” Student test. It costs almost nothing when the variances are equal and protects you when they aren’t.

χ² tests for counts

  • Goodness of fit: X2=∑(O−E)2EX^2 = \sum\frac{(O - E)^2}E on (categories − 1 − fitted parameters) df. For example, is a die fair?
  • Independence in an r×cr\times c table: Eij=(rowi)(colj)NE_{ij} = \frac{(\text{row}_i)(\text{col}_j)}N, with (r−1)(c−1)(r - 1)(c - 1) df.
  • Rule of thumb: expected counts ≥5\ge5. Otherwise use exact tests (Fisher).

One-way ANOVA

For kk groups and NN observations, SST=SSB+SSW\text{SST} = \text{SS}_B + \text{SS}_W, and F=SSB/(k−1)SSW/(N−k)F = \frac{\text{SS}_B/(k - 1)}{\text{SS}_W/(N - k)} on (k−1,N−k)(k - 1, N - k) df. A large FF means the groups differ by more than the within-group noise explains.

📝 Quick check (Level 1)
0/3 answered
  1. 1. A paired t-test is a one-sample t-test on…
  2. 2. Degrees of freedom for independence in a 3 × 4 table?
  3. 3. ANOVA's F compares…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

Where the p-values come from

  • Student’s t: p=Iν/(ν+t2)(ν/2,1/2)p = I_{\nu/(\nu + t^2)}(\nu/2, 1/2), via the regularized incomplete beta function (A5.1-6).
  • F distribution: P(Fd1,d2>f)=Id2/(d2+d1f)(d2/2,d1/2)P(F_{d_1,d_2} > f) = I_{d_2/(d_2 + d_1f)}(d_2/2, d_1/2).
  • χ²: P(χk2>x)=Q(k/2,x/2)P(\chi^2_k > x) = Q(k/2, x/2), the regularized upper incomplete gamma function.

Exercise 1 implements all three, plus the tests themselves.

Designing for power: pairing and blocking

If two controllers run on the same courses with course variance σc2\sigma_c^2 and residual variance σe2\sigma_e^2, then

  • the unpaired difference has variance 2(σc2+σe2)/n2(\sigma_c^2 + \sigma_e^2)/n;
  • the paired difference has variance 2σe2/n2\sigma_e^2/n.

When σc≫σe\sigma_c\gg\sigma_e, pairing can multiply the effective sample size many times over. With more than two treatments, the same idea is the randomized complete block design: SST=SStreat+SSblock+SSerror\text{SST} = \text{SS}_{\text{treat}} + \text{SS}_{\text{block}} + \text{SS}_{\text{error}}, and you test treatments against the error mean square on (k−1)(b−1)(k - 1)(b - 1) df.

Assumptions, and what to do when they fail

  • Independence: the most important assumption, and the one no test can fix.
  • Normality: for t and ANOVA, the CLT helps for moderate n. Skewed small samples call for rank tests (A5.1-9) or the bootstrap (A5.1-11).
  • Equal variances: use Welch’s t and Welch’s ANOVA.
  • χ²: expected counts not too small.
🐍 Exercise 1: t, F and χ² tests from scratch
Implement the incomplete beta and gamma functions and the t, F and χ² tail probabilities, then Welch's t, the paired t, one-way ANOVA and the χ² independence test. Check the critical values, the 5% type I error of Welch and ANOVA, and that pairing on shared courses reveals an effect the unpaired test misses.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Pairing helps most when the shared (course) variance is…
  2. 2. χ² tests need expected counts of at least about…
  3. 3. With unequal variances, prefer…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Why F follows an F distribution

Under H0H_0 (all means equal, normal errors, common σ2\sigma^2):

  1. Within-group part. SSW/σ2∼χN−k2\text{SS}_W/\sigma^2\sim\chi^2_{N-k}, a sum of within-group χnj−12\chi^2_{n_j - 1} terms (A5.1-2).
  2. Between-group part. SSB/σ2∼χk−12\text{SS}_B/\sigma^2\sim\chi^2_{k-1}: the group means are normal, and projecting out the grand mean removes one degree of freedom.
  3. Independence. The two are independent, as orthogonal projections of a Gaussian vector (Cochran’s theorem).
  4. Conclude. The ratio of independent χ2\chi^2 variables, each divided by its df, is Fk−1,N−kF_{k-1, N-k}. ∎

With k=2k = 2, F=t2F = t^2 for the equal-variance t-test.

Why Pearson’s X² is χ²

Counts OiO_i are multinomial with EOi=Ei=npi\mathbb EO_i = E_i = np_i. By the multivariate CLT, (Oi−Ei)/Ei(O_i - E_i)/\sqrt{E_i} is approximately Gaussian, with covariance I−pp⊤I - \sqrt p\sqrt p^\top, a projection of rank k−1k - 1. So X2=∑(Oi−Ei)2Ei→χk−12X^2 = \sum\frac{(O_i - E_i)^2}{E_i}\to\chi^2_{k-1}. Each estimated parameter removes one more degree of freedom. X2X^2 is also the second-order expansion of the likelihood-ratio statistic G2=2∑Olog⁡(O/E)G^2 = 2\sum O\log(O/E).

After ANOVA: which groups differ?

A significant F says “not all equal”, not which. Use post-hoc comparisons with multiplicity control: Tukey’s HSD, or Holm/Bonferroni-corrected pairwise tests (A5.1-10).

Exercise 3.1: Show that SST = SS_B + SS_W

∑j,i(xji−xˉ)2=∑j,i[(xji−xˉj)+(xˉj−xˉ)]2\sum_{j,i}(x_{ji} - \bar x)^2 = \sum_{j,i}\big[(x_{ji} - \bar x_j) + (\bar x_j - \bar x)\big]^2. The cross term is 2∑j(xˉj−xˉ)∑i(xji−xˉj)=02\sum_j(\bar x_j - \bar x)\sum_i(x_{ji} - \bar x_j) = 0, because within-group deviations sum to zero.

📝 Quick check (Level 3)
0/3 answered
  1. 1. For two groups, ANOVA's F equals…
  2. 2. The independence of SS_B and SS_W follows from…
  3. 3. Pearson's X² approximates which statistic?

Level 4: Expert

★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.

Comparing controllers across terrain: blocking

Three controller variants are each timed on the same 12 terrain courses. The courses differ by tens of seconds, while the controllers differ by 3–6 s. A one-way ANOVA that ignores the courses lumps the course variation into “noise” and finds nothing (p ≈ 0.7). The blocked analysis removes the course effect first and finds the difference decisively (p ≈ 2·10⁻⁵), with a correct false-alarm rate under the null. Benchmarking planners on the same maps, learning algorithms on the same seeds and data splits, or hardware on the same units are all blocking. Analysing them as independent groups throws the advantage away.

🐍 Exercise 2 (research): blocked ANOVA for controller benchmarking
Simulate 3 controllers × 12 courses with large course effects. Compare a one-way ANOVA that ignores courses with a randomized-complete-block ANOVA, and check that the blocked test has a 5% type I error under the null.

These tests in AI, robotics and aerospace

  • Benchmark comparisons. Paired tests over the same seeds, scenarios or datasets. Multiple algorithms across multiple datasets use Friedman/Nemenyi rank tests (Demšar, 2006).
  • Failure analysis. χ² tests of whether failure modes depend on site, temperature band or software version, as in contingency tables of anomaly reports.
  • Design of experiments. ANOVA and factorial designs identify which factors (gains, sampling rates, materials) matter in hardware testing, with blocking on test day or unit (Montgomery, 2017).
  • Sensor and part acceptance. Two-sample tests compare a new supplier’s parts with the qualified baseline. Equivalence tests (TOST) demonstrate “no meaningful difference”.
  • Simulation-to-reality checks. t-tests on paired sim/real metrics per scenario flag where a simulator is biased.

References

  1. “Student” (1908). “The probable error of a mean.” Biometrika, 6(1).
  2. B. L. Welch (1947). “The generalization of Student’s problem when several different population variances are involved.” Biometrika, 34.
  3. K. Pearson (1900). “On the criterion that a given system of deviations from the probable in the case of a correlated system of variables is such that it can be reasonably supposed to have arisen from random sampling.” Philosophical Magazine, 50(302).
  4. R. A. Fisher (1935). The Design of Experiments. Oliver & Boyd.
  5. W. G. Cochran (1934). “The distribution of quadratic forms in a normal system.” Math. Proc. Cambridge Phil. Soc., 30(2).
  6. D. Montgomery (2017). Design and Analysis of Experiments, 9th ed. Wiley.
  7. J. Demšar (2006). “Statistical comparisons of classifiers over multiple data sets.” JMLR, 7.
  8. J. Tukey (1949). “Comparing individual means in the analysis of variance.” Biometrics, 5(2).
  9. A. Agresti (2013). Categorical Data Analysis, 3rd ed. Wiley.
  10. D. Schuirmann (1987). “A comparison of the two one-sided tests procedure and the power approach for assessing equivalence.” J. Pharmacokinetics and Biopharmaceutics, 15.