Module 2, topic 19, week 10, Tuesday
Hypothesis testing: p-values, significance, common tests (t-test, chi-square)
Is it real, or could it be chance? The logic of a test, the p-value and the significance level, the two kinds of errors, then the tests used most: z, t for one sample, two samples and pairs, proportions and chi-square. Worked by hand.
Introduction: is it real, or could it be chance?
The seventh lesson of Module 2. The last lesson described a sample with a few numbers. But a sample is never exactly like the population it comes from: take another sample and the mean is a little different. So whenever data shows something interesting (the new design sells more, the machine fills too little, the second model is more accurate), one question must be answered first: is it real, or could it be chance? Hypothesis testing is the method for this question. This guide explains the logic of a test, the null and the alternative hypothesis, the test statistic, the p-value and the significance level, the two kinds of errors and the power of a test. Then it teaches the tests that are used most: the z-test, the t-test for one sample, for two samples and for pairs, the tests for proportions, and the two chi-square tests. Every idea comes as a definition and worked examples calculated by hand. Then the guide walks through 26 solved problems from the easiest to the hardest, and at the end there are 40 practice problems with a hint and a full solution for each.
You need the last three lessons: probabilities (Topic 16), the normal distribution, z-scores, the normal table and the standard error (Topic 17), and the sample mean and the sample standard deviation (Topic 18). A calculator with a square root key is enough. As in the whole module, the work is done by hand; Python appears only in one short part near the end.
What you will learn
- 1.Why a difference in data must be tested before it is believed
- 2.The logic of a test: the null hypothesis, the alternative hypothesis, and "innocent until proven guilty"
- 3.The steps of every test, and one-tailed against two-tailed tests
- 4.What a p-value is, what it is not, and how the significance level turns it into a decision
- 5.The z-test for a mean when is known
- 6.The two kinds of errors (Type I and Type II) and the power of a test
- 7.The t distribution, degrees of freedom and the t-test for one sample
- 8.The t-test for two independent samples and the paired t-test
- 9.The z-tests for one proportion and for two proportions (the A/B test)
- 10.The chi-square tests: goodness of fit and independence
- 11.How to choose the right test, and the conditions each test needs
- 12.Why "significant" does not mean "important": effect size, sample size and multiple testing
- 13.Where tests are used in machine learning
| Written | Meaning |
|---|---|
| the null hypothesis: "nothing special is going on" | |
| the alternative hypothesis: what we look for evidence of | |
| , | the value of the mean or of the proportion that claims |
| the significance level: the chance of rejecting a true that we accept (the Greek letter alpha) | |
| the probability of not rejecting a false (the Greek letter beta); is the power | |
| , , | test statistics; is the Greek letter chi, "chi-square" |
| p-value | the probability of a result at least as extreme as ours, if is true |
| degrees of freedom | |
| , , | the mean, the standard deviation and the size of a sample |
| the proportion in a sample ("p hat") | |
| , | an observed count and the count expected under |
| the standard error: how much a sample mean or a sample proportion varies from sample to sample |
1Why hypothesis testing?
1.1A difference is not yet a discovery
A website shows a new button to 1,000 visitors, and 122 of them buy. With the old button, 10 percent used to buy. Is the new button better?
It looks so: 12.2 percent is more than 10 percent. But toss a fair coin 100 times and you will rarely get exactly 50 heads. Samples vary by chance (Topic 17), so 122 buyers instead of the usual 100 may mean nothing at all. Before the company rebuilds its website, it needs to know how likely such a result would be if the button had changed nothing. If that is likely, there is no evidence. If it is very unlikely, the button probably works.
That is the whole idea of a hypothesis test: measure how surprising the data would be if nothing had changed.
1.2Where the question appears
| Field | The question | What is tested |
|---|---|---|
| Product and marketing | Does version B of a page sell more than version A? | two proportions (an A/B test) |
| Production | Is the machine still filling 500 g on average? | one mean |
| Medicine | Does the new medicine lower blood pressure more than the old one? | two means |
| Teaching | Did the course improve the students' results? | pairs (before and after) |
| Surveys | Does the preferred product depend on the age group? | two categorical variables |
| Machine learning | Is model B really more accurate than model A, or was it luck on this test set? | pairs of results |
1.3Where you will meet these ideas again
| Later in the course | What it takes from this lesson |
|---|---|
| Confidence intervals (Topic 20) | the same standard errors and the same t table, read the other way round |
| Correlation (Topic 20) | a test of whether a correlation is different from 0 |
| Linear and logistic regression | every coefficient comes with a p-value: "is this feature's effect different from 0?" |
| Feature selection | the chi-square test ranks categorical features by their link with the target |
| Model evaluation | comparing two models on the same folds with a paired t-test |
| A/B testing in practice | sample size, power and the danger of stopping an experiment early |
2The logic of a test
2.1Innocent until proven guilty
A court does not ask the accused to prove innocence. It assumes innocence and asks: is the evidence so strong that innocence is no longer believable? If yes, the verdict is "guilty". If no, the verdict is "not guilty", which is not the same as "proven innocent": it only says that the evidence was not enough.
A hypothesis test works in exactly this way.
| In court | In a test |
|---|---|
| the accused is assumed innocent | the null hypothesis is assumed true |
| the prosecutor claims guilt | the alternative hypothesis |
| the evidence | the data of the sample |
| "beyond reasonable doubt" | the significance level |
| verdict "guilty" | reject |
| verdict "not guilty" | fail to reject (not: "accept ") |
2.2The two hypotheses
- The null hypothesis says that nothing special is going on: no difference, no effect, the old value still holds. It always contains an equals sign: , , .
- The alternative hypothesis is what the researcher looks for evidence of: , , .
Hypotheses are statements about the population (about or ), never about the sample. We know the sample mean exactly; there is nothing to test about it.
2.3The idea in one example
A coin is tossed 100 times and shows 60 heads. Does it show heads too often?
- : the coin is fair,
- : the coin shows heads too often,
Assume . Then about 50 heads are expected, and Topic 17 calculated how unusual 60 or more are: the probability is about 0.03. So if the coin is fair, a result like ours happens in about 3 of 100 experiments. That is rare. We have two explanations: either something rare happened, or the coin is not fair. The smaller this probability, the harder it is to believe the first explanation.
The test does not prove that the coin is unfair. It says: the data fits badly to a fair coin.
Try it yourself
Show a hint
Show the solution
Answer: , , and 497 is the sample mean .
The producer's claim is the null hypothesis: it is believed until the data speaks against it. The customer looks for evidence of "lighter", so says . The number 497 was measured on 40 packs: it is the sample mean . The hypotheses are about , the mean of all packs, which nobody knows.
3The steps of every test
3.1Five steps
Every test in this lesson, whatever its name, follows the same five steps.
- Write and , and choose the significance level (usually 0.05).
- Choose the test and check its conditions.
- Calculate the test statistic from the sample.
- Find the p-value (or compare the statistic with the critical value).
- Decide (reject or fail to reject it) and say in plain words what that means.
3.2The test statistic
A test statistic is one number that measures how far the sample is from what claims, counted in standard errors:
A statistic near 0 means that the sample agrees with . A statistic far from 0, such as 3 or , means that the sample lies far from the claim. This is the z-score of Topic 17, now used for a sample mean or a sample proportion.
3.3One-tailed and two-tailed tests
The alternative hypothesis decides where "far from " is.
| Name | The evidence against is | Typical words | |
|---|---|---|---|
| two-tailed | a statistic far from 0 on either side | "different", "changed", "not equal" | |
| right-tailed | a large positive statistic | "more", "longer", "increased", "better" | |
| left-tailed | a large negative statistic | "less", "shorter", "decreased" |
Left-tailed:
reject below −1.645
Two-tailed:
reject below −1.960 or above 1.960
Right-tailed:
reject above 1.645
The shaded areas are the rejection regions for : the values of the statistic that are so unusual under that is rejected. In a two-tailed test the 5 percent are split, 2.5 percent on each side.
Try it yourself
Show a hint
Show the solution
Answer: (a) ; (b) two-tailed; (c) right-tailed.
The sample mean lies 2.5 standard errors above the claim. "Changed" has no direction, so both sides count: two-tailed. "Longer" names one direction: right-tailed, .
4The p-value and the significance level
4.1The p-value
It is a number between 0 and 1 that measures how well the data fits to :
| p-value | The data under | Evidence against |
|---|---|---|
| large, such as 0.40 | quite ordinary | none |
| about 0.05 | unusual | some |
| below 0.01 | very unusual | strong |
| below 0.001 | almost impossible | very strong |
"At least as extreme" follows the tails of the test: in a right-tailed test it is the area to the right of the statistic, in a left-tailed test the area to the left, and in a two-tailed test the area in both tails, which is twice the area of one tail.
right-tailed test
two-tailed test
A test gave the statistic z. Choose the kind of test and the level, and move z.
p-value = P(Z > 1.9) = 1 − Φ(1.9) = 1 − 0.9713 = 0.0287
0.0287 ≤ 0.05: reject H₀. The result is statistically significant.
The same z gives twice the p-value in a two-tailed test. In a one-tailed test, a z on the wrong side gives a p-value above 0.5: that data can never speak against H₀.
4.2The significance level
Before the test, we fix how much evidence we demand. This is the significance level : the largest p-value at which is still rejected. The usual choice is ; stricter fields use 0.01 or less.
| p-value | Decision | The result is called |
|---|---|---|
| reject | "statistically significant" | |
| fail to reject | "not significant" |
is also the risk we accept: if is true, a test at still rejects it in 5 percent of all samples (Part 6).
4.3What the p-value is not
The p-value is the most misread number in statistics. Three sentences are wrong:
- Wrong: " means that is true with the probability 3 percent." The p-value is calculated assuming . It is , not . Topic 16 showed that these two are different things.
- Wrong: " means that there is a 3 percent chance that the result is due to chance." Same mistake in other words.
- Wrong: " proves that is true." A large p-value says only that the data does not contradict . A small sample almost always gives a large p-value, whatever the truth is.
And one sentence is right: "If were true, a result at least this extreme would occur in 3 percent of all samples."
4.4The wording of the conclusion
| Decision | Say | Do not say |
|---|---|---|
| reject | "There is significant evidence that the mean has changed." | "We proved that the mean has changed." |
| fail to reject | "There is not enough evidence that the mean has changed." | "We proved that the mean is still 500." "We accept ." |
Always finish with a sentence about the real question, not about : "the data gives evidence that the new button sells more".
Try it yourself
Show a hint
Show the solution
Answer: (a) ; (b) ; (c) fail to reject .
: fail to reject . Even the one-tailed p-value is above 0.05. A statistic of 1.5 standard errors is not unusual enough.
5The z-test for a mean
5.1When it is used
The simplest test compares the mean of a sample with a claimed value , when the standard deviation of the population is known (from long experience with a machine, for example).
Topic 17 showed: if is true, the sample mean varies around with the standard error , and it is normal when the population is normal or the sample is large (). So the statistic
follows the standard normal distribution if is true, and the normal table gives the p-value.
5.2Example 1: a two-tailed test
A machine should fill bags with 500 g. Its standard deviation is known to be 10 g. A sample of 25 bags has the mean 496 g. Is the machine still set correctly? Test at .
The test is two-tailed: too much and too little are both a fault. It is a z-test: is known, and the weights of bags are close to normal.
There is significant evidence that the machine no longer fills 500 g on average; it seems to fill too little. If the machine were set correctly, a sample mean 4 g or more away from 500 would occur in fewer than 5 of 100 samples.
Note the level: at the same data would not reject , because . A result can be significant at one level and not at another, which is why the p-value itself should always be reported.
5.3Example 2: a right-tailed test
The marks of an exam have had the mean 70 and the standard deviation 10 for many years. This year 36 students were taught with a new method, and their mean is 73.2. Is there evidence that the new method gives higher marks?
There is significant evidence that the new method raises the mean mark.
With a two-tailed test the p-value would be , just above 0.05, and would not be rejected. The same data, another question, another answer. That is why the tails must be chosen before the data is seen (Part 3).
5.4The critical value method
Instead of the p-value, the statistic can be compared with a critical value: the border of the rejection region for the chosen .
| Test | |||
|---|---|---|---|
| two-tailed: reject if is above | 1.645 | 1.960 | 2.576 |
| right-tailed: reject if is above | 1.282 | 1.645 | 2.326 |
| left-tailed: reject if is below |
In Example 1: , so is rejected at 5 percent; but , so not at 1 percent. In Example 2: , rejected.
Both methods always give the same decision. The p-value says more: not only "rejected", but how strong the evidence is.
5.5The table of the standard normal distribution
is the area to the left of . The row gives to one decimal, the column the second decimal (Topic 17).
Φ(1.25) = 0.8944: the area to the left of z = 1.25. To the right: 1 − 0.8944 = 0.1056. For z = −1.25 the two areas change places.
| z | .00 | .01 | .02 | .03 | .04 | .05 | .06 | .07 | .08 | .09 |
|---|---|---|---|---|---|---|---|---|---|---|
| 0.0 | 0.5000 | 0.5040 | 0.5080 | 0.5120 | 0.5160 | 0.5199 | 0.5239 | 0.5279 | 0.5319 | 0.5359 |
| 0.1 | 0.5398 | 0.5438 | 0.5478 | 0.5517 | 0.5557 | 0.5596 | 0.5636 | 0.5675 | 0.5714 | 0.5753 |
| 0.2 | 0.5793 | 0.5832 | 0.5871 | 0.5910 | 0.5948 | 0.5987 | 0.6026 | 0.6064 | 0.6103 | 0.6141 |
| 0.3 | 0.6179 | 0.6217 | 0.6255 | 0.6293 | 0.6331 | 0.6368 | 0.6406 | 0.6443 | 0.6480 | 0.6517 |
| 0.4 | 0.6554 | 0.6591 | 0.6628 | 0.6664 | 0.6700 | 0.6736 | 0.6772 | 0.6808 | 0.6844 | 0.6879 |
| 0.5 | 0.6915 | 0.6950 | 0.6985 | 0.7019 | 0.7054 | 0.7088 | 0.7123 | 0.7157 | 0.7190 | 0.7224 |
| 0.6 | 0.7257 | 0.7291 | 0.7324 | 0.7357 | 0.7389 | 0.7422 | 0.7454 | 0.7486 | 0.7517 | 0.7549 |
| 0.7 | 0.7580 | 0.7611 | 0.7642 | 0.7673 | 0.7704 | 0.7734 | 0.7764 | 0.7794 | 0.7823 | 0.7852 |
| 0.8 | 0.7881 | 0.7910 | 0.7939 | 0.7967 | 0.7995 | 0.8023 | 0.8051 | 0.8078 | 0.8106 | 0.8133 |
| 0.9 | 0.8159 | 0.8186 | 0.8212 | 0.8238 | 0.8264 | 0.8289 | 0.8315 | 0.8340 | 0.8365 | 0.8389 |
| 1.0 | 0.8413 | 0.8438 | 0.8461 | 0.8485 | 0.8508 | 0.8531 | 0.8554 | 0.8577 | 0.8599 | 0.8621 |
| 1.1 | 0.8643 | 0.8665 | 0.8686 | 0.8708 | 0.8729 | 0.8749 | 0.8770 | 0.8790 | 0.8810 | 0.8830 |
| 1.2 | 0.8849 | 0.8869 | 0.8888 | 0.8907 | 0.8925 | 0.8944 | 0.8962 | 0.8980 | 0.8997 | 0.9015 |
| 1.3 | 0.9032 | 0.9049 | 0.9066 | 0.9082 | 0.9099 | 0.9115 | 0.9131 | 0.9147 | 0.9162 | 0.9177 |
| 1.4 | 0.9192 | 0.9207 | 0.9222 | 0.9236 | 0.9251 | 0.9265 | 0.9279 | 0.9292 | 0.9306 | 0.9319 |
| 1.5 | 0.9332 | 0.9345 | 0.9357 | 0.9370 | 0.9382 | 0.9394 | 0.9406 | 0.9418 | 0.9429 | 0.9441 |
| 1.6 | 0.9452 | 0.9463 | 0.9474 | 0.9484 | 0.9495 | 0.9505 | 0.9515 | 0.9525 | 0.9535 | 0.9545 |
| 1.7 | 0.9554 | 0.9564 | 0.9573 | 0.9582 | 0.9591 | 0.9599 | 0.9608 | 0.9616 | 0.9625 | 0.9633 |
| 1.8 | 0.9641 | 0.9649 | 0.9656 | 0.9664 | 0.9671 | 0.9678 | 0.9686 | 0.9693 | 0.9699 | 0.9706 |
| 1.9 | 0.9713 | 0.9719 | 0.9726 | 0.9732 | 0.9738 | 0.9744 | 0.9750 | 0.9756 | 0.9761 | 0.9767 |
| 2.0 | 0.9772 | 0.9778 | 0.9783 | 0.9788 | 0.9793 | 0.9798 | 0.9803 | 0.9808 | 0.9812 | 0.9817 |
| 2.1 | 0.9821 | 0.9826 | 0.9830 | 0.9834 | 0.9838 | 0.9842 | 0.9846 | 0.9850 | 0.9854 | 0.9857 |
| 2.2 | 0.9861 | 0.9864 | 0.9868 | 0.9871 | 0.9875 | 0.9878 | 0.9881 | 0.9884 | 0.9887 | 0.9890 |
| 2.3 | 0.9893 | 0.9896 | 0.9898 | 0.9901 | 0.9904 | 0.9906 | 0.9909 | 0.9911 | 0.9913 | 0.9916 |
| 2.4 | 0.9918 | 0.9920 | 0.9922 | 0.9925 | 0.9927 | 0.9929 | 0.9931 | 0.9932 | 0.9934 | 0.9936 |
| 2.5 | 0.9938 | 0.9940 | 0.9941 | 0.9943 | 0.9945 | 0.9946 | 0.9948 | 0.9949 | 0.9951 | 0.9952 |
| 2.6 | 0.9953 | 0.9955 | 0.9956 | 0.9957 | 0.9959 | 0.9960 | 0.9961 | 0.9962 | 0.9963 | 0.9964 |
| 2.7 | 0.9965 | 0.9966 | 0.9967 | 0.9968 | 0.9969 | 0.9970 | 0.9971 | 0.9972 | 0.9973 | 0.9974 |
| 2.8 | 0.9974 | 0.9975 | 0.9976 | 0.9977 | 0.9977 | 0.9978 | 0.9979 | 0.9979 | 0.9980 | 0.9981 |
| 2.9 | 0.9981 | 0.9982 | 0.9982 | 0.9983 | 0.9984 | 0.9984 | 0.9985 | 0.9985 | 0.9986 | 0.9986 |
| 3.0 | 0.9987 | 0.9987 | 0.9987 | 0.9988 | 0.9988 | 0.9989 | 0.9989 | 0.9989 | 0.9990 | 0.9990 |
Try it yourself
Show a hint
Show the solution
Answer: , , ; reject .
: reject . There is significant evidence that the mean weight is no longer 60 g; the apples are heavier. With critical values: , the same decision.
6Two kinds of errors, and power
6.1A test can be wrong in two ways
A decision made from a sample can be wrong, and there are two different mistakes.
| is really true | is really false | |
|---|---|---|
| The test rejects | Type I error (a false alarm), probability | correct, probability (the power) |
| The test does not reject | correct, probability | Type II error (a missed effect), probability |
- A Type I error is rejecting a true : the button changed nothing, but the test says it works. In court: convicting an innocent person.
- A Type II error is failing to reject a false : the button works, but the test does not notice. In court: letting a guilty person go.
The significance level is the probability of a Type I error, and we choose it. The probability of a Type II error depends on how large the real effect is and on the sample.
6.2The trade-off
The picture shows the sample mean in two worlds: the left curve if is true, the right curve if the true mean is 75. The dashed line is the border: above it, is rejected. The area is the chance of a false alarm; the area is the chance of missing the real effect.
Move the border to the right and shrinks, but grows. For a fixed sample, a smaller risk of one error means a larger risk of the other. Which error is worse depends on the situation: a missed disease may be worse than a false alarm that leads to a second test; a wrongly convicted person is worse than an acquittal.
6.3Power
The power of a test is : the probability that it detects an effect that really exists. A common goal is a power of at least 0.80.
Example. Marks have the mean 70 and . A right-tailed z-test at is done with students. If the new method really raises the mean to 75, how likely is it that the test notices?
The test finds the improvement in about 80 of 100 such experiments, and misses it in 20.
Marks with σ = 10. A right-tailed test of H₀: μ = 70 with n students. Move n and the true mean, and choose α.
- if H₀ is true
- if the true mean is 75
- α
- β
Standard error: 10 / √25 = 2
H₀ is rejected when the sample mean is above 70 + 1.645 × 2 = 73.3
If the true mean is 75: z = (73.3 − 75) / 2 = −0.85, so β = Φ(−0.85) = 0.1977
Power = 1 − β = 0.8023
A larger sample pulls the two curves apart and raises the power. A smaller α moves the border to the right: fewer false alarms, more missed effects.
6.4What makes a test more powerful
| Change | Effect on the power |
|---|---|
| a larger sample | higher: the standard error shrinks, the two curves move apart |
| a larger real effect | higher: a big difference is easier to see |
| a smaller spread | higher |
| a larger (0.10 instead of 0.05) | higher, but at the price of more false alarms |
| a one-tailed test instead of a two-tailed one | higher in that direction, and blind in the other |
The same example with other sample sizes:
| Standard error | Reject when is above | Power when the true mean is 75 | |
|---|---|---|---|
| 9 | 3.33 | 75.5 | 0.44 |
| 25 | 2 | 73.3 | 0.80 |
| 100 | 1 | 71.6 | more than 0.99 |
With 9 students the test would miss a real improvement of 5 marks more often than it finds it. A result that is "not significant" in a small study does not show that there is no effect. It may only show that the study was too small. Planning the sample size before an experiment, so that the power is high enough, is part of every serious A/B test.
Try it yourself
Show a hint
Show the solution
Answer: (a) a Type I error; (b) a Type II error; (c) about .
In (a), was true and was rejected: a false alarm, a Type I error. In (b), was false and was not rejected: a miss, a Type II error. For a fire alarm the miss is far worse, so it is built to ring easily.
If the mean has really risen to 23, the test notices it in about 92 of 100 samples.
7The t distribution and the t-test for one sample
7.1When σ is not known
The z-test needs the standard deviation of the population, and in real work nobody knows it. The natural idea is to use the sample standard deviation of Topic 18 instead:
But is itself only an estimate, and for a small sample it can be quite wrong. This extra uncertainty makes the statistic vary more than a standard normal variable. Its distribution was worked out in 1908 by William Gosset, who wrote under the name "Student": the t distribution.
7.2The t distribution
- normal (z)
- t with df = 2
- t with df = 5
- t with df = 30
- It is a bell, symmetric around 0, like the standard normal curve.
- It has heavier tails: extreme values are more likely, because adds uncertainty.
- It has one parameter, the degrees of freedom. For a one-sample test, (the number of deviations that carry free information, Topic 18).
- The larger , the closer it comes to the normal curve. From about the two are almost the same.
The t distribution with df degrees of freedom (a sample of df + 1 values) against the normal curve.
- t with df = 4
- normal (z)
Two-tailed test at 5 percent: reject H₀ when |t| is above 2.776 (for z: 1.960)
Beyond ±1.960 lies 12.2 percent of this t distribution, not 5 percent
Using 1.960 with a small sample would give many more false alarms than the 5 percent that were promised. From about df = 30 the two curves, and the two borders, are almost the same.
7.3The t table
The table gives critical values: the value of that cuts off the area in the right tail.
t = 2.228 cuts off the area 0.025 in the right tail: the critical value of a one-tailed test at 2.5 percent, or of a two-tailed test at 5 percent.
| df | 0.10 | 0.05 | 0.025 | 0.01 | 0.005 |
|---|---|---|---|---|---|
| 1 | 3.078 | 6.314 | 12.706 | 31.821 | 63.657 |
| 2 | 1.886 | 2.920 | 4.303 | 6.965 | 9.925 |
| 3 | 1.638 | 2.353 | 3.182 | 4.541 | 5.841 |
| 4 | 1.533 | 2.132 | 2.776 | 3.747 | 4.604 |
| 5 | 1.476 | 2.015 | 2.571 | 3.365 | 4.032 |
| 6 | 1.440 | 1.943 | 2.447 | 3.143 | 3.707 |
| 7 | 1.415 | 1.895 | 2.365 | 2.998 | 3.499 |
| 8 | 1.397 | 1.860 | 2.306 | 2.896 | 3.355 |
| 9 | 1.383 | 1.833 | 2.262 | 2.821 | 3.250 |
| 10 | 1.372 | 1.812 | 2.228 | 2.764 | 3.169 |
| 11 | 1.363 | 1.796 | 2.201 | 2.718 | 3.106 |
| 12 | 1.356 | 1.782 | 2.179 | 2.681 | 3.055 |
| 13 | 1.350 | 1.771 | 2.160 | 2.650 | 3.012 |
| 14 | 1.345 | 1.761 | 2.145 | 2.624 | 2.977 |
| 15 | 1.341 | 1.753 | 2.131 | 2.602 | 2.947 |
| 16 | 1.337 | 1.746 | 2.120 | 2.583 | 2.921 |
| 17 | 1.333 | 1.740 | 2.110 | 2.567 | 2.898 |
| 18 | 1.330 | 1.734 | 2.101 | 2.552 | 2.878 |
| 19 | 1.328 | 1.729 | 2.093 | 2.539 | 2.861 |
| 20 | 1.325 | 1.725 | 2.086 | 2.528 | 2.845 |
| 21 | 1.323 | 1.721 | 2.080 | 2.518 | 2.831 |
| 22 | 1.321 | 1.717 | 2.074 | 2.508 | 2.819 |
| 23 | 1.319 | 1.714 | 2.069 | 2.500 | 2.807 |
| 24 | 1.318 | 1.711 | 2.064 | 2.492 | 2.797 |
| 25 | 1.316 | 1.708 | 2.060 | 2.485 | 2.787 |
| 26 | 1.315 | 1.706 | 2.056 | 2.479 | 2.779 |
| 27 | 1.314 | 1.703 | 2.052 | 2.473 | 2.771 |
| 28 | 1.313 | 1.701 | 2.048 | 2.467 | 2.763 |
| 29 | 1.311 | 1.699 | 2.045 | 2.462 | 2.756 |
| 30 | 1.310 | 1.697 | 2.042 | 2.457 | 2.750 |
| 40 | 1.303 | 1.684 | 2.021 | 2.423 | 2.704 |
| 60 | 1.296 | 1.671 | 2.000 | 2.390 | 2.660 |
| 120 | 1.289 | 1.658 | 1.980 | 2.358 | 2.617 |
| ∞ (z) | 1.282 | 1.645 | 1.960 | 2.326 | 2.576 |
How to use it:
- Right-tailed test at : take the column . Reject if is above the value.
- Left-tailed test at : the same column with a minus sign. Reject if is below it.
- Two-tailed test at : take the column (for 5 percent, the column 0.025). Reject if is above the value.
- If your is not in the table, take the next smaller one (that is the careful side).
The last row is the normal distribution: with a huge sample, and are the same, and the critical values 1.645 and 1.960 of Part 5 appear again.
The p-value from the table. A printed t table cannot give an exact p-value, only two borders. In the row of your , find the two values between which your lies, and read the two columns above them. For a two-tailed test, double both. Software gives the exact value (Part 18).
7.4Example 1: summary numbers
A café says that its cups hold 250 ml. A customer measures 9 cups: mean 246 ml, standard deviation 6 ml. Is the mean different from 250 ml? Test at .
is unknown and the sample is small: a one-sample t-test (the amounts are roughly normal).
There is not enough evidence that the cups hold a different amount on average. The p-value: lies between (column 0.05) and (column 0.025), so one tail is between 0.025 and 0.05, and the two-tailed p-value is between 0.05 and 0.10. Software gives .
Compare with a z-test: would have been significant (). With only 9 cups and an estimated , the t-test is rightly more careful.
7.5Example 2: from raw data
A restaurant promises delivery in 30 minutes. Eight deliveries took 34, 31, 36, 29, 33, 35, 32 and 34 minutes. Is there evidence that the mean delivery time is longer than 30 minutes? Test at .
| 34 | 31 | 36 | 29 | 33 | 35 | 32 | 34 | |
|---|---|---|---|---|---|---|---|---|
| 1 | 3 | 0 | 2 | 1 | ||||
| 1 | 4 | 9 | 16 | 0 | 4 | 1 | 1 |
There is significant evidence that deliveries take longer than 30 minutes on average. The statistic is even above (column 0.005), so ; software gives .
7.6Conditions of the t-test
- The sample is random, and its values are independent of each other.
- The population is roughly normal, or the sample is large (, by the central limit theorem). For small samples, look at the data: strong skew or an outlier makes the test unreliable.
7.7z or t?
| z-test | t-test | |
|---|---|---|
| Standard deviation | of the population is known | only of the sample is known |
| Statistic | ||
| Distribution | standard normal | t with |
| In practice | rare | the usual case |
Try it yourself
Show a hint
Show the solution
Answer: , , , critical value ; fail to reject .
Fail to reject . A mean of 43 instead of 40 looks like a clear fault, but the screws vary a lot () and there are only 16 of them: the difference is 1.5 standard errors. In row 15, 1.5 lies between and , so the two-tailed p-value is between 0.10 and 0.20.
8Comparing two means
Most real questions compare two groups: the new method against the old one, version A against version B, before against after. There are two different situations, and mixing them up is a common and serious mistake.
8.1Independent samples or pairs?
| Independent samples | Paired samples | |
|---|---|---|
| What they are | two separate groups of different people or things | two measurements of the same people or things, or matched pairs |
| Examples | 10 students taught with method A, 10 other students with method B | the same 8 students before and after a course |
| Sizes | may be different | always the same |
| Test | two-sample t-test | paired t-test |
8.2The two-sample t-test
Two independent samples with the sizes , , the means , and the standard deviations , . The null hypothesis says that the two population means are equal: .
When the two groups have a similar spread (a usual rule: the larger is less than twice the smaller), their variances are combined into one, the pooled variance, a weighted mean of the two:
When the two groups have the same size, the pooled variance is simply the mean of the two variances: .
Example. Ten students were taught with method A and ten others with method B. Group A: mean 80, standard deviation 8. Group B: mean 72, standard deviation 6. Do the methods differ? Test at .
There is significant evidence that the two methods lead to different mean marks; method A does better. The statistic lies between and , so the two-tailed p-value is between 0.02 and 0.05; software gives .
When the spreads are very different, the variances are not pooled. The statistic is then
with a number of degrees of freedom that software calculates. This is Welch's t-test. In Python, ttest_ind does the pooled test by default and Welch's test with equal_var=False. With equal group sizes the two statistics are the same number.
8.3The paired t-test
With pairs, the two columns are not independent: a strong student is strong before and after. The trick is to stop looking at two columns. Calculate the difference for every pair, and test whether the mean of the differences is 0. That is a one-sample t-test on the differences:
is the mean of the differences, their standard deviation, and the number of pairs.
Example. Eight students take a test before and after a course.
| Student | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Before | 60 | 72 | 55 | 68 | 80 | 65 | 70 | 58 |
| After | 65 | 75 | 54 | 72 | 86 | 67 | 70 | 63 |
| 5 | 3 | 4 | 6 | 2 | 0 | 5 | ||
| 2 | 0 | 1 | 3 | 2 | ||||
| 4 | 0 | 16 | 1 | 9 | 1 | 9 | 4 |
The row is the difference after − before. Did the course improve the results? Test at .
There is significant evidence that the course improved the results, by 3 marks on average. The statistic lies between and , so the p-value is between 0.005 and 0.01 (software: ).
Before
mean 66
After
mean 69
The differences: after − before
mean 3
Why pairing helps. The students differ a lot from each other (from 55 to 80 marks). Compared as two independent groups, this large spread between students would hide an improvement of 3 marks. The differences remove it: each student is compared only with himself or herself. A paired design is more powerful whenever the pairs are strongly related, and it is wrong to use the two-sample test on paired data.
Try it yourself
Show a hint
Show the solution
Answer: , , , ; reject .
| 1 | 4 | 2 | 5 | 3 | Sum | |
|---|---|---|---|---|---|---|
| 1 | 2 | 0 | 0 | |||
| 4 | 1 | 1 | 4 | 0 | 10 |
Reject : there is significant evidence that the training lowered the pulse, by 3 beats on average. The five people have very different pulses (from 75 to 88), but every one of them went down.
9Tests for proportions
Many questions are about a share: the share of visitors who buy, of patients who recover, of emails that are spam. Topic 17 showed that a sample proportion is approximately normal with the standard error . So proportions are tested with a z-test.
9.1One proportion
The standard error uses , the value claimed by , because the test is calculated under the assumption that is true.
Condition: and (some books ask for 10), so that the normal curve fits.
Example. With the old button, 10 percent of the visitors bought. With the new button, 122 of 1,000 visitors buy. Is the share of buyers higher now? Test at .
There is significant evidence that the new button raises the share of buyers. At the stricter level the result would just miss significance ().
9.2Two proportions: the A/B test
Two independent groups, with successes among and successes among . The null hypothesis says that both groups have the same true proportion: . Under the two samples are estimates of one and the same proportion, so they are combined into the pooled proportion:
Example. Version A of a page is shown to 2,000 visitors, and 200 click. Version B is shown to 2,000 other visitors, and 250 click. Do the versions differ? Test at .
There is significant evidence that the two versions have different click rates; version B does better.
An A/B test. Each version is shown to n visitors; A percent of them buy with version A and B percent with version B.
Version A: 200 of 2,000 buy. Version B: 250 of 2,000 buy.
Pooled share: (200 + 250) / 4,000 = 0.1125
Standard error: √(0.1125 × 0.8875 × 2 / 2,000) = 0.00999
z = (0.125 − 0.1) / 0.00999 = 2.5, p-value = 2 × (1 − Φ(2.5)) = 0.0124
At the 5 percent level: reject H₀. The versions differ significantly.
Keep the two rates and move n: the same difference is not significant with a few hundred visitors and clearly significant with a few thousand.
This is the standard calculation behind an A/B test. Three rules of practice: decide the sample size before the experiment starts, assign visitors to the versions at random, and do not stop the experiment the moment the p-value dips below 0.05 (Part 12).
Try it yourself
Show a hint
Show the solution
Answer: , , ; reject .
: reject . There is significant evidence that more readers open the newsletter now, but only just: with a two-tailed test the p-value would be and the result would not be significant.
10The chi-square tests
The tests so far compared means and proportions. The chi-square tests work with counts in categories: how many customers chose each product, how many patients in each group recovered. They answer two questions:
- Goodness of fit: do the counts of one categorical variable fit a claimed distribution?
- Independence: are two categorical variables related?
10.1The idea: observed against expected
Both tests compare the counts that were observed () with the counts that would be expected () if were true:
with one term for every category or cell.
- If the data fits , observed and expected counts are close, and is small.
- If the data contradicts , some differences are large, and is large.
- The differences are squared, so is never negative, and only large values speak against . A chi-square test is always right-tailed.
- Dividing by makes the differences comparable: a difference of 10 is huge when 20 were expected and nothing when 10,000 were expected.
10.2The chi-square distribution
- df = 2
- df = 4
- df = 8
Three chi-square curves
df = 5
the filled tail is 5 percent of the area
If is true, the statistic follows a chi-square distribution. It starts at 0, is skewed to the right, and has one parameter, the degrees of freedom. Its mean is equal to : with , values around 5 are ordinary.
The table gives critical values: the value that cuts off the area in the right tail.
χ² = 11.070 cuts off the area 0.05 in the right tail. At the 5 percent level, H₀ is rejected when χ² is above it.
| df | 0.10 | 0.05 | 0.025 | 0.01 | 0.005 |
|---|---|---|---|---|---|
| 1 | 2.706 | 3.841 | 5.024 | 6.635 | 7.879 |
| 2 | 4.605 | 5.991 | 7.378 | 9.210 | 10.597 |
| 3 | 6.251 | 7.815 | 9.348 | 11.345 | 12.838 |
| 4 | 7.779 | 9.488 | 11.143 | 13.277 | 14.860 |
| 5 | 9.236 | 11.070 | 12.833 | 15.086 | 16.750 |
| 6 | 10.645 | 12.592 | 14.449 | 16.812 | 18.548 |
| 7 | 12.017 | 14.067 | 16.013 | 18.475 | 20.278 |
| 8 | 13.362 | 15.507 | 17.535 | 20.090 | 21.955 |
| 9 | 14.684 | 16.919 | 19.023 | 21.666 | 23.589 |
| 10 | 15.987 | 18.307 | 20.483 | 23.209 | 25.188 |
| 12 | 18.549 | 21.026 | 23.337 | 26.217 | 28.300 |
| 15 | 22.307 | 24.996 | 27.488 | 30.578 | 32.801 |
| 20 | 28.412 | 31.410 | 34.170 | 37.566 | 39.997 |
| 25 | 34.382 | 37.652 | 40.646 | 44.314 | 46.928 |
| 30 | 40.256 | 43.773 | 46.979 | 50.892 | 53.672 |
Reject if is above the critical value. For the p-value, find the two values of the row between which your lies; the p-value is between the two column headings.
10.3Goodness of fit
Example. A die is rolled 120 times. Is it fair? Test at .
| Face | Observed | Expected | ||
|---|---|---|---|---|
| 1 | 15 | 20 | 1.25 | |
| 2 | 22 | 20 | 2 | 0.20 |
| 3 | 18 | 20 | 0.20 | |
| 4 | 25 | 20 | 5 | 1.25 |
| 5 | 16 | 20 | 0.80 | |
| 6 | 24 | 20 | 4 | 0.80 |
| Total | 120 | 120 | 0 | 4.50 |
The counts are consistent with a fair die. The value 4.50 is even below the first column (), so ; software gives . Differences like these are just what chance produces.
For a goodness-of-fit test:
The expected counts need not be equal: if says "50 percent, 30 percent, 20 percent", then is 50, 30 and 20 percent of (Problem 21). The observed and the expected counts always have the same total.
10.4Independence
Example. A company looks at 200 customers: which plan they had, and whether they stayed or left.
| Observed | Stayed | Left | Total |
|---|---|---|---|
| Basic plan | 70 | 50 | 120 |
| Premium plan | 60 | 20 | 80 |
| Total | 130 | 70 | 200 |
Of the basic customers percent left, of the premium customers only percent. Is leaving related to the plan, or could this difference be chance? Test at .
says that the two variables are independent. Topic 16: for independent events, . So if the plan and leaving are independent, the expected count of a cell is
| Expected | Stayed | Left | Total |
|---|---|---|---|
| Basic plan | 78 | 42 | 120 |
| Premium plan | 52 | 28 | 80 |
| Total | 130 | 70 | 200 |
There is significant evidence that leaving is related to the plan: basic customers leave more often. The statistic lies between and , so the p-value is between 0.01 and 0.025 (software: ).
Customers by their plan and by whether they stayed or left. Move the four counts; the expected counts stand in brackets.
| Stayed | Left | Total | |
|---|---|---|---|
| Basic plan | a = 70 (78) | b = 50 (42) | 120 |
| Premium plan | c = 60 (52) | d = 20 (28) | 80 |
| Total | 130 | 70 | 200 |
Left: 41.7 percent of the basic customers and 25 percent of the premium customers
χ² = 0.8205 + 1.5238 + 1.2308 + 2.2857 ≈ 5.861 (every term is (O − E)² / E)
df = 1, critical value at 5 percent: 3.841. p-value = 0.0155
5.861 > 3.841: reject H₀. Leaving is related to the plan.
When the two rows have the same share of customers who left, observed and expected counts agree and χ² is 0. The further the shares are apart, the larger χ².
For a test of independence with rows and columns:
The totals row and column are not counted in and .
10.5Conditions and remarks
- The data are counts, not percentages or means. Convert percentages back into counts first.
- Every person or thing is counted in exactly one cell, and the observations are independent.
- Every expected count should be at least 5. If not, combine categories.
- The test says whether there is a relation, not how strong it is and not what causes what.
- For a table of 2 rows and 2 columns, the chi-square test is the same as the two-tailed z-test for two proportions: . The critical value is .
Try it yourself
Show a hint
Show the solution
Answer: , , , critical value ; reject .
| Kind | |||
|---|---|---|---|
| Vanilla | 24 | 15 | 81 |
| Chocolate | 15 | 15 | 0 |
| Strawberry | 9 | 15 | 36 |
| Lemon | 12 | 15 | 9 |
| Total | 60 | 60 | 126 |
Reject : the kinds are not equally popular. Most of the statistic comes from vanilla, which was chosen 9 times more often than expected. The value 8.4 lies between and , so the p-value is between 0.025 and 0.05.
11Choosing the test
| The question | The data | Test | Statistic | Distribution |
|---|---|---|---|---|
| Is a mean equal to a claimed value? | one sample, known | z-test | normal | |
| Is a mean equal to a claimed value? | one sample, unknown | one-sample t-test | t, | |
| Do two groups have the same mean? | two independent samples | two-sample t-test | t, | |
| Did a treatment change the mean? | pairs (before and after) | paired t-test | t, | |
| Is a proportion equal to a claimed value? | one sample of yes or no | z-test for a proportion | normal | |
| Do two groups have the same proportion? | two independent samples | z-test for two proportions | normal | |
| Do counts fit a claimed distribution? | one categorical variable | chi-square goodness of fit | chi-square, = categories − 1 | |
| Are two categorical variables related? | a table of counts | chi-square test of independence | chi-square, |
Three questions lead to the right row:
- What kind of data? Numbers (means), yes or no (proportions), or categories (counts).
- How many groups? One sample against a claim, or two groups.
- For two groups of numbers: are they independent, or pairs?
Which test is it?
1 of 8
The reaction times of 12 drivers are measured before and after a cup of coffee. Did the coffee change them?
Try it yourself
Show a hint
Show the solution
Answer: (a) one-sample t-test; (b) z-test for two proportions; (c) paired t-test; (d) chi-square test of independence.
(a) Numbers, one sample against a claimed mean, and only is known: the one-sample t-test. (b) Yes or no for every part, in two independent groups: the z-test for two proportions. (c) Numbers, the same 15 patients twice: the paired t-test on the differences. (d) Two categorical variables, sport and boy or girl, in a table of counts: the chi-square test of independence, with .
12Significant is not the same as important
12.1Statistical and practical significance
"Statistically significant" means only: the difference is unlikely to be pure chance. It says nothing about whether the difference is large enough to matter.
The standard error shrinks with , so with enough data any difference becomes significant, however tiny. A test with 10,000 customers finds that a new design raises the mean order from 100.0 to 100.4 with (Problem 24). The effect is real and it is 0.4 percent. Whether that pays for the new design is a business question, not a statistical one.
standard error 1.5
standard error 0.15
The opposite also happens: a large and important effect in a small study that is not significant, because the test had too little power (Part 6).
So a good report gives three things: the size of the effect (the difference itself), the p-value, and the sample size.
12.2Effect size
For the difference of two means, a common measure of its size is Cohen's d: the difference counted in standard deviations.
A usual reading: about 0.2 is small, 0.5 medium, 0.8 large. It does not depend on the sample size, so it tells what the p-value cannot: how big the difference is.
12.3Many tests, many false alarms
At , one test of a true gives a false alarm with the probability 0.05. Run 20 such tests, and Topic 16 gives the chance of at least one false alarm:
Test enough things and something will always be "significant". This is called the multiple comparisons problem. Looking for significant results by trying many variables, many subgroups or many ways of analysing, and then reporting only the ones that worked, is called p-hacking; the results cannot be trusted.
Twenty tests in which H₀ is true every time: nothing is going on. Each one is a two-tailed z-test at the 5 percent level.
No tests yet.
The calculation says: at least one false alarm in 64 percent of the rounds, and 1 false alarm per round on average. Whoever runs 20 tests and reports only the significant one reports chance. Up to 10,000 rounds.
The simplest protection is the Bonferroni correction: when tests are made, each one is tested at the level . For 20 tests: .
The same danger is in an A/B test that is checked every day and stopped as soon as : each look is another test. Fix the sample size first.
12.4Other things a test does not tell
- A test does not prove . "Not significant" means "no evidence", not "evidence of nothing".
- A test does not show causes. A significant difference between two groups that were not formed at random may come from something else in which the groups differ.
- A test trusts its conditions. A biased sample (Topic 17) gives a wrong answer with a beautiful p-value.
12.5A look ahead: tests and confidence intervals
The next lesson answers the same question in another way: instead of "is the mean different from 500?", it asks "between which values does the mean probably lie?". The two are connected: a two-tailed test at rejects exactly when lies outside the 95 percent confidence interval. An interval shows the size of the effect and its uncertainty at once, which is why many reports prefer it.
Try it yourself
Show a hint
Show the solution
Answer: , , effect size : significant, but very small.
: the change is statistically significant. But half a minute a day is 1 percent more, and 0.05 standard deviations: far below even a "small" effect of 0.2. With 2,500 users the test can see a difference that hardly matters.
13Hypothesis tests in machine learning
A/B tests. A new recommendation model, a new ranking, a new button: before it replaces the old one, it is shown to a random part of the users, and the two groups are compared with a test for two proportions (clicks, purchases) or two means (time, money spent).
Is model B really better than model A? Two models are evaluated on the same folds of a cross-validation. Their scores on a fold are a pair, because both saw the same data. A paired t-test on the differences says whether B is better beyond the luck of the folds (Problem 26). An accuracy of 0.91 against 0.90 on one small test set is usually not significant (Topic 17: the standard error of an accuracy).
Feature selection. For a categorical feature and a categorical target, the chi-square test of independence measures how strongly they are related. Features with a large are kept; this is what chi2 in scikit-learn does.
Coefficients of a regression. The summary of a linear or logistic regression prints a p-value for every coefficient. It belongs to the test : this coefficient is 0, a t-test or z-test like the ones of this lesson. A large p-value says that the data gives no evidence that the feature matters, given the other features.
Has the data changed? A model in production was trained on last year's data. Tests compare the distribution of a feature then and now (two means, or a chi-square test on the categories) to notice data drift.
The warnings apply too. With a million rows every tiny difference is significant, so look at the effect size. And trying 50 features or 50 model settings and keeping the one with is the multiple comparisons problem of Part 12.
Try it yourself
Show a hint
Show the solution
Answer: (a) ; (b) the paired t-test; (c) the size of the effect.
(a) features look significant by pure chance. (b) Both models saw the same folds, so the two scores of a fold are a pair: the paired t-test on the 10 differences. (c) With a million rows the standard error is tiny, and even a difference of no practical value gets a small p-value. The size of the effect decides whether it matters.
14Common mistakes
Reading the p-value as the probability that is true. It is the probability of data this extreme if is true.
Saying "we accept " or "we proved ". A test can only fail to reject it.
Writing hypotheses about the sample. is wrong. Hypotheses are about or .
Choosing a one-tailed test after seeing the data. The tails follow from the question, before the data.
Forgetting to double the p-value in a two-tailed test. Or doubling it in a one-tailed test.
Using where belongs. The statistic divides by the standard error.
Using z when only is known and the sample is small. That needs the t distribution with .
Taking the wrong column of the t table. Two-tailed at 5 percent uses the column 0.025.
Treating pairs as independent samples. Before and after on the same people is a paired test.
Using percentages in a chi-square test. It needs counts.
Using the observed counts where the expected ones belong. Divide by , not by .
Wrong degrees of freedom. Goodness of fit: categories minus 1. Independence: , without the totals.
Confusing significant with important. With a huge sample, tiny effects are significant.
Running many tests and reporting the significant one. Correct for the number of tests.
| Wrong | Right |
|---|---|
| : " is true with 3 percent probability" | "if is true, data this extreme occurs in 3 percent of samples" |
| : "the means are equal" | "there is not enough evidence of a difference" |
| two-tailed test, : | |
| two-tailed at 5 percent with : critical value 1.812 | column 0.025: 2.228 |
| before and after on the same people: two-sample t-test | paired t-test on the differences |
| table of 3 rows and 4 columns: | |
| "significant, so the effect is large" | look at the size of the effect itself |
15Solved problems (from the easiest to the hardest)
Each problem has the question, the idea behind it with the part of the lesson it uses, and the calculation step by step. Cover the solution and try first. Unless a problem says something else, the significance level is .
Level 1: Hypotheses, z-tests and p-values
Problem 1: Writing the hypotheses
Write and , and say whether the test is two-tailed, right-tailed or left-tailed. (a) A bakery says that its loaves weigh 400 g on average; an inspector wants to know whether that is true. (b) A company claims that its new battery lasts longer than the old one, which lasted 20 hours on average. (c) A city says that at most 8 percent of its buses are late; passengers believe that the share is higher.
Idea. contains the equals sign and the old or claimed value. is what somebody wants to show; its direction gives the tails (Parts 2 and 3).
| Test | |||
|---|---|---|---|
| (a) | two-tailed | ||
| (b) | right-tailed | ||
| (c) | right-tailed |
Why: in (a) too heavy and too light are both a deviation, so the test is two-tailed. In (c) the question is about a share, so the parameter is , not ; the passengers look for evidence of "more", which is .
Problem 2: From a p-value to a decision
A test gives the p-value 0.03. What is the decision at , and at ?
Idea. Reject when the p-value is at most (Part 4).
Why: the evidence is the same; the amount of evidence that is demanded is different. That is why the level is fixed before the test, and why the p-value itself is reported.
Problem 3: A two-tailed z-test
A test has the mean 100 and the standard deviation 15 in the whole country. A school's 36 students have the mean 105.5. Is the school different from the country?
Idea. is known: z-test for a mean (Part 5).
: reject . There is significant evidence that the school's mean differs from the country's; it is higher.
Why: the question asks "different", so both tails count and the area of one tail is doubled.
Problem 4: A right-tailed z-test
A producer says that a new battery lasts longer than 40 hours on average. The standard deviation is known to be 3 hours. A sample of 49 batteries has the mean 40.6 hours. Is the claim supported?
Idea. "Longer" gives a right-tailed test (Parts 3 and 5).
: fail to reject . There is not enough evidence that the batteries last longer than 40 hours.
Why: the sample mean is above 40, but a sample mean 0.6 hours above 40 happens by chance in 8 of 100 samples even when the true mean is exactly 40. That is not rare enough.
Problem 5: A left-tailed z-test
Customers of a bank waited 12 minutes on average, with a standard deviation of 4 minutes. After a new system was installed, 64 customers waited 11.1 minutes on average. Has the waiting time become shorter?
Idea. "Shorter" gives a left-tailed test: the p-value is the area to the left (Part 5).
: reject . There is significant evidence that the waiting time has become shorter.
Why: in a left-tailed test only a negative statistic speaks against . A sample mean above 12 could never lead to a rejection here, however large.
Problem 6: The critical value method
A two-tailed z-test gives . Decide at and at with critical values.
Idea. Compare with the critical value of the level (Part 5).
Why: the p-value tells the same story: , which lies between 0.01 and 0.05.
Problem 7: One tail or two
A z-test gives . Find the p-value for a right-tailed test and for a two-tailed test, and decide at in each case.
Idea. The two-tailed p-value is twice the one-tailed one (Part 4).
Why: the same statistic leads to opposite decisions. A one-tailed test is easier to pass in its direction, which is exactly why the direction must be fixed before the data is seen.
Problem 8: Which error is it?
A spam filter tests : "this email is not spam". (a) It moves a real letter from a friend into the spam folder. (b) It lets an advertisement for a fake lottery into the inbox. Name the type of error in each case. Which one is worse here?
Idea. Type I: rejecting a true . Type II: not rejecting a false (Part 6).
- (a) was true (not spam) and was rejected: a Type I error (a false alarm).
- (b) was false (it was spam) and was not rejected: a Type II error (a miss).
For a spam filter, a Type I error is usually worse: losing an important letter costs more than seeing one advertisement. So the filter should be built with a small .
Why: which error is "I" and which is "II" depends only on how is written. Always write down first.
Problem 9: A test for one proportion
Before an election, 216 of 400 randomly chosen voters say they will vote for candidate A. Is the share of A's voters different from 50 percent?
Idea. A share: z-test for a proportion, with in the standard error (Part 9).
: fail to reject . The poll does not give enough evidence that candidate A is ahead.
Why: 54 percent looks like a clear lead, but with 400 voters a sample share varies by 2.5 points from sample to sample. A lead of 4 points is 1.6 standard errors, which chance produces quite often.
Level 2: t-tests, errors and power
Problem 10: Reading the t table
Find the critical value for (a) a two-tailed test at 5 percent with , (b) a right-tailed test at 1 percent with , (c) a left-tailed test at 5 percent with .
Idea. One tail: the column . Two tails: the column (Part 7).
| Column | Row | Value | Reject if | |
|---|---|---|---|---|
| (a) | 0.025 | 15 | 2.131 | |
| (b) | 0.01 | 24 | 2.492 | |
| (c) | 0.05 | 9 | 1.833 |
Why: the table shows only the right tail, because the t distribution is symmetric. For the left tail the value gets a minus sign.
Problem 11: A one-sample t-test
A machine should cut rods of 50 cm. A sample of 16 rods has the mean 52.5 cm and the standard deviation 4 cm. Is the machine set wrongly?
Idea. is unknown: one-sample t-test with (Part 7).
Reject . There is significant evidence that the mean length is not 50 cm.
Why: the statistic lies between and in row 15, so the two-tailed p-value is between 0.02 and 0.05. Software gives .
Problem 12: A t-test from raw data
A producer says that a phone runs 20 hours on one charge. Six phones ran 18, 21, 17, 19, 20 and 16 hours. Is there evidence that the mean is less than 20 hours?
Idea. First and (Topic 18), then the t-test (Part 7).
| 18 | 21 | 17 | 19 | 20 | 16 | |
|---|---|---|---|---|---|---|
| 2.5 | 0.5 | 1.5 | ||||
| 0.25 | 6.25 | 2.25 | 0.25 | 2.25 | 6.25 |
is not below : fail to reject . There is not enough evidence that the phones run less than 20 hours.
Why: the result is close to the border (software: ). With only six phones the test has little power; a larger sample might well show a difference. "Not significant" is not "the claim is true".
Problem 13: Two independent samples of the same size
Machine A filled 8 bottles with a mean of 50.2 cl and a standard deviation of 1.2 cl. Machine B filled 8 bottles with a mean of 48.9 cl and a standard deviation of 1.0 cl. Do the machines fill different amounts?
Idea. Two separate groups: two-sample t-test with the pooled variance (Part 8).
Reject . There is significant evidence that the machines fill different amounts; machine A fills more.
Why: with equal group sizes the pooled variance is just the mean of the two variances. The p-value is between 0.02 and 0.05 (software: ).
Problem 14: Two independent samples of different sizes
A new feed is given to 10 chickens, which gain 24 g a day on average (). Fifteen chickens with the old feed gain 21.5 g a day on average (). Do the two feeds differ?
Idea. The pooled variance weights each variance with (Part 8).
Fail to reject . There is not enough evidence that the feeds differ.
Why: a difference of 2.5 g looks promising, but with these spreads and sample sizes it is only 1.7 standard errors. The p-value is between 0.10 and 0.20 (software: ).
Problem 15: A paired t-test
Six runners are timed over 400 metres before and after a month of training. Did the training make the runners faster?
| Runner | 1 | 2 | 3 | 4 | 5 | 6 | Sum |
|---|---|---|---|---|---|---|---|
| Before | 62 | 58 | 65 | 60 | 63 | 59 | |
| After | 60 | 57 | 61 | 60 | 60 | 57 | |
| 2 | 1 | 4 | 0 | 3 | 2 | 12 | |
| 0 | 2 | 1 | 0 | 0 | |||
| 0 | 1 | 4 | 4 | 1 | 0 | 10 |
Idea. The same runners twice: a paired test on the differences (Part 8). Faster means a positive .
Reject . There is significant evidence that the runners became faster, by 2 seconds on average.
Why: the statistic lies between and , so the p-value is between 0.005 and 0.01. Treated as two independent groups, the same data would give and no significance at all: the differences between the runners would hide the improvement.
Problem 16: Paired or independent?
Which test fits? (a) The blood pressure of 12 patients is measured before and after a medicine. (b) The salaries of 30 men and 30 women of a company are compared. (c) Two models are evaluated on the same 10 folds of the data. (d) Two different groups of users see two different prices.
Idea. Pairs are two measurements that belong together: the same person, the same fold (Part 8).
| Test | Reason | |
|---|---|---|
| (a) | paired t-test | the same patients twice |
| (b) | two-sample t-test | two separate groups (equal sizes do not make pairs) |
| (c) | paired t-test | the same folds for both models |
| (d) | two-sample t-test | two separate groups |
Why: the test of the question is "is there a natural partner for every value?". In (b) no woman is the partner of a particular man, although the groups have the same size.
Problem 17: The power of a test
A right-tailed z-test at checks with and . If the true mean is 54, what is the power of the test?
Idea. Find the border for , then the probability of crossing it when the true mean is 54 (Part 6).
Why: even though the mean has really risen by 4, the test notices it in only 64 of 100 samples. To reach a power of 0.80, a larger sample is needed.
Problem 18: A p-value from the t table
A two-tailed t-test with gives . Between which two values does the p-value lie?
Idea. Find the neighbours of 2.9 in row 12 and double their columns (Part 7).
Why: a larger statistic belongs to a smaller tail area. Software gives , inside the interval.
Level 3: Proportions, chi-square and judgement
Problem 19: An A/B test
An online shop shows two versions of its product page. Version A: 60 of 500 visitors buy. Version B: 90 of 500 visitors buy. Is there a difference between the versions? Test at .
Idea. Two proportions from independent groups: z-test with the pooled proportion (Part 9).
: reject . Even at the strict level there is significant evidence that version B sells better.
Why: the pooled proportion is used because says that there is only one true proportion; under the best estimate of it uses all 1,000 visitors.
Problem 20: Goodness of fit with equal shares
Ninety customers are asked which of three package designs they prefer: 38 choose design 1, 30 choose design 2 and 22 choose design 3. Are the designs equally popular?
Idea. One categorical variable against a claimed distribution: chi-square goodness of fit (Part 10).
Fail to reject . The data does not give enough evidence that the designs differ in popularity.
Why: 38 against 22 looks like a clear winner, but with 90 customers such a spread is not unusual. The statistic is even below , so (software: ).
Problem 21: Goodness of fit with unequal shares
A shop expects 50 percent of its sales to be product A, 30 percent product B and 20 percent product C. Of 200 sales, 85 were A, 70 were B and 45 were C. Do the sales fit the expectation?
Idea. The expected counts are the claimed shares of (Part 10).
| Product | A | B | C | Total |
|---|---|---|---|---|
| Observed | 85 | 70 | 45 | 200 |
| Expected | 100 | 60 | 40 | 200 |
| 2.250 | 1.667 | 0.625 | 4.542 |
Fail to reject . The sales are consistent with the expected shares.
Why: the largest contribution comes from product A, which sold 15 fewer than expected. Looking at the single terms shows where a misfit comes from.
Problem 22: A test of independence, 2 by 2
Of 100 customers who saw an advertisement, 45 bought the product. Of 100 customers who did not see it, 30 bought. Is buying related to seeing the advertisement?
Idea. Two categorical variables: chi-square test of independence, with (Part 10).
| Observed | Bought | Did not buy | Total |
|---|---|---|---|
| Saw the advertisement | 45 | 55 | 100 |
| Did not see it | 30 | 70 | 100 |
| Total | 75 | 125 | 200 |
Reject . There is significant evidence that buying is related to seeing the advertisement.
Why: the statistic lies between and , so the p-value is between 0.025 and 0.05. The test shows a relation, not a cause: if the advertisement was shown mainly to people who were already interested, the difference would not be the advertisement's work.
Problem 23: A test of independence, 2 by 3
A company asks 200 users on which device they use its app and whether they are satisfied. Is satisfaction related to the device?
| Observed | Phone | Tablet | Laptop | Total |
|---|---|---|---|---|
| Satisfied | 50 | 30 | 40 | 120 |
| Not satisfied | 30 | 30 | 20 | 80 |
| Total | 80 | 60 | 60 | 200 |
Idea. The same test with more columns; (Part 10).
The critical value (column 0.05, row 2) is , and : fail to reject . There is not enough evidence that satisfaction depends on the device.
Why: tablet users look less satisfied (50 percent against 62.5 and 67 percent), and most of the statistic comes from their column. But 60 tablet users are too few to be sure (software: ).
Problem 24: Significant, but important?
The mean order of a shop was 100, with a standard deviation of 15. After a new design, 10,000 orders have the mean 100.4. Test whether the mean has changed. Then judge the size of the effect.
Idea. A z-test (Part 5), then the effect size (Part 12).
: reject . The change is statistically significant. But the mean rose by 0.4, which is 0.4 percent, or 0.027 standard deviations: a very small effect.
Why: with 10,000 orders the standard error is so small that even a tiny difference is many standard errors wide. Whether 0.4 more per order is worth a new design is a question of costs, not of the p-value.
Problem 25: Twenty tests
A team tests 20 different colours of a button against the old one, each at . Suppose that no colour has any real effect. (a) How many "significant" results are expected? (b) What is the probability of at least one? (c) Which level does the Bonferroni correction give for each test?
Idea. Every test of a true gives a false alarm with the probability (Part 12).
Why: with 20 tests, finding one "winning" colour is the normal result of pure chance. A colour that wins should be tested again on new data before anyone believes it.
Problem 26: Comparing two models
Two models are evaluated on the same 8 folds. Their accuracies are given in percent. Is one model better than the other?
| Fold | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| A | 81 | 79 | 84 | 80 | 82 | 78 | 83 | 81 |
| B | 83 | 80 | 85 | 83 | 82 | 80 | 86 | 83 |
| 2 | 1 | 1 | 3 | 0 | 2 | 3 | 2 |
The row is the difference B − A.
Idea. The same folds for both models: a paired t-test on the differences (Parts 8 and 13).
Reject . There is significant evidence that model B is more accurate, by 1.75 points on average.
Why: model B is ahead in 7 of the 8 folds and never behind, and the differences are very regular, so the standard error is small. The statistic is above , so (software: ). The mean accuracies alone, 81 against 82.75, would not have shown how sure the difference is.
16Practice problems
Forty problems in three levels. Solve each one on paper, with the three tables of this lesson next to you, type your result into the answer boxes and press Check. Under every problem there is a hint and a full solution: look at the hint only when you are stuck, and at the solution only after you have an answer of your own. Unless a problem says something else, the significance level is . Round to two decimals before you use the normal table, and give and with three decimals.
Level 1: Hypotheses, z-tests and p-values
- 1.A producer says that its light bulbs last 1,000 hours on average. A consumer group believes that they last less. Write and . Is the test two-tailed, right-tailed or left-tailed?
Show a hint
has the equals sign. is what the consumer group wants to show.Show one possible solution
Answer: , , left-tailed.
The claimed value is 1,000 hours, so . The group looks for evidence of "less", so . Only small sample means speak against : the test is left-tailed.
- 2.A test gives the p-value 0.08. What is the decision at ? And at ?
Show a hint
Reject when the p-value is at most .Show one possible solution
Answer: Fail to reject at 0.05; reject at 0.10.
- 3.. The standard deviation of the population is 20. A sample of 25 values has the mean 208. Find the test statistic .
Show a hint
Divide by the standard error , not by .Show one possible solution
Answer: .
- 4.Find the p-value of the two-tailed test for , and decide at .
Show a hint
. Two tails.Show one possible solution
Answer: ; reject .
This is at most 0.05, so is rejected.
- 5.A machine should cut pieces of 50 mm; the standard deviation is known to be 6 mm. A sample of 36 pieces has the mean 48.2 mm. Test whether the mean differs from 50 mm.
Show a hint
The standard error is 1. The test is two-tailed.Show one possible solution
Answer: , ; fail to reject .
This is above 0.05: there is not enough evidence that the mean differs from 50 mm.
- 6.A fertiliser is said to raise the mean yield of a plant above 30 kg. The standard deviation is known to be 5 kg. A sample of 100 plants with the fertiliser has the mean 31.1 kg. Test the claim.
Show a hint
Right-tailed: the p-value is the area to the right of .Show one possible solution
Answer: , ; reject .
. There is significant evidence that the mean yield is above 30 kg.
- 7.A call centre answered calls after 8 minutes on average, with a standard deviation of 1.5 minutes. After a reorganisation, 25 calls were answered after 7.4 minutes on average. Has the waiting time become shorter?
Show a hint
Left-tailed: the p-value is the area to the left of .Show one possible solution
Answer: , ; reject .
. There is significant evidence that the waiting time has become shorter.
- 8.Give the critical value of a z-test for (a) a two-tailed test at (the positive one), (b) a right-tailed test at , (c) a left-tailed test at .
Show a hint
The table of critical values in Part 5.Show one possible solution
Answer: (a) (reject if is above it), (b) , (c) .
(a) Two tails share 0.10, so 0.05 in each: reject if . (b) The whole 0.01 is in the right tail: reject if . (c) The whole 0.05 is in the left tail: reject if .
- 9.A medical test has : "the patient is healthy". The test says that a healthy patient is ill. Which type of error is this?
Show a hint
Was true? Was it rejected?Show one possible solution
Answer: A Type I error.
was true (the patient is healthy) and the test rejected it. Rejecting a true is a Type I error, a false alarm.
- 10.A factory checks every batch with : "the batch is good". A bad batch passes the check. Which type of error is this? What can the factory do to make this error rarer without raising ?
Show a hint
was false and was not rejected.Show one possible solution
Answer: A Type II error. Take a larger sample from every batch.
was false (the batch is bad) and the test did not reject it: a Type II error, a miss. Its probability becomes smaller when the power grows, and for a fixed the power grows with the sample size.
- 11.A laboratory runs 200 tests at . Suppose that is true in every one of them. How many times is rejected, on average?
Show a hint
is the probability of a Type I error in one test.Show one possible solution
Answer: times.
Every test rejects a true with the probability 0.05, so 10 false alarms are expected.
- 12.A coin is tossed 100 times and shows heads 45 times. Is the coin unfair?
Show a hint
The standard error uses : .Show one possible solution
Answer: , ; fail to reject .
Far above 0.05: 45 heads in 100 tosses is a very ordinary result for a fair coin.
- 13.In a survey, 230 of 400 customers prefer the new packaging. Is there evidence that more than half of all customers prefer it?
Show a hint
. Right-tailed.Show one possible solution
Answer: , ; reject .
There is strong evidence that more than half of the customers prefer the new packaging.
- 14.Is the test two-tailed, right-tailed or left-tailed? (a) "Is the mean weight of the packets different from 500 g?" (b) "Does the medicine lower the blood pressure?" (c) "Do more than 60 percent of the users prefer the new design?" (d) "Has the average delivery time changed?"
Show a hint
Look for a direction in the question: "more", "less", or only "different".Show one possible solution
Answer: (a) two-tailed, (b) left-tailed, (c) right-tailed, (d) two-tailed.
(a) "Different" has no direction: . (b) "Lower": . (c) "More than": . (d) "Changed" can be up or down: .
Level 2: t-tests, errors and power
- 15.A t-test for one sample uses 20 values. How many degrees of freedom does it have? What is the critical value of the two-tailed test at 5 percent?
Show a hint
. Two tails at 5 percent: the column 0.025.Show one possible solution
Answer: ; .
. Column 0.025, row 19: . is rejected if .
- 16.. A sample of 25 values has the mean 83 and the standard deviation 10. Find the test statistic .
Show a hint
The standard error is .Show one possible solution
Answer: .
- 17.Decide the two-tailed test of Problem 16 at . Between which two values does the p-value lie?
Show a hint
. In row 24, find the neighbours of 1.5.Show one possible solution
Answer: Fail to reject ; the p-value is between 0.10 and 0.20.
is not rejected. One tail is between 0.05 and 0.10, so two tails are between 0.10 and 0.20. Software gives .
- 18.A shop claims that its customers spend 30 manat on average. An owner believes that they spend more. Nine customers spent 31.5 manat on average, with a standard deviation of 3 manat. Test the owner's belief.
Show a hint
Right-tailed with : the column 0.05.Show one possible solution
Answer: , critical value ; fail to reject .
is not rejected: nine customers do not give enough evidence.
- 19.A filling machine is set to 100 ml. A sample of 16 bottles has the mean 97 ml and the standard deviation 6 ml. Is the machine set wrongly?
Show a hint
Two-tailed with .Show one possible solution
Answer: , critical value ; fail to reject .
is not rejected. The result is close to the border (software: ), so a larger sample would be a good idea.
- 20.A teacher says that her students solve 7 problems an hour on average. Five students solved 7, 9, 8, 10 and 11 problems. Does the mean differ from 7?
Show a hint
First and .Show one possible solution
Answer: , , critical value ; reject .
The five values, their deviations from the mean 9 and the squares 7 9 8 10 11 Sum 0 1 2 0 4 0 1 1 4 10 is rejected, but only just (software: ).
- 21.Two classes of 12 students each write the same test. Class A: mean 65, standard deviation 5. Class B: mean 61, standard deviation 5. Do the classes differ?
Show a hint
Both standard deviations are 5, so . .Show one possible solution
Answer: , critical value ; fail to reject .
is not rejected: a difference of 4 marks is not enough evidence with 12 students in each class.
- 22.Two samples: with the variance , and with the variance . Find the pooled variance.
Show a hint
Weight each variance with .Show one possible solution
Answer: .
It lies between 10 and 17, closer to 17, because the second sample is larger.
- 23.A new teaching method is tried with 16 students, who reach a mean of 30 points (standard deviation 4). Sixteen other students with the old method reach a mean of 26 points (standard deviation 4). Do the methods differ?
Show a hint
, and .Show one possible solution
Answer: , , critical value ; reject .
is rejected: there is significant evidence that the methods differ, and the new one gives 4 points more.
- 24.Six people learn a typing method. The change in their speed (after minus before, in words a minute) is , 3, 2, 0, 4, 4. Did the method raise the speed?
Show a hint
A one-sample t-test on the differences: , .Show one possible solution
Answer: , , critical value ; reject .
The six differences, their deviations from the mean 2 and the squares 3 2 0 4 4 1 0 2 2 9 1 0 4 4 4 is rejected: there is significant evidence that the method raised the speed (software: ).
- 25.Paired or independent samples? (a) The weights of 15 people before and after a diet. (b) The test marks of two different classes. (c) The reaction times of the left and of the right hand of 20 people. (d) The prices of the same 25 products in two shops.
Show a hint
Does every value in one column have a natural partner in the other?Show one possible solution
Answer: (a) paired, (b) independent, (c) paired, (d) paired.
(a) The same person twice. (b) Different students, no partners. (c) Two hands of the same person. (d) The same product in both shops: compare the price differences product by product.
- 26.A two-tailed t-test with gives . Between which two values does the p-value lie?
Show a hint
Find the neighbours of 2.3 in row 10 and double their columns.Show one possible solution
Answer: Between 0.02 and 0.05.
Software gives .
- 27.A right-tailed z-test at checks with and . Find the power of the test if the true mean is 112. Round the border for to one decimal.
Show a hint
is rejected when is above .Show one possible solution
Answer: About .
The test notices such a rise in 91 of 100 samples.
- 28.. Three samples all have the mean 52 and the standard deviation 6, but different sizes: , and . Find for each. Which results are significant in a two-tailed test at 5 percent?
Show a hint
Only the standard error changes. For a that is not in the table, take the next smaller one.Show one possible solution
Answer: , and . Only is significant.
The three samples: standard error, statistic and critical value Row Critical value 9 1 8 8 2.306 36 2 35 30 2.042 144 4 143 120 1.980 Only the last is above its critical value: with the result is significant, with and it is not. The difference of 2 is the same every time. Four times as many values make twice as large.
Level 3: Proportions, chi-square and judgement
- 29.Two versions of an email are sent to 400 people each. Version A is opened by 40 people, version B by 60 people. Do the versions differ?
Show a hint
The pooled proportion is .Show one possible solution
Answer: , ; reject .
. There is significant evidence that version B is opened more often.
- 30.A coin is tossed 200 times: 112 heads and 88 tails. Test with a chi-square test whether the coin is fair.
Show a hint
The expected counts are 100 and 100. .Show one possible solution
Answer: , critical value ; fail to reject .
is not rejected: the data is consistent with a fair coin.
- 31.In a table of counts, a cell stands in a row with the total 60 and in a column with the total 50. The grand total is 200. Find the expected count of the cell under independence.
Show a hint
.Show one possible solution
Answer: .
- 32.A test of independence uses a table with 3 rows and 4 columns (without the totals). How many degrees of freedom does it have, and what is the critical value at 5 percent?
Show a hint
.Show one possible solution
Answer: ; .
Column 0.05, row 6 of the chi-square table: .
- 33.A travel agency sold 100 trips in a year: 30 in spring, 20 in summer, 22 in autumn and 28 in winter. Are the four seasons equally popular?
Show a hint
Every season is expected 25 times. .Show one possible solution
Answer: , critical value ; fail to reject .
is not rejected: such differences between the seasons can be chance.
- 34.Of 50 students who took a preparation course, 40 passed an exam. Of 50 students who did not take it, 30 passed. Is passing related to the course? Test at 5 percent and at 1 percent.
Show a hint
Write the table of 2 rows and 2 columns. The expected counts are 35 and 15 in both rows.Show one possible solution
Answer: , ; reject at 5 percent, fail to reject at 1 percent.
The 100 students: observed counts, with the expected counts in brackets Passed Failed Total Took the course 50 Did not take it 50 Total 70 30 100 lies between them: is rejected at 5 percent and not at 1 percent.
- 35.One hundred students are asked for their favourite subject. Of the 60 students of the day group, 20 name mathematics, 30 programming and 10 statistics. Of the 40 students of the evening group, 20 name mathematics, 10 programming and 10 statistics. Is the favourite subject related to the group?
Show a hint
The column totals are 40, 40 and 20. The expected counts of the day group are 24, 24 and 12.Show one possible solution
Answer: , , critical value ; reject .
The 100 students of the day group and of the evening group: observed counts, with the expected counts in brackets Day Evening Total Mathematics 40 Programming 40 Statistics 20 Total 60 40 100 is rejected: the favourite subject is related to the group. The largest term belongs to programming in the evening group, which has 10 students where 16 were expected.
- 36.A researcher makes 10 tests, each at . If every is true, what is the probability of at least one false alarm? Which level does the Bonferroni correction give for each test?
Show a hint
.Show one possible solution
Answer: About ; .
About 40 percent: with ten tests a false alarm is no surprise.
- 37.Two groups have the means 75 and 72, and the pooled standard deviation is 12. Find Cohen's d. Is the effect small, medium or large?
Show a hint
.Show one possible solution
Answer: ; small.
The means differ by a quarter of a standard deviation: a small effect.
- 38.An online shop shows the old page to 40,000 visitors, of whom 4,000 buy (10.0 percent), and the new page to 40,000 visitors, of whom 4,240 buy (10.6 percent). Is the difference significant? Is it important?
Show a hint
The pooled proportion is .Show one possible solution
Answer: , : significant. Whether it is important depends on what the difference is worth.
The difference is statistically significant. Its size is 0.6 percentage points, or 6 more buyers in every 1,000 visitors. For a large shop that can be worth a lot; for a small one perhaps not. The test cannot answer that part.
- 39.Two models are evaluated on the same 5 folds. Accuracies in percent: model A: 88, 90, 87, 91, 89; model B: 90, 91, 90, 92, 92. Is there a difference between the models?
Show a hint
The differences B − A are 2, 1, 3, 1, 3. A paired t-test with .Show one possible solution
Answer: , critical value ; reject .
The five differences, their deviations from the mean 2 and the squares 2 1 3 1 3 Sum 0 1 1 0 0 1 1 1 1 4 is rejected: model B is significantly more accurate, by 2 points on average (software: ).
- 40.A data scientist wants to know whether the feature "got a discount" is useful for predicting the target "bought". Of 60 customers with a discount, 36 bought. Of 140 customers without a discount, 54 bought. Test at whether the feature and the target are related.
Show a hint
Write the table of 2 rows and 2 columns. The expected counts are 27, 33, 63 and 77.Show one possible solution
Answer: , , critical value ; reject .
The 200 customers: observed counts, with the expected counts in brackets Bought Did not buy Total Discount 60 No discount 140 Total 90 110 200 is rejected even at 1 percent (software: ). Customers with a discount bought in 60 percent of the cases, the others in about 39 percent: the feature carries information about the target and is worth keeping.
17Check yourself
Twelve quick questions. Your answers are saved in this browser, so you can leave the page and continue later. Each finished round is kept as an attempt below the questions; you can delete the attempts at any time.
0 of 12 answered.
18Checking your work with Python
This part has the only code of the lesson. NumPy calculates the test statistics of three examples of this lesson with the same formulas as by hand.
import numpy as np
# One-sample t-test: the deliveries of Part 7 against 30 minutes
x = np.array([34, 31, 36, 29, 33, 35, 32, 34])
t = (x.mean() - 30) / (x.std(ddof=1) / np.sqrt(len(x)))
print(x.mean(), round(x.std(ddof=1), 3), round(t, 3))
# Paired t-test: the marks before and after the course of Part 8
before = np.array([60, 72, 55, 68, 80, 65, 70, 58])
after = np.array([65, 75, 54, 72, 86, 67, 70, 63])
d = after - before
t = d.mean() / (d.std(ddof=1) / np.sqrt(len(d)))
print(d.mean(), round(d.std(ddof=1), 3), round(t, 3))
# Chi-square test of independence: the plans and the customers of Part 10
observed = np.array([[70, 50], [60, 20]])
rows = observed.sum(axis=1)
columns = observed.sum(axis=0)
expected = rows.reshape(2, 1) * columns / observed.sum()
print(expected)
print(round(((observed - expected) ** 2 / expected).sum(), 3))Output
33.0 2.268 3.742 3.0 2.507 3.384 [[78. 42.] [52. 28.]] 5.861
Change it and run: put the data of a problem you solved by hand into x and replace 30 by the value that your null hypothesis claims. Then change one count in observed and watch the expected counts and the last number.
The first line prints the mean, the standard deviation and the statistic of the eight deliveries; ddof=1 makes NumPy divide by (Topic 18). The second line does the same for the differences of the paired example. Then come the expected counts of the table of plans, made from the totals of the rows and of the columns (a column of two numbers times a row of two numbers gives the whole table: broadcasting, Topic 9), and the statistic . All numbers agree with the calculations by hand.
The statistic is only half of a test. For the p-value, the library SciPy has a function for every test of this lesson. Each one returns the statistic and the exact p-value, so no table is needed:
| By hand | In Python (from scipy import stats) |
|---|---|
| one-sample t-test | stats.ttest_1samp(x, 30) |
| two-sample t-test with the pooled variance | stats.ttest_ind(a, b) |
| two-sample t-test without equal variances (Welch) | stats.ttest_ind(a, b, equal_var=False) |
| paired t-test | stats.ttest_rel(after, before) |
| one-tailed instead of two-tailed | add alternative="greater" or alternative="less" |
| chi-square goodness of fit | stats.chisquare(observed, expected) |
| chi-square test of independence | stats.chi2_contingency(table, correction=False) |
| area to the right of , of , of | stats.norm.sf(z), stats.t.sf(t, df), stats.chi2.sf(x, df) |
Here are the same three examples with SciPy. Each line prints the statistic and the exact p-value; the last line gives three of the p-values that the lesson quoted as "software gives".
import numpy as np
from scipy import stats
# One-sample t-test, right-tailed: the deliveries against 30 minutes
x = np.array([34, 31, 36, 29, 33, 35, 32, 34])
result = stats.ttest_1samp(x, 30, alternative="greater")
print(round(result.statistic, 3), round(result.pvalue, 4))
# Paired t-test, right-tailed: after against before
before = np.array([60, 72, 55, 68, 80, 65, 70, 58])
after = np.array([65, 75, 54, 72, 86, 67, 70, 63])
result = stats.ttest_rel(after, before, alternative="greater")
print(round(result.statistic, 3), round(result.pvalue, 4))
# Chi-square test of independence: the plans and the customers
observed = np.array([[70, 50], [60, 20]])
result = stats.chi2_contingency(observed, correction=False)
print(round(result.statistic, 3), round(result.pvalue, 4), result.dof)
# Three areas: right of z = 1.92, both tails of t = 2.0 (df 8), right of chi-square = 4.5 (df 5)
print(round(stats.norm.sf(1.92), 4), round(2 * stats.t.sf(2.0, 8), 3), round(stats.chi2.sf(4.5, 5), 2))Output
3.742 0.0036 3.384 0.0058 5.861 0.0155 1 0.0274 0.081 0.48
Change it and run: take away alternative="greater" in the first test and see the p-value double. Then set correction=False to True in the chi-square test.
The first two lines repeat and and give the p-values 0.0036 and 0.0058, which the t table could only place "below 0.005" and "between 0.005 and 0.01". The third line gives with its p-value and the degrees of freedom. The last line is the area to the right of (Part 4), the two-tailed p-value of the cups (Part 7) and the p-value of the die (Part 10). The first run of this example takes a little longer, because SciPy is downloaded once.
19Quick summary
| Idea | Definition or rule |
|---|---|
| Null hypothesis | "no difference, no effect"; contains the equals sign; believed until the data speaks against it |
| Alternative hypothesis | what we look for evidence of: (two-tailed), (right-tailed), (left-tailed) |
| Test statistic | |
| p-value | the probability of a result at least as extreme as ours, if is true |
| Significance level | the border for the p-value, fixed before the test; usually 0.05 |
| Decision | : reject ; : fail to reject (never "accept") |
| Two-tailed p-value | twice the area of one tail |
| Critical values of z | 1.645 (5 percent, one tail), 1.960 (5 percent, two tails), 2.576 (1 percent, two tails) |
| Type I error | rejecting a true ; its probability is |
| Type II error | not rejecting a false ; its probability is |
| Power | ; grows with the sample size, the size of the effect and , falls with the spread |
| z-test for a mean | , known |
| One-sample t-test | , |
| Two-sample t-test | , |
| Pooled variance | |
| Paired t-test | on the differences, |
| One proportion | |
| Two proportions | with the pooled |
| Chi-square statistic | , always right-tailed |
| Goodness of fit | , = categories − 1 |
| Independence | , |
| Effect size | Cohen's d: ; about 0.2 small, 0.5 medium, 0.8 large |
| Bonferroni correction | tests: each at the level |