Two-Sample Test + p-value
Background
Every offline A/B decision lands on the same scalar: a -value from a two-sample test. The Welch / large-sample form is the workhorse — it does not assume equal variances between arms, which is the canonical L4 mistake of using a pooled Student- when the variances differ. For sample sizes typical of online experiments (thousands or more per arm) the normal approximation is exact enough that practitioners report a -statistic and a two-sided -value.
Problem statement
Implement two_sample_test(x, y) returning for the difference in means between two independent samples:
where are the sample means, are the sample variances (Bessel-corrected, divisor ), and is the standard-normal CDF.
Input
x- array-like of floats, sample from arm A. Must have elements.y- array-like of floats, sample from arm B. Must have elements.
Output
(z, p)- tuple of plain Pythonfloats.zmay be negative;plies in (two-sided).
Examples
Example 1 - clear separation between arms
Input: x = [1, 2, 3, 4, 5], y = [6, 7, 8, 9, 10]
Output: (z, p) ~= (-5.0, 5.73e-7)
Explanation: , , , . . .
Example 2 - identical samples
Input: x = [1, 2, 3], y = [1, 2, 3]
Output: (0.0, 1.0)
Constraints
- Use the sample variance ( in the denominator), not the population variance.
- Two-sided -value: , computed from
math.erf(). - Raise
ValueErrorif either sample has fewer than 2 elements (sample variance undefined). - Tests use
atol ~ 1e-6on andrtol ~ 1e-3on .
Notes
- Why Welch's form (unpooled SE). Pooled-variance Student- assumes equal variances. In A/B-test data the variances of treatment and control differ routinely (a feature change that shifts watch-time means almost always shifts its variance too). Welch's form drops the assumption at no cost.
- When the normal approximation breaks. For per arm, prefer the Welch- with its degrees-of-freedom approximation. At experimentation scale this is rarely the binding constraint.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •reference clear-separation case
- •example moderate overlap
- •sample with larger arrays