Knowledge / Artificial Intelligence and Data Science / Hypothesis testing

Hypothesis testing

Artificial Intelligence and Data Science (B.E./B.Tech) · Mathematics and statistics for AI · free study notes for exams, placements and interviews

In short

Hypothesis testing is a formal procedure for deciding whether sample data provide enough evidence to reject a default claim (the null hypothesis) about a population or a model. It converts a business or scientific question into a test statistic, compares that statistic against a reference distribution, and reports a decision with a quantified error risk. It is examined in the GATE Data Science and AI paper, in university AI/DS statistics papers (AICTE model curriculum, UGC CBCS), and in CAT/placement aptitude rounds. In industry it underpins A/B tests at Flipkart, Swiggy, Zomato and PhonePe, clinical trial analysis under CDSCO/ICMR rules, model-comparison claims in ML, and quality acceptance sampling in Indian manufacturing.

Explained

Framework and definitions

Errors, power and the Neyman–Pearson view

Step-by-step procedure (exam-standard)

  1. State H₀ and H₁ in terms of a parameter; decide one- or two-tailed.
  2. Fix α and check assumptions (random sampling, independence, normality or large n, variance known/unknown, equal variances).
  3. Choose the test statistic and its null distribution, with degrees of freedom.
  4. Compute the critical value(s) from tables, or compute the p-value.
  5. Compute the statistic from the sample.
  6. Decide: reject H₀ if the statistic falls in the critical region (or p ≤ α).
  7. State the conclusion in the language of the problem, with the confidence interval and effect size.

Parametric tests for means, proportions and variances

Chi-square, ANOVA and non-parametric tests

Multiple testing, practical issues and misuse

Key points

Formulas and examples

z = (x̄ − μ₀) / (σ/√n) — one-sample z for a mean. x̄ = sample mean, μ₀ = hypothesised mean, σ = known population SD, n = sample size. Units: same as the variable; z is dimensionless.
t = (x̄ − μ₀) / (s/√n), df = n − 1 — one-sample t; s = sample SD with divisor (n − 1).
t = (x̄₁ − x̄₂) / [s_p √(1/n₁ + 1/n₂)], s_p² = [(n₁−1)s₁² + (n₂−1)s₂²]/(n₁+n₂−2), df = n₁+n₂−2 — pooled two-sample t.
t = d̄ / (s_d/√n), df = n − 1 — paired t on differences dᵢ; d̄ = mean difference, s_d = SD of differences.
z = (p̂ − p₀)/√[p₀(1−p₀)/n] — one proportion. z = (p̂₁ − p̂₂)/√[p̂(1−p̂)(1/n₁ + 1/n₂)], p̂ = (x₁+x₂)/(n₁+n₂) — two proportions.
χ² = (n − 1)s²/σ₀², df = n − 1 — test for a normal variance. χ² = Σ(O − E)²/E — goodness of fit / independence.
F = s₁²/s₂², df = (n₁ − 1, n₂ − 1) — equality of variances. F = MSB/MSW, df = (k − 1, N − k) — one-way ANOVA.
Power: 1 − β = P(reject H₀ | θ = θ₁). Sample size (two-sided, one mean): n = (z_{α/2} + z_β)² σ² / δ², where δ = μ₁ − μ₀ is the effect to detect.
Two-proportion sample size per arm: n = (z_{α/2} + z_β)² [p₁(1−p₁) + p₂(1−p₂)] / (p₁ − p₂)².
Confidence interval (mean, σ unknown): x̄ ± t_{α/2, n−1} · s/√n. Family-wise error with m tests: FWER ≈ 1 − (1 − α)m.

Example 1 — One-sample z-test (manufacturing tolerance). A Pune auto-component plant claims the mean length of a shaft is 50.0 mm with σ = 0.40 mm. A sample of n = 64 gives x̄ = 50.12 mm. Test at α = 0.05 whether the mean has shifted (two-tailed).

Example 2 — Paired t-test (training effectiveness). A BPO measures calls handled per hour for 8 agents before and after training. Differences (after − before): 4, 2, 5, −1, 3, 6, 2, 3.

Example 3 — Two-proportion z-test (A/B test on checkout). An Indian e-commerce app tests a new checkout page. Control: 1,200 users, 96 conversions (p̂₁ = 0.080). Variant: 1,200 users, 132 conversions (p̂₂ = 0.110). Test at α = 0.05, two-tailed.

Example 4 — Chi-square test of independence. 400 final-year students are classified by stream and whether they were placed.

StreamPlacedNot placedTotal
AI & DS12030150
CSE13020150
Others7030100
Total32080400

Example 5 — One-way ANOVA (three model variants). Accuracy (%) of a classifier on 4 folds each: A: 82, 84, 86, 88 (mean 85); B: 88, 90, 90, 92 (mean 90); C: 84, 86, 88, 90 (mean 87). Grand mean = (85 + 90 + 87)/3 = 87.333.

TestUseStatisticdf
z-test (mean)σ known or n ≥ 30(x̄ − μ₀)/(σ/√n)—
t-test (one sample)σ unknown, normal population(x̄ − μ₀)/(s/√n)n − 1
Pooled two-sample tTwo independent groups, equal variancesee formula boxn₁ + n₂ − 2
Welch's tTwo groups, unequal variance(x̄₁ − x̄₂)/√(s₁²/n₁ + s₂²/n₂)approximate
Paired tMatched before/afterd̄/(s_d/√n)n − 1
χ² goodness of fitObserved vs expected countsΣ(O − E)²/Ek − 1 − p
χ² independencer × c tableΣ(O − E)²/E(r−1)(c−1)
F-testEquality of two variancess₁²/s₂²(n₁−1, n₂−1)
One-way ANOVAk ≥ 3 meansMSB/MSW(k−1, N−k)
Mann–Whitney UTwo groups, no normalityrank-sum based—
from scipy import stats
import numpy as np

# Example 3 reproduced: two-proportion z-test
x1, n1, x2, n2 = 96, 1200, 132, 1200
p1, p2 = x1/n1, x2/n2
p  = (x1 + x2)/(n1 + n2)
se = np.sqrt(p*(1-p)*(1/n1 + 1/n2))
z  = (p2 - p1)/se
print(z, 2*(1 - stats.norm.cdf(abs(z))))     # 2.506, 0.0122

# Paired t-test (Example 2) and chi-square (Example 4)
d = np.array([4, 2, 5, -1, 3, 6, 2, 3])
print(stats.ttest_1samp(d, 0))               # t = 3.97, df = 7
obs = np.array([[120, 30], [130, 20], [70, 30]])
print(stats.chi2_contingency(obs, correction=False))   # chi2 = 10.42, df = 2

Where it is used

Interview questions

Common mistakes

Related topics

Statistical inference and estimation · Probability distributions · Experiment design and A/B testing · Bayesian statistics · Descriptive statistics · Causal inference basics

Open in the Knowledge reader →Find jobs →