Machine LearningStatistics and Model Evaluation

What is hypothesis testing in machine learning?

Question 283 of 303 · Updated

On this page

Hypothesis testing is a statistical framework for deciding whether observed data provides enough evidence against a specific claim about a population or process. In machine learning, it helps answer questions such as:

  • Did a new model genuinely improve performance, or could the measured difference be sampling noise?
  • Is a feature associated with the target beyond what random variation might explain?
  • Did an experiment change conversion, error rate, or another business metric?
  • Is a measured difference large and reliable enough to justify a production decision?

A hypothesis test does not prove that a model is better or that a claim is true. It quantifies how compatible the observed data is with a specified null hypothesis under the assumptions of a chosen statistical procedure.

This lesson covers foundational, prespecified hypothesis testing. Repeated monitoring and sequential tests, and correction across many simultaneous metrics or segments, are separate topics and require additional methods.

1. The two competing hypotheses#

A statistical test begins by defining two statements about a population parameter or data-generating process.

  • Null hypothesis (H₀): the reference claim, often “no difference,” “no effect,” or “the parameter equals a specified value.”
  • Alternative hypothesis (H₁ or Hₐ): the competing claim that the data may support.

Example: a team compares a new classifier with the existing classifier.

  • H₀: the population difference in their error rates is zero.
  • H₁: the population difference in their error rates is not zero.

The hypotheses should be written before inspecting the outcome. Define the population, metric, unit of analysis, direction of interest, and comparison clearly. A vague statement such as “the new model is better” is not yet a testable hypothesis.

One-sided and two-sided alternatives#

A two-sided test asks whether the parameter differs in either direction:

H₀: μ = μ₀ H₁: μ ≠ μ₀

A one-sided test asks about a prespecified direction:

H₀: μ ≤ μ₀ H₁: μ > μ₀

Use a one-sided test only when the direction was justified before looking at the data and an effect in the opposite direction would not count as evidence for the stated claim. Choosing the tail after seeing results inflates the chance of a false positive.

2. The test statistic and the p-value#

A test statistic compresses the observed data into a quantity whose behavior is known or approximated under H₀. It measures how far the observed result is from what the null model predicts, relative to its uncertainty.

For a one-sample mean with unknown population standard deviation, a common statistic is:

LaTeX

where:

  • x̄ is the sample mean.
  • μ₀ is the mean specified by H₀.
  • s is the sample standard deviation.
  • n is the sample size.
  • s/√n is the estimated standard error of the sample mean.

The p-value is the probability, assuming H₀ and the test assumptions are true, of obtaining a test statistic at least as extreme as the observed statistic in the direction(s) defined by H₁.

For a two-sided test, “at least as extreme” includes outcomes in both tails.

What a p-value does not mean#

A p-value of 0.03 does not mean:

  • There is a 3% probability that H₀ is true.
  • There is a 97% probability that H₁ is true.
  • The result has a 3% chance of being wrong.
  • The effect is large, useful, or important.
  • The model will perform better in production.

It means that, if the null hypothesis and assumptions held, data at least this extreme would occur with probability 3% under the test's reference distribution.

3. Significance level and the decision rule#

The significance level, written α, is a threshold chosen before the test. Common choices are 0.05, 0.01, and 0.10.

A conventional decision rule is:

  • If p > α: fail to reject H₀.

Suppose α = 0.05:

Failing to reject H₀ is not the same as accepting or proving H₀. The data may be too noisy or the sample too small to detect a real effect.

The significance threshold is a decision rule, not a natural boundary between important and unimportant results. A p-value of 0.049 and one of 0.051 are very similar evidence; they should not be described as fundamentally different discoveries.

4. Numerical example: did a model reduce prediction error?#

Suppose an ML team evaluates a new regression model on 100 independent cases and compares its absolute error with the current model on the same cases.

Define each paired difference as:

LaTeX

Positive values mean the new model has lower error.

The team observes:

  • Number of paired cases, n = 100.
  • Mean improvement, d̄ = 2.4 error units.
  • Standard deviation of paired differences, sᵈ = 6.0.
  • Test statistic = 2.4 / 0.6 = 4.0.

The hypotheses for a two-sided paired t-test are:

  • H₁: μd ≠ 0.

With 99 degrees of freedom, a t statistic of 4.0 gives a two-sided p-value of roughly 0.0001 (the exact value depends on the calculation and rounding). At α = 0.05, the team rejects H₀.

An approximate 95% confidence interval is:

2.4 ± 1.984 × 0.6 = 2.4 ± 1.19 ≈ [1.21, 3.59]

Under the assumptions of this paired analysis, the interval estimates the population mean improvement. It excludes zero, consistent with rejecting H₀ at the 5% level.

Interpretation: the measured improvement is unlikely to be explained by sampling variation alone under the null model. The team should still check whether a 2.4-unit reduction is practically valuable, whether the sample represents production traffic, and whether the test design was valid.

This paired example is appropriate only if the 100 case-level differences are independent or the analysis accounts for their dependence. If many cases belong to the same customer, device, or time block, ordinary independent-case standard errors may be too optimistic.

5. Type I and Type II errors#

A hypothesis test can make two kinds of mistakes.

Type I error (false positive)#

Rejecting H₀ even though H₀ is true.

Example: the team concludes that a new fraud model improves performance when the true population improvement is zero.

The test's significance level α controls the long-run probability of Type I error under the null and the test assumptions. If α = 0.05, the procedure has a 5% Type I error rate in repeated use under those conditions. It does not mean every individual rejected result has a 5% probability of being false.

Type II error (false negative)#

Failing to reject H₀ even though a specified alternative is true.

Example: the new model genuinely reduces error, but the experiment is too small or noisy to detect the improvement.

The probability of Type II error is denoted β and depends on the particular alternative effect, sample size, variability, and test procedure.

Statistical power#

Power is the probability of rejecting H₀ when a particular alternative is true:

Power = 1 − β

If β = 0.20 for a prespecified effect size, power is 0.80, or 80%.

Power is not a fixed property of a test independent of the effect being studied. A test can have high power for a large improvement and low power for a small one.

6. What affects power and sample size?#

Power generally increases when:

  • Sample size increases.
  • The true effect is larger.
  • Measurement noise or outcome variability is lower.
  • The test uses an appropriate design and statistic.
  • The significance threshold α is increased, although that also raises Type I error risk.

Before an experiment, choose a minimum practically important effect. For example, the team may decide that a reduction smaller than 1 error unit does not justify the added inference cost or operational complexity. Then estimate the sample size needed to detect that effect at a chosen α and target power.

For a simple two-sided comparison of independent means with equal group sizes, a rough planning approximation is:

LaTeX

where σ is the assumed common standard deviation and δ is the target mean difference. This approximation relies on distribution and design assumptions; use an appropriate power calculation for the actual metric and test.

Do not decide sample size by repeatedly checking whether p has crossed 0.05 unless the experiment uses a valid sequential design. That is a different testing procedure.

7. Confidence intervals and hypothesis tests#

A confidence interval communicates a range of parameter values compatible with the data and the interval procedure. It often provides more information than a binary reject/fail-to-reject decision.

For a two-sided test of H₀: μ = μ₀ at significance level α, the corresponding (1 − α) confidence interval excludes μ₀ when the test rejects, provided both procedures use compatible assumptions and methods.

For the numerical example, the 95% interval [1.21, 3.59] excludes zero, matching rejection of H₀: μd = 0 at α = 0.05.

Report the estimated effect and interval, not only the p-value. The interval helps readers assess both uncertainty and plausible effect magnitude.

8. Which statistical test should an ML practitioner use?#

The test depends on the outcome, study design, sampling process, and assumptions. There is no single test that is correct for every ML comparison.

  • One-sample t-test: compare a sample mean with a reference value when the assumptions for the t procedure are reasonable.
  • Independent two-sample t-test: compare means from independent groups. Welch's t-test is a common choice when equal population variances should not be assumed.
  • Paired t-test: compare two measurements on the same units, such as old and new model errors on the same examples; analyze within-unit differences.
  • Proportion tests: compare rates such as conversion, click-through, or classification error proportions in independent groups, using a method appropriate to sample size and design.
  • Chi-square test: examine association between categorical variables, such as treatment assignment and conversion outcome, when expected-count assumptions are suitable.
  • Permutation/randomization test: construct a reference distribution by rearranging labels or assignments in a way justified by the experiment's randomization or exchangeability structure.
  • Mann–Whitney U test: compare distributions or stochastic ordering for independent samples; it is not automatically a test of medians unless additional distribution-shape assumptions hold.
  • Bootstrap confidence intervals: estimate uncertainty by resampling at the correct independent sampling unit. Bootstrap intervals are estimation tools; they do not remove the need for a valid design.

For cross-validation comparisons, do not automatically treat each fold score as an independent observation. Training sets overlap, fold scores are dependent, and a naive t-test over a small number of folds can produce misleading uncertainty. Use a method designed for the evaluation design, repeated independent experiments where appropriate, or a properly randomized held-out evaluation.

9. How hypothesis testing is used in machine learning#

A. Comparing two model versions#

Define the primary metric and evaluation unit before testing. If both models predict the same held-out examples, preserve the pairing. For example, compare per-customer loss differences rather than treating the two sets of scores as unrelated samples.

For classification, the right comparison may be paired correctness outcomes, a paired loss such as log loss, or a task-specific metric. The test must match the metric and data structure.

B. Feature and coefficient analysis#

In statistical models, tests may assess whether a parameter is compatible with a reference value such as zero. For example, a regression coefficient test asks whether the data provide evidence of an association under the model assumptions.

A small p-value does not establish causality, predictive value, or that the feature should remain in the model. Correlated predictors, model misspecification, feature selection, and many simultaneous tests complicate interpretation.

C. A/B experiments#

Randomly assign eligible users or entities to control and treatment, define the primary metric and analysis window, and compare outcomes using a method consistent with the randomization unit.

If assignment is by user, multiple events from one user are not automatically independent observations. If randomization is by region or store, the analysis must respect that cluster-level assignment.

D. Data and process checks#

Hypothesis tests can help investigate whether a measured difference in a data pipeline, feature distribution, or process metric is inconsistent with a specified reference. Statistical significance should be combined with domain checks: a statistically detectable change may be operationally harmless, while a nonsignificant result may still be too uncertain to declare the system safe.

10. A practical ML experiment workflow#

  1. Write the question in measurable terms. Example: “Does model B reduce mean per-customer absolute error relative to model A?”
  2. Specify H₀ and H₁. Define the population parameter and whether the test is one-sided or two-sided.
  3. Choose the primary metric and meaningful effect size. Avoid changing the target metric after looking at results.
  4. Choose the design and unit of analysis. Preserve pairing, grouping, time ordering, or randomization clusters.
  5. Select a suitable test. Check assumptions and use a robust or resampling alternative when appropriate.
  6. Plan sample size and power. State the assumed variability, target effect, α, and desired power.
  7. Run the prespecified analysis. Report the estimate, confidence interval, p-value, sample size, and method.
  8. Assess practical significance. Compare the estimated improvement with the minimum useful improvement and operational costs.
  9. Validate generalization. Use a separate test period or production experiment as appropriate; avoid reusing the same test set for repeated tuning.
  10. Document the decision. Record the hypotheses, analysis plan, exclusions, metric definitions, results, and limitations.

11. Common mistakes and misconceptions#

  • “p < 0.05 proves the alternative hypothesis.” No. It indicates incompatibility with H₀ at the chosen threshold under the test assumptions; it does not prove H₁.
  • “p > 0.05 proves there is no effect.” No. The study may have low power or wide uncertainty. Consider the confidence interval and whether it rules out effects that matter.
  • “A p-value is the probability the result happened by chance.” This is not its definition. It is calculated assuming H₀ and the test model.
  • “Statistical significance means business significance.” No. A tiny effect can be statistically detectable in a huge sample but irrelevant in practice.
  • “A large observed improvement must be real.” No. Small samples, noisy metrics, leakage, selection, and invalid independence assumptions can produce unstable results.
  • “Use a t-test on any two lists of model scores.” No. Determine whether observations are paired, independent, grouped, or time-dependent.
  • “Try many tests and report the smallest p-value.” This inflates false discoveries. Prespecify the primary analysis and use appropriate multiple-testing control when examining many hypotheses. Repeated peeking requires sequentially valid methods, not an ordinary fixed-sample test.

12. Short worked interpretation#

A model experiment reports:

  • Estimated reduction in average error: 0.8 units.
  • 95% confidence interval: [0.1, 1.5].
  • p-value: 0.024.
  • Prespecified α: 0.05.
  • Minimum useful reduction: 1.0 unit.

The test rejects the null of zero average reduction at the 5% level because 0.024 < 0.05, and the confidence interval excludes zero. But the estimated reduction is 0.8, below the team's 1.0-unit practical threshold. The interval includes both a negligible improvement and an improvement greater than 1.0.

A careful conclusion is: there is evidence of a nonzero average reduction, but the experiment does not yet establish that the improvement is large enough to meet the practical target. The team may need a larger or more precise study, and should weigh inference cost and operational effects before deployment.

Key takeaways#

  • Hypothesis testing evaluates data against a clearly stated null hypothesis.
  • The p-value measures how extreme the observed result is under H₀; it is not the probability that H₀ is true.
  • α controls the Type I error rate under the procedure's assumptions; β is Type II error, and power is 1 − β.
  • Confidence intervals and effect sizes communicate magnitude and uncertainty; statistical significance alone does not establish practical value.
  • In ML, the test must respect pairing, grouping, time, randomization, and the metric being evaluated.
  • Prespecify analyses and avoid naive repeated testing or treating dependent cross-validation folds as independent samples.

Bottom line: use hypothesis testing as one part of disciplined ML experimentation—not as an automatic “winner detector.” A valid design, appropriate statistical test, uncertainty interval, practical effect threshold, and independent validation matter just as much as the p-value.

Related questions

Explore other topics:Deep LearningMLOps