Sub topic 3
Significance, Power, and Sample Size
This chapter assumes you can serve a model behind an HTTP or RPC endpoint, read accuracy, precision, recall, and latency, and understand proportions. You should also be comfortable reading short Python.
Significance, power, and sample size
A significance test asks whether an observed difference could be chance. The p-value is the probability of seeing a difference at least this large if both versions perform the same. Power is the probability that the test detects a real difference of a given size. Sample size controls power: small samples miss real gains.
from math import sqrt
from statistics import NormalDist
def two_proportion_z_test(x_a: int, n_a: int, x_b: int, n_b: int):
p_a, p_b = x_a / n_a, x_b / n_b
pooled = (x_a + x_b) / (n_a + n_b)
se = sqrt(pooled * (1 - pooled) * (1 / n_a + 1 / n_b))
z = (p_b - p_a) / se
p_value = 2 * (1 - NormalDist().cdf(abs(z)))
return z, p_value
z, p = two_proportion_z_test(x_a=480, n_a=10000, x_b=540, n_b=10000)
print(round(z, 2), round(p, 3))Handling delayed and missing labels
Ground truth, such as whether a loan defaulted, can arrive days later or never. Log each prediction with a request ID and model version, then join labels to predictions when they arrive. Fix an observation window in advance and report how many predictions are still unlabeled. Missing labels are rarely random, so compare labeled and unlabeled groups before trusting the result.
A/B testing models against a business outcome
Choose the business outcome, such as revenue or retention, as the primary metric. Keep model metrics like precision as diagnostics and guardrails. Assign variants by a deterministic hash of the user ID so each user sees one variant for the whole test.
Pitfalls: sample ratio mismatch, peeking, novelty, and interference
- Sample ratio mismatch (SRM): a planned 50/50 split that lands at 52/48 usually signals a bug in assignment or logging. Run a chi-square test on the counts before reading results.
- Peeking: checking the p-value daily and stopping at the first value below 0.05 inflates false positives. Fix the sample size in advance.
- Novelty: users react to anything new, and the effect often fades. Run long enough to get past the first week.
- Interference: when one user's variant affects another user, such as shared inventory, the independence assumption fails.
Choosing and combining strategies
Start with shadow deployment for a new model with unknown risk, because users see no output. Move to a canary to check errors and latency on real users in small stages. Run an A/B test last, when the question is whether the business outcome improves.
Beyond fixed splits: bandits and interleaving
A bandit shifts traffic toward the better variant while the test runs. This lowers the cost of serving a weak model, but makes the statistics harder to interpret. Interleaving, used for ranking, merges results from two rankers into one list and credits each click to the ranker that produced the clicked item. It needs fewer users to detect differences. Both are advanced options, so start with fixed splits.
Exercise
The test above returns z = 1.93. Is the difference significant at alpha 0.05 with a two-sided test? Why does a larger sample help?
Show answer
No. The two-sided p-value is about 0.054, which is above 0.05. A larger sample shrinks the standard error, so the same difference produces a larger z and a smaller p-value. The test gains power.