Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Hypothesis testing: t-test and chi-square

Hypothesis testing with Student's t-tests (one-sample, two-sample, paired, Welch) and Chi-square tests (goodness-of-fit and test of independence) for deciding if sample data support a claim about populations.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Hypothesis testing (t-tests and chi-square) lets an engineer decide, from sample data, whether a claimed value or a claimed relationship is plausible for the whole population. Use cases: checking if a new manufacturing process changes a mean dimension (t-test), or if defect type is associated with supplier (chi-square for contingency tables).

Key ideas

  • Alternative hypothesis (H1 or Ha) — the claim we accept when data contradict H0; can be one-sided (>, <) or two-sided (≠).
  • Significance level (α) — the maximum probability of a Type I error (rejecting H0 when it is true). Common values: 0.05, 0.01.
  • Test statistic — a standardized number computed from sample data that has a known probability distribution under H0 (for example, Student's t-distribution or chi-square distribution).
  • p-value — the probability, under H0, of observing a test statistic as extreme as (or more extreme than) the observed value. If p ≤ α, reject H0.
  • Type I and Type II errors — Type I error: reject true H0 (probability α). Type II error: fail to reject false H0 (probability β).
  • Degrees of freedom (df) — a number that parameterizes sampling distributions (for t: df = n−1 for one-sample; for chi-square: df depends on categories or table dimensions). Always compute df correctly for critical values.

Student's t-test (derivation and assumptions)

  • Student's t-distribution — the sampling distribution of the t-statistic when the population is Normal with unknown variance. It depends on df (n−1). Properties used in exams: it is symmetric, broader than Normal for small df, and approaches Normal as df → ∞.
  • Assumptions for the t-test: (1) the sample is a simple random sample (independent observations), (2) the underlying population is approximately Normal (important for small n; for n≥30 CLT often helps), (3) measurement scale is interval or ratio.

Derive the one-sample t-statistic (what examiners expect)

  1. Let X1,...,Xn be i.i.d. from N(µ, σ^2). Define the sample mean X̄ = (1/n) Σ Xi and the sample variance S^2 = (1/(n−1)) Σ (Xi − X̄)^2.
  2. Under H0: µ = µ0, the numerator X̄ − µ0 has distribution Normal(0, σ^2/n).
  3. The quantity (n−1)S^2/σ^2 has a chi-square distribution with n−1 degrees of freedom: (n−1)S^2/σ^2 ~ χ^2_{n−1}.
  4. X̄ and S^2 are independent for normal samples. Therefore the ratio (X̄ − µ0) / (S/√n) has the Student's t-distribution with n−1 degrees of freedom. This is the standard one-sample t-statistic.

Two-sample t-tests

  • Pooled (Student) two-sample t-test assumes equal population variances σ1^2 = σ2^2. Pool sample variances to get sp^2 and use df = n1 + n2 − 2. It also assumes the two samples are independent.
  • Welch's t-test does not assume equal variances; it uses a t-statistic with approximated (non-integer) df given by the Welch–Satterthwaite formula.
  • Paired t-test is for matched pairs or before–after measurements: compute differences Di and run a one-sample t-test on D. Assumes the differences Di are approximately Normal.

Chi-square tests

  • Chi-square distribution — distribution of a sum of squared independent standard normal variables. Use it for tests where the test statistic is sum of squared standardized differences.
  • Chi-square goodness-of-fit test: compare observed counts in k categories to expected counts under H0 (a model for proportions). The test statistic is Σ (Oi − Ei)^2/Ei and under H0 approximately follows χ^2_{k−1−m} where m is number of parameters estimated from data.
  • Chi-square test of independence (contingency table): for an r×c table, expected count for cell (i,j) is (row_i_total × col_j_total)/N. The test statistic Σ (Oi − Ei)^2/Ei ~ χ^2_{(r−1)(c−1)} approximately, under H0 of independence. Assumes a random sample and independent observations.
  • Rule of thumb: expected counts Ei should be ≥ 5 for the approximation to χ^2 to be reliable. If not, combine categories or use exact tests (Fisher's exact test for 2×2).

When to use which test

  • Use a t-test when you test hypotheses about means (with continuous data). Use chi-square when you test hypotheses about categorical counts (frequencies) and relationships between categorical variables.

Formulas

X̄ = (1/n) · Σ_{i=1..n} X_i — sample mean, unit same as Xi, applies for any numeric sample. S^2 = (1/(n−1)) · Σ_{i=1..n} (X_i − X̄)^2 — sample variance, units = (units of X)^2, applies for one-sample/paired t. t = (X̄ − µ0) / (S / √n) — one-sample t-statistic, unitless, applies when population is Normal with unknown σ, df = n−1. t = (X̄1 − X̄2) / (sp · √(1/n1 + 1/n2)) — pooled two-sample t-statistic, where sp^2 = ((n1−1)S1^2 + (n2−1)S2^2) / (n1+n2−2), df = n1 + n2 − 2, unitless, applies if σ1^2 = σ2^2. Welch t = (X̄1 − X̄2) / √(S1^2/n1 + S2^2/n2) — Welch's t-statistic, unitless, df approximated by Welch–Satterthwaite formula, use when variances unequal. Degrees of freedom (Welch–Satterthwaite): df ≈ (S1^2/n1 + S2^2/n2)^2 / ((S1^2/n1)^2/(n1−1) + (S2^2/n2)^2/(n2−1)). (n−1)·S^2 / σ^2 ~ χ^2_{n−1} — relation used in t derivation, unitless. χ^2 = Σ_{cells} (O_i − E_i)^2 / E_i — chi-square test statistic, unitless, applies for goodness-of-fit and independence tests. E_{ij} = (row_i_total × col_j_total) / N — expected cell count in contingency table, units = counts, used for chi-square test of independence. df (goodness-of-fit) = k − 1 − m — k categories, m parameters estimated from data (e.g., mean known or estimated). df (contingency table) = (r − 1) · (c − 1) — r rows, c columns.

Worked examples

Example 1 — One-sample t-test (university exam style) Question: A batch claims resistors have mean resistance µ0 = 100.0 Ω. A random sample of n = 10 resistors has readings (Ω): 99.1, 100.9, 101.5, 98.7, 100.3, 100.8, 99.6, 101.1, 100.0, 101.7. Test H0: µ = 100.0 Ω vs H1: µ ≠ 100.0 Ω at α = 0.05. Steps:

  1. Compute X̄ and S. (Compute sum = 1003.7 Ω so X̄ = 1003.7/10 = 100.37 Ω.)
  2. Compute sample standard deviation S. (Compute squared deviations, find S ≈ 1.01 Ω.)
  3. Statistic: t = (X̄ − µ0) / (S / √n). t = (100.37 − 100.00) / (1.01 / √10) = 0.37 / (1.01 / 3.1623) = 0.37 / 0.3196 = 1.16 (rounded).
  4. Degrees of freedom df = n − 1 = 9. Two-tailed critical t at α = 0.05, df=9 is t_crit ≈ 2.262.
  5. Compare: observed t = 1.16 < 2.262 ⇒ fail to reject H0 at 0.05. Answer: The sample does not provide sufficient evidence at α = 0.05 to conclude the mean differs from 100.0 Ω. (Final statistic t ≈ 1.16, df = 9, decision: fail to reject H0 at 5%.)

Note: Small rounding differences in S may change t slightly (here around 1.16), but not the conclusion.

Example 2 — Two-sample pooled t-test (equal variance assumption) Question: Machine A produces n1 = 12 items with mean weight X̄1 = 50.2 g and S1 = 0.80 g. Machine B produces n2 = 10 items with X̄2 = 49.6 g and S2 = 0.60 g. Test H0: µ1 = µ2 vs H1: µ1 ≠ µ2 at α = 0.05. Steps:

  1. Compute pooled variance: sp^2 = ((n1−1)S1^2 + (n2−1)S2^2)/(n1+n2−2) = ((11·0.64) + (9·0.36))/20 = (7.04 + 3.24)/20 = 10.28/20 = 0.514 (g^2). So sp = 0.717 g.
  2. Standard error = sp · √(1/n1 + 1/n2) = 0.717 · √(1/12 + 1/10) = 0.717 · 0.4282 = 0.307 g.
  3. t = (50.2 − 49.6)/0.307 = 0.6 / 0.307 = 1.96.
  4. df = n1 + n2 − 2 = 20. Two-tailed t_crit ≈ 2.086. Since 1.96 < 2.086, fail to reject H0. Answer: No significant difference at α = 0.05 (t ≈ 1.96, df = 20).

Example 3 — Chi-square test of independence (contingency table) Question: 100 students were asked whether they prefer Subject A or B. Observed counts: Male: A = 30, B = 20 (row total 50) Female: A = 10, B = 40 (row total 50) Column totals: A = 40, B = 60, N = 100. Test H0: preference independent of gender vs H1: not independent at α = 0.05. Steps:

  1. Expected counts: E_{male,A} = (50·40)/100 = 20, E_{male,B} = 30, E_{female,A} = 20, E_{female,B} = 30.
  2. Compute χ^2 = Σ (O − E)^2 / E: (30−20)^2/20 = 100/20 = 5.00; (20−30)^2/30 = 100/30 = 3.333; (10−20)^2/20 = 100/20 = 5.00; (40−30)^2/30 = 100/30 = 3.333. Sum χ^2 = 16.666.
  3. df = (2−1)(2−1) = 1. Critical χ^2 at α = 0.05, df = 1 is 3.841. Observed 16.666 > 3.841 → reject H0. Answer: There is strong evidence of association between gender and preference (χ^2 ≈ 16.67, df = 1, p << 0.05).

Common mistakes

  • Using pooled t-test when variances are clearly unequal — use Welch's test instead.
  • For chi-square: failing to compute expected counts from marginal totals, or using chi-square when expected counts < 5 (approximation invalid).
  • Interpreting p-value as the probability H0 is true (wrong). p-value is probability of the observed data (or more extreme) given H0.
  • Forgetting to state H0, H1, α, test statistic, df and final decision in exam answers.

For your exam

Question types you will see:

  • Definitions: define t-distribution, chi-square distribution, p-value, degrees of freedom.
  • Derivations: derive the one-sample t-statistic and show why it follows Student's t with n−1 df (show the independence of X̄ and S^2 and the chi-square relation).
  • Numericals: compute t or χ^2, df, p-value or compare to critical values, and state decision and interpretation in context.
  • Short notes: compare Student vs Welch t-tests; list assumptions.
  • Diagrams: draw the t-distribution (label mean 0, tails for α/2 for two-sided test) and the chi-square distribution (skewed right, label critical value for given df).

What a full-marks answer includes:

  • Clear statement of H0 and H1, type of test (one/two-sided), and α.
  • List of assumptions and whether they hold.
  • Show formula and substitution with units, compute statistic and df.
  • Compare with critical value or compute p-value, make decision, and give an interpretation in plain language (contextual).

Quick check

  1. What is the one-sample t-statistic formula?
  2. For a 2×3 contingency table, what is the degrees of freedom used in the chi-square test of independence?
  3. Why use Welch's t-test instead of the pooled t-test? Answers: t = (X̄ − µ0)/(S/√n); (2−1)·(3−1) = 2; because Welch's test does not assume equal population variances and is safer when variances differ.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?