Asiman Data School

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 xˉ\bar{x} and the sample standard deviation ss (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. 1.Why a difference in data must be tested before it is believed
  2. 2.The logic of a test: the null hypothesis, the alternative hypothesis, and "innocent until proven guilty"
  3. 3.The steps of every test, and one-tailed against two-tailed tests
  4. 4.What a p-value is, what it is not, and how the significance level α\alpha turns it into a decision
  5. 5.The z-test for a mean when σ\sigma is known
  6. 6.The two kinds of errors (Type I and Type II) and the power of a test
  7. 7.The t distribution, degrees of freedom and the t-test for one sample
  8. 8.The t-test for two independent samples and the paired t-test
  9. 9.The z-tests for one proportion and for two proportions (the A/B test)
  10. 10.The chi-square tests: goodness of fit and independence
  11. 11.How to choose the right test, and the conditions each test needs
  12. 12.Why "significant" does not mean "important": effect size, sample size and multiple testing
  13. 13.Where tests are used in machine learning
Notation used in this lesson
WrittenMeaning
H0H_0the null hypothesis: "nothing special is going on"
H1H_1the alternative hypothesis: what we look for evidence of
μ0\mu_0, p0p_0the value of the mean or of the proportion that H0H_0 claims
α\alphathe significance level: the chance of rejecting a true H0H_0 that we accept (the Greek letter alpha)
β\betathe probability of not rejecting a false H0H_0 (the Greek letter beta); 1−β1 - \beta is the power
zz, tt, χ2\chi^2test statistics; χ\chi is the Greek letter chi, "chi-square"
p-valuethe probability of a result at least as extreme as ours, if H0H_0 is true
dfdfdegrees of freedom
xˉ\bar{x}, ss, nnthe mean, the standard deviation and the size of a sample
p^\hat{p}the proportion in a sample ("p hat")
OO, EEan observed count and the count expected under H0H_0
SE\SEthe 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

Where the question appears, and what is tested
FieldThe questionWhat is tested
Product and marketingDoes version B of a page sell more than version A?two proportions (an A/B test)
ProductionIs the machine still filling 500 g on average?one mean
MedicineDoes the new medicine lower blood pressure more than the old one?two means
TeachingDid the course improve the students' results?pairs (before and after)
SurveysDoes the preferred product depend on the age group?two categorical variables
Machine learningIs 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

Where the ideas of this lesson come back in the course
Later in the courseWhat 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 regressionevery coefficient comes with a p-value: "is this feature's effect different from 0?"
Feature selectionthe chi-square test ranks categorical features by their link with the target
Model evaluationcomparing two models on the same folds with a paired t-test
A/B testing in practicesample 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.

A court and a test side by side
In courtIn a test
the accused is assumed innocentthe null hypothesis H0H_0 is assumed true
the prosecutor claims guiltthe alternative hypothesis H1H_1
the evidencethe data of the sample
"beyond reasonable doubt"the significance level α\alpha
verdict "guilty"reject H0H_0
verdict "not guilty"fail to reject H0H_0 (not: "accept H0H_0")

2.2The two hypotheses

  • The null hypothesis H0H_0 says that nothing special is going on: no difference, no effect, the old value still holds. It always contains an equals sign: μ=500\mu = 500, p=0.10p = 0.10, μ1=μ2\mu_1 = \mu_2.
  • The alternative hypothesis H1H_1 is what the researcher looks for evidence of: μ≠500\mu \ne 500, p>0.10p > 0.10, μ1≠μ2\mu_1 \ne \mu_2.

Hypotheses are statements about the population (about μ\mu or pp), 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?

  • H0H_0: the coin is fair, p=0.5p = 0.5
  • H1H_1: the coin shows heads too often, p>0.5p > 0.5

Assume H0H_0. 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.

00.050.135404550556065
The number of heads in 100 tosses of a fair coin. Results near 50 are ordinary. The strong bars, 60 heads or more, together have a probability of only about 0.03: if the coin is fair, our result is a rare one.

The test does not prove that the coin is unfair. It says: the data fits badly to a fair coin.

Try it yourself

A producer says that a pack of its pasta weighs 500 g on average. A customer believes that the packs are lighter. (a) What is H0H_0? (b) What is H1H_1? (c) A sample of 40 packs has the mean 497 g. Is 497 the value of μ\mu or of xˉ\bar{x}?

Show a hint
H0H_0 has the equals sign and the claimed value. H1H_1 is what the customer wants to show. Hypotheses speak about the population.
Show the solution

Answer: H0 ⁣:μ=500H_0\colon \mu = 500, H1 ⁣:μ<500H_1\colon \mu < 500, and 497 is the sample mean xˉ\bar{x}.

H0 ⁣:μ=500H1 ⁣:μ<500H_0\colon \mu = 500 \qquad H_1\colon \mu < 500

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 H1H_1 says μ<500\mu < 500. The number 497 was measured on 40 packs: it is the sample mean xˉ\bar{x}. The hypotheses are about μ\mu, 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.

  1. Write H0H_0 and H1H_1, and choose the significance level α\alpha (usually 0.05).
  2. Choose the test and check its conditions.
  3. Calculate the test statistic from the sample.
  4. Find the p-value (or compare the statistic with the critical value).
  5. Decide (reject H0H_0 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 H0H_0 claims, counted in standard errors:

test statistic=what the sample shows− what H0 claimsstandard error\text{test statistic} = \frac{\begin{gathered} \text{what the sample shows} \\ -\ \text{what } H_0 \text{ claims} \end{gathered}}{\text{standard error}}

A statistic near 0 means that the sample agrees with H0H_0. A statistic far from 0, such as 3 or −3-3, 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 H0H_0" is.

The three kinds of alternative hypotheses
H1H_1NameThe evidence against H0H_0 isTypical words
μ≠μ0\mu \ne \mu_0two-taileda statistic far from 0 on either side"different", "changed", "not equal"
μ>μ0\mu > \mu_0right-taileda large positive statistic"more", "longer", "increased", "better"
μ<μ0\mu < \mu_0left-taileda large negative statistic"less", "shorter", "decreased"
−1.6450

Left-tailed: H1 ⁣:μ<μ0H_1\colon \mu < \mu_0

reject below −1.645

−1.96001.960

Two-tailed: H1 ⁣:μ≠μ0H_1\colon \mu \ne \mu_0

reject below −1.960 or above 1.960

01.645

Right-tailed: H1 ⁣:μ>μ0H_1\colon \mu > \mu_0

reject above 1.645

The rejection regions of the three kinds of tests at the 5 percent level: the values of the statistic that are so unusual under the null hypothesis that it is rejected. In a two-tailed test the 5 percent are split, 2.5 percent on each side.

The shaded areas are the rejection regions for α=0.05\alpha = 0.05: the values of the statistic that are so unusual under H0H_0 that H0H_0 is rejected. In a two-tailed test the 5 percent are split, 2.5 percent on each side.

Try it yourself

(a) A sample has the mean 52. The null hypothesis claims the mean 50, and the standard error is 0.8. Find the test statistic. (b) Which kind of test fits the question "Has the average delivery time changed?" (c) And the question "Does the new battery last longer?"

Show a hint
The statistic is the difference divided by the standard error. Look for a direction in the question.
Show the solution

Answer: (a) 2.52.5; (b) two-tailed; (c) right-tailed.

statistic=52−500.8=2.5\text{statistic} = \frac{52 - 50}{0.8} = 2.5

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, H1 ⁣:μ>μ0H_1\colon \mu > \mu_0.

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 H0H_0:

How to read a p-value
p-valueThe data under H0H_0Evidence against H0H_0
large, such as 0.40quite ordinarynone
about 0.05unusualsome
below 0.01very unusualstrong
below 0.001almost impossiblevery 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.

0z = 1.92

p=0.0274p = 0.0274

right-tailed test

−1.920z = 1.92

p=2×0.0274=0.0548p = 2 \times 0.0274 = 0.0548

two-tailed test

The p-value of the statistic z = 1.92 as an area. In a right-tailed test it is the area to the right of z. In a two-tailed test a result as far away on the other side counts as just as extreme, so the area is doubled.

A test gave the statistic z. Choose the kind of test and the level, and move z.

Significance level α
−3−2−10123

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 α\alpha: the largest p-value at which H0H_0 is still rejected. The usual choice is α=0.05\alpha = 0.05; stricter fields use 0.01 or less.

The decision rule
p-valueDecisionThe result is called
p≤αp \le \alphareject H0H_0"statistically significant"
p>αp > \alphafail to reject H0H_0"not significant"

α\alpha is also the risk we accept: if H0H_0 is true, a test at α=0.05\alpha = 0.05 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: "p=0.03p = 0.03 means that H0H_0 is true with the probability 3 percent." The p-value is calculated assuming H0H_0. It is P(data this extreme∣H0)P(\text{data this extreme} \mid H_0), not P(H0∣data)P(H_0 \mid \text{data}). Topic 16 showed that these two are different things.
  • Wrong: "p=0.03p = 0.03 means that there is a 3 percent chance that the result is due to chance." Same mistake in other words.
  • Wrong: "p=0.30p = 0.30 proves that H0H_0 is true." A large p-value says only that the data does not contradict H0H_0. A small sample almost always gives a large p-value, whatever the truth is.

And one sentence is right: "If H0H_0 were true, a result at least this extreme would occur in 3 percent of all samples."

4.4The wording of the conclusion

What to say after a test, and what not
DecisionSayDo not say
reject H0H_0"There is significant evidence that the mean has changed.""We proved that the mean has changed."
fail to reject H0H_0"There is not enough evidence that the mean has changed.""We proved that the mean is still 500." "We accept H0H_0."

Always finish with a sentence about the real question, not about H0H_0: "the data gives evidence that the new button sells more".

Try it yourself

A test gives the statistic z=1.5z = 1.5. Use Φ(1.50)=0.9332\Phi(1.50) = 0.9332. (a) Find the p-value of the right-tailed test. (b) Find the p-value of the two-tailed test. (c) What is the decision of the two-tailed test at α=0.05\alpha = 0.05?

Show a hint
Right-tailed: the area to the right of zz, which is 1−Φ(z)1 - \Phi(z). Two-tailed: twice as much.
Show the solution

Answer: (a) 0.06680.0668; (b) 0.13360.1336; (c) fail to reject H0H_0.

right-tailed:p=1−Φ(1.50)=1−0.9332=0.0668two-tailed:p=2×0.0668=0.1336\begin{aligned} \text{right-tailed:}\quad p &= 1 - \Phi(1.50) \\ &= 1 - 0.9332 = 0.0668 \\[4pt] \text{two-tailed:}\quad p &= 2 \times 0.0668 = 0.1336 \end{aligned}

0.1336>0.050.1336 > 0.05: fail to reject H0H_0. 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 μ0\mu_0, when the standard deviation σ\sigma of the population is known (from long experience with a machine, for example).

Topic 17 showed: if H0H_0 is true, the sample mean xˉ\bar{x} varies around μ0\mu_0 with the standard error σn\dfrac{\sigma}{\sqrt{n}}, and it is normal when the population is normal or the sample is large (n≥30n \ge 30). So the statistic

z=xˉ−μ0σ/nz = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}

follows the standard normal distribution if H0H_0 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 α=0.05\alpha = 0.05.

The test is two-tailed: too much and too little are both a fault. It is a z-test: σ\sigma is known, and the weights of bags are close to normal.

1. HypothesesH0 ⁣:μ=500H1 ⁣:μ≠500α=0.052. Testz-test: σ is known3. StatisticSE=1025=2z=496−5002=−2.04. p-valueone tail:P(Z<−2.0)=1−0.9772=0.0228two tails:p=2×0.0228=0.04565. Decision0.0456≤0.05 ⁣: reject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon \mu = 500 \qquad H_1\colon \mu \ne 500 \\ & \quad \alpha = 0.05 \\[6pt] & \text{2. Test} \\ & \quad \text{z-test: } \sigma \text{ is known} \\[6pt] & \text{3. Statistic} \\ & \quad \SE = \frac{10}{\sqrt{25}} = 2 \\[2pt] & \quad z = \frac{496 - 500}{2} = -2.0 \\[6pt] & \text{4. p-value} \\ & \quad \text{one tail:} \\ & \quad P(Z < -2.0) = 1 - 0.9772 = 0.0228 \\ & \quad \text{two tails:} \\ & \quad p = 2 \times 0.0228 = 0.0456 \\[6pt] & \text{5. Decision} \\ & \quad 0.0456 \le 0.05\colon\ \text{reject } H_0 \end{aligned}

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 α=0.01\alpha = 0.01 the same data would not reject H0H_0, because 0.0456>0.010.0456 > 0.01. 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?

1. HypothesesH0 ⁣:μ=70H1 ⁣:μ>70α=0.053. StatisticSE=1036≈1.667z=73.2−701.667≈1.924. p-valuep=P(Z>1.92)p=1−0.9726=0.02745. Decision0.0274≤0.05 ⁣: reject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon \mu = 70 \qquad H_1\colon \mu > 70 \\ & \quad \alpha = 0.05 \\[6pt] & \text{3. Statistic} \\ & \quad \SE = \frac{10}{\sqrt{36}} \approx 1.667 \\[2pt] & \quad z = \frac{73.2 - 70}{1.667} \approx 1.92 \\[6pt] & \text{4. p-value} \\ & \quad p = P(Z > 1.92) \\ & \quad \phantom{p} = 1 - 0.9726 = 0.0274 \\[6pt] & \text{5. Decision} \\ & \quad 0.0274 \le 0.05\colon\ \text{reject } H_0 \end{aligned}

There is significant evidence that the new method raises the mean mark.

With a two-tailed test the p-value would be 2×0.0274=0.05482 \times 0.0274 = 0.0548, just above 0.05, and H0H_0 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 α\alpha.

Critical values of the z-test
Testα=0.10\alpha = 0.10α=0.05\alpha = 0.05α=0.01\alpha = 0.01
two-tailed: reject if ∣z∣\len{z} is above1.6451.9602.576
right-tailed: reject if zz is above1.2821.6452.326
left-tailed: reject if zz is below−1.282-1.282−1.645-1.645−2.326-2.326

In Example 1: ∣z∣=2.0>1.960\len{z} = 2.0 > 1.960, so H0H_0 is rejected at 5 percent; but 2.0<2.5762.0 < 2.576, so not at 1 percent. In Example 2: z=1.92>1.645z = 1.92 > 1.645, 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

Φ(z)\Phi(z) is the area to the left of zz. The row gives zz to one decimal, the column the second decimal (Topic 17).

Look up z =+

Φ(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.

The standard normal distribution: the area to the left of z, for z from 0.00 to 3.09
z.00.01.02.03.04.05.06.07.08.09
0.00.50000.50400.50800.51200.51600.51990.52390.52790.53190.5359
0.10.53980.54380.54780.55170.55570.55960.56360.56750.57140.5753
0.20.57930.58320.58710.59100.59480.59870.60260.60640.61030.6141
0.30.61790.62170.62550.62930.63310.63680.64060.64430.64800.6517
0.40.65540.65910.66280.66640.67000.67360.67720.68080.68440.6879
0.50.69150.69500.69850.70190.70540.70880.71230.71570.71900.7224
0.60.72570.72910.73240.73570.73890.74220.74540.74860.75170.7549
0.70.75800.76110.76420.76730.77040.77340.77640.77940.78230.7852
0.80.78810.79100.79390.79670.79950.80230.80510.80780.81060.8133
0.90.81590.81860.82120.82380.82640.82890.83150.83400.83650.8389
1.00.84130.84380.84610.84850.85080.85310.85540.85770.85990.8621
1.10.86430.86650.86860.87080.87290.87490.87700.87900.88100.8830
1.20.88490.88690.88880.89070.89250.89440.89620.89800.89970.9015
1.30.90320.90490.90660.90820.90990.91150.91310.91470.91620.9177
1.40.91920.92070.92220.92360.92510.92650.92790.92920.93060.9319
1.50.93320.93450.93570.93700.93820.93940.94060.94180.94290.9441
1.60.94520.94630.94740.94840.94950.95050.95150.95250.95350.9545
1.70.95540.95640.95730.95820.95910.95990.96080.96160.96250.9633
1.80.96410.96490.96560.96640.96710.96780.96860.96930.96990.9706
1.90.97130.97190.97260.97320.97380.97440.97500.97560.97610.9767
2.00.97720.97780.97830.97880.97930.97980.98030.98080.98120.9817
2.10.98210.98260.98300.98340.98380.98420.98460.98500.98540.9857
2.20.98610.98640.98680.98710.98750.98780.98810.98840.98870.9890
2.30.98930.98960.98980.99010.99040.99060.99090.99110.99130.9916
2.40.99180.99200.99220.99250.99270.99290.99310.99320.99340.9936
2.50.99380.99400.99410.99430.99450.99460.99480.99490.99510.9952
2.60.99530.99550.99560.99570.99590.99600.99610.99620.99630.9964
2.70.99650.99660.99670.99680.99690.99700.99710.99720.99730.9974
2.80.99740.99750.99760.99770.99770.99780.99790.99790.99800.9981
2.90.99810.99820.99820.99830.99840.99840.99850.99850.99860.9986
3.00.99870.99870.99870.99880.99880.99890.99890.99890.99900.9990
The table of the standard normal distribution: Φ(z), the area to the left of z. The row gives z to one decimal, the column its second decimal. For z above 3 the area is practically 1.
right-tailed:p=1−Φ(z)left-tailed:p=Φ(z)two-tailed:p=2×(1−Φ(∣z∣))\begin{aligned} \text{right-tailed:}\quad & p = 1 - \Phi(z) \\ \text{left-tailed:}\quad & p = \Phi(z) \\ \text{two-tailed:}\quad & p = 2 \times \bigl(1 - \Phi(\len{z})\bigr) \end{aligned}

Try it yourself

The weight of a kind of apple has the mean 60 g and the standard deviation 12 g. A farmer tries a new fertiliser, and 36 of his apples have the mean 64.5 g. Is the mean weight different from 60 g? Find the standard error, zz and the p-value, and decide at α=0.05\alpha = 0.05.

Show a hint
The standard error is 1236\dfrac{12}{\sqrt{36}}. The question says "different", so the test is two-tailed.
Show the solution

Answer: SE=2\SE = 2, z=2.25z = 2.25, p=0.0244p = 0.0244; reject H0H_0.

H0 ⁣:μ=60H1 ⁣:μ≠60SE=1236=2z=64.5−602=2.25p=2×(1−0.9878)=0.0244\begin{aligned} & H_0\colon \mu = 60 \qquad H_1\colon \mu \ne 60 \\[4pt] & \SE = \frac{12}{\sqrt{36}} = 2 \\[2pt] & z = \frac{64.5 - 60}{2} = 2.25 \\[4pt] & p = 2 \times (1 - 0.9878) = 0.0244 \end{aligned}

0.0244≤0.050.0244 \le 0.05: reject H0H_0. There is significant evidence that the mean weight is no longer 60 g; the apples are heavier. With critical values: 2.25>1.9602.25 > 1.960, 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.

The four possible outcomes of a test
H0H_0 is really trueH0H_0 is really false
The test rejects H0H_0Type I error (a false alarm), probability α\alphacorrect, probability 1−β1 - \beta (the power)
The test does not reject H0H_0correct, probability 1−α1 - \alphaType II error (a missed effect), probability β\beta
  • A Type I error is rejecting a true H0H_0: 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 H0H_0: the button works, but the test does not notice. In court: letting a guilty person go.

The significance level α\alpha is the probability of a Type I error, and we choose it. The probability β\beta of a Type II error depends on how large the real effect is and on the sample.

6.2The trade-off

if H₀ is true: mean 70if the mean is really 75βα667073.37579
The sample mean of 25 students in two worlds: the left curve if the null hypothesis is true (mean 70), the right curve if the mean is really 75. Above the dashed border, 73.3, the null hypothesis is rejected. The area α is the chance of a false alarm; the area β is the chance of missing the real effect.

The picture shows the sample mean in two worlds: the left curve if H0H_0 is true, the right curve if the true mean is 75. The dashed line is the border: above it, H0H_0 is rejected. The area α\alpha is the chance of a false alarm; the area β\beta is the chance of missing the real effect.

Move the border to the right and α\alpha shrinks, but β\beta 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 1−β1 - \beta: 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 σ=10\sigma = 10. A right-tailed z-test at α=0.05\alpha = 0.05 is done with n=25n = 25 students. If the new method really raises the mean to 75, how likely is it that the test notices?

SE=1025=2H0 is rejected when z>1.645:xˉ>70+1.645×2=73.29≈73.3If the true mean is 75:z=73.3−752=−0.85β=P(xˉ<73.3)β=Φ(−0.85)=0.1977power=1−0.1977=0.8023\begin{aligned} & \SE = \frac{10}{\sqrt{25}} = 2 \\[6pt] & H_0 \text{ is rejected when } z > 1.645\text{:} \\ & \quad \bar{x} > 70 + 1.645 \times 2 = 73.29 \approx 73.3 \\[6pt] & \text{If the true mean is 75:} \\ & \quad z = \frac{73.3 - 75}{2} = -0.85 \\[2pt] & \quad \beta = P(\bar{x} < 73.3) \\ & \quad \phantom{\beta} = \Phi(-0.85) = 0.1977 \\[6pt] & \text{power} = 1 - 0.1977 = 0.8023 \end{aligned}

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 α.

Significance level α
60657075808590
  • 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

What raises the power of a test
ChangeEffect on the power
a larger sample nnhigher: the standard error shrinks, the two curves move apart
a larger real effecthigher: a big difference is easier to see
a smaller spread σ\sigmahigher
a larger α\alpha (0.10 instead of 0.05)higher, but at the price of more false alarms
a one-tailed test instead of a two-tailed onehigher in that direction, and blind in the other

The same example with other sample sizes:

The power of the same test with 9, 25 and 100 students
nnStandard errorReject when xˉ\bar{x} is abovePower when the true mean is 75
93.3375.50.44
25273.30.80
100171.6more 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

A fire alarm tests H0H_0: "there is no fire". (a) It rings although nothing is burning. Which error is that? (b) It stays silent while a room is burning. Which error is that? (c) A right-tailed z-test at α=0.05\alpha = 0.05 checks H0 ⁣:μ=20H_0\colon \mu = 20 with σ=6\sigma = 6 and n=36n = 36. Find its power if the true mean is 23. Round the border for xˉ\bar{x} to one decimal.

Show a hint
Type I: a true H0H_0 is rejected. Type II: a false H0H_0 is not rejected. For the power: the standard error is 1, and H0H_0 is rejected when xˉ\bar{x} is above 20+1.645×120 + 1.645 \times 1.
Show the solution

Answer: (a) a Type I error; (b) a Type II error; (c) about 0.91920.9192.

In (a), H0H_0 was true and was rejected: a false alarm, a Type I error. In (b), H0H_0 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.

SE=636=1border: 20+1.645×1=21.645≈21.6z=21.6−231=−1.40β=Φ(−1.40)=0.0808power=1−0.0808=0.9192\begin{aligned} & \SE = \frac{6}{\sqrt{36}} = 1 \\[4pt] & \text{border: } 20 + 1.645 \times 1 = 21.645 \approx 21.6 \\[4pt] & z = \frac{21.6 - 23}{1} = -1.40 \\[2pt] & \beta = \Phi(-1.40) = 0.0808 \\[4pt] & \text{power} = 1 - 0.0808 = 0.9192 \end{aligned}

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 σ\sigma of the population, and in real work nobody knows it. The natural idea is to use the sample standard deviation ss of Topic 18 instead:

t=xˉ−μ0s/nt = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}

But ss 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

−4−3−2−101234
  • normal (z)
  • t with df = 2
  • t with df = 5
  • t with df = 30
The standard normal curve and three t distributions. With few degrees of freedom the t curve is lower in the middle and has heavier tails. With 30 degrees of freedom it can hardly be told apart from the normal curve.
  • It is a bell, symmetric around 0, like the standard normal curve.
  • It has heavier tails: extreme values are more likely, because ss adds uncertainty.
  • It has one parameter, the degrees of freedom. For a one-sample test, df=n−1df = n - 1 (the number of deviations that carry free information, Topic 18).
  • The larger dfdf, the closer it comes to the normal curve. From about df=30df = 30 the two are almost the same.

The t distribution with df degrees of freedom (a sample of df + 1 values) against the normal curve.

−4−2024
  • 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 tt that cuts off the area α\alpha in the right tail.

Look up df =and the area

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.

The t distribution: the value that cuts off a given area in the right tail, for every number of degrees of freedom
df0.100.050.0250.010.005
13.0786.31412.70631.82163.657
21.8862.9204.3036.9659.925
31.6382.3533.1824.5415.841
41.5332.1322.7763.7474.604
51.4762.0152.5713.3654.032
61.4401.9432.4473.1433.707
71.4151.8952.3652.9983.499
81.3971.8602.3062.8963.355
91.3831.8332.2622.8213.250
101.3721.8122.2282.7643.169
111.3631.7962.2012.7183.106
121.3561.7822.1792.6813.055
131.3501.7712.1602.6503.012
141.3451.7612.1452.6242.977
151.3411.7532.1312.6022.947
161.3371.7462.1202.5832.921
171.3331.7402.1102.5672.898
181.3301.7342.1012.5522.878
191.3281.7292.0932.5392.861
201.3251.7252.0862.5282.845
211.3231.7212.0802.5182.831
221.3211.7172.0742.5082.819
231.3191.7142.0692.5002.807
241.3181.7112.0642.4922.797
251.3161.7082.0602.4852.787
261.3151.7062.0562.4792.779
271.3141.7032.0522.4732.771
281.3131.7012.0482.4672.763
291.3111.6992.0452.4622.756
301.3101.6972.0422.4572.750
401.3031.6842.0212.4232.704
601.2961.6712.0002.3902.660
1201.2891.6581.9802.3582.617
∞ (z)1.2821.6451.9602.3262.576
The table of the t distribution: the value that cuts off the area in the column heading in the right tail. The row is the number of degrees of freedom; the last row is the normal distribution.

How to use it:

  • Right-tailed test at α\alpha: take the column α\alpha. Reject if tt is above the value.
  • Left-tailed test at α\alpha: the same column with a minus sign. Reject if tt is below it.
  • Two-tailed test at α\alpha: take the column α2\dfrac{\alpha}{2} (for 5 percent, the column 0.025). Reject if ∣t∣\len{t} is above the value.
  • If your dfdf 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, tt and zz 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 dfdf, find the two values between which your ∣t∣\len{t} 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 α=0.05\alpha = 0.05.

σ\sigma is unknown and the sample is small: a one-sample t-test (the amounts are roughly normal).

1. HypothesesH0 ⁣:μ=250H1 ⁣:μ≠250α=0.052. Testone-sample t-test: σ is unknown3. StatisticSE=69=2t=246−2502=−2.0df=9−1=84. Critical valuetwo-tailed at 5 percent:column 0.025, row 8: 2.306∣t∣=2.0<2.3065. Decisionfail to reject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon \mu = 250 \qquad H_1\colon \mu \ne 250 \\ & \quad \alpha = 0.05 \\[6pt] & \text{2. Test} \\ & \quad \text{one-sample t-test: } \sigma \text{ is unknown} \\[6pt] & \text{3. Statistic} \\ & \quad \SE = \frac{6}{\sqrt{9}} = 2 \\[2pt] & \quad t = \frac{246 - 250}{2} = -2.0 \\[2pt] & \quad df = 9 - 1 = 8 \\[6pt] & \text{4. Critical value} \\ & \quad \text{two-tailed at 5 percent:} \\ & \quad \text{column 0.025, row 8: } 2.306 \\ & \quad \len{t} = 2.0 < 2.306 \\[6pt] & \text{5. Decision} \\ & \quad \text{fail to reject } H_0 \end{aligned}

There is not enough evidence that the cups hold a different amount on average. The p-value: 2.02.0 lies between 1.8601.860 (column 0.05) and 2.3062.306 (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 p≈0.081p \approx 0.081.

Compare with a z-test: ∣z∣=2.0\len{z} = 2.0 would have been significant (p=0.0456p = 0.0456). With only 9 cups and an estimated ss, 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 α=0.05\alpha = 0.05.

The eight delivery times, their deviations from the mean 33 and the squares
xx3431362933353234
x−xˉx - \bar{x}1−2-23−4-402−1-11
(x−xˉ)2(x - \bar{x})^2149160411
1. HypothesesH0 ⁣:μ=30H1 ⁣:μ>30α=0.053. Statisticxˉ=2648=33sum of the squares: 36s2=367≈5.1429s≈2.2678SE=2.26788≈0.8018t=33−300.8018≈3.742df=74. Critical valueright-tailed at 5 percent:column 0.05, row 7: 1.895t=3.742>1.8955. Decisionreject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon \mu = 30 \qquad H_1\colon \mu > 30 \\ & \quad \alpha = 0.05 \\[6pt] & \text{3. Statistic} \\ & \quad \bar{x} = \frac{264}{8} = 33 \\[2pt] & \quad \text{sum of the squares: } 36 \\[2pt] & \quad s^2 = \frac{36}{7} \approx 5.1429 \qquad s \approx 2.2678 \\[2pt] & \quad \SE = \frac{2.2678}{\sqrt{8}} \approx 0.8018 \\[2pt] & \quad t = \frac{33 - 30}{0.8018} \approx 3.742 \\[2pt] & \quad df = 7 \\[6pt] & \text{4. Critical value} \\ & \quad \text{right-tailed at 5 percent:} \\ & \quad \text{column 0.05, row 7: } 1.895 \\ & \quad t = 3.742 > 1.895 \\[6pt] & \text{5. Decision} \\ & \quad \text{reject } H_0 \end{aligned}

There is significant evidence that deliveries take longer than 30 minutes on average. The statistic is even above 3.4993.499 (column 0.005), so p<0.005p < 0.005; software gives p≈0.0036p \approx 0.0036.

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 (n≥30n \ge 30, 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?

The z-test and the t-test side by side
z-testt-test
Standard deviationσ\sigma of the population is knownonly ss of the sample is known
Statisticxˉ−μ0σ/n\dfrac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}xˉ−μ0s/n\dfrac{\bar{x} - \mu_0}{s / \sqrt{n}}
Distributionstandard normalt with df=n−1df = n - 1
In practicerarethe usual case

Try it yourself

A machine should make screws of 40 mm. A sample of 16 screws has the mean 43 mm and the standard deviation 8 mm. Is the mean length different from 40 mm? Find the standard error, tt, the degrees of freedom and the critical value of the two-tailed test at 5 percent, and decide.

Show a hint
Only ss is known, so it is a t-test with df=n−1df = n - 1. Two tails at 5 percent: the column 0.025.
Show the solution

Answer: SE=2\SE = 2, t=1.5t = 1.5, df=15df = 15, critical value 2.1312.131; fail to reject H0H_0.

H0 ⁣:μ=40H1 ⁣:μ≠40SE=816=2t=43−402=1.5df=15column 0.025, row 15: 2.1311.5<2.131\begin{aligned} & H_0\colon \mu = 40 \qquad H_1\colon \mu \ne 40 \\[4pt] & \SE = \frac{8}{\sqrt{16}} = 2 \\[2pt] & t = \frac{43 - 40}{2} = 1.5 \qquad df = 15 \\[4pt] & \text{column 0.025, row 15: } 2.131 \\ & 1.5 < 2.131 \end{aligned}

Fail to reject H0H_0. A mean of 43 instead of 40 looks like a clear fault, but the screws vary a lot (s=8s = 8) and there are only 16 of them: the difference is 1.5 standard errors. In row 15, 1.5 lies between 1.3411.341 and 1.7531.753, 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 and paired samples
Independent samplesPaired samples
What they aretwo separate groups of different people or thingstwo measurements of the same people or things, or matched pairs
Examples10 students taught with method A, 10 other students with method Bthe same 8 students before and after a course
Sizesmay be differentalways the same
Testtwo-sample t-testpaired t-test

8.2The two-sample t-test

Two independent samples with the sizes n1n_1, n2n_2, the means xˉ1\bar{x}_1, xˉ2\bar{x}_2 and the standard deviations s1s_1, s2s_2. The null hypothesis says that the two population means are equal: H0 ⁣:μ1=μ2H_0\colon \mu_1 = \mu_2.

When the two groups have a similar spread (a usual rule: the larger ss is less than twice the smaller), their variances are combined into one, the pooled variance, a weighted mean of the two:

sp2=(n1−1)×s12+(n2−1)×s22n1+n2−2t=xˉ1−xˉ2sp×1n1+1n2df=n1+n2−2\begin{aligned} s_p^2 &= \frac{(n_1 - 1) \times s_1^2 + (n_2 - 1) \times s_2^2}{n_1 + n_2 - 2} \\[8pt] t &= \frac{\bar{x}_1 - \bar{x}_2}{s_p \times \sqrt{\dfrac{1}{n_1} + \dfrac{1}{n_2}}} \\[8pt] df &= n_1 + n_2 - 2 \end{aligned}

When the two groups have the same size, the pooled variance is simply the mean of the two variances: sp2=s12+s222s_p^2 = \dfrac{s_1^2 + s_2^2}{2}.

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 α=0.05\alpha = 0.05.

1. HypothesesH0 ⁣:μ1=μ2H1 ⁣:μ1≠μ2α=0.053. Statisticsp2=82+622=64+362=50sp≈7.071SE=7.071×110+110SE=7.071×0.4472≈3.162t=80−723.162≈2.530df=10+10−2=184. Critical valuetwo-tailed at 5 percent:column 0.025, row 18: 2.101∣t∣=2.530>2.1015. Decisionreject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon \mu_1 = \mu_2 \qquad H_1\colon \mu_1 \ne \mu_2 \\ & \quad \alpha = 0.05 \\[6pt] & \text{3. Statistic} \\ & \quad s_p^2 = \frac{8^2 + 6^2}{2} = \frac{64 + 36}{2} = 50 \\[2pt] & \quad s_p \approx 7.071 \\[2pt] & \quad \SE = 7.071 \times \sqrt{\frac{1}{10} + \frac{1}{10}} \\ & \quad \phantom{\SE} = 7.071 \times 0.4472 \approx 3.162 \\[2pt] & \quad t = \frac{80 - 72}{3.162} \approx 2.530 \\[2pt] & \quad df = 10 + 10 - 2 = 18 \\[6pt] & \text{4. Critical value} \\ & \quad \text{two-tailed at 5 percent:} \\ & \quad \text{column 0.025, row 18: } 2.101 \\ & \quad \len{t} = 2.530 > 2.101 \\[6pt] & \text{5. Decision} \\ & \quad \text{reject } H_0 \end{aligned}

There is significant evidence that the two methods lead to different mean marks; method A does better. The statistic lies between 2.1012.101 and 2.5522.552, so the two-tailed p-value is between 0.02 and 0.05; software gives p≈0.021p \approx 0.021.

When the spreads are very different, the variances are not pooled. The statistic is then

t=xˉ1−xˉ2s12n1+s22n2t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{\dfrac{s_1^2}{n_1} + \dfrac{s_2^2}{n_2}}}

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 dd for every pair, and test whether the mean of the differences is 0. That is a one-sample t-test on the differences:

t=dˉsd/ndf=n−1t = \frac{\bar{d}}{s_d / \sqrt{n}} \qquad df = n - 1

dˉ\bar{d} is the mean of the differences, sds_d their standard deviation, and nn the number of pairs.

Example. Eight students take a test before and after a course.

The marks of eight students before and after a course
Student12345678
Before6072556880657058
After6575547286677063
dd53−1-146205
d−dˉd - \bar{d}20−4-413−1-1−3-32
(d−dˉ)2(d - \bar{d})^2401619194

The row dd is the difference after − before. Did the course improve the results? Test at α=0.05\alpha = 0.05.

1. HypothesesH0 ⁣:μd=0H1 ⁣:μd>0α=0.053. Statisticdˉ=248=3sum of the squares: 44sd2=447≈6.2857sd≈2.5071SE=2.50718≈0.8864t=30.8864≈3.384df=74. Critical valueright-tailed at 5 percent:column 0.05, row 7: 1.895t=3.384>1.8955. Decisionreject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon \mu_d = 0 \qquad H_1\colon \mu_d > 0 \\ & \quad \alpha = 0.05 \\[6pt] & \text{3. Statistic} \\ & \quad \bar{d} = \frac{24}{8} = 3 \\[2pt] & \quad \text{sum of the squares: } 44 \\[2pt] & \quad s_d^2 = \frac{44}{7} \approx 6.2857 \qquad s_d \approx 2.5071 \\[2pt] & \quad \SE = \frac{2.5071}{\sqrt{8}} \approx 0.8864 \\[2pt] & \quad t = \frac{3}{0.8864} \approx 3.384 \\[2pt] & \quad df = 7 \\[6pt] & \text{4. Critical value} \\ & \quad \text{right-tailed at 5 percent:} \\ & \quad \text{column 0.05, row 7: } 1.895 \\ & \quad t = 3.384 > 1.895 \\[6pt] & \text{5. Decision} \\ & \quad \text{reject } H_0 \end{aligned}

There is significant evidence that the course improved the results, by 3 marks on average. The statistic lies between 2.9982.998 and 3.4993.499, so the p-value is between 0.005 and 0.01 (software: p≈0.0058p \approx 0.0058).

665060708090

Before

mean 66

695060708090

After

mean 69

no change3−202468

The differences: after − before

mean 3

The eight students before and after the course. As two groups, the marks overlap almost completely: the students differ far more from each other than the course changed them. The differences, one for every student, lie clearly to the right of 0.

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

The resting pulse of five people is measured before and after a month of training. Before: 78, 82, 75, 88, 80. After (the same people in the same order): 77, 78, 73, 83, 77. Did the training lower the pulse? Use the differences dd = before − after. Find dˉ\bar{d}, sd2s_d^2, tt and the degrees of freedom, and decide at α=0.05\alpha = 0.05.

Show a hint
The differences are 1, 4, 2, 5 and 3. It is a one-sample t-test on them, right-tailed, because lower means a positive dd.
Show the solution

Answer: dˉ=3\bar{d} = 3, sd2=2.5s_d^2 = 2.5, t≈4.243t \approx 4.243, df=4df = 4; reject H0H_0.

The five differences, their deviations from the mean 3 and the squares
dd14253Sum
d−dˉd - \bar{d}−2-21−1-1200
(d−dˉ)2(d - \bar{d})^24114010
H0 ⁣:μd=0H1 ⁣:μd>0dˉ=155=3sd2=104=2.5sd≈1.5811SE=1.58115≈0.7071t=30.7071≈4.243df=4column 0.05, row 4: 2.1324.243>2.132\begin{aligned} & H_0\colon \mu_d = 0 \qquad H_1\colon \mu_d > 0 \\[4pt] & \bar{d} = \frac{15}{5} = 3 \\[2pt] & s_d^2 = \frac{10}{4} = 2.5 \qquad s_d \approx 1.5811 \\[2pt] & \SE = \frac{1.5811}{\sqrt{5}} \approx 0.7071 \\[2pt] & t = \frac{3}{0.7071} \approx 4.243 \qquad df = 4 \\[4pt] & \text{column 0.05, row 4: } 2.132 \\ & 4.243 > 2.132 \end{aligned}

Reject H0H_0: 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 p^\hat{p} is approximately normal with the standard error p×(1−p)n\sqrt{\dfrac{p \times (1 - p)}{n}}. So proportions are tested with a z-test.

9.1One proportion

z=p^−p0p0×(1−p0)nz = \frac{\hat{p} - p_0}{\sqrt{\dfrac{p_0 \times (1 - p_0)}{n}}}

The standard error uses p0p_0, the value claimed by H0H_0, because the test is calculated under the assumption that H0H_0 is true.

Condition: n×p0≥5n \times p_0 \ge 5 and n×(1−p0)≥5n \times (1 - p_0) \ge 5 (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 α=0.05\alpha = 0.05.

1. HypothesesH0 ⁣:p=0.10H1 ⁣:p>0.10α=0.052. Conditionn×p0=100,n×(1−p0)=900the z-test may be used3. Statisticp^=1221000=0.122SE=0.10×0.901000=0.00009SE≈0.00949z=0.122−0.100.00949≈2.324. p-valuep=1−Φ(2.32)p=1−0.9898=0.01025. Decision0.0102≤0.05 ⁣: reject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon p = 0.10 \qquad H_1\colon p > 0.10 \\ & \quad \alpha = 0.05 \\[6pt] & \text{2. Condition} \\ & \quad n \times p_0 = 100, \quad n \times (1 - p_0) = 900 \\ & \quad \text{the z-test may be used} \\[6pt] & \text{3. Statistic} \\ & \quad \hat{p} = \frac{122}{1000} = 0.122 \\[2pt] & \quad \SE = \sqrt{\frac{0.10 \times 0.90}{1000}} = \sqrt{0.00009} \\ & \quad \phantom{\SE} \approx 0.00949 \\[2pt] & \quad z = \frac{0.122 - 0.10}{0.00949} \approx 2.32 \\[6pt] & \text{4. p-value} \\ & \quad p = 1 - \Phi(2.32) \\ & \quad \phantom{p} = 1 - 0.9898 = 0.0102 \\[6pt] & \text{5. Decision} \\ & \quad 0.0102 \le 0.05\colon\ \text{reject } H_0 \end{aligned}

There is significant evidence that the new button raises the share of buyers. At the stricter level α=0.01\alpha = 0.01 the result would just miss significance (0.0102>0.010.0102 > 0.01).

9.2Two proportions: the A/B test

Two independent groups, with x1x_1 successes among n1n_1 and x2x_2 successes among n2n_2. The null hypothesis says that both groups have the same true proportion: H0 ⁣:p1=p2H_0\colon p_1 = p_2. Under H0H_0 the two samples are estimates of one and the same proportion, so they are combined into the pooled proportion:

p^=x1+x2n1+n2z=p^1−p^2p^×(1−p^)×(1n1+1n2)\begin{aligned} \hat{p} &= \frac{x_1 + x_2}{n_1 + n_2} \\[8pt] z &= \frac{\hat{p}_1 - \hat{p}_2}{\sqrt{\hat{p} \times (1 - \hat{p}) \times \left(\dfrac{1}{n_1} + \dfrac{1}{n_2}\right)}} \end{aligned}

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 α=0.05\alpha = 0.05.

1. HypothesesH0 ⁣:p1=p2H1 ⁣:p1≠p2α=0.053. Statisticp^1=2002000=0.100p^2=2502000=0.125pooled: p^=4504000=0.1125SE=0.1125×0.8875×22000SE=0.00009984≈0.00999z=0.125−0.1000.00999≈2.504. p-valuep=2×(1−Φ(2.50))p=2×(1−0.9938)=0.01245. Decision0.0124≤0.05 ⁣: reject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon p_1 = p_2 \qquad H_1\colon p_1 \ne p_2 \\ & \quad \alpha = 0.05 \\[6pt] & \text{3. Statistic} \\ & \quad \hat{p}_1 = \frac{200}{2000} = 0.100 \qquad \hat{p}_2 = \frac{250}{2000} = 0.125 \\[2pt] & \quad \text{pooled: } \hat{p} = \frac{450}{4000} = 0.1125 \\[2pt] & \quad \SE = \sqrt{0.1125 \times 0.8875 \times \frac{2}{2000}} \\ & \quad \phantom{\SE} = \sqrt{0.00009984} \approx 0.00999 \\[2pt] & \quad z = \frac{0.125 - 0.100}{0.00999} \approx 2.50 \\[6pt] & \text{4. p-value} \\ & \quad p = 2 \times (1 - \Phi(2.50)) \\ & \quad \phantom{p} = 2 \times (1 - 0.9938) = 0.0124 \\[6pt] & \text{5. Decision} \\ & \quad 0.0124 \le 0.05\colon\ \text{reject } H_0 \end{aligned}

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.

−3−2−10123

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

A shop's newsletter used to be opened by 15 percent of its readers. After a new subject line, 72 of 400 readers open it. Has the share of readers who open it risen? Find p^\hat{p}, zz and the p-value, and decide at α=0.05\alpha = 0.05.

Show a hint
The standard error uses p0=0.15p_0 = 0.15: 0.15×0.85400\sqrt{\dfrac{0.15 \times 0.85}{400}}. The test is right-tailed.
Show the solution

Answer: p^=0.18\hat{p} = 0.18, z≈1.68z \approx 1.68, p=0.0465p = 0.0465; reject H0H_0.

H0 ⁣:p=0.15H1 ⁣:p>0.15p^=72400=0.18SE=0.15×0.85400=0.00031875SE≈0.01785z=0.18−0.150.01785≈1.68p=1−Φ(1.68)=1−0.9535=0.0465\begin{aligned} & H_0\colon p = 0.15 \qquad H_1\colon p > 0.15 \\[4pt] & \hat{p} = \frac{72}{400} = 0.18 \\[2pt] & \SE = \sqrt{\frac{0.15 \times 0.85}{400}} = \sqrt{0.00031875} \\ & \phantom{\SE} \approx 0.01785 \\[2pt] & z = \frac{0.18 - 0.15}{0.01785} \approx 1.68 \\[4pt] & p = 1 - \Phi(1.68) = 1 - 0.9535 = 0.0465 \end{aligned}

0.0465≤0.050.0465 \le 0.05: reject H0H_0. There is significant evidence that more readers open the newsletter now, but only just: with a two-tailed test the p-value would be 0.09300.0930 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 (OO) with the counts that would be expected (EE) if H0H_0 were true:

χ2=∑(O−E)2E\chi^2 = \sum \frac{(O - E)^2}{E}

with one term for every category or cell.

  • If the data fits H0H_0, observed and expected counts are close, and χ2\chi^2 is small.
  • If the data contradicts H0H_0, some differences are large, and χ2\chi^2 is large.
  • The differences are squared, so χ2\chi^2 is never negative, and only large values speak against H0H_0. A chi-square test is always right-tailed.
  • Dividing by EE 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

05101520
  • df = 2
  • df = 4
  • df = 8

Three chi-square curves

04.5011.07020

df = 5

the filled tail is 5 percent of the area

Left: the shape of the chi-square distribution depends on the degrees of freedom; its mean is equal to df. Right: with 5 degrees of freedom, the null hypothesis is rejected at the 5 percent level when the statistic is above 11.070. The value 4.50 of the die lies in the ordinary part.

If H0H_0 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 dfdf: with df=5df = 5, values around 5 are ordinary.

The table gives critical values: the value that cuts off the area α\alpha in the right tail.

Look up df =and the area

χ² = 11.070 cuts off the area 0.05 in the right tail. At the 5 percent level, H₀ is rejected when χ² is above it.

The chi-square distribution: the value that cuts off a given area in the right tail, for every number of degrees of freedom
df0.100.050.0250.010.005
12.7063.8415.0246.6357.879
24.6055.9917.3789.21010.597
36.2517.8159.34811.34512.838
47.7799.48811.14313.27714.860
59.23611.07012.83315.08616.750
610.64512.59214.44916.81218.548
712.01714.06716.01318.47520.278
813.36215.50717.53520.09021.955
914.68416.91919.02321.66623.589
1015.98718.30720.48323.20925.188
1218.54921.02623.33726.21728.300
1522.30724.99627.48830.57832.801
2028.41231.41034.17037.56639.997
2534.38237.65240.64644.31446.928
3040.25643.77346.97950.89253.672
The table of the chi-square distribution: the value that cuts off the area in the column heading in the right tail. The row is the number of degrees of freedom.

Reject H0H_0 if χ2\chi^2 is above the critical value. For the p-value, find the two values of the row between which your χ2\chi^2 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 α=0.05\alpha = 0.05.

The 120 rolls of the die: observed and expected counts
FaceObserved OOExpected EEO−EO - E(O−E)2E\frac{(O - E)^2}{E}
11520−5-51.25
2222020.20
31820−2-20.20
4252051.25
51620−4-40.80
6242040.80
Total12012004.50
01020151222183254165246
The 120 rolls of the die. The bars are the observed counts; the dashed line is the count that a fair die leads us to expect, 20 for every face. The chi-square statistic adds up how far the bars are from the line, each difference squared and divided by 20.
1. HypothesesH0 ⁣:every face has the probability 16H1 ⁣:at least one face has another probability3. Statisticexpected: 120×16=20 for every faceχ2=1.25+0.20+0.20χ2=+1.25+0.80+0.80=4.50df=6−1=54. Critical valueat 5 percent, row 5: 11.070χ2=4.50<11.0705. Decisionfail to reject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon \text{every face has the probability } \tfrac{1}{6} \\ & \quad H_1\colon \text{at least one face has another probability} \\[6pt] & \text{3. Statistic} \\ & \quad \text{expected: } 120 \times \tfrac{1}{6} = 20 \text{ for every face} \\[2pt] & \quad \chi^2 = 1.25 + 0.20 + 0.20 \\ & \quad \phantom{\chi^2 =} + 1.25 + 0.80 + 0.80 = 4.50 \\[2pt] & \quad df = 6 - 1 = 5 \\[6pt] & \text{4. Critical value} \\ & \quad \text{at 5 percent, row 5: } 11.070 \\ & \quad \chi^2 = 4.50 < 11.070 \\[6pt] & \text{5. Decision} \\ & \quad \text{fail to reject } H_0 \end{aligned}

The counts are consistent with a fair die. The value 4.50 is even below the first column (9.2369.236), so p>0.10p > 0.10; software gives p≈0.48p \approx 0.48. Differences like these are just what chance produces.

For a goodness-of-fit test:

E=n×(the probability that H0E=n×(gives to the category)df=number of categories−1\begin{aligned} & E = n \times (\text{the probability that } H_0 \\ & \phantom{E = n \times (} \text{gives to the category}) \\[6pt] & df = \text{number of categories} - 1 \end{aligned}

The expected counts need not be equal: if H0H_0 says "50 percent, 30 percent, 20 percent", then EE is 50, 30 and 20 percent of nn (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.

The 200 customers by their plan: observed counts
ObservedStayedLeftTotal
Basic plan7050120
Premium plan602080
Total13070200

Of the basic customers 50120≈42\dfrac{50}{120} \approx 42 percent left, of the premium customers only 2080=25\dfrac{20}{80} = 25 percent. Is leaving related to the plan, or could this difference be chance? Test at α=0.05\alpha = 0.05.

H0H_0 says that the two variables are independent. Topic 16: for independent events, P(A and B)=P(A)×P(B)P(A \text{ and } B) = P(A) \times P(B). So if the plan and leaving are independent, the expected count of a cell is

E=n×row totaln×column totaln=row total×column totalgrand total\begin{aligned} E &= n \times \frac{\text{row total}}{n} \times \frac{\text{column total}}{n} \\[4pt] &= \frac{\text{row total} \times \text{column total}}{\text{grand total}} \end{aligned}
basic, stayed:120×130200=78basic, left:120×70200=42premium, stayed:80×130200=52premium, left:80×70200=28\begin{aligned} \text{basic, stayed:}\quad & \frac{120 \times 130}{200} = 78 \\[2pt] \text{basic, left:}\quad & \frac{120 \times 70}{200} = 42 \\[2pt] \text{premium, stayed:}\quad & \frac{80 \times 130}{200} = 52 \\[2pt] \text{premium, left:}\quad & \frac{80 \times 70}{200} = 28 \end{aligned}
The 200 customers by their plan: expected counts if the plan and leaving are independent
ExpectedStayedLeftTotal
Basic plan7842120
Premium plan522880
Total13070200
1. HypothesesH0 ⁣:the plan and leaving are independentH1 ⁣:they are related3. Statisticχ2=(70−78)278+(50−42)242χ2=+(60−52)252+(20−28)228χ2=6478+6442+6452+6428χ2≈0.8205+1.5238χ2=+1.2308+2.2857χ2≈5.861df=(2−1)×(2−1)=14. Critical valueat 5 percent, row 1: 3.841χ2=5.861>3.8415. Decisionreject H0\begin{aligned} & \text{1. Hypotheses} \\ & \quad H_0\colon \text{the plan and leaving are independent} \\ & \quad H_1\colon \text{they are related} \\[6pt] & \text{3. Statistic} \\ & \quad \chi^2 = \frac{(70 - 78)^2}{78} + \frac{(50 - 42)^2}{42} \\ & \quad \phantom{\chi^2 =} + \frac{(60 - 52)^2}{52} + \frac{(20 - 28)^2}{28} \\[2pt] & \quad \phantom{\chi^2} = \frac{64}{78} + \frac{64}{42} + \frac{64}{52} + \frac{64}{28} \\[2pt] & \quad \phantom{\chi^2} \approx 0.8205 + 1.5238 \\ & \quad \phantom{\chi^2 =} + 1.2308 + 2.2857 \\ & \quad \phantom{\chi^2} \approx 5.861 \\[2pt] & \quad df = (2 - 1) \times (2 - 1) = 1 \\[6pt] & \text{4. Critical value} \\ & \quad \text{at 5 percent, row 1: } 3.841 \\ & \quad \chi^2 = 5.861 > 3.841 \\[6pt] & \text{5. Decision} \\ & \quad \text{reject } H_0 \end{aligned}

There is significant evidence that leaving is related to the plan: basic customers leave more often. The statistic lies between 5.0245.024 and 6.6356.635, so the p-value is between 0.01 and 0.025 (software: p≈0.0155p \approx 0.0155).

Customers by their plan and by whether they stayed or left. Move the four counts; the expected counts stand in brackets.

The observed counts with the expected counts in brackets, and the totals
StayedLeftTotal
Basic plana = 70 (78)b = 50 (42)120
Premium planc = 60 (52)d = 20 (28)80
Total13070200

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 rr rows and cc columns:

E=row total×column totalgrand totaldf=(r−1)×(c−1)\begin{aligned} E &= \frac{\text{row total} \times \text{column total}}{\text{grand total}} \\[6pt] df &= (r - 1) \times (c - 1) \end{aligned}

The totals row and column are not counted in rr and cc.

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: χ2=z2\chi^2 = z^2. The critical value 3.8413.841 is 1.96021.960^2.

Try it yourself

Sixty customers of a café choose one of four kinds of ice cream: 24 take vanilla, 15 chocolate, 9 strawberry and 12 lemon. Are the four kinds equally popular? Find the expected count, χ2\chi^2 and the degrees of freedom, and decide at α=0.05\alpha = 0.05.

Show a hint
If the kinds are equally popular, each one is expected 604\dfrac{60}{4} times. dfdf = number of categories − 1.
Show the solution

Answer: E=15E = 15, χ2=8.4\chi^2 = 8.4, df=3df = 3, critical value 7.8157.815; reject H0H_0.

The four kinds of ice cream: observed and expected counts
KindOOEE(O−E)2(O - E)^2
Vanilla241581
Chocolate15150
Strawberry91536
Lemon12159
Total6060126
H0 ⁣:every kind has the probability 14χ2=81+0+36+915=12615=8.4df=4−1=3column 0.05, row 3: 7.8158.4>7.815\begin{aligned} & H_0\colon \text{every kind has the probability } \tfrac{1}{4} \\[4pt] & \chi^2 = \frac{81 + 0 + 36 + 9}{15} = \frac{126}{15} = 8.4 \\[2pt] & df = 4 - 1 = 3 \\[4pt] & \text{column 0.05, row 3: } 7.815 \\ & 8.4 > 7.815 \end{aligned}

Reject H0H_0: 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 7.8157.815 and 9.3489.348, so the p-value is between 0.025 and 0.05.

11Choosing the test

The tests of this lesson: question, data, statistic and distribution
The questionThe dataTestStatisticDistribution
Is a mean equal to a claimed value?one sample, σ\sigma knownz-testxˉ−μ0σ/n\dfrac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}normal
Is a mean equal to a claimed value?one sample, σ\sigma unknownone-sample t-testxˉ−μ0s/n\dfrac{\bar{x} - \mu_0}{s / \sqrt{n}}t, df=n−1df = n - 1
Do two groups have the same mean?two independent samplestwo-sample t-testxˉ1−xˉ2sp×1/n1+1/n2\dfrac{\bar{x}_1 - \bar{x}_2}{s_p \times \sqrt{1/n_1 + 1/n_2}}t, df=n1+n2−2df = n_1 + n_2 - 2
Did a treatment change the mean?pairs (before and after)paired t-testdˉsd/n\dfrac{\bar{d}}{s_d / \sqrt{n}}t, df=n−1df = n - 1
Is a proportion equal to a claimed value?one sample of yes or noz-test for a proportionp^−p0p0×(1−p0)/n\dfrac{\hat{p} - p_0}{\sqrt{p_0 \times (1 - p_0) / n}}normal
Do two groups have the same proportion?two independent samplesz-test for two proportionsp^1−p^2p^×(1−p^)×(1/n1+1/n2)\dfrac{\hat{p}_1 - \hat{p}_2}{\sqrt{\hat{p} \times (1 - \hat{p}) \times (1/n_1 + 1/n_2)}}normal
Do counts fit a claimed distribution?one categorical variablechi-square goodness of fit∑(O−E)2E\sum \dfrac{(O - E)^2}{E}chi-square, dfdf = categories − 1
Are two categorical variables related?a table of countschi-square test of independence∑(O−E)2E\sum \dfrac{(O - E)^2}{E}chi-square, df=(r−1)×(c−1)df = (r - 1) \times (c - 1)

Three questions lead to the right row:

  1. What kind of data? Numbers (means), yes or no (proportions), or categories (counts).
  2. How many groups? One sample against a claim, or two groups.
  3. 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

Which test fits? (a) The weights of 30 bags are compared with the 500 g that the producer claims; the standard deviation comes from the sample. (b) The shares of defective parts in the samples of two factories are compared. (c) The blood sugar of 15 patients is measured before and after a meal. (d) Boys and girls name their favourite sport: football, tennis or chess. Does the choice depend on being a boy or a girl?

Show a hint
Ask the three questions: what kind of data, how many groups, and for two groups of numbers: independent or pairs?
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 ss 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 df=(2−1)×(3−1)=2df = (2 - 1) \times (3 - 1) = 2.

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 n\sqrt{n}, 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 p<0.01p < 0.01 (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.

979899100101102103

n=100 ⁣: z=0.27, p=0.7872n = 100\colon\ z = 0.27,\ p = 0.7872

standard error 1.5

979899100101102103

n=10,000 ⁣: z=2.67, p=0.0076n = 10{,}000\colon\ z = 2.67,\ p = 0.0076

standard error 0.15

The same sample mean, 100.4, when the null hypothesis says 100 and σ = 15. With 100 orders the sample mean varies by 1.5 from sample to sample, and 100.4 is nothing unusual. With 10,000 orders it varies by only 0.15, and 100.4 lies far out in the tail. The difference of 0.4 is the same in both pictures.

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.

d=xˉ1−xˉ2spd = \frac{\bar{x}_1 - \bar{x}_2}{s_p}

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 α=0.05\alpha = 0.05, one test of a true H0H_0 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:

1−0.9520≈0.641 - 0.95^{20} \approx 0.64

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 mm tests are made, each one is tested at the level αm\dfrac{\alpha}{m}. For 20 tests: 0.0520=0.0025\dfrac{0.05}{20} = 0.0025.

The same danger is in an A/B test that is checked every day and stopped as soon as p<0.05p < 0.05: each look is another test. Fix the sample size first.

12.4Other things a test does not tell

  • A test does not prove H0H_0. "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 α=0.05\alpha = 0.05 rejects H0 ⁣:μ=μ0H_0\colon \mu = \mu_0 exactly when μ0\mu_0 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

The users of a video service watched 50 minutes a day on average, with a standard deviation of 10 minutes. After a new start page, 2,500 users watch 50.5 minutes on average. (a) Find zz. (b) Find the two-tailed p-value. (c) Find the effect size: the difference divided by the standard deviation. (d) How would you describe the result?

Show a hint
The standard error is 102500=0.2\dfrac{10}{\sqrt{2500}} = 0.2. The effect size does not use nn.
Show the solution

Answer: z=2.5z = 2.5, p=0.0124p = 0.0124, effect size 0.050.05: significant, but very small.

SE=102500=0.2z=50.5−500.2=2.5p=2×(1−0.9938)=0.0124d=50.5−5010=0.05\begin{aligned} & \SE = \frac{10}{\sqrt{2500}} = 0.2 \\[2pt] & z = \frac{50.5 - 50}{0.2} = 2.5 \\[4pt] & p = 2 \times (1 - 0.9938) = 0.0124 \\[4pt] & d = \frac{50.5 - 50}{10} = 0.05 \end{aligned}

0.0124≤0.050.0124 \le 0.05: 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 χ2\chi^2 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 H0H_0: 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 p<0.05p < 0.05 is the multiple comparisons problem of Part 12.

Try it yourself

(a) A team tests 40 features, each at α=0.05\alpha = 0.05. Suppose that none of them has any real link with the target. How many features are expected to look "significant"? (b) Two models are evaluated on the same 10 folds. Which test compares them? (c) In a table of a million rows, a difference between two groups has p=0.001p = 0.001. What must be looked at before the difference is called important?

Show a hint
Every test of a true H0H_0 gives a false alarm with the probability α\alpha. The two scores of one fold belong together.
Show the solution

Answer: (a) 22; (b) the paired t-test; (c) the size of the effect.

(a) 40×0.05=240 \times 0.05 = 2 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 H0H_0 is true. It is the probability of data this extreme if H0H_0 is true.

Saying "we accept H0H_0" or "we proved H0H_0". A test can only fail to reject it.

Writing hypotheses about the sample. H0 ⁣:xˉ=500H_0\colon \bar{x} = 500 is wrong. Hypotheses are about μ\mu or pp.

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 σ\sigma where σn\dfrac{\sigma}{\sqrt{n}} belongs. The statistic divides by the standard error.

Using z when only ss is known and the sample is small. That needs the t distribution with df=n−1df = n - 1.

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 EE, not by OO.

Wrong degrees of freedom. Goodness of fit: categories minus 1. Independence: (r−1)×(c−1)(r - 1) \times (c - 1), 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.

Common mistakes and what is right
WrongRight
p=0.03p = 0.03: "H0H_0 is true with 3 percent probability""if H0H_0 is true, data this extreme occurs in 3 percent of samples"
p=0.20p = 0.20: "the means are equal""there is not enough evidence of a difference"
H0 ⁣:xˉ=70H_0\colon \bar{x} = 70H0 ⁣:μ=70H_0\colon \mu = 70
two-tailed test, z=2.0z = 2.0: p=0.0228p = 0.0228p=2×0.0228=0.0456p = 2 \times 0.0228 = 0.0456
z=xˉ−μ0σz = \dfrac{\bar{x} - \mu_0}{\sigma}z=xˉ−μ0σ/nz = \dfrac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}
two-tailed at 5 percent with df=10df = 10: critical value 1.812column 0.025: 2.228
before and after on the same people: two-sample t-testpaired t-test on the differences
χ2=∑(O−E)2O\chi^2 = \sum \dfrac{(O - E)^2}{O}χ2=∑(O−E)2E\chi^2 = \sum \dfrac{(O - E)^2}{E}
table of 3 rows and 4 columns: df=12df = 12df=2×3=6df = 2 \times 3 = 6
"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 α=0.05\alpha = 0.05.

Level 1: Hypotheses, z-tests and p-values

Problem 1: Writing the hypotheses

Write H0H_0 and H1H_1, 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. H0H_0 contains the equals sign and the old or claimed value. H1H_1 is what somebody wants to show; its direction gives the tails (Parts 2 and 3).

The hypotheses of the three situations
H0H_0H1H_1Test
(a)μ=400\mu = 400μ≠400\mu \ne 400two-tailed
(b)μ=20\mu = 20μ>20\mu > 20right-tailed
(c)p=0.08p = 0.08p>0.08p > 0.08right-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 pp, not μ\mu; the passengers look for evidence of "more", which is H1H_1.

Problem 2: From a p-value to a decision

A test gives the p-value 0.03. What is the decision at α=0.05\alpha = 0.05, and at α=0.01\alpha = 0.01?

Idea. Reject H0H_0 when the p-value is at most α\alpha (Part 4).

α=0.05 ⁣:0.03≤0.05, reject H0α=0.01 ⁣:0.03>0.01, fail to reject H0\begin{aligned} \alpha = 0.05\colon\quad & 0.03 \le 0.05, \text{ reject } H_0 \\ \alpha = 0.01\colon\quad & 0.03 > 0.01, \text{ fail to reject } H_0 \end{aligned}

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. σ\sigma is known: z-test for a mean (Part 5).

H0 ⁣:μ=100H1 ⁣:μ≠100SE=1536=2.5z=105.5−1002.5=2.2p=2×(1−Φ(2.2))p=2×(1−0.9861)=0.0278\begin{aligned} & H_0\colon \mu = 100 \qquad H_1\colon \mu \ne 100 \\[4pt] & \SE = \frac{15}{\sqrt{36}} = 2.5 \\[2pt] & z = \frac{105.5 - 100}{2.5} = 2.2 \\[4pt] & p = 2 \times (1 - \Phi(2.2)) \\ & \phantom{p} = 2 \times (1 - 0.9861) = 0.0278 \end{aligned}

0.0278≤0.050.0278 \le 0.05: reject H0H_0. 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).

H0 ⁣:μ=40H1 ⁣:μ>40SE=349≈0.4286z=40.6−400.4286=1.40p=1−Φ(1.4)p=1−0.9192=0.0808\begin{aligned} & H_0\colon \mu = 40 \qquad H_1\colon \mu > 40 \\[4pt] & \SE = \frac{3}{\sqrt{49}} \approx 0.4286 \\[2pt] & z = \frac{40.6 - 40}{0.4286} = 1.40 \\[4pt] & p = 1 - \Phi(1.4) \\ & \phantom{p} = 1 - 0.9192 = 0.0808 \end{aligned}

0.0808>0.050.0808 > 0.05: fail to reject H0H_0. 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).

H0 ⁣:μ=12H1 ⁣:μ<12SE=464=0.5z=11.1−120.5=−1.8p=Φ(−1.8)p=1−0.9641=0.0359\begin{aligned} & H_0\colon \mu = 12 \qquad H_1\colon \mu < 12 \\[4pt] & \SE = \frac{4}{\sqrt{64}} = 0.5 \\[2pt] & z = \frac{11.1 - 12}{0.5} = -1.8 \\[4pt] & p = \Phi(-1.8) \\ & \phantom{p} = 1 - 0.9641 = 0.0359 \end{aligned}

0.0359≤0.050.0359 \le 0.05: reject H0H_0. There is significant evidence that the waiting time has become shorter.

Why: in a left-tailed test only a negative statistic speaks against H0H_0. 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 z=2.4z = 2.4. Decide at α=0.05\alpha = 0.05 and at α=0.01\alpha = 0.01 with critical values.

Idea. Compare ∣z∣\len{z} with the critical value of the level (Part 5).

α=0.05 ⁣: critical value 1.9602.4>1.960, reject H0α=0.01 ⁣: critical value 2.5762.4<2.576, fail to reject H0\begin{aligned} & \alpha = 0.05\colon \text{ critical value } 1.960 \\ & \quad 2.4 > 1.960, \text{ reject } H_0 \\[4pt] & \alpha = 0.01\colon \text{ critical value } 2.576 \\ & \quad 2.4 < 2.576, \text{ fail to reject } H_0 \end{aligned}

Why: the p-value tells the same story: 2×(1−Φ(2.4))=2×(1−0.9918)=0.01642 \times (1 - \Phi(2.4)) = 2 \times (1 - 0.9918) = 0.0164, which lies between 0.01 and 0.05.

Problem 7: One tail or two

A z-test gives z=1.75z = 1.75. Find the p-value for a right-tailed test and for a two-tailed test, and decide at α=0.05\alpha = 0.05 in each case.

Idea. The two-tailed p-value is twice the one-tailed one (Part 4).

right-tailed:p=1−Φ(1.75)=1−0.9599=0.0401reject H0two-tailed:p=2×0.0401=0.0802fail to reject H0\begin{aligned} & \text{right-tailed:} \\ & \quad p = 1 - \Phi(1.75) = 1 - 0.9599 = 0.0401 \\ & \quad \text{reject } H_0 \\[4pt] & \text{two-tailed:} \\ & \quad p = 2 \times 0.0401 = 0.0802 \\ & \quad \text{fail to reject } H_0 \end{aligned}

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 H0H_0: "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 H0H_0. Type II: not rejecting a false H0H_0 (Part 6).

  • (a) H0H_0 was true (not spam) and was rejected: a Type I error (a false alarm).
  • (b) H0H_0 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 α\alpha.

Why: which error is "I" and which is "II" depends only on how H0H_0 is written. Always write H0H_0 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 p0p_0 in the standard error (Part 9).

H0 ⁣:p=0.5H1 ⁣:p≠0.5p^=216400=0.54SE=0.5×0.5400=0.000625=0.025z=0.54−0.50.025=1.6p=2×(1−Φ(1.6))p=2×(1−0.9452)=0.1096\begin{aligned} & H_0\colon p = 0.5 \qquad H_1\colon p \ne 0.5 \\[4pt] & \hat{p} = \frac{216}{400} = 0.54 \\[2pt] & \SE = \sqrt{\frac{0.5 \times 0.5}{400}} = \sqrt{0.000625} = 0.025 \\[2pt] & z = \frac{0.54 - 0.5}{0.025} = 1.6 \\[4pt] & p = 2 \times (1 - \Phi(1.6)) \\ & \phantom{p} = 2 \times (1 - 0.9452) = 0.1096 \end{aligned}

0.1096>0.050.1096 > 0.05: fail to reject H0H_0. 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 df=15df = 15, (b) a right-tailed test at 1 percent with df=24df = 24, (c) a left-tailed test at 5 percent with df=9df = 9.

Idea. One tail: the column α\alpha. Two tails: the column α2\dfrac{\alpha}{2} (Part 7).

Where the three critical values stand in the t table
ColumnRowValueReject if
(a)0.025152.131∣t∣>2.131\len{t} > 2.131
(b)0.01242.492t>2.492t > 2.492
(c)0.0591.833t<−1.833t < -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. σ\sigma is unknown: one-sample t-test with df=n−1df = n - 1 (Part 7).

H0 ⁣:μ=50H1 ⁣:μ≠50SE=416=1t=52.5−501=2.5df=15column 0.025, row 15: 2.1312.5>2.131\begin{aligned} & H_0\colon \mu = 50 \qquad H_1\colon \mu \ne 50 \\[4pt] & \SE = \frac{4}{\sqrt{16}} = 1 \\[2pt] & t = \frac{52.5 - 50}{1} = 2.5 \qquad df = 15 \\[4pt] & \text{column 0.025, row 15: } 2.131 \\ & 2.5 > 2.131 \end{aligned}

Reject H0H_0. There is significant evidence that the mean length is not 50 cm.

Why: the statistic lies between 2.1312.131 and 2.6022.602 in row 15, so the two-tailed p-value is between 0.02 and 0.05. Software gives p≈0.025p \approx 0.025.

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 xˉ\bar{x} and ss (Topic 18), then the t-test (Part 7).

The six running times, their deviations from the mean 18.5 and the squares
xx182117192016
x−xˉx - \bar{x}−0.5-0.52.5−1.5-1.50.51.5−2.5-2.5
(x−xˉ)2(x - \bar{x})^20.256.252.250.252.256.25
H0 ⁣:μ=20H1 ⁣:μ<20xˉ=1116=18.5sum of the squares: 17.5s2=17.55=3.5s≈1.8708SE=1.87086≈0.7638t=18.5−200.7638≈−1.964df=5left-tailed, column 0.05, row 5: −2.015\begin{aligned} & H_0\colon \mu = 20 \qquad H_1\colon \mu < 20 \\[4pt] & \bar{x} = \frac{111}{6} = 18.5 \\[2pt] & \text{sum of the squares: } 17.5 \\[2pt] & s^2 = \frac{17.5}{5} = 3.5 \qquad s \approx 1.8708 \\[2pt] & \SE = \frac{1.8708}{\sqrt{6}} \approx 0.7638 \\[2pt] & t = \frac{18.5 - 20}{0.7638} \approx -1.964 \qquad df = 5 \\[4pt] & \text{left-tailed, column 0.05, row 5: } -2.015 \end{aligned}

−1.964-1.964 is not below −2.015-2.015: fail to reject H0H_0. There is not enough evidence that the phones run less than 20 hours.

Why: the result is close to the border (software: p≈0.053p \approx 0.053). 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).

H0 ⁣:μ1=μ2H1 ⁣:μ1≠μ2sp2=1.22+1.022=1.44+1.002=1.22sp≈1.1045SE=1.1045×18+18SE=1.1045×0.5≈0.5523t=50.2−48.90.5523≈2.354df=8+8−2=14column 0.025, row 14: 2.1452.354>2.145\begin{aligned} & H_0\colon \mu_1 = \mu_2 \qquad H_1\colon \mu_1 \ne \mu_2 \\[4pt] & s_p^2 = \frac{1.2^2 + 1.0^2}{2} = \frac{1.44 + 1.00}{2} = 1.22 \\[2pt] & s_p \approx 1.1045 \\[2pt] & \SE = 1.1045 \times \sqrt{\frac{1}{8} + \frac{1}{8}} \\ & \phantom{\SE} = 1.1045 \times 0.5 \approx 0.5523 \\[2pt] & t = \frac{50.2 - 48.9}{0.5523} \approx 2.354 \\[2pt] & df = 8 + 8 - 2 = 14 \\[4pt] & \text{column 0.025, row 14: } 2.145 \\ & 2.354 > 2.145 \end{aligned}

Reject H0H_0. 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: p≈0.034p \approx 0.034).

Problem 14: Two independent samples of different sizes

A new feed is given to 10 chickens, which gain 24 g a day on average (s=3s = 3). Fifteen chickens with the old feed gain 21.5 g a day on average (s=4s = 4). Do the two feeds differ?

Idea. The pooled variance weights each variance with n−1n - 1 (Part 8).

H0 ⁣:μ1=μ2H1 ⁣:μ1≠μ2sp2=9×32+14×4210+15−2sp2=81+22423≈13.261sp≈3.642SE=3.642×110+115SE=3.642×0.4082≈1.4867t=24−21.51.4867≈1.682df=23column 0.025, row 23: 2.0691.682<2.069\begin{aligned} & H_0\colon \mu_1 = \mu_2 \qquad H_1\colon \mu_1 \ne \mu_2 \\[4pt] & s_p^2 = \frac{9 \times 3^2 + 14 \times 4^2}{10 + 15 - 2} \\[2pt] & \phantom{s_p^2} = \frac{81 + 224}{23} \approx 13.261 \\[2pt] & s_p \approx 3.642 \\[2pt] & \SE = 3.642 \times \sqrt{\frac{1}{10} + \frac{1}{15}} \\ & \phantom{\SE} = 3.642 \times 0.4082 \approx 1.4867 \\[2pt] & t = \frac{24 - 21.5}{1.4867} \approx 1.682 \qquad df = 23 \\[4pt] & \text{column 0.025, row 23: } 2.069 \\ & 1.682 < 2.069 \end{aligned}

Fail to reject H0H_0. 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: p≈0.106p \approx 0.106).

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?

The times of the six runners in seconds, the differences d = before − after, their deviations from the mean 2 and the squares
Runner123456Sum
Before625865606359
After605761606057
dd21403212
d−dˉd - \bar{d}0−1-12−2-2100
(d−dˉ)2(d - \bar{d})^201441010

Idea. The same runners twice: a paired test on the differences (Part 8). Faster means a positive dd.

H0 ⁣:μd=0H1 ⁣:μd>0dˉ=126=2sd2=105=2sd=2≈1.414SE=26=26≈0.5774t=20.5774≈3.464df=5right-tailed, column 0.05, row 5: 2.0153.464>2.015\begin{aligned} & H_0\colon \mu_d = 0 \qquad H_1\colon \mu_d > 0 \\[4pt] & \bar{d} = \frac{12}{6} = 2 \\[2pt] & s_d^2 = \frac{10}{5} = 2 \qquad s_d = \sqrt{2} \approx 1.414 \\[2pt] & \SE = \frac{\sqrt{2}}{\sqrt{6}} = \sqrt{\frac{2}{6}} \approx 0.5774 \\[2pt] & t = \frac{2}{0.5774} \approx 3.464 \qquad df = 5 \\[4pt] & \text{right-tailed, column 0.05, row 5: } 2.015 \\ & 3.464 > 2.015 \end{aligned}

Reject H0H_0. There is significant evidence that the runners became faster, by 2 seconds on average.

Why: the statistic lies between 3.3653.365 and 4.0324.032, so the p-value is between 0.005 and 0.01. Treated as two independent groups, the same data would give t≈1.55t \approx 1.55 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).

The test for each of the four situations
TestReason
(a)paired t-testthe same patients twice
(b)two-sample t-testtwo separate groups (equal sizes do not make pairs)
(c)paired t-testthe same folds for both models
(d)two-sample t-testtwo 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 α=0.05\alpha = 0.05 checks H0 ⁣:μ=50H_0\colon \mu = 50 with σ=8\sigma = 8 and n=16n = 16. If the true mean is 54, what is the power of the test?

Idea. Find the border for xˉ\bar{x}, then the probability of crossing it when the true mean is 54 (Part 6).

SE=816=2H0 is rejected whenxˉ>50+1.645×2=53.29≈53.3If the true mean is 54:z=53.3−542=−0.35β=Φ(−0.35)=0.3632power=1−0.3632=0.6368\begin{aligned} & \SE = \frac{8}{\sqrt{16}} = 2 \\[4pt] & H_0 \text{ is rejected when} \\ & \quad \bar{x} > 50 + 1.645 \times 2 = 53.29 \approx 53.3 \\[4pt] & \text{If the true mean is 54:} \\ & \quad z = \frac{53.3 - 54}{2} = -0.35 \\[2pt] & \quad \beta = \Phi(-0.35) = 0.3632 \\[4pt] & \text{power} = 1 - 0.3632 = 0.6368 \end{aligned}

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 df=12df = 12 gives t=2.9t = 2.9. 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).

row 12:2.681 (column 0.01)3.055 (column 0.005)2.681<2.9<3.055one tail: between 0.005 and 0.01two tails: between 0.01 and 0.02\begin{aligned} & \text{row 12:} \\ & \quad 2.681 \text{ (column 0.01)} \\ & \quad 3.055 \text{ (column 0.005)} \\[4pt] & 2.681 < 2.9 < 3.055 \\[4pt] & \text{one tail: between 0.005 and 0.01} \\ & \text{two tails: between 0.01 and 0.02} \end{aligned}

Why: a larger statistic belongs to a smaller tail area. Software gives p≈0.0133p \approx 0.0133, 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 α=0.01\alpha = 0.01.

Idea. Two proportions from independent groups: z-test with the pooled proportion (Part 9).

H0 ⁣:p1=p2H1 ⁣:p1≠p2α=0.01p^1=60500=0.12p^2=90500=0.18pooled: p^=1501000=0.15SE=0.15×0.85×2500SE=0.00051≈0.02258z=0.18−0.120.02258≈2.66p=2×(1−Φ(2.66))p=2×(1−0.9961)=0.0078\begin{aligned} & H_0\colon p_1 = p_2 \qquad H_1\colon p_1 \ne p_2 \qquad \alpha = 0.01 \\[4pt] & \hat{p}_1 = \frac{60}{500} = 0.12 \qquad \hat{p}_2 = \frac{90}{500} = 0.18 \\[2pt] & \text{pooled: } \hat{p} = \frac{150}{1000} = 0.15 \\[2pt] & \SE = \sqrt{0.15 \times 0.85 \times \frac{2}{500}} \\ & \phantom{\SE} = \sqrt{0.00051} \approx 0.02258 \\[2pt] & z = \frac{0.18 - 0.12}{0.02258} \approx 2.66 \\[4pt] & p = 2 \times (1 - \Phi(2.66)) \\ & \phantom{p} = 2 \times (1 - 0.9961) = 0.0078 \end{aligned}

0.0078≤0.010.0078 \le 0.01: reject H0H_0. Even at the strict level there is significant evidence that version B sells better.

Why: the pooled proportion is used because H0H_0 says that there is only one true proportion; under H0H_0 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).

H0 ⁣:each design has the probability 13H1 ⁣:the designs are not equally popularexpected: 90×13=30 for each designχ2=(38−30)230+(30−30)230+(22−30)230χ2=6430+0+6430≈4.267df=3−1=2column 0.05, row 2: 5.9914.267<5.991\begin{aligned} & H_0\colon \text{each design has the probability } \tfrac{1}{3} \\ & H_1\colon \text{the designs are not equally popular} \\[4pt] & \text{expected: } 90 \times \tfrac{1}{3} = 30 \text{ for each design} \\[4pt] & \chi^2 = \frac{(38 - 30)^2}{30} + \frac{(30 - 30)^2}{30} + \frac{(22 - 30)^2}{30} \\[2pt] & \phantom{\chi^2} = \frac{64}{30} + 0 + \frac{64}{30} \approx 4.267 \\[2pt] & df = 3 - 1 = 2 \\[4pt] & \text{column 0.05, row 2: } 5.991 \\ & 4.267 < 5.991 \end{aligned}

Fail to reject H0H_0. 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 4.6054.605, so p>0.10p > 0.10 (software: p≈0.118p \approx 0.118).

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 nn (Part 10).

The 200 sales: observed and expected counts
ProductABCTotal
Observed857045200
Expected1006040200
(O−E)2E\frac{(O - E)^2}{E}2.2501.6670.6254.542
H0 ⁣:the shares are 0.5, 0.3 and 0.2H1 ⁣:at least one share is differentχ2=225100+10060+2540≈4.542df=2column 0.05, row 2: 5.9914.542<5.991\begin{aligned} & H_0\colon \text{the shares are 0.5, 0.3 and 0.2} \\ & H_1\colon \text{at least one share is different} \\[4pt] & \chi^2 = \frac{225}{100} + \frac{100}{60} + \frac{25}{40} \approx 4.542 \\[2pt] & df = 2 \\[4pt] & \text{column 0.05, row 2: } 5.991 \\ & 4.542 < 5.991 \end{aligned}

Fail to reject H0H_0. 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 E=row total×column totalgrand totalE = \dfrac{\text{row total} \times \text{column total}}{\text{grand total}} (Part 10).

The 200 customers: observed counts
ObservedBoughtDid not buyTotal
Saw the advertisement4555100
Did not see it3070100
Total75125200
expected (the same in both rows):100×75200=37.5100×125200=62.5χ2=(45−37.5)237.5+(55−62.5)262.5χ2=+(30−37.5)237.5+(70−62.5)262.5χ2=1.5+0.9+1.5+0.9=4.8df=(2−1)×(2−1)=1column 0.05, row 1: 3.8414.8>3.841\begin{aligned} & \text{expected (the same in both rows):} \\ & \quad \frac{100 \times 75}{200} = 37.5 \qquad \frac{100 \times 125}{200} = 62.5 \\[4pt] & \chi^2 = \frac{(45 - 37.5)^2}{37.5} + \frac{(55 - 62.5)^2}{62.5} \\ & \phantom{\chi^2 =} + \frac{(30 - 37.5)^2}{37.5} + \frac{(70 - 62.5)^2}{62.5} \\[2pt] & \phantom{\chi^2} = 1.5 + 0.9 + 1.5 + 0.9 = 4.8 \\[2pt] & df = (2 - 1) \times (2 - 1) = 1 \\[4pt] & \text{column 0.05, row 1: } 3.841 \\ & 4.8 > 3.841 \end{aligned}

Reject H0H_0. There is significant evidence that buying is related to seeing the advertisement.

Why: the statistic lies between 3.8413.841 and 5.0245.024, 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?

The 200 users: observed counts
ObservedPhoneTabletLaptopTotal
Satisfied503040120
Not satisfied30302080
Total806060200

Idea. The same test with more columns; df=(r−1)×(c−1)df = (r - 1) \times (c - 1) (Part 10).

expected, satisfied:120×80200=48120×60200=3636expected, not satisfied:80×80200=3280×60200=2424\begin{aligned} & \text{expected, satisfied:} \\ & \quad \frac{120 \times 80}{200} = 48 \qquad \frac{120 \times 60}{200} = 36 \qquad 36 \\[2pt] & \text{expected, not satisfied:} \\ & \quad \frac{80 \times 80}{200} = 32 \qquad \frac{80 \times 60}{200} = 24 \qquad 24 \end{aligned}
χ2=(50−48)248+(30−36)236+(40−36)236+(30−32)232+(30−24)224+(20−24)224≈0.083+1.000+0.444+0.125+1.500+0.667=3.819df=(2−1)×(3−1)=2\begin{aligned} \chi^2 &= \frac{(50 - 48)^2}{48} + \frac{(30 - 36)^2}{36} + \frac{(40 - 36)^2}{36} \\ &\quad + \frac{(30 - 32)^2}{32} + \frac{(30 - 24)^2}{24} + \frac{(20 - 24)^2}{24} \\[2pt] &\approx 0.083 + 1.000 + 0.444 \\ &\quad + 0.125 + 1.500 + 0.667 = 3.819 \\[4pt] df &= (2 - 1) \times (3 - 1) = 2 \end{aligned}

The critical value (column 0.05, row 2) is 5.9915.991, and 3.819<5.9913.819 < 5.991: fail to reject H0H_0. 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: p≈0.148p \approx 0.148).

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).

H0 ⁣:μ=100H1 ⁣:μ≠100SE=1510,000=0.15z=100.4−1000.15≈2.67p=2×(1−Φ(2.67))p=2×(1−0.9962)=0.0076effect size: d=0.415≈0.027\begin{aligned} & H_0\colon \mu = 100 \qquad H_1\colon \mu \ne 100 \\[4pt] & \SE = \frac{15}{\sqrt{10{,}000}} = 0.15 \\[2pt] & z = \frac{100.4 - 100}{0.15} \approx 2.67 \\[4pt] & p = 2 \times (1 - \Phi(2.67)) \\ & \phantom{p} = 2 \times (1 - 0.9962) = 0.0076 \\[4pt] & \text{effect size: } d = \frac{0.4}{15} \approx 0.027 \end{aligned}

0.0076≤0.050.0076 \le 0.05: reject H0H_0. 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 α=0.05\alpha = 0.05. 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 H0H_0 gives a false alarm with the probability α\alpha (Part 12).

(a)20×0.05=1 false alarm is expected(b)P(at least one)=1−0.9520(b)≈1−0.3585=0.6415(c)0.0520=0.0025\begin{aligned} & \text{(a)}\quad 20 \times 0.05 = 1 \text{ false alarm is expected} \\[4pt] & \text{(b)}\quad P(\text{at least one}) = 1 - 0.95^{20} \\ & \phantom{\text{(b)}\quad} \approx 1 - 0.3585 = 0.6415 \\[4pt] & \text{(c)}\quad \frac{0.05}{20} = 0.0025 \end{aligned}

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?

The accuracies of model A and model B on the eight folds and the differences d = B − A
Fold12345678
A8179848082788381
B8380858382808683
dd21130232

The row dd is the difference B − A.

Idea. The same folds for both models: a paired t-test on the differences (Parts 8 and 13).

H0 ⁣:μd=0H1 ⁣:μd≠0dˉ=148=1.75squares of the deviations:0.0625+0.5625+0.5625+1.5625+3.0625+0.0625+1.5625+0.0625=7.5sd2=7.57≈1.0714sd≈1.0351SE=1.03518≈0.36596t=1.750.36596≈4.782df=7column 0.025, row 7: 2.3654.782>2.365\begin{aligned} & H_0\colon \mu_d = 0 \qquad H_1\colon \mu_d \ne 0 \\[4pt] & \bar{d} = \frac{14}{8} = 1.75 \\[4pt] & \text{squares of the deviations:} \\ & \quad 0.0625 + 0.5625 + 0.5625 + 1.5625 \\ & \quad + 3.0625 + 0.0625 + 1.5625 + 0.0625 = 7.5 \\[4pt] & s_d^2 = \frac{7.5}{7} \approx 1.0714 \qquad s_d \approx 1.0351 \\[2pt] & \SE = \frac{1.0351}{\sqrt{8}} \approx 0.36596 \\[2pt] & t = \frac{1.75}{0.36596} \approx 4.782 \qquad df = 7 \\[4pt] & \text{column 0.025, row 7: } 2.365 \\ & 4.782 > 2.365 \end{aligned}

Reject H0H_0. 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 3.4993.499, so p<0.01p < 0.01 (software: p≈0.002p \approx 0.002). 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 α=0.05\alpha = 0.05. Round zz to two decimals before you use the normal table, and give tt and χ2\chi^2 with three decimals.

Level 1: Hypotheses, z-tests and p-values

  1. 1.A producer says that its light bulbs last 1,000 hours on average. A consumer group believes that they last less. Write H0H_0 and H1H_1. Is the test two-tailed, right-tailed or left-tailed?

    Show a hint
    H0H_0 has the equals sign. H1H_1 is what the consumer group wants to show.
    Show one possible solution

    Answer: H0 ⁣:μ=1000H_0\colon \mu = 1000, H1 ⁣:μ<1000H_1\colon \mu < 1000, left-tailed.

    H0 ⁣:μ=1000H1 ⁣:μ<1000H_0\colon \mu = 1000 \qquad H_1\colon \mu < 1000

    The claimed value is 1,000 hours, so H0 ⁣:μ=1000H_0\colon \mu = 1000. The group looks for evidence of "less", so H1 ⁣:μ<1000H_1\colon \mu < 1000. Only small sample means speak against H0H_0: the test is left-tailed.

  2. 2.A test gives the p-value 0.08. What is the decision at α=0.05\alpha = 0.05? And at α=0.10\alpha = 0.10?

    Show a hint
    Reject H0H_0 when the p-value is at most α\alpha.
    Show one possible solution

    Answer: Fail to reject at 0.05; reject at 0.10.

    α=0.05 ⁣:0.08>0.05, fail to reject H0α=0.10 ⁣:0.08≤0.10, reject H0\begin{aligned} \alpha = 0.05\colon\quad & 0.08 > 0.05, \text{ fail to reject } H_0 \\ \alpha = 0.10\colon\quad & 0.08 \le 0.10, \text{ reject } H_0 \end{aligned}
  3. 3.H0 ⁣:μ=200H_0\colon \mu = 200. The standard deviation of the population is 20. A sample of 25 values has the mean 208. Find the test statistic zz.

    Show a hint
    Divide by the standard error σn\dfrac{\sigma}{\sqrt{n}}, not by σ\sigma.
    Show one possible solution

    Answer: z=2.0z = 2.0.

    SE=2025=4z=208−2004=2.0\begin{aligned} \SE &= \frac{20}{\sqrt{25}} = 4 \\[2pt] z &= \frac{208 - 200}{4} = 2.0 \end{aligned}
  4. 4.Find the p-value of the two-tailed test for z=2.0z = 2.0, and decide at α=0.05\alpha = 0.05.

    Show a hint
    Φ(2.00)=0.9772\Phi(2.00) = 0.9772. Two tails.
    Show one possible solution

    Answer: p=0.0456p = 0.0456; reject H0H_0.

    one tail:1−0.9772=0.0228two tails:2×0.0228=0.0456\begin{aligned} \text{one tail:}\quad & 1 - 0.9772 = 0.0228 \\ \text{two tails:}\quad & 2 \times 0.0228 = 0.0456 \end{aligned}

    This is at most 0.05, so H0H_0 is rejected.

  5. 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: z=−1.8z = -1.8, p=0.0718p = 0.0718; fail to reject H0H_0.

    H0 ⁣:μ=50H1 ⁣:μ≠50SE=636=1z=48.2−501=−1.8p=2×(1−0.9641)p=2×0.0359=0.0718\begin{aligned} & H_0\colon \mu = 50 \qquad H_1\colon \mu \ne 50 \\[4pt] & \SE = \frac{6}{\sqrt{36}} = 1 \\[2pt] & z = \frac{48.2 - 50}{1} = -1.8 \\[4pt] & p = 2 \times (1 - 0.9641) \\ & \phantom{p} = 2 \times 0.0359 = 0.0718 \end{aligned}

    This is above 0.05: there is not enough evidence that the mean differs from 50 mm.

  6. 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 zz.
    Show one possible solution

    Answer: z=2.2z = 2.2, p=0.0139p = 0.0139; reject H0H_0.

    H0 ⁣:μ=30H1 ⁣:μ>30SE=5100=0.5z=31.1−300.5=2.2p=1−0.9861=0.0139\begin{aligned} & H_0\colon \mu = 30 \qquad H_1\colon \mu > 30 \\[4pt] & \SE = \frac{5}{\sqrt{100}} = 0.5 \\[2pt] & z = \frac{31.1 - 30}{0.5} = 2.2 \\[4pt] & p = 1 - 0.9861 = 0.0139 \end{aligned}

    0.0139≤0.050.0139 \le 0.05. There is significant evidence that the mean yield is above 30 kg.

  7. 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 zz.
    Show one possible solution

    Answer: z=−2.0z = -2.0, p=0.0228p = 0.0228; reject H0H_0.

    H0 ⁣:μ=8H1 ⁣:μ<8SE=1.525=0.3z=7.4−80.3=−2.0p=Φ(−2.0)=1−0.9772=0.0228\begin{aligned} & H_0\colon \mu = 8 \qquad H_1\colon \mu < 8 \\[4pt] & \SE = \frac{1.5}{\sqrt{25}} = 0.3 \\[2pt] & z = \frac{7.4 - 8}{0.3} = -2.0 \\[4pt] & p = \Phi(-2.0) = 1 - 0.9772 = 0.0228 \end{aligned}

    0.0228≤0.050.0228 \le 0.05. There is significant evidence that the waiting time has become shorter.

  8. 8.Give the critical value of a z-test for (a) a two-tailed test at α=0.10\alpha = 0.10 (the positive one), (b) a right-tailed test at α=0.01\alpha = 0.01, (c) a left-tailed test at α=0.05\alpha = 0.05.

    Show a hint
    The table of critical values in Part 5.
    Show one possible solution

    Answer: (a) 1.6451.645 (reject if ∣z∣\len{z} is above it), (b) 2.3262.326, (c) −1.645-1.645.

    (a) Two tails share 0.10, so 0.05 in each: reject if ∣z∣>1.645\len{z} > 1.645. (b) The whole 0.01 is in the right tail: reject if z>2.326z > 2.326. (c) The whole 0.05 is in the left tail: reject if z<−1.645z < -1.645.

  9. 9.A medical test has H0H_0: "the patient is healthy". The test says that a healthy patient is ill. Which type of error is this?

    Show a hint
    Was H0H_0 true? Was it rejected?
    Show one possible solution

    Answer: A Type I error.

    H0H_0 was true (the patient is healthy) and the test rejected it. Rejecting a true H0H_0 is a Type I error, a false alarm.

  10. 10.A factory checks every batch with H0H_0: "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 α\alpha?

    Show a hint
    H0H_0 was false and was not rejected.
    Show one possible solution

    Answer: A Type II error. Take a larger sample from every batch.

    H0H_0 was false (the batch is bad) and the test did not reject it: a Type II error, a miss. Its probability β\beta becomes smaller when the power grows, and for a fixed α\alpha the power grows with the sample size.

  11. 11.A laboratory runs 200 tests at α=0.05\alpha = 0.05. Suppose that H0H_0 is true in every one of them. How many times is H0H_0 rejected, on average?

    Show a hint
    α\alpha is the probability of a Type I error in one test.
    Show one possible solution

    Answer: 1010 times.

    200×0.05=10200 \times 0.05 = 10

    Every test rejects a true H0H_0 with the probability 0.05, so 10 false alarms are expected.

  12. 12.A coin is tossed 100 times and shows heads 45 times. Is the coin unfair?

    Show a hint
    The standard error uses p0=0.5p_0 = 0.5: 0.5×0.5100\sqrt{\dfrac{0.5 \times 0.5}{100}}.
    Show one possible solution

    Answer: z=−1.0z = -1.0, p=0.3174p = 0.3174; fail to reject H0H_0.

    H0 ⁣:p=0.5H1 ⁣:p≠0.5p^=0.45SE=0.25100=0.05z=0.45−0.50.05=−1.0p=2×(1−0.8413)p=2×0.1587=0.3174\begin{aligned} & H_0\colon p = 0.5 \qquad H_1\colon p \ne 0.5 \\[4pt] & \hat{p} = 0.45 \\[2pt] & \SE = \sqrt{\frac{0.25}{100}} = 0.05 \\[2pt] & z = \frac{0.45 - 0.5}{0.05} = -1.0 \\[4pt] & p = 2 \times (1 - 0.8413) \\ & \phantom{p} = 2 \times 0.1587 = 0.3174 \end{aligned}

    Far above 0.05: 45 heads in 100 tosses is a very ordinary result for a fair coin.

  13. 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
    p^=0.575\hat{p} = 0.575. Right-tailed.
    Show one possible solution

    Answer: z=3.0z = 3.0, p=0.0013p = 0.0013; reject H0H_0.

    H0 ⁣:p=0.5H1 ⁣:p>0.5p^=230400=0.575SE=0.25400=0.025z=0.575−0.50.025=3.0p=1−0.9987=0.0013\begin{aligned} & H_0\colon p = 0.5 \qquad H_1\colon p > 0.5 \\[4pt] & \hat{p} = \frac{230}{400} = 0.575 \\[2pt] & \SE = \sqrt{\frac{0.25}{400}} = 0.025 \\[2pt] & z = \frac{0.575 - 0.5}{0.025} = 3.0 \\[4pt] & p = 1 - 0.9987 = 0.0013 \end{aligned}

    There is strong evidence that more than half of the customers prefer the new packaging.

  14. 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: H1 ⁣:μ≠500H_1\colon \mu \ne 500. (b) "Lower": H1 ⁣:μ<μ0H_1\colon \mu < \mu_0. (c) "More than": H1 ⁣:p>0.6H_1\colon p > 0.6. (d) "Changed" can be up or down: H1 ⁣:μ≠μ0H_1\colon \mu \ne \mu_0.

Level 2: t-tests, errors and power

  1. 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
    df=n−1df = n - 1. Two tails at 5 percent: the column 0.025.
    Show one possible solution

    Answer: df=19df = 19; 2.0932.093.

    df=20−1=19df = 20 - 1 = 19. Column 0.025, row 19: 2.0932.093. H0H_0 is rejected if ∣t∣>2.093\len{t} > 2.093.

  2. 16.H0 ⁣:μ=80H_0\colon \mu = 80. A sample of 25 values has the mean 83 and the standard deviation 10. Find the test statistic tt.

    Show a hint
    The standard error is sn\dfrac{s}{\sqrt{n}}.
    Show one possible solution

    Answer: t=1.5t = 1.5.

    SE=1025=2t=83−802=1.5\begin{aligned} \SE &= \frac{10}{\sqrt{25}} = 2 \\[2pt] t &= \frac{83 - 80}{2} = 1.5 \end{aligned}
  3. 17.Decide the two-tailed test of Problem 16 at α=0.05\alpha = 0.05. Between which two values does the p-value lie?

    Show a hint
    df=24df = 24. In row 24, find the neighbours of 1.5.
    Show one possible solution

    Answer: Fail to reject H0H_0; the p-value is between 0.10 and 0.20.

    column 0.025, row 24: 2.0641.5<2.064row 24: 1.318<1.5<1.711(the columns 0.10 and 0.05)\begin{aligned} & \text{column 0.025, row 24: } 2.064 \\ & 1.5 < 2.064 \\[4pt] & \text{row 24: } 1.318 < 1.5 < 1.711 \\ & \text{(the columns 0.10 and 0.05)} \end{aligned}

    H0H_0 is not rejected. One tail is between 0.05 and 0.10, so two tails are between 0.10 and 0.20. Software gives p≈0.147p \approx 0.147.

  4. 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 df=8df = 8: the column 0.05.
    Show one possible solution

    Answer: t=1.5t = 1.5, critical value 1.8601.860; fail to reject H0H_0.

    H0 ⁣:μ=30H1 ⁣:μ>30SE=39=1t=31.5−301=1.5df=8column 0.05, row 8: 1.8601.5<1.860\begin{aligned} & H_0\colon \mu = 30 \qquad H_1\colon \mu > 30 \\[4pt] & \SE = \frac{3}{\sqrt{9}} = 1 \\[2pt] & t = \frac{31.5 - 30}{1} = 1.5 \qquad df = 8 \\[4pt] & \text{column 0.05, row 8: } 1.860 \\ & 1.5 < 1.860 \end{aligned}

    H0H_0 is not rejected: nine customers do not give enough evidence.

  5. 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 df=15df = 15.
    Show one possible solution

    Answer: t=−2.0t = -2.0, critical value 2.1312.131; fail to reject H0H_0.

    H0 ⁣:μ=100H1 ⁣:μ≠100SE=616=1.5t=97−1001.5=−2.0df=15column 0.025, row 15: 2.131∣t∣=2.0<2.131\begin{aligned} & H_0\colon \mu = 100 \qquad H_1\colon \mu \ne 100 \\[4pt] & \SE = \frac{6}{\sqrt{16}} = 1.5 \\[2pt] & t = \frac{97 - 100}{1.5} = -2.0 \qquad df = 15 \\[4pt] & \text{column 0.025, row 15: } 2.131 \\ & \len{t} = 2.0 < 2.131 \end{aligned}

    H0H_0 is not rejected. The result is close to the border (software: p≈0.064p \approx 0.064), so a larger sample would be a good idea.

  6. 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 xˉ=9\bar{x} = 9 and s2=2.5s^2 = 2.5.
    Show one possible solution

    Answer: t≈2.828t \approx 2.828, df=4df = 4, critical value 2.7762.776; reject H0H_0.

    The five values, their deviations from the mean 9 and the squares
    xx7981011Sum
    x−xˉx - \bar{x}−2-20−1-1120
    (x−xˉ)2(x - \bar{x})^24011410
    H0 ⁣:μ=7H1 ⁣:μ≠7xˉ=455=9s2=104=2.5s≈1.5811SE=1.58115≈0.7071t=9−70.7071≈2.828df=4column 0.025, row 4: 2.7762.828>2.776\begin{aligned} & H_0\colon \mu = 7 \qquad H_1\colon \mu \ne 7 \\[4pt] & \bar{x} = \frac{45}{5} = 9 \\[2pt] & s^2 = \frac{10}{4} = 2.5 \qquad s \approx 1.5811 \\[2pt] & \SE = \frac{1.5811}{\sqrt{5}} \approx 0.7071 \\[2pt] & t = \frac{9 - 7}{0.7071} \approx 2.828 \qquad df = 4 \\[4pt] & \text{column 0.025, row 4: } 2.776 \\ & 2.828 > 2.776 \end{aligned}

    H0H_0 is rejected, but only just (software: p≈0.047p \approx 0.047).

  7. 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 sp=5s_p = 5. df=22df = 22.
    Show one possible solution

    Answer: t≈1.960t \approx 1.960, critical value 2.0742.074; fail to reject H0H_0.

    H0 ⁣:μ1=μ2H1 ⁣:μ1≠μ2sp2=25+252=25sp=5SE=5×112+112SE=5×0.4082≈2.041t=65−612.041≈1.960df=12+12−2=22column 0.025, row 22: 2.0741.960<2.074\begin{aligned} & H_0\colon \mu_1 = \mu_2 \qquad H_1\colon \mu_1 \ne \mu_2 \\[4pt] & s_p^2 = \frac{25 + 25}{2} = 25 \qquad s_p = 5 \\[2pt] & \SE = 5 \times \sqrt{\frac{1}{12} + \frac{1}{12}} \\ & \phantom{\SE} = 5 \times 0.4082 \approx 2.041 \\[2pt] & t = \frac{65 - 61}{2.041} \approx 1.960 \\[2pt] & df = 12 + 12 - 2 = 22 \\[4pt] & \text{column 0.025, row 22: } 2.074 \\ & 1.960 < 2.074 \end{aligned}

    H0H_0 is not rejected: a difference of 4 marks is not enough evidence with 12 students in each class.

  8. 22.Two samples: n1=6n_1 = 6 with the variance s12=10s_1^2 = 10, and n2=10n_2 = 10 with the variance s22=17s_2^2 = 17. Find the pooled variance.

    Show a hint
    Weight each variance with n−1n - 1.
    Show one possible solution

    Answer: sp2=14.5s_p^2 = 14.5.

    sp2=5×10+9×176+10−2=50+15314=20314=14.5\begin{aligned} s_p^2 &= \frac{5 \times 10 + 9 \times 17}{6 + 10 - 2} \\[2pt] &= \frac{50 + 153}{14} = \frac{203}{14} = 14.5 \end{aligned}

    It lies between 10 and 17, closer to 17, because the second sample is larger.

  9. 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
    sp=4s_p = 4, and 116+116=18\sqrt{\dfrac{1}{16} + \dfrac{1}{16}} = \sqrt{\dfrac{1}{8}}.
    Show one possible solution

    Answer: t≈2.828t \approx 2.828, df=30df = 30, critical value 2.0422.042; reject H0H_0.

    H0 ⁣:μ1=μ2H1 ⁣:μ1≠μ2sp2=16+162=16sp=4SE=4×18=4×0.35355≈1.4142t=30−261.4142≈2.828df=30column 0.025, row 30: 2.0422.828>2.042\begin{aligned} & H_0\colon \mu_1 = \mu_2 \qquad H_1\colon \mu_1 \ne \mu_2 \\[4pt] & s_p^2 = \frac{16 + 16}{2} = 16 \qquad s_p = 4 \\[2pt] & \SE = 4 \times \sqrt{\frac{1}{8}} = 4 \times 0.35355 \approx 1.4142 \\[2pt] & t = \frac{30 - 26}{1.4142} \approx 2.828 \qquad df = 30 \\[4pt] & \text{column 0.025, row 30: } 2.042 \\ & 2.828 > 2.042 \end{aligned}

    H0H_0 is rejected: there is significant evidence that the methods differ, and the new one gives 4 points more.

  10. 24.Six people learn a typing method. The change in their speed (after minus before, in words a minute) is −1-1, 3, 2, 0, 4, 4. Did the method raise the speed?

    Show a hint
    A one-sample t-test on the differences: dˉ=2\bar{d} = 2, sd2=4.4s_d^2 = 4.4.
    Show one possible solution

    Answer: t≈2.335t \approx 2.335, df=5df = 5, critical value 2.0152.015; reject H0H_0.

    The six differences, their deviations from the mean 2 and the squares
    dd−1-132044
    d−dˉd - \bar{d}−3-310−2-222
    (d−dˉ)2(d - \bar{d})^2910444
    H0 ⁣:μd=0H1 ⁣:μd>0dˉ=126=2sum of the squares: 22sd2=225=4.4sd≈2.098SE=sd6=4.46≈0.85635t=20.85635≈2.335df=5right-tailed, column 0.05, row 5: 2.0152.335>2.015\begin{aligned} & H_0\colon \mu_d = 0 \qquad H_1\colon \mu_d > 0 \\[4pt] & \bar{d} = \frac{12}{6} = 2 \\[2pt] & \text{sum of the squares: } 22 \\[2pt] & s_d^2 = \frac{22}{5} = 4.4 \qquad s_d \approx 2.098 \\[2pt] & \SE = \frac{s_d}{\sqrt{6}} = \sqrt{\frac{4.4}{6}} \approx 0.85635 \\[2pt] & t = \frac{2}{0.85635} \approx 2.335 \qquad df = 5 \\[4pt] & \text{right-tailed, column 0.05, row 5: } 2.015 \\ & 2.335 > 2.015 \end{aligned}

    H0H_0 is rejected: there is significant evidence that the method raised the speed (software: p≈0.033p \approx 0.033).

  11. 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.

  12. 26.A two-tailed t-test with df=10df = 10 gives t=2.3t = 2.3. 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.

    row 10: 2.228<2.3<2.764(the columns 0.025 and 0.01)one tail: between 0.01 and 0.025two tails: between 0.02 and 0.05\begin{aligned} & \text{row 10: } 2.228 < 2.3 < 2.764 \\ & \text{(the columns 0.025 and 0.01)} \\[4pt] & \text{one tail: between 0.01 and 0.025} \\ & \text{two tails: between 0.02 and 0.05} \end{aligned}

    Software gives p≈0.044p \approx 0.044.

  13. 27.A right-tailed z-test at α=0.05\alpha = 0.05 checks H0 ⁣:μ=100H_0\colon \mu = 100 with σ=20\sigma = 20 and n=25n = 25. Find the power of the test if the true mean is 112. Round the border for xˉ\bar{x} to one decimal.

    Show a hint
    H0H_0 is rejected when xˉ\bar{x} is above 100+1.645×4100 + 1.645 \times 4.
    Show one possible solution

    Answer: About 0.91150.9115.

    SE=2025=4border: 100+1.645×4=106.58≈106.6z=106.6−1124=−1.35β=Φ(−1.35)=0.0885power=1−0.0885=0.9115\begin{aligned} & \SE = \frac{20}{\sqrt{25}} = 4 \\[4pt] & \text{border: } 100 + 1.645 \times 4 = 106.58 \approx 106.6 \\[4pt] & z = \frac{106.6 - 112}{4} = -1.35 \\[2pt] & \beta = \Phi(-1.35) = 0.0885 \\[4pt] & \text{power} = 1 - 0.0885 = 0.9115 \end{aligned}

    The test notices such a rise in 91 of 100 samples.

  14. 28.H0 ⁣:μ=50H_0\colon \mu = 50. Three samples all have the mean 52 and the standard deviation 6, but different sizes: n=9n = 9, n=36n = 36 and n=144n = 144. Find tt for each. Which results are significant in a two-tailed test at 5 percent?

    Show a hint
    Only the standard error 6n\dfrac{6}{\sqrt{n}} changes. For a dfdf that is not in the table, take the next smaller one.
    Show one possible solution

    Answer: t=1t = 1, 22 and 44. Only n=144n = 144 is significant.

    The three samples: standard error, statistic and critical value
    nnSE\SEttdfdfRowCritical value
    963=2\frac{6}{3} = 21882.306
    3666=1\frac{6}{6} = 1235302.042
    144612=0.5\frac{6}{12} = 0.541431201.980

    Only the last tt is above its critical value: with n=144n = 144 the result is significant, with n=9n = 9 and n=36n = 36 it is not. The difference of 2 is the same every time. Four times as many values make tt twice as large.

Level 3: Proportions, chi-square and judgement

  1. 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 100800=0.125\dfrac{100}{800} = 0.125.
    Show one possible solution

    Answer: z≈2.14z \approx 2.14, p=0.0324p = 0.0324; reject H0H_0.

    H0 ⁣:p1=p2H1 ⁣:p1≠p2p^1=40400=0.10p^2=60400=0.15pooled: p^=100800=0.125SE=0.125×0.875×2400SE=0.0005469≈0.02339z=0.15−0.100.02339≈2.14p=2×(1−0.9838)=0.0324\begin{aligned} & H_0\colon p_1 = p_2 \qquad H_1\colon p_1 \ne p_2 \\[4pt] & \hat{p}_1 = \frac{40}{400} = 0.10 \qquad \hat{p}_2 = \frac{60}{400} = 0.15 \\[2pt] & \text{pooled: } \hat{p} = \frac{100}{800} = 0.125 \\[2pt] & \SE = \sqrt{0.125 \times 0.875 \times \frac{2}{400}} \\ & \phantom{\SE} = \sqrt{0.0005469} \approx 0.02339 \\[2pt] & z = \frac{0.15 - 0.10}{0.02339} \approx 2.14 \\[4pt] & p = 2 \times (1 - 0.9838) = 0.0324 \end{aligned}

    0.0324≤0.050.0324 \le 0.05. There is significant evidence that version B is opened more often.

  2. 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. df=1df = 1.
    Show one possible solution

    Answer: χ2=2.88\chi^2 = 2.88, critical value 3.8413.841; fail to reject H0H_0.

    H0 ⁣:heads and tails both haveH0 ⁣:the probability 0.5χ2=(112−100)2100+(88−100)2100χ2=1.44+1.44=2.88df=2−1=1column 0.05, row 1: 3.8412.88<3.841\begin{aligned} & H_0\colon \text{heads and tails both have} \\ & \phantom{H_0\colon} \text{the probability } 0.5 \\[4pt] & \chi^2 = \frac{(112 - 100)^2}{100} + \frac{(88 - 100)^2}{100} \\[2pt] & \phantom{\chi^2} = 1.44 + 1.44 = 2.88 \\[2pt] & df = 2 - 1 = 1 \\[4pt] & \text{column 0.05, row 1: } 3.841 \\ & 2.88 < 3.841 \end{aligned}

    H0H_0 is not rejected: the data is consistent with a fair coin.

  3. 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
    E=row total×column totalgrand totalE = \dfrac{\text{row total} \times \text{column total}}{\text{grand total}}.
    Show one possible solution

    Answer: 1515.

    E=60×50200=3000200=15E = \frac{60 \times 50}{200} = \frac{3000}{200} = 15
  4. 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
    df=(r−1)×(c−1)df = (r - 1) \times (c - 1).
    Show one possible solution

    Answer: df=6df = 6; 12.59212.592.

    df=(3−1)×(4−1)=2×3=6df = (3 - 1) \times (4 - 1) = 2 \times 3 = 6

    Column 0.05, row 6 of the chi-square table: 12.59212.592.

  5. 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. df=3df = 3.
    Show one possible solution

    Answer: χ2=2.72\chi^2 = 2.72, critical value 7.8157.815; fail to reject H0H_0.

    H0 ⁣:every season has the probability 14χ2=52+52+32+3225χ2=25+25+9+925=6825=2.72df=4−1=3column 0.05, row 3: 7.8152.72<7.815\begin{aligned} & H_0\colon \text{every season has the probability } \tfrac{1}{4} \\[4pt] & \chi^2 = \frac{5^2 + 5^2 + 3^2 + 3^2}{25} \\[2pt] & \phantom{\chi^2} = \frac{25 + 25 + 9 + 9}{25} = \frac{68}{25} = 2.72 \\[2pt] & df = 4 - 1 = 3 \\[4pt] & \text{column 0.05, row 3: } 7.815 \\ & 2.72 < 7.815 \end{aligned}

    H0H_0 is not rejected: such differences between the seasons can be chance.

  6. 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: χ2≈4.762\chi^2 \approx 4.762, df=1df = 1; reject at 5 percent, fail to reject at 1 percent.

    The 100 students: observed counts, with the expected counts in brackets
    PassedFailedTotal
    Took the course40  (35)40 \; (35)10  (15)10 \; (15)50
    Did not take it30  (35)30 \; (35)20  (15)20 \; (15)50
    Total7030100
    expected: 50×70100=3550×30100=15χ2=2535+2515+2535+2515χ2≈0.714+1.667+0.714+1.667χ2=4.762df=1row 1: 3.841 (5 percent),6.635 (1 percent)\begin{aligned} & \text{expected: } \frac{50 \times 70}{100} = 35 \qquad \frac{50 \times 30}{100} = 15 \\[4pt] & \chi^2 = \frac{25}{35} + \frac{25}{15} + \frac{25}{35} + \frac{25}{15} \\[2pt] & \phantom{\chi^2} \approx 0.714 + 1.667 + 0.714 + 1.667 \\ & \phantom{\chi^2} = 4.762 \qquad df = 1 \\[4pt] & \text{row 1: } 3.841 \text{ (5 percent)}, \quad 6.635 \text{ (1 percent)} \end{aligned}

    4.7624.762 lies between them: H0H_0 is rejected at 5 percent and not at 1 percent.

  7. 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: χ2=6.25\chi^2 = 6.25, df=2df = 2, critical value 5.9915.991; reject H0H_0.

    The 100 students of the day group and of the evening group: observed counts, with the expected counts in brackets
    DayEveningTotal
    Mathematics20  (24)20 \; (24)20  (16)20 \; (16)40
    Programming30  (24)30 \; (24)10  (16)10 \; (16)40
    Statistics10  (12)10 \; (12)10  (8)10 \; (8)20
    Total6040100
    expected, day group:60×40100=242460×20100=12expected, evening group: 16, 16, 8χ2=1624+3624+412+1616+3616+48χ2≈0.667+1.500+0.333χ2=+1.000+2.250+0.500=6.25df=(3−1)×(2−1)=2column 0.05, row 2: 5.9916.25>5.991\begin{aligned} & \text{expected, day group:} \\ & \quad \frac{60 \times 40}{100} = 24 \qquad 24 \qquad \frac{60 \times 20}{100} = 12 \\[2pt] & \text{expected, evening group: } 16, \ 16, \ 8 \\[4pt] & \chi^2 = \frac{16}{24} + \frac{36}{24} + \frac{4}{12} + \frac{16}{16} + \frac{36}{16} + \frac{4}{8} \\[2pt] & \phantom{\chi^2} \approx 0.667 + 1.500 + 0.333 \\ & \phantom{\chi^2 =} + 1.000 + 2.250 + 0.500 = 6.25 \\[2pt] & df = (3 - 1) \times (2 - 1) = 2 \\[4pt] & \text{column 0.05, row 2: } 5.991 \\ & 6.25 > 5.991 \end{aligned}

    H0H_0 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.

  8. 36.A researcher makes 10 tests, each at α=0.05\alpha = 0.05. If every H0H_0 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
    P(no false alarm in 10 tests)=0.9510P(\text{no false alarm in 10 tests}) = 0.95^{10}.
    Show one possible solution

    Answer: About 0.40130.4013; 0.0050.005.

    P(at least one)=1−0.9510P≈1−0.5987=0.4013Bonferroni: 0.0510=0.005\begin{aligned} & P(\text{at least one}) = 1 - 0.95^{10} \\ & \phantom{P} \approx 1 - 0.5987 = 0.4013 \\[4pt] & \text{Bonferroni: } \frac{0.05}{10} = 0.005 \end{aligned}

    About 40 percent: with ten tests a false alarm is no surprise.

  9. 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
    d=xˉ1−xˉ2spd = \dfrac{\bar{x}_1 - \bar{x}_2}{s_p}.
    Show one possible solution

    Answer: d=0.25d = 0.25; small.

    d=75−7212=0.25d = \frac{75 - 72}{12} = 0.25

    The means differ by a quarter of a standard deviation: a small effect.

  10. 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 824080,000=0.103\dfrac{8240}{80{,}000} = 0.103.
    Show one possible solution

    Answer: z≈2.79z \approx 2.79, p=0.0052p = 0.0052: significant. Whether it is important depends on what the difference is worth.

    H0 ⁣:p1=p2H1 ⁣:p1≠p2pooled: p^=824080,000=0.103SE=0.103×0.897×240,000SE=0.0000046196≈0.00215z=0.106−0.1000.00215≈2.79p=2×(1−0.9974)=0.0052\begin{aligned} & H_0\colon p_1 = p_2 \qquad H_1\colon p_1 \ne p_2 \\[4pt] & \text{pooled: } \hat{p} = \frac{8240}{80{,}000} = 0.103 \\[2pt] & \SE = \sqrt{0.103 \times 0.897 \times \frac{2}{40{,}000}} \\ & \phantom{\SE} = \sqrt{0.0000046196} \approx 0.00215 \\[2pt] & z = \frac{0.106 - 0.100}{0.00215} \approx 2.79 \\[4pt] & p = 2 \times (1 - 0.9974) = 0.0052 \end{aligned}

    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.

  11. 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 df=4df = 4.
    Show one possible solution

    Answer: t≈4.472t \approx 4.472, critical value 2.7762.776; reject H0H_0.

    The five differences, their deviations from the mean 2 and the squares
    dd21313Sum
    d−dˉd - \bar{d}0−1-11−1-110
    (d−dˉ)2(d - \bar{d})^2011114
    H0 ⁣:μd=0H1 ⁣:μd≠0dˉ=105=2sd2=44=1sd=1SE=15≈0.4472t=20.4472≈4.472df=4column 0.025, row 4: 2.7764.472>2.776\begin{aligned} & H_0\colon \mu_d = 0 \qquad H_1\colon \mu_d \ne 0 \\[4pt] & \bar{d} = \frac{10}{5} = 2 \\[2pt] & s_d^2 = \frac{4}{4} = 1 \qquad s_d = 1 \\[2pt] & \SE = \frac{1}{\sqrt{5}} \approx 0.4472 \\[2pt] & t = \frac{2}{0.4472} \approx 4.472 \qquad df = 4 \\[4pt] & \text{column 0.025, row 4: } 2.776 \\ & 4.472 > 2.776 \end{aligned}

    H0H_0 is rejected: model B is significantly more accurate, by 2 points on average (software: p≈0.011p \approx 0.011).

  12. 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 α=0.01\alpha = 0.01 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: χ2≈7.792\chi^2 \approx 7.792, df=1df = 1, critical value 6.6356.635; reject H0H_0.

    The 200 customers: observed counts, with the expected counts in brackets
    BoughtDid not buyTotal
    Discount36  (27)36 \; (27)24  (33)24 \; (33)60
    No discount54  (63)54 \; (63)86  (77)86 \; (77)140
    Total90110200
    expected:60×90200=2760×110200=33140×90200=63140×110200=77every difference O−E is 9 or −9χ2=8127+8133+8163+8177χ2≈3+2.4545+1.2857+1.0519χ2≈7.792df=1column 0.01, row 1: 6.6357.792>6.635\begin{aligned} & \text{expected:} \\ & \quad \frac{60 \times 90}{200} = 27 \qquad \frac{60 \times 110}{200} = 33 \\[2pt] & \quad \frac{140 \times 90}{200} = 63 \qquad \frac{140 \times 110}{200} = 77 \\[4pt] & \text{every difference } O - E \text{ is 9 or } -9 \\[4pt] & \chi^2 = \frac{81}{27} + \frac{81}{33} + \frac{81}{63} + \frac{81}{77} \\[2pt] & \phantom{\chi^2} \approx 3 + 2.4545 + 1.2857 + 1.0519 \\ & \phantom{\chi^2} \approx 7.792 \qquad df = 1 \\[4pt] & \text{column 0.01, row 1: } 6.635 \\ & 7.792 > 6.635 \end{aligned}

    H0H_0 is rejected even at 1 percent (software: p≈0.0052p \approx 0.0052). 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.

Question 1

1.Which of these can be a null hypothesis?

Question 2

2.A test gives the p-value 0.03. Which sentence is right?

Question 3

3.A test gives the p-value 0.04, and the significance level is α=0.01\alpha = 0.01. What is the decision?

Question 4

4.H0 ⁣:μ=50H_0\colon \mu = 50, the standard deviation of the population is 12, and a sample of 36 values has the mean 53. What is the statistic zz?

Question 5

5.A two-tailed z-test gives z=1.0z = 1.0, and Φ(1.00)=0.8413\Phi(1.00) = 0.8413. What is the p-value?

Question 6

6.What is a Type I error?

Question 7

7.A two-tailed t-test at the 5 percent level uses a sample of 10 values. Where in the t table is its critical value?

Question 8

8.The blood pressure of the same 20 patients is measured before and after a treatment. Which test compares the two means?

Question 9

9.H0 ⁣:p=0.2H_0\colon p = 0.2. In a sample of 100, the proportion is p^=0.26\hat{p} = 0.26. What is the statistic zz?

Question 10

10.In a table of counts, a cell stands in a row with the total 40 and in a column with the total 30. The grand total is 120. What is its expected count under independence?

Question 11

11.A chi-square test of independence uses a table with 3 rows and 3 columns (without the totals). How many degrees of freedom does it have?

Question 12

12.In a table of a million rows, the difference between two groups has p=0.0001p = 0.0001 and Cohen's d=0.01d = 0.01. Which conclusion is right?

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.

Python
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 tt of the eight deliveries; ddof=1 makes NumPy divide by n−1n - 1 (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 χ2\chi^2. 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 and in Python (from scipy import stats)
By handIn Python (from scipy import stats)
one-sample t-teststats.ttest_1samp(x, 30)
two-sample t-test with the pooled variancestats.ttest_ind(a, b)
two-sample t-test without equal variances (Welch)stats.ttest_ind(a, b, equal_var=False)
paired t-teststats.ttest_rel(after, before)
one-tailed instead of two-tailedadd alternative="greater" or alternative="less"
chi-square goodness of fitstats.chisquare(observed, expected)
chi-square test of independencestats.chi2_contingency(table, correction=False)
area to the right of zz, of tt, of χ2\chi^2stats.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".

Python
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 t≈3.742t \approx 3.742 and t≈3.384t \approx 3.384 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 χ2≈5.861\chi^2 \approx 5.861 with its p-value and the degrees of freedom. The last line is the area to the right of z=1.92z = 1.92 (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

Every idea of the lesson with its definition or rule
IdeaDefinition or rule
Null hypothesis H0H_0"no difference, no effect"; contains the equals sign; believed until the data speaks against it
Alternative hypothesis H1H_1what we look for evidence of: ≠\ne (two-tailed), >> (right-tailed), << (left-tailed)
Test statisticwhat the sample shows−what H0 claimsstandard error\dfrac{\text{what the sample shows} - \text{what } H_0 \text{ claims}}{\text{standard error}}
p-valuethe probability of a result at least as extreme as ours, if H0H_0 is true
Significance level α\alphathe border for the p-value, fixed before the test; usually 0.05
Decisionp≤αp \le \alpha: reject H0H_0; p>αp > \alpha: fail to reject H0H_0 (never "accept")
Two-tailed p-valuetwice the area of one tail
Critical values of z1.645 (5 percent, one tail), 1.960 (5 percent, two tails), 2.576 (1 percent, two tails)
Type I errorrejecting a true H0H_0; its probability is α\alpha
Type II errornot rejecting a false H0H_0; its probability is β\beta
Power1−β1 - \beta; grows with the sample size, the size of the effect and α\alpha, falls with the spread
z-test for a meanz=xˉ−μ0σ/nz = \dfrac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}, σ\sigma known
One-sample t-testt=xˉ−μ0s/nt = \dfrac{\bar{x} - \mu_0}{s / \sqrt{n}}, df=n−1df = n - 1
Two-sample t-testt=xˉ1−xˉ2sp×1/n1+1/n2t = \dfrac{\bar{x}_1 - \bar{x}_2}{s_p \times \sqrt{1/n_1 + 1/n_2}}, df=n1+n2−2df = n_1 + n_2 - 2
Pooled variancesp2=(n1−1)×s12+(n2−1)×s22n1+n2−2s_p^2 = \dfrac{(n_1 - 1) \times s_1^2 + (n_2 - 1) \times s_2^2}{n_1 + n_2 - 2}
Paired t-testt=dˉsd/nt = \dfrac{\bar{d}}{s_d / \sqrt{n}} on the differences, df=n−1df = n - 1
One proportionz=p^−p0p0×(1−p0)/nz = \dfrac{\hat{p} - p_0}{\sqrt{p_0 \times (1 - p_0) / n}}
Two proportionsz=p^1−p^2p^×(1−p^)×(1/n1+1/n2)z = \dfrac{\hat{p}_1 - \hat{p}_2}{\sqrt{\hat{p} \times (1 - \hat{p}) \times (1/n_1 + 1/n_2)}} with the pooled p^\hat{p}
Chi-square statisticχ2=∑(O−E)2E\chi^2 = \sum \dfrac{(O - E)^2}{E}, always right-tailed
Goodness of fitE=n×(claimed share)E = n \times (\text{claimed share}), dfdf = categories − 1
IndependenceE=row total×column totalgrand totalE = \dfrac{\text{row total} \times \text{column total}}{\text{grand total}}, df=(r−1)×(c−1)df = (r - 1) \times (c - 1)
Effect sizeCohen's d: xˉ1−xˉ2sp\dfrac{\bar{x}_1 - \bar{x}_2}{s_p}; about 0.2 small, 0.5 medium, 0.8 large
Bonferroni correctionmm tests: each at the level αm\dfrac{\alpha}{m}