Practice / Probability for ML
Hypothesis tests and p-values: z and t tests, power and multiple comparisons
Before you start
A hypothesis test asks whether the data are surprising if nothing interesting is going on. The "nothing interesting" is the null hypothesis : the new model is no faster, the new button converts no better, the two classifiers are equally accurate. The test turns the data into one statistic whose distribution under is known, usually a standard normal or a distribution because of the central limit theorem, and the p-value is the probability, computed as if were true, of a statistic at least as extreme as the one observed. A small p-value says the data would be unusual under ; it does not say how likely is, and a large one does not say the effect is zero. These ten problems run the z test, the t test, the two-proportion test behind every A/B test and the paired test behind every model comparison by hand, then work out power, sample size, the cost of running many tests at once, and an exact test that needs no normal approximation at all.
- The recipe. State and the alternative , and pick the level (usually ) before looking at the data. Compute a test statistic, find its p-value under , and reject when .
- z test for a mean with known : . Two-sided, ; one-sided for , . The two-sided cutoff is , the one-sided one .
- t test when is estimated by the sample standard deviation : has a distribution with degrees of freedom, whose tails are heavier than the normal's. For degrees of freedom the two-sided cutoff is .
- Two proportions. For and under , pool them, , and use .
- Confidence interval duality. A two-sided test at level rejects exactly when lies outside the confidence interval .
- Errors and power. A type I error rejects a true , and happens with probability . A type II error keeps a false one. The power is the probability of rejecting when a specific alternative is true. For a two-sided z test with true shift standard errors, power .
- Sample size for power against a difference : , rounded up. For two-sided and power, .
- Many tests. With independent true nulls each tested at , . Bonferroni tests each at , which keeps that probability at most .
Builds on: Sampling distributions and the central limit theorem: standard errors, confidence intervals and the √N rule, Continuous random variables: densities, CDFs, expectation and the uniform, exponential and normal, Discrete random variables: expectation, variance and the binomial, geometric and Poisson
Problems
- ·
A serving stack is documented at a mean latency of ms with a standard deviation of ms. After an update, requests average ms. Test against at the level, assuming is unchanged.
- ·
For the data of Problem 1, find the p-value of the one-sided test of . Then suppose the sample mean had been ms instead, so : find the one-sided and two-sided p-values, and say which one may be reported.
- ··
For the data of Problem 1, find the confidence interval for . Use it to decide, without computing a new statistic, whether a two-sided test rejects and , and check the second with its p-value.
- ··
Nine runs of a training job take hours on average, with sample standard deviation hours. The previous version averaged hours. Test against at the level.
- ··
In an A/B test, of users convert with the old checkout and of with the new one. Test whether the conversion rates differ, two-sided at .
- ··
For the test of Problem 1 (, , two-sided ), find the probability of a type I error, and the power if the true mean latency is ms.
- ··
How many requests are needed for the test of Problem 1 to have power against a ms change in mean latency, still two-sided at with ? Check the power at that .
- ···
Two classifiers are scored on the same test examples. Both are right on , both wrong on , only A is right on and only B is right on . Test whether their accuracies differ with McNemar's test, using the normal approximation and the exact binomial version, and compare with an unpaired two-proportion test.
- ··
A team tracks metrics in an experiment where, in fact, nothing changed, and tests each at the level, independently. Find the probability that at least one metric comes out significant and the expected number that do. Repeat with the Bonferroni level, and say whether a metric with survives it.
- ···
Three seeds of model A score , and ; three seeds of model B score , and . Run an exact permutation test of whether A scores higher, using the difference in mean scores, one-sided and two-sided. What is the smallest p-value this design can produce?
Answers
- , : reject ; the update changed the mean latency
- one-sided for ; for , one-sided and two-sided. The one-sided value is valid only if was fixed before the data were seen
- The interval is : it rejects and keeps . For , and
- and : do not reject at the level
- and : the new checkout converts better, just significantly
- Type I error ; power
- ; then and the power is
- McNemar: , (exact ), so A is more accurate. The unpaired test gives ,
- and one false positive expected; with Bonferroni, . The metric with does not survive, since
- One-sided , two-sided ; the smallest possible one-sided p-value is
Worked solutions
Problem 1
A serving stack is documented at a mean latency of ms with a standard deviation of ms. After an update, requests average ms. Test against at the level, assuming is unchanged.
- The standard error of the mean of requests.
- The sample mean is two standard errors above the null value.
- The upper tail of the standard normal beyond .
- Two-sided: a mean of ms would have been just as surprising, so both tails count.
- , : reject ; the update changed the mean latencyEquivalently, . The p-value is the chance of data this extreme if the mean were still ms, not the chance that it is ms (Mistake 1).
Problem 2
For the data of Problem 1, find the p-value of the one-sided test of . Then suppose the sample mean had been ms instead, so : find the one-sided and two-sided p-values, and say which one may be reported.
- Only the upper tail counts when says the mean went up.
- The new sample mean.
- Not significant at .
- Significant at , if the one-sided test had been chosen.
- one-sided for ; for , one-sided and two-sided. The one-sided value is valid only if was fixed before the data were seenA one-sided test is right when only one direction matters, such as "is it slower?". Picking the side that the data happen to favor doubles the false-positive rate (Mistake 2).
Problem 3
For the data of Problem 1, find the confidence interval for . Use it to decide, without computing a new statistic, whether a two-sided test rejects and , and check the second with its p-value.
- The margin of error: standard errors.
- The confidence interval.
- So the test rejects , as in Problem 1.
- So the test keeps .
- The interval is : it rejects and keeps . For , and The interval is exactly the set of null values a two-sided test would keep, and it also says how big the change plausibly is, which the p-value does not.
Problem 4
Nine runs of a training job take hours on average, with sample standard deviation hours. The previous version averaged hours. Test against at the level.
- The estimated standard error.
- The statistic, with replaced by .
- , cutoff Estimating from nine runs adds uncertainty, so the cutoff is wider than .
- The two tails of the distribution with degrees of freedom beyond .
- and : do not reject at the levelThe evidence that the job got slower is suggestive, not conclusive. Treating as a z statistic gives and the opposite conclusion (Mistake 3).
Problem 5
In an A/B test, of users convert with the old checkout and of with the new one. Test whether the conversion rates differ, two-sided at .
- , , difference Two percentage points.
- Under the rates are equal, so pool all users to estimate the common rate.
- The Bernoulli variance at the pooled rate.
- The variance of a difference of independent estimates is the sum of their variances.
- and : the new checkout converts better, just significantlyThe unpooled standard error is also about here, so the choice barely matters at these sizes. Using one group's standard error alone gives (Mistake 4).
Problem 6
For the test of Problem 1 (, , two-sided ), find the probability of a type I error, and the power if the true mean latency is ms.
- By construction: under , has probability .
- Reject when or , the cutoffs on the scale of the sample mean.
- Under the alternative, .
- and The lower tail is negligible.
- Type I error ; power The same as with . A real ms slowdown is missed one time in three, so a p-value above from this design is weak evidence of no change.
Problem 7
How many requests are needed for the test of Problem 1 to have power against a ms change in mean latency, still two-sided at with ? Check the power at that .
- , .
- The number of standard deviations of the data the effect must be, scaled up.
- The formula .
- Round up: requests fall just short of .
- ; then and the power is Halving the effect to detect quadruples the sample size, since grows like .
Problem 8
Two classifiers are scored on the same test examples. Both are right on , both wrong on , only A is right on and only B is right on . Test whether their accuracies differ with McNemar's test, using the normal approximation and the exact binomial version, and compare with an unpaired two-proportion test.
- Accuracies and A is right on , B on .
- Only the disagreements carry informationOn the examples where the models agree, the difference in accuracy is . Under , each disagreement favors A with probability .
- , so Under , A's wins are , with mean and standard deviation ; is the same .
- Exact: The normal approximation is a little optimistic here.
- McNemar: , (exact ), so A is more accurate. The unpaired test gives , The unpaired test treats the two accuracies as independent samples, but they come from the same examples. Pairing removes the shared difficulty of the examples, which is why it is the right test, and the more powerful one.
Problem 9
A team tracks metrics in an experiment where, in fact, nothing changed, and tests each at the level, independently. Find the probability that at least one metric comes out significant and the expected number that do. Repeat with the Bonferroni level, and say whether a metric with survives it.
- Each true null is kept with probability , independently.
- At least one false positive is more likely than not.
- The expected number of false positives, by linearity, independent or not.
- , and Bonferroni: test each metric at .
- and one false positive expected; with Bonferroni, . The metric with does not survive, since Bonferroni holds the chance of any false alarm at even when the tests are dependent, at the price of power (Mistake 5).
Problem 10
Three seeds of model A score , and ; three seeds of model B score , and . Run an exact permutation test of whether A scores higher, using the difference in mean scores, one-sided and two-sided. What is the smallest p-value this design can produce?
- Observed difference: The two groups' means.
- There are ways to label three of the six scores "A"Under the labels are exchangeable, so each split is equally likely.
- Splits with mean difference at least : and The difference is at least exactly when the "A" scores sum to at least , since the total is .
- Splits at least apart in either direction: those two and their mirror images , Sums at most .
- One-sided , two-sided ; the smallest possible one-sided p-value is Three seeds per model can never reach significance below , however large the gap. The test is exact: no normal approximation or equal-variance assumption is needed.
Where this goes wrong
1. The p-value read as the probability that the null is true
A p-value of invites the sentence "there is only a chance the update did nothing".
- Right so far: Problem 1.
- “So .”That flips the conditional. Getting needs Bayes' rule, a prior and the power.
- Among significant results, only are false alarmsSuppose of tested changes do nothing, and the test has power for the rest. False alarms then make up of all tests and true discoveries , so : over a third of the significant results are false alarms.
2. Choosing the side after seeing the data
The data in Problem 2 point upward, so a one-sided test toward "up" looks natural.
- , two-sided Right so far: Problem 2.
- “The mean went up, so test .”The direction was chosen by the data. Had the mean gone down, the same analyst would have tested .
- , significant at The rule "reject if or , whichever way the data went" is a two-sided test at level , not . The honest p-value is .
3. z instead of t with nine observations
The t statistic in Problem 4 has the same form as a z statistic, so the normal table seems to apply.
- Right so far: Problem 4.
- “Compare with the normal: .” was estimated from the same nine runs, so the statistic varies more than a standard normal. The reference is with degrees of freedom.
- , rejectThe right p-value is , and falls short of the cutoff . For large the two agree; at they give opposite conclusions.
4. One group's standard error used for the difference
Each conversion rate has a standard error, and the formula is the familiar one.
- , per groupRight so far: Problem 5.
- “.”That is the standard error of one rate. The difference of two independent rates has variance equal to the sum of theirs, which adds the second .
- , The right standard error is , giving and . The mistake makes the result look ten times more significant than it is.
5. Testing twenty metrics and reporting the one that passed
Each metric was tested correctly at , so any one that comes out significant seems to be a real finding.
- Each test alone has a false-positive rateRight so far.
- “Report the metric with as a significant improvement.”The chance that some metric out of twenty passes by luck is not .
- The improvement is significant at the levelWith true nulls, , and one false positive is expected (Problem 9). At the Bonferroni level , is nowhere near significant; or pick one primary metric before the experiment starts.
Print this set: hypothesis-tests-and-p-values.pdf (problems, answers, and worked solutions on separate pages).
Read the guide: Hypothesis tests and p-values: standardize under the null, choose the tails first, and never read p as the chance the null is true
More at eulerpad.app. This is one of 82 free practice sets with worked solutions at eulerpad.app/practice, alongside guides to the topics behind them.
EulerPad, the app, reads your handwritten math on iPad, iPhone and Android and checks it line by line. It's free to start.
This set is also Chapter 18 of Probability & Statistics for Machine Learning, a 250-problem workbook with worked solutions: eulerpad.app/workbooks