Asiman Data School

Module 2, topic 20, week 10, Thursday

Confidence intervals and correlation

How far off may an estimate be, and do two variables move together? Confidence intervals for means, proportions and differences, sample size planning, then scatter plots, covariance, the correlation coefficient with its traps, and a first look at the regression line. Worked by hand.

Introduction: how far off, and do they move together?

The eighth and last lesson of Module 2. The last lesson asked a yes-or-no question about a sample: is the mean different from 500? This lesson asks two better questions. The first: between which values does the true mean probably lie? A single number from a sample is never exactly right, and a confidence interval says honestly how far off it may be. The second question is about two columns at once: when one value goes up, does the other go up too? Correlation measures how strongly two variables move together, and it is the door to regression, the first model of the machine learning module. This guide explains point estimates and the margin of error, confidence intervals for a mean (with z and with t), for a proportion and for differences, how to plan a sample size, and how intervals and tests are connected. Then it teaches scatter plots, covariance, the Pearson correlation coefficient with its properties and its traps, the test of a correlation, the rank correlation of Spearman, and a first look at the regression line. 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: the normal distribution and the standard error (Topic 17), the sample mean xˉ\bar{x} and the sample standard deviation ss (Topic 18), and the t distribution, the t table and the logic of a test (Topic 19). 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 single number from a sample is not enough, and what a margin of error is
  2. 2.The confidence interval for a mean when σ\sigma is known (z) and when it is not (t)
  3. 3.What "95 percent confident" means, and what it does not mean
  4. 4.The confidence interval for a proportion, and the margin of error of a poll
  5. 5.What makes an interval wide or narrow, and how to plan the sample size
  6. 6.Confidence intervals for differences: two means, pairs, two proportions
  7. 7.How a confidence interval and a hypothesis test say the same thing in two ways
  8. 8.Scatter plots: direction, form and strength of a relationship
  9. 9.Covariance and the Pearson correlation coefficient rr, calculated by hand
  10. 10.The properties of rr, and its traps: curves, outliers, and "correlation is not causation"
  11. 11.r2r^2, the test of a correlation, and Spearman's rank correlation
  12. 12.A first look at the regression line
  13. 13.Where intervals and correlation are used in machine learning
Notation used in this lesson
WrittenMeaning
xˉ\bar{x}, ss, nnthe mean, the standard deviation and the size of a sample
μ\mu, σ\sigma, ppthe mean, the standard deviation and a proportion of the population (unknown)
p^\hat{p}the proportion in a sample ("p hat")
SE\SEthe standard error: how much an estimate varies from sample to sample
z∗z^*, t∗t^*the critical value that belongs to the confidence level
EEthe margin of error: half the width of the interval
dfdfdegrees of freedom
xˉ\bar{x}, yˉ\bar{y}the means of the two variables xx and yy
sxs_x, sys_ytheir standard deviations
sxys_{xy}the covariance of xx and yy
rrthe Pearson correlation coefficient of a sample; ρ\rho (the Greek letter rho) for a population
rsr_sSpearman's rank correlation coefficient

1Why intervals, and why correlation?

1.1One number is not enough

A shop measures the delivery times of 25 orders and finds a mean of 31 minutes. Is the true mean delivery time 31 minutes? Almost certainly not exactly. Another 25 orders would give 30.2 or 32.1. The number 31 is a point estimate: the best single guess. What is missing is how good the guess is.

"31 minutes, give or take 2" is a much more useful sentence than "31 minutes". It says that the true mean is probably somewhere between 29 and 33. That range is a confidence interval, and "give or take 2" is its margin of error.

Every poll you read does this: "candidate A has 46 percent, with a margin of error of 3 points". Every serious report of a model does it too: "accuracy 0.85 ± 0.02".

1.2From a test to an interval

A test of Topic 19 answers "is the mean 30?" with yes or no. But the manager wants to know more: if not 30, then what? And how sure are we? The interval answers all of that at once, and it shows the size of an effect, which a p-value hides.

1.3Two columns instead of one

Until now every lesson looked at one variable: one column of delivery times, one column of marks. Data tables have many columns, and the interesting questions are about pairs of them:

  • Do students who study more hours get higher marks?
  • Do more expensive products get fewer orders?
  • Does a larger advertising budget go with more sales?

Correlation puts a number on "these two move together": a value between −1-1 and 11. It is the first thing a data scientist looks at before building a model, and the regression line of Topic 22 grows directly out of it.

Where the ideas of this lesson come back in the course
Later in the courseWhat it takes from this lesson
Linear regression (Topic 22)the slope of the line is r×sysxr \times \dfrac{s_y}{s_x}; r2r^2 measures how well the line fits
Regularization and feature selectionstrongly correlated features carry the same information twice
Classification metrics, cross-validationan accuracy is reported with its interval; folds give a mean and a standard error
Embeddings and cosine similaritythe correlation is the cosine of the angle between two centred vectors

2Point estimates and the idea of an interval

2.1Point estimates

A point estimate is one number calculated from a sample that stands for an unknown number of the population.

What is unknown, and what estimates it
Unknown in the populationPoint estimate from the sample
the mean μ\muthe sample mean xˉ\bar{x}
the proportion ppthe sample proportion p^\hat{p}
the standard deviation σ\sigmathe sample standard deviation ss
the difference μ1−μ2\mu_1 - \mu_2xˉ1−xˉ2\bar{x}_1 - \bar{x}_2

2.2The shape of every interval

Topic 17 showed how much a sample mean varies: its standard error is σn\dfrac{\sigma}{\sqrt{n}}, and it is approximately normal. So the sample mean is rarely more than about 2 standard errors away from the true mean. Turn this sentence around: the true mean is rarely more than about 2 standard errors away from the sample mean. That is the whole idea.

Every confidence interval of this lesson has the same shape:

estimate±margin of errormargin of error=critical value×standard error\begin{aligned} & \text{estimate} \pm \text{margin of error} \\[6pt] & \text{margin of error} = \text{critical value} \times \text{standard error} \end{aligned}
  • The estimate is the centre of the interval.
  • The standard error says how much the estimate varies from sample to sample.
  • The critical value (z∗z^* or t∗t^*) says how many standard errors we go to each side. It depends on how confident we want to be.

The interval goes from estimate−E\text{estimate} - E to estimate+E\text{estimate} + E; its width is 2×E2 \times E.

Try it yourself

A report says: "the mean waiting time is between 37 and 45 minutes (95 percent confidence interval)". Find the point estimate, the margin of error and the width of the interval.

Show a hint
The estimate is the middle of the interval; the margin of error is half its width.
Show the solution

Answer: Estimate 41, margin of error 4, width 8.

estimate=37+452=41width=45−37=8E=82=4\begin{aligned} \text{estimate} &= \frac{37 + 45}{2} = 41 \\[4pt] \text{width} &= 45 - 37 = 8 \\[4pt] E &= \frac{8}{2} = 4 \end{aligned}

The report could also have said "41 ± 4 minutes".

3The confidence interval for a mean when σ is known

3.1The formula

When the standard deviation σ\sigma of the population is known:

xˉ±z∗×σn\bar{x} \pm z^* \times \frac{\sigma}{\sqrt{n}}

The critical value z∗z^* cuts off the middle part of the standard normal curve:

The critical value z for the three usual confidence levels
Confidence levelArea in each tailz∗z^*
90 percent0.051.645
95 percent0.0251.960
99 percent0.0052.576

For 95 percent: between −1.960-1.960 and 1.9601.960 lies 95 percent of the standard normal distribution, and 2.5 percent in each tail. These are the two-tailed critical values of Topic 19.

3.2Example

A machine fills bags; its standard deviation is known to be 10 g. A sample of 25 bags has the mean 496 g. Find the 95 percent confidence interval for the mean weight of all bags.

SE=1025=2E=1.960×2=3.92interval:496±3.92=(492.08, 499.92)\begin{aligned} \SE &= \frac{10}{\sqrt{25}} = 2 \\[4pt] E &= 1.960 \times 2 = 3.92 \\[4pt] \text{interval:}\quad & 496 \pm 3.92 = (492.08,\ 499.92) \end{aligned}

We are 95 percent confident that the mean weight of all bags is between 492.08 g and 499.92 g. The machine should fill 500 g, and 500 is not inside the interval: the same conclusion as the test of the last lesson (which rejected μ=500\mu = 500 with p=0.0456p = 0.0456), but now we also see how far off the machine is: between 0.08 g and 7.92 g too little.

3.3Other levels

The same sample at the three usual levels:

The interval of the bags at three confidence levels
Levelz∗z^*Margin of errorInterval
90 percent1.6453.29(492.71, 499.29)(492.71,\ 499.29)
95 percent1.9603.92(492.08, 499.92)(492.08,\ 499.92)
99 percent2.5765.15(490.85, 501.15)(490.85,\ 501.15)
90 percent95 percent99 percent490492494496498500502
The three intervals of the bags: the same sample, the same centre 496, and a wider interval for every step up in confidence.

More confidence costs width. To be more sure that the interval catches the true mean, it must be made wider. An interval "from 0 to 1000 g" would be 100 percent safe and completely useless.

Try it yourself

The marks of a national test have the known standard deviation 12. A sample of 36 students of one school has the mean 75. Find the standard error, the margin of error and the 95 percent confidence interval for the mean of the school.

Show a hint
The standard error is 1236\dfrac{12}{\sqrt{36}}, and z∗=1.960z^* = 1.960.
Show the solution

Answer: SE=2\SE = 2, E=3.92E = 3.92, interval (71.08, 78.92)(71.08,\ 78.92).

SE=1236=2E=1.960×2=3.92interval:75±3.92=(71.08, 78.92)\begin{aligned} \SE &= \frac{12}{\sqrt{36}} = 2 \\[4pt] E &= 1.960 \times 2 = 3.92 \\[4pt] \text{interval:}\quad & 75 \pm 3.92 = (71.08,\ 78.92) \end{aligned}

The mean of all students of the school is between about 71 and 79, with 95 percent confidence.

4What "95 percent confident" means

4.1The procedure is right 95 times in 100

The true mean μ\mu is a fixed number. It does not move. What moves is the sample, and with it the interval. If we took 100 samples and built a 95 percent interval from each, about 95 of the 100 intervals would contain μ\mu, and about 5 would miss it.

490495500505510
25 samples of 25 bags from a machine whose true mean really is 500 g, the dashed line. Every line is one 95 percent confidence interval with its sample mean as a dot. 2 of the 25 intervals miss the true mean and are drawn in the second colour. We never know whether our own interval is one of the lucky ones; we only know that the method is right in about 95 of 100 cases.

A machine whose true mean is 500 g, with σ = 10 g. Take samples of 25 bags and build an interval from each.

Confidence level
490495500505510

Every interval is the sample mean ± 1.960 × 2 = ± 3.92

No samples yet.

The picture shows the last 25 intervals. In the long run the share of intervals that contain 500 comes close to the confidence level: that is all the level promises. Up to 10,000 samples.

4.2What it does not mean

  • Wrong: "There is a 95 percent probability that μ\mu lies in (492.08, 499.92)(492.08,\ 499.92)." After the interval is calculated, nothing is random any more: μ\mu either is inside or is not. The 95 percent describe the method, not this one interval. (In everyday speech "we are 95 percent confident" is accepted, and that is why the word is "confident" and not "probable".)
  • Wrong: "95 percent of the bags weigh between 492.08 and 499.92 g." The interval is about the mean, not about single bags. Single bags vary with σ=10\sigma = 10, far more than the mean does.
  • Wrong: "95 percent of all sample means lie in this interval." Other samples have other intervals.

Right: "We used a method that catches the true mean in 95 percent of all samples, and it gave the interval from 492.08 to 499.92."

A 95 percent confidence interval for the mean delivery time is (29, 33) minutes. Right or wrong?

1 of 8

The sample mean is 31 minutes.

Try it yourself

A 95 percent confidence interval for the mean height of the students of a university is (168, 172)(168,\ 172) cm. Right or wrong? (a) 95 percent of the students are between 168 and 172 cm tall. (b) The method that gave this interval catches the true mean in 95 percent of all samples. (c) The sample mean is 170 cm.

Show a hint
The interval speaks about the mean of all students, and the level about the method.
Show the solution

Answer: (a) wrong; (b) right; (c) right.

(a) is wrong: single students vary far more than 2 cm around the mean; the interval says where the mean lies. (b) is exactly the meaning of the level. (c) The estimate is the middle of the interval: 168+1722=170\dfrac{168 + 172}{2} = 170.

5The confidence interval for a mean when σ is not known

5.1The t interval

In real work σ\sigma is unknown and the sample standard deviation ss takes its place. As in the last lesson, the price is the t distribution with df=n−1df = n - 1:

xˉ±t∗×sn\bar{x} \pm t^* \times \frac{s}{\sqrt{n}}

t∗t^* is read from the t table: for a 95 percent interval take the column 0.025 (2.5 percent in each tail), for 90 percent the column 0.05, for 99 percent the column 0.005.

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.
The column of the t table for each confidence level
Confidence levelColumn of the t table
90 percent0.05
95 percent0.025
99 percent0.005

If your dfdf is not in the table, take the next smaller one: the interval becomes a little wider, which is the careful side.

5.2Example 1: summary numbers

A café measures 9 cups: mean 246 ml, standard deviation 6 ml. Find the 95 percent confidence interval for the mean amount in a cup.

SE=69=2df=8t∗ (column 0.025, row 8)=2.306E=2.306×2≈4.61interval: 246±4.61=(241.39, 250.61)\begin{aligned} & \SE = \frac{6}{\sqrt{9}} = 2 \qquad df = 8 \\[4pt] & t^* \text{ (column 0.025, row 8)} = 2.306 \\[4pt] & E = 2.306 \times 2 \approx 4.61 \\[4pt] & \text{interval: } 246 \pm 4.61 = (241.39,\ 250.61) \end{aligned}

The café claims 250 ml, and 250 lies inside the interval. The data does not contradict the claim, which is what the t-test of the last lesson found too (t=−2.0t = -2.0, not rejected). With z∗=1.960z^* = 1.960 instead of t∗=2.306t^* = 2.306 the interval would have been (242.08, 249.92)(242.08,\ 249.92) and would have wrongly excluded 250: for small samples the t value matters.

5.3Example 2: from raw data

Eight deliveries took 34, 31, 36, 29, 33, 35, 32 and 34 minutes. Find the 90 percent confidence interval for the mean delivery time.

xˉ=2648=33squares of the deviations:1+4+9+16+0+4+1+1=36s2=367≈5.1429s≈2.2678SE=2.26788≈0.8018df=7t∗ (column 0.05, row 7)=1.895E=1.895×0.8018≈1.52interval: 33±1.52=(31.48, 34.52)\begin{aligned} & \bar{x} = \frac{264}{8} = 33 \\[4pt] & \text{squares of the deviations:} \\ & \quad 1 + 4 + 9 + 16 + 0 + 4 + 1 + 1 = 36 \\[4pt] & s^2 = \frac{36}{7} \approx 5.1429 \qquad s \approx 2.2678 \\[4pt] & \SE = \frac{2.2678}{\sqrt{8}} \approx 0.8018 \qquad df = 7 \\[4pt] & t^* \text{ (column 0.05, row 7)} = 1.895 \\[4pt] & E = 1.895 \times 0.8018 \approx 1.52 \\[4pt] & \text{interval: } 33 \pm 1.52 = (31.48,\ 34.52) \end{aligned}

The restaurant promised 30 minutes. The whole interval lies above 30: the promise is not kept.

A sample of n values with the mean 50 and the standard deviation s. Move n and s, and choose the level.

Confidence level
3040506070

Standard error: 10 / √25 = 2

z* = 1.960, margin of error = 1.960 × 2 = 3.92

Interval: 50 ± 3.92 = (46.08, 53.92)

Four times the sample halves the margin. A higher level and more spread make the interval wider. For a small sample t* is clearly larger than z*; from about n = 30 the two are close.

5.4Conditions

The same as for the t-test: a random sample, and a population that is roughly normal or a sample that is large (n≥30n \ge 30). An outlier in a small sample makes the interval unreliable.

Try it yourself

A sample of 16 screws has the mean 43 mm and the standard deviation 8 mm. Find t∗t^*, the margin of error and the 95 percent confidence interval for the mean length.

Show a hint
Only ss is known: t∗t^* with df=15df = 15 from the column 0.025. The standard error is 816\dfrac{8}{\sqrt{16}}.
Show the solution

Answer: t∗=2.131t^* = 2.131, E≈4.26E \approx 4.26, interval (38.74, 47.26)(38.74,\ 47.26).

SE=816=2df=15t∗ (column 0.025, row 15)=2.131E=2.131×2≈4.26interval: 43±4.26=(38.74, 47.26)\begin{aligned} & \SE = \frac{8}{\sqrt{16}} = 2 \qquad df = 15 \\[4pt] & t^* \text{ (column 0.025, row 15)} = 2.131 \\[4pt] & E = 2.131 \times 2 \approx 4.26 \\[4pt] & \text{interval: } 43 \pm 4.26 = (38.74,\ 47.26) \end{aligned}

The machine should make screws of 40 mm, and 40 lies inside the interval: the data does not contradict it, as the t-test of the last lesson found (t=1.5t = 1.5).

6The confidence interval for a proportion

6.1The formula

For a share (the share of customers who buy, of voters for a candidate, of right predictions of a model):

p^±z∗×p^×(1−p^)n\hat{p} \pm z^* \times \sqrt{\frac{\hat{p} \times (1 - \hat{p})}{n}}

There is one difference from the test of the last lesson. A test assumes H0H_0 and uses the claimed p0p_0 in the standard error. An interval assumes nothing, so the only value available is p^\hat{p} itself.

Condition: at least 10 successes and 10 failures in the sample (n×p^≥10n \times \hat{p} \ge 10 and n×(1−p^)≥10n \times (1 - \hat{p}) \ge 10).

6.2Example: a poll

In a poll, 460 of 1,000 voters say they will vote for candidate A. Find the 95 percent confidence interval for the share of A's voters.

p^=4601000=0.46SE=0.46×0.541000=0.0002484SE≈0.0158E=1.960×0.0158≈0.0310interval: 0.46±0.0310=(0.4290, 0.4910)\begin{aligned} & \hat{p} = \frac{460}{1000} = 0.46 \\[4pt] & \SE = \sqrt{\frac{0.46 \times 0.54}{1000}} = \sqrt{0.0002484} \\ & \phantom{\SE} \approx 0.0158 \\[4pt] & E = 1.960 \times 0.0158 \approx 0.0310 \\[4pt] & \text{interval: } 0.46 \pm 0.0310 \\ & \quad = (0.4290,\ 0.4910) \end{aligned}

"46 percent, with a margin of error of about 3 points": the true share is between 42.9 and 49.1 percent. Whether A is below 50 percent is likely but, since the interval ends at 49.1, the race is closer than the single number suggests.

6.3The margin of error of a poll

The product p^×(1−p^)\hat{p} \times (1 - \hat{p}) is largest when p^=0.5\hat{p} = 0.5, where it is 0.25. So the widest possible 95 percent margin for a sample of nn is

1.960×0.25n≈0.98n≈1n1.960 \times \sqrt{\frac{0.25}{n}} \approx \frac{0.98}{\sqrt{n}} \approx \frac{1}{\sqrt{n}}
The largest margin of error of a poll at 95 percent confidence
Sample size nnMargin of error (at most)
1000.098
4000.049
1,0000.031
2,5000.020
10,0000.010

That is why most polls ask about 1,000 people: 3 points of error are good enough, and one point less would need 2,500.

A poll of n people in which p percent answer yes. Move n and p, and choose the level.

Confidence level
020406080100

Standard error: √(0.46 × 0.54 / 1,000) = 0.01576

Margin of error: 1.960 × 0.01576 = 0.0309, about 3.1 percentage points

Interval: (0.4291, 0.4909)

The margin is largest for p = 50 and shrinks towards 0 and 100. To halve it, the poll needs four times as many people: 4,000.

Try it yourself

Of 500 visitors of a website, 150 sign up for the newsletter. Find p^\hat{p}, the margin of error and the 95 percent confidence interval for the share of all visitors who sign up.

Show a hint
p^=0.3\hat{p} = 0.3, and the standard error is 0.3×0.7500\sqrt{\dfrac{0.3 \times 0.7}{500}}.
Show the solution

Answer: p^=0.3\hat{p} = 0.3, E≈0.0402E \approx 0.0402, interval (0.2598, 0.3402)(0.2598,\ 0.3402).

p^=150500=0.30SE=0.3×0.7500=0.00042≈0.02049E=1.960×0.02049≈0.0402interval: 0.30±0.0402=(0.2598, 0.3402)\begin{aligned} & \hat{p} = \frac{150}{500} = 0.30 \\[4pt] & \SE = \sqrt{\frac{0.3 \times 0.7}{500}} = \sqrt{0.00042} \approx 0.02049 \\[4pt] & E = 1.960 \times 0.02049 \approx 0.0402 \\[4pt] & \text{interval: } 0.30 \pm 0.0402 = (0.2598,\ 0.3402) \end{aligned}

Between about 26 and 34 percent of all visitors sign up. The condition holds: 150 successes and 350 failures.

7Narrow or wide? Planning the sample size

7.1What changes the width

E=critical value×standard deviationnE = \text{critical value} \times \frac{\text{standard deviation}}{\sqrt{n}}
What makes an interval narrower or wider
ChangeThe interval becomes
a larger sample nnnarrower: four times the sample, half the margin
a higher confidence levelwider: z∗z^* grows from 1.645 to 1.960 to 2.576
more spread in the data (σ\sigma or ss)wider

7.2The sample size for a mean

Before a study, turn the formula round: how many values are needed for a margin of error of at most EE?

E=z∗×σngivesn=(z∗×σE)2E = z^* \times \frac{\sigma}{\sqrt{n}} \qquad \text{gives} \qquad n = \left(\frac{z^* \times \sigma}{E}\right)^2

Always round up: a smaller nn would give a margin that is a little too large. σ\sigma must be guessed from earlier data or a small pilot study.

Example. Delivery times have a standard deviation of about 6 minutes. How many deliveries must be measured to know the mean within 1 minute, with 95 percent confidence?

n=(1.960×61)2=11.762=138.2976round up: n=139\begin{aligned} n &= \left(\frac{1.960 \times 6}{1}\right)^2 = 11.76^2 = 138.2976 \\[4pt] & \text{round up: } n = 139 \end{aligned}

7.3The sample size for a proportion

n=(z∗)2×p×(1−p)E2n = \frac{(z^*)^2 \times p \times (1 - p)}{E^2}

pp is not known before the study. If there is an earlier estimate, use it. If not, use p=0.5p = 0.5: it gives the largest nn, so the plan is safe whatever the truth is.

Example. A poll should have a margin of error of 2 points at 95 percent confidence.

n=1.9602×0.250.022=3.8416×0.250.0004=2401round up: n=2401\begin{aligned} n &= \frac{1.960^2 \times 0.25}{0.02^2} = \frac{3.8416 \times 0.25}{0.0004} = 2401 \\[4pt] & \text{round up: } n = 2401 \end{aligned}

Half the margin needs four times the people: for 1 point the poll would need 9,604.

Try it yourself

(a) The weight of parcels has a standard deviation of about 8 kg. How many parcels must be weighed to know the mean within 2 kg, with 95 percent confidence? (b) A poll should have a margin of error of 4 percentage points at 95 percent confidence, and nothing is known about the share. How many people must be asked?

Show a hint
(a) n=(z∗×σE)2n = \left(\dfrac{z^* \times \sigma}{E}\right)^2. (b) Use p=0.5p = 0.5. Round up both times.
Show the solution

Answer: (a) 6262; (b) 601601.

(a)n=(1.960×82)2=7.842≈61.47, round up: 62(b)n=1.9602×0.250.042=3.8416×0.250.0016=600.25, round up: 601\begin{aligned} \text{(a)}\quad n &= \left(\frac{1.960 \times 8}{2}\right)^2 = 7.84^2 \\[2pt] &\approx 61.47, \text{ round up: } 62 \\[6pt] \text{(b)}\quad n &= \frac{1.960^2 \times 0.25}{0.04^2} = \frac{3.8416 \times 0.25}{0.0016} \\[2pt] &= 600.25, \text{ round up: } 601 \end{aligned}

Rounding to the nearest whole number would give 61 and 600, and both margins would be a little too large.

8Confidence intervals for differences

When two groups are compared, the interesting number is the difference, and it gets an interval of its own. The key question is whether 0 lies inside: if it does, "no difference" is still possible.

8.1Two independent means

With the pooled standard deviation sps_p of the last lesson:

(xˉ1−xˉ2)±t∗×sp×1n1+1n2df=n1+n2−2\begin{aligned} & (\bar{x}_1 - \bar{x}_2) \pm t^* \times s_p \times \sqrt{\frac{1}{n_1} + \frac{1}{n_2}} \\[6pt] & df = n_1 + n_2 - 2 \end{aligned}

Example. Ten students with method A have the mean 80 (standard deviation 8); ten others with method B have the mean 72 (standard deviation 6). Find the 95 percent confidence interval for the difference of the means.

sp2=64+362=50sp≈7.071SE=7.071×110+110≈3.162df=18t∗ (column 0.025)=2.101E=2.101×3.162≈6.64interval: 8±6.64=(1.36, 14.64)\begin{aligned} & s_p^2 = \frac{64 + 36}{2} = 50 \qquad s_p \approx 7.071 \\[4pt] & \SE = 7.071 \times \sqrt{\frac{1}{10} + \frac{1}{10}} \approx 3.162 \\[4pt] & df = 18 \qquad t^* \text{ (column 0.025)} = 2.101 \\[4pt] & E = 2.101 \times 3.162 \approx 6.64 \\[4pt] & \text{interval: } 8 \pm 6.64 = (1.36,\ 14.64) \end{aligned}

Method A is better by somewhere between 1.36 and 14.64 marks. The interval does not contain 0, so the difference is significant (the test gave t≈2.530t \approx 2.530). But look how wide it is: with 10 students in each group we know that A is better, and hardly by how much.

8.2Pairs

For paired data, the interval is the t interval of the differences:

dˉ±t∗×sdndf=n−1\bar{d} \pm t^* \times \frac{s_d}{\sqrt{n}} \qquad df = n - 1

Example. The eight students of the last lesson improved by dˉ=3\bar{d} = 3 marks with sd≈2.5071s_d \approx 2.5071. The 95 percent interval:

SE=2.50718≈0.8864t∗ (row 7)=2.365E=2.365×0.8864≈2.10interval: 3±2.10=(0.90, 5.10)\begin{aligned} & \SE = \frac{2.5071}{\sqrt{8}} \approx 0.8864 \qquad t^* \text{ (row 7)} = 2.365 \\[4pt] & E = 2.365 \times 0.8864 \approx 2.10 \\[4pt] & \text{interval: } 3 \pm 2.10 = (0.90,\ 5.10) \end{aligned}

The course improves the marks by between 0.90 and 5.10 on average.

8.3Two proportions

(p^1−p^2)±z∗×p^1(1−p^1)n1+p^2(1−p^2)n2(\hat{p}_1 - \hat{p}_2) \pm z^* \times \sqrt{\frac{\hat{p}_1 (1 - \hat{p}_1)}{n_1} + \frac{\hat{p}_2 (1 - \hat{p}_2)}{n_2}}

Here the two proportions are not pooled: pooling belongs to the test, where H0H_0 says that they are equal. The interval does not assume that.

Example. Version A of a page: 200 clicks among 2,000 visitors. Version B: 250 among 2,000. Find the 95 percent confidence interval for the difference of the click rates (B minus A).

p^1=0.100p^2=0.125difference=0.025SE=0.1×0.92000+0.125×0.8752000SE=0.000045+0.0000546875SE≈0.00998E=1.960×0.00998≈0.0196interval: 0.025±0.0196=(0.0054, 0.0446)\begin{aligned} & \hat{p}_1 = 0.100 \qquad \hat{p}_2 = 0.125 \qquad \text{difference} = 0.025 \\[4pt] & \SE = \sqrt{\frac{0.1 \times 0.9}{2000} + \frac{0.125 \times 0.875}{2000}} \\ & \phantom{\SE} = \sqrt{0.000045 + 0.0000546875} \\ & \phantom{\SE} \approx 0.00998 \\[4pt] & E = 1.960 \times 0.00998 \approx 0.0196 \\[4pt] & \text{interval: } 0.025 \pm 0.0196 \\ & \quad = (0.0054,\ 0.0446) \end{aligned}

Version B is better by between 0.5 and 4.5 percentage points. For a business decision this is the number that counts: is even the lower end, half a point, worth the change?

Try it yourself

Sixteen patients are measured before and after a treatment. Their blood pressure fell by dˉ=5\bar{d} = 5 units on average, and the differences have the standard deviation sd=4s_d = 4. Find the 95 percent confidence interval for the mean fall. Does it contain 0?

Show a hint
A t interval of the differences with df=15df = 15; the standard error is 416\dfrac{4}{\sqrt{16}}.
Show the solution

Answer: (2.87, 7.13)(2.87,\ 7.13); it does not contain 0.

SE=416=1t∗ (column 0.025, row 15)=2.131E=2.131×1≈2.13interval: 5±2.13=(2.87, 7.13)\begin{aligned} & \SE = \frac{4}{\sqrt{16}} = 1 \\[4pt] & t^* \text{ (column 0.025, row 15)} = 2.131 \\[4pt] & E = 2.131 \times 1 \approx 2.13 \\[4pt] & \text{interval: } 5 \pm 2.13 = (2.87,\ 7.13) \end{aligned}

The whole interval lies above 0: the treatment lowers the blood pressure, by between about 2.9 and 7.1 units on average.

9Intervals and tests say the same thing

A 95 percent confidence interval contains exactly the values μ0\mu_0 that a two-tailed test at α=0.05\alpha = 0.05 would not reject.

A value against the 95 percent interval, and the test at 5 percent
The value μ0\mu_0 liesThe two-tailed test at α=0.05\alpha = 0.05
inside the 95 percent intervalfails to reject H0 ⁣:μ=μ0H_0\colon \mu = \mu_0
outside the 95 percent intervalrejects H0 ⁣:μ=μ0H_0\colon \mu = \mu_0

The reason is one inequality read in two directions: μ0\mu_0 is inside the interval when ∣xˉ−μ0∣\len{\bar{x} - \mu_0} is at most z∗×SEz^* \times \SE, and that is exactly when ∣z∣\len{z} is at most z∗z^*.

The confidence level and the significance level that belong together
Confidence levelBelongs to the two-tailed test at
90 percentα=0.10\alpha = 0.10
95 percentα=0.05\alpha = 0.05
99 percentα=0.01\alpha = 0.01

For a difference, the value to look for is 0: an interval for μ1−μ2\mu_1 - \mu_2 that does not contain 0 means a significant difference.

What the interval adds. A test says "significant" or "not". An interval says how large the effect is and how precisely we know it. Two studies can both be "significant", one with the interval (0.1, 0.3)(0.1,\ 0.3) and one with (5, 40)(5,\ 40): a tiny effect known precisely, and a large effect known vaguely. That is why many journals and most good reports ask for intervals.

(For proportions the match is only approximate, because the test uses p0p_0 in the standard error and the interval uses p^\hat{p}.)

Try it yourself

A 95 percent confidence interval for the mean weight of a product is (12.4, 15.8)(12.4,\ 15.8) kg. Without a new calculation: would a two-tailed test at α=0.05\alpha = 0.05 reject (a) H0 ⁣:μ=12H_0\colon \mu = 12, (b) H0 ⁣:μ=15H_0\colon \mu = 15? (c) To which significance level does a 99 percent interval belong?

Show a hint
Is the value inside or outside the interval?
Show the solution

Answer: (a) reject; (b) fail to reject; (c) α=0.01\alpha = 0.01.

(a) 12 lies outside the interval, so it is rejected. (b) 15 lies inside, so the data does not contradict it. (c) A 99 percent interval leaves 1 percent outside: α=0.01\alpha = 0.01.

10Two variables and the scatter plot

10.1Pairs of values

From here on every person or thing gives two numbers: the hours a student studied and the mark, the price of a product and the number sold, the size of a flat and its rent. The data is a list of pairs (x,y)(x, y).

The first step is always a picture. A scatter plot draws every pair as one point: xx to the right, yy upwards.

10.2What to look for

What to look for in a scatter plot
Look forQuestionWords
DirectionWhen xx grows, does yy grow or fall?positive, negative, none
FormDo the points follow a line, a curve, or nothing?linear, curved, no pattern
StrengthHow closely do the points follow the pattern?strong, moderate, weak
Unusual pointsIs there a point far from all the others?outlier

r=0.95r = 0.95

r=0.6r = 0.6

r=0r = 0

r=−0.6r = -0.6

r=−0.95r = -0.95

r≈0.00r \approx 0.00

a curve: a strong relationship

Five linear relationships of different direction and strength with their correlation coefficient r, the number this lesson builds. The last picture is a warning that comes back in Part 13: a clear curve can have an r near 0.

Forty points whose correlation is exactly r. Move r and watch the cloud.

r = 0.60: positive, moderate

r² = 0.3600: 36 percent of the variation of y goes with x

The points are the same forty for every r; only how closely they follow the line changes. Compare r = 0.5 with r² = 0.25: a correlation of one half explains a quarter.

Try it yourself

Positive, negative or no relationship? (a) The number of rooms of a flat and its rent. (b) The speed of a car and the time it needs for 100 km. (c) The number of letters in a person's name and the person's age.

Show a hint
When xx grows, does yy usually grow, usually fall, or neither?
Show the solution

Answer: (a) positive; (b) negative; (c) none.

(a) More rooms, higher rent: positive. (b) The faster the car, the shorter the time: negative (and curved, because the time is 100 divided by the speed). (c) The two have nothing to do with each other.

11Covariance

11.1The idea: do the deviations agree?

Take the hours that five students studied and their marks:

The hours and the marks of five students
Student12345Sum
Hours xx2456825
Mark yy5060657580330

The means are xˉ=5\bar{x} = 5 and yˉ=66\bar{y} = 66. A student who studied more than the mean has a positive deviation x−xˉx - \bar{x}; one with a mark above the mean has a positive y−yˉy - \bar{y}. Multiply the two deviations of each student:

  • both above the mean, or both below: the product is positive
  • one above and one below: the product is negative
The deviations of the five students and their products
Student12345Sum
x−xˉx - \bar{x}−3-3−1-10130
y−yˉy - \bar{y}−16-16−6-6−1-19140
product4860942105

Every product is positive or zero: students below the mean in hours are below the mean in marks, and the other way round.

246850607080hoursmark++−−mean 5mean 66
The five students with the two means as dashed lines. Points in the upper right and in the lower left field give positive products of the deviations; points in the other two fields give negative products. Here all points lie in the plus fields.

11.2The definition

The covariance is the average product of the deviations. For a sample, the sum is divided by n−1n - 1, as for the variance:

sxy=∑(x−xˉ)(y−yˉ)n−1s_{xy} = \frac{\sum (x - \bar{x})(y - \bar{y})}{n - 1}

For the five students:

sxy=1054=26.25s_{xy} = \frac{105}{4} = 26.25
  • sxy>0s_{xy} > 0: the variables tend to move in the same direction.
  • sxy<0s_{xy} < 0: when one is above its mean, the other tends to be below.
  • sxys_{xy} near 0: no linear tendency.

The covariance of a variable with itself is its variance: sxx=∑(x−xˉ)2n−1=sx2s_{xx} = \dfrac{\sum (x - \bar{x})^2}{n - 1} = s_x^2.

11.3The weakness of the covariance

Is 26.25 large? Nobody can say. Its unit is "hours times marks". Measure the time in minutes instead of hours and every x−xˉx - \bar{x} becomes 60 times larger, and so does the covariance: 1,575. The relationship has not changed at all. The covariance tells the direction, but its size depends on the units. This is repaired in the next part.

Try it yourself

Three shops spend x=1,2,3x = 1, 2, 3 thousand manat on advertising and have the sales y=2,4,9y = 2, 4, 9 thousand manat. Find the two means, the sum of the products of the deviations, and the covariance.

Show a hint
The means are 2 and 5. Divide the sum of the products by n−1=2n - 1 = 2.
Show the solution

Answer: xˉ=2\bar{x} = 2, yˉ=5\bar{y} = 5, sum of the products 7, sxy=3.5s_{xy} = 3.5.

The deviations of the three shops and their products
Shop 1Shop 2Shop 3Sum
x−xˉx - \bar{x}−1-1010
y−yˉy - \bar{y}−3-3−1-140
product3047
sxy=72=3.5s_{xy} = \frac{7}{2} = 3.5

The covariance is positive: more advertising goes with more sales.

12The correlation coefficient

12.1The definition

Divide the covariance by both standard deviations and the units cancel:

r=sxysx×syr = \frac{s_{xy}}{s_x \times s_y}

This is the Pearson correlation coefficient. The n−1n - 1 cancels too, which gives the formula that is easiest by hand:

r=∑(x−xˉ)(y−yˉ)∑(x−xˉ)2×∑(y−yˉ)2r = \frac{\sum (x - \bar{x})(y - \bar{y})}{\sqrt{\sum (x - \bar{x})^2 \times \sum (y - \bar{y})^2}}

12.2The calculation in a table

The calculation of r for the five students
Studentxxyyx−xˉx - \bar{x}y−yˉy - \bar{y}product(x−xˉ)2(x - \bar{x})^2(y−yˉ)2(y - \bar{y})^2
1250−3-3−16-16489256
2460−1-1−6-66136
35650−1-1001
4675199181
5880314429196
Sum253300010520570
r=10520×570=10511400≈105106.771≈0.9834\begin{aligned} r &= \frac{105}{\sqrt{20 \times 570}} = \frac{105}{\sqrt{11400}} \\[4pt] &\approx \frac{105}{106.771} \approx 0.9834 \end{aligned}

A very strong positive linear relationship: more hours go with higher marks, almost on a straight line.

The same through the definition: sx=204≈2.2361s_x = \sqrt{\dfrac{20}{4}} \approx 2.2361, sy=5704≈11.9373s_y = \sqrt{\dfrac{570}{4}} \approx 11.9373, and

r=26.252.2361×11.9373≈0.9834r = \frac{26.25}{2.2361 \times 11.9373} \approx 0.9834

12.3The steps

  1. Calculate the two means.
  2. Write the deviations x−xˉx - \bar{x} and y−yˉy - \bar{y} (each column adds up to 0).
  3. Multiply them pair by pair, and square each of them.
  4. Add the three columns.
  5. rr is the sum of the products, divided by the square root of (the sum of squares of xx times the sum of squares of yy).

The five students studied 2, 4, 5, 6 and 8 hours. Move their marks and follow the calculation of r.

2468406080100hoursmark
The deviations of the hours and of the marks from their means, and their products
hours − 5−3−1013sum 0
mark − mean−16−6−1914sum 0
product4860942sum 105

Mean mark: 66. Sums of squares: 20 for the hours, 570 for the marks

r = 105 / √(20 × 570) = 0.9834, covariance = 105 / 4 = 26.25

Line: slope 105 / 20 = 5.25, intercept 66 − 5.25 × 5 = 39.75

Put the marks on a straight line (50, 60, 65, 70, 80) and r becomes 1. Turn the order round and r is negative. The line always passes through the point of the two means.

12.4A shortcut without deviations

When the means are not round numbers, use the raw sums:

∑(x−xˉ)(y−yˉ)=∑xy−n×xˉ×yˉ∑(x−xˉ)2=∑x2−n×xˉ2\begin{aligned} \sum (x - \bar{x})(y - \bar{y}) &= \sum xy - n \times \bar{x} \times \bar{y} \\[4pt] \sum (x - \bar{x})^2 &= \sum x^2 - n \times \bar{x}^2 \end{aligned}

For the five students:

∑xy=100+240+325+450+640=17551755−5×5×66=105\begin{aligned} \sum xy &= 100 + 240 + 325 + 450 + 640 = 1755 \\[4pt] & 1755 - 5 \times 5 \times 66 = 105 \end{aligned}

The same sum of products.

12.5Reading r

rr is always between −1-1 and 11.

What a value of r means
rrMeaning
11all points lie exactly on a rising line
near 11strong positive linear relationship
near 00no linear relationship
near −1-1strong negative linear relationship
−1-1all points lie exactly on a falling line

A rough guide for the strength, by the size of ∣r∣\len{r}:

A rough guide for the strength of a correlation
∣r∣\len{r}Strength
below 0.3weak
0.3 to 0.7moderate
above 0.7strong

These borders are a habit, not a law. In physics an rr of 0.9 may be disappointing; in psychology or marketing 0.3 can be an important finding.

12.6r is a z-score product

Another way to see the formula: standardise both variables (Topic 18) and average the products of the z-scores:

r=∑(zx×zy)n−1r = \frac{\sum (z_x \times z_y)}{n - 1}

That is why rr has no unit, and why it does not care in which units xx and yy were measured.

Try it yourself

For the three shops of the last task (x=1,2,3x = 1, 2, 3 and y=2,4,9y = 2, 4, 9), the sum of the products of the deviations is 7. Find the two sums of squares and rr.

Show a hint
The deviations are −1,0,1-1, 0, 1 and −3,−1,4-3, -1, 4. Divide 7 by the square root of the product of the two sums of squares.
Show the solution

Answer: Sums of squares 2 and 26, r≈0.9707r \approx 0.9707.

∑(x−xˉ)2=1+0+1=2∑(y−yˉ)2=9+1+16=26r=72×26=752≈77.2111≈0.9707\begin{aligned} \sum (x - \bar{x})^2 &= 1 + 0 + 1 = 2 \\[4pt] \sum (y - \bar{y})^2 &= 9 + 1 + 16 = 26 \\[4pt] r &= \frac{7}{\sqrt{2 \times 26}} = \frac{7}{\sqrt{52}} \\[4pt] &\approx \frac{7}{7.2111} \approx 0.9707 \end{aligned}

A strong positive correlation, but not 1: the three points do not lie on one line, because the sales rise by 2 and then by 5.

13Properties of r, and its traps

13.1Properties

  • No unit, and always between −1-1 and 11.
  • Symmetric: the correlation of xx with yy is the correlation of yy with xx.
  • Shifting and rescaling change nothing: hours or minutes, Celsius or Fahrenheit, rr stays the same. (Multiplying one variable by a negative number changes the sign.)
  • r=±1r = \pm 1 only for a perfect line. The slope of the line does not matter: y=0.01×xy = 0.01 \times x and y=100×xy = 100 \times x both give r=1r = 1. rr measures how close the points are to a line, not how steep the line is.

13.2r² : the share of the variation explained

The square of rr is called the coefficient of determination. It is the share of the variation of yy that the linear relationship with xx accounts for.

For the students, r2=0.98342≈0.967r^2 = 0.9834^2 \approx 0.967: about 97 percent of the differences between the marks go together with the differences in hours. An rr of 0.5 sounds like "half", but r2=0.25r^2 = 0.25: only a quarter of the variation is explained.

y=x2 ⁣: r=0y = x^2\colon\ r = 0

r=0.87r = 0.87

without the far point: r = −0.33

r=−0.74r = −0.74

three groups, each rising

Three traps. Left: y is known exactly from x, and r is 0, because the relationship is not a line. Middle: twelve points without a pattern and one point far away, which alone creates a strong correlation. Right: inside each of the three groups the relationship is positive; with all points together it is negative.

13.3Trap 1: r sees only lines

Take x=−2,−1,0,1,2x = -2, -1, 0, 1, 2 and y=x2y = x^2, so y=4,1,0,1,4y = 4, 1, 0, 1, 4. yy is known exactly from xx: the strongest relationship there can be. Yet the products of the deviations are −4,1,0,−1,4-4, 1, 0, -1, 4, which add up to 0, and r=0r = 0. r=0r = 0 means "no linear relationship", not "no relationship". Always look at the scatter plot.

13.4Trap 2: outliers

One point far away from the others can create a strong correlation where there is none (the middle picture), or destroy one that exists. rr is built from means and squared deviations, and like them it is not robust (Topic 18).

Eight points without a clear pattern, and a ninth point that you can move.

0510152005101520

Without the ninth point: r = 0.2739

With the ninth point at (18, 18): r = 0.9519

Far away at the upper right, the single point creates a strong positive correlation; at the lower right a negative one. In the middle of the others it changes almost nothing. One value can decide a correlation, so look at the picture first.

13.5Trap 3: groups mixed together

In the right picture the relationship is positive inside every group, and negative when all points are thrown together, because the groups themselves lie on a falling line. A correlation of mixed groups can point in the opposite direction from the correlation inside each group. (This is called Simpson's paradox.)

13.6Trap 4: a restricted range

Among all students, hours of study and marks correlate strongly. Among only the students with marks above 85, the correlation almost vanishes: the range of yy was cut, and with it most of the pattern. A correlation is valid for the range of the data it was calculated from.

13.7Trap 5: correlation is not causation

In summer more ice cream is sold, and more people drown. The two numbers correlate strongly. Ice cream does not cause drowning: hot weather raises both. A third variable behind the two is called a lurking variable or confounder.

A correlation between xx and yy can have four explanations:

Four explanations of a correlation
ExplanationExample
xx causes yymore study, higher marks
yy causes xx"people who take vitamins are healthier": or do healthy people look after themselves more?
a third variable causes bothice cream and drowning (the weather); shoe size and reading skill of children (age)
chancewith many pairs of columns, some correlate by accident (the many tests of Topic 19)

Only an experiment with randomly formed groups (an A/B test) can show a cause. Data that was merely observed shows that two things go together, not why.

Try it yourself

(a) The correlation between the price of a product in manat and the number sold is −0.8-0.8. What is it when the price is given in dollars? (b) Two variables have r=0.6r = 0.6. Which share of the variation of one goes with the other? (c) Towns with more hospitals have more deaths a year. Which explanation is the most likely?

Show a hint
Rescaling does not change rr. The share is r2r^2. Think of what makes both numbers large.
Show the solution

Answer: (a) −0.8-0.8; (b) 0.36; (c) a third variable: the size of the town.

(a) A change of currency multiplies every price by a positive factor: rr stays −0.8-0.8. (b) r2=0.62=0.36r^2 = 0.6^2 = 0.36, so 36 percent. (c) Large towns have more hospitals and more people who die: the number of inhabitants is the lurking variable.

14Is the correlation real? A test for r

Five students gave r=0.9834r = 0.9834. But five points are very few: even if hours and marks had nothing to do with each other in the population (ρ=0\rho = 0), a sample of five would show some correlation by chance. The question of Topic 19 comes back: could this rr be chance?

H0 ⁣:ρ=0H1 ⁣:ρ≠0t=r×n−21−r2df=n−2\begin{aligned} & H_0\colon \rho = 0 \qquad H_1\colon \rho \ne 0 \\[6pt] & t = \frac{r \times \sqrt{n - 2}}{\sqrt{1 - r^2}} \qquad df = n - 2 \end{aligned}

For the students:

t=0.9834×31−0.98342=0.9834×1.73210.0329≈1.70330.1814≈9.39df=3\begin{aligned} t &= \frac{0.9834 \times \sqrt{3}}{\sqrt{1 - 0.9834^2}} = \frac{0.9834 \times 1.7321}{\sqrt{0.0329}} \\[4pt] &\approx \frac{1.7033}{0.1814} \approx 9.39 \qquad df = 3 \end{aligned}

The critical value (two-tailed, 5 percent, row 3) is 3.1823.182, and 9.39>3.1829.39 > 3.182: reject H0H_0. Even with five students the correlation is significant, because it is so strong.

The sample size matters a great deal. The same rr can be chance in a small sample and beyond doubt in a large one:

The smallest correlation that is significant at 5 percent (two-tailed)
nnSmallest significant ∣r∣\len{r}
50.88
100.63
200.44
300.36
1000.20

With 10 pairs, even r=0.6r = 0.6 could be chance. With 100 pairs, r=0.2r = 0.2 is already significant, and still weak: as in Topic 19, significant is not the same as important. r2r^2 tells how much the relationship matters.

Try it yourself

In a sample of 11 towns, the correlation between the number of parks and the price of flats is r=0.8r = 0.8. Find tt and the degrees of freedom. Is the correlation significantly different from 0 at the 5 percent level?

Show a hint
t=r×n−21−r2t = \dfrac{r \times \sqrt{n - 2}}{\sqrt{1 - r^2}}, and 1−0.64=0.6\sqrt{1 - 0.64} = 0.6.
Show the solution

Answer: t=4.0t = 4.0, df=9df = 9, critical value 2.2622.262; significant.

t=0.8×91−0.64=0.8×30.6=4.0df=9column 0.025, row 9: 2.2624.0>2.262\begin{aligned} t &= \frac{0.8 \times \sqrt{9}}{\sqrt{1 - 0.64}} = \frac{0.8 \times 3}{0.6} = 4.0 \qquad df = 9 \\[4pt] & \text{column 0.025, row 9: } 2.262 \\ & 4.0 > 2.262 \end{aligned}

Reject H0H_0: the correlation is significant. It says that parks and prices go together, not that parks raise prices: richer districts may have both.

15Rank correlation

15.1When Pearson's r does not fit

Pearson's rr measures closeness to a line and is sensitive to outliers. Two situations call for something else:

  • The data are ranks or ordered categories (ordinal data, Topic 18): places in a competition, ratings from "bad" to "excellent".
  • The relationship always rises (or always falls) but is curved, or there are outliers.

Spearman's rank correlation rsr_s replaces every value by its rank (1 for the smallest, 2 for the next, and so on) and then calculates Pearson's rr of the ranks. When there are no ties, a short formula gives the same number:

rs=1−6×∑d2n×(n2−1)r_s = 1 - \frac{6 \times \sum d^2}{n \times (n^2 - 1)}

dd is the difference between the two ranks of the same person or thing.

15.2Example

Two judges rank six dishes of a cooking competition.

The ranks that two judges give to six dishes
DishABCDEFSum
Judge 1123456
Judge 2214365
dd−1-11−1-11−1-110
d2d^21111116
rs=1−6×66×35=1−36210≈0.8286r_s = 1 - \frac{6 \times 6}{6 \times 35} = 1 - \frac{36}{210} \approx 0.8286

The judges agree strongly: they never differ by more than one place.

15.3Reading r_s

rsr_s is read like rr: between −1-1 and 11. rs=1r_s = 1 means that the two orders are exactly the same; rs=−1r_s = -1 that one is the reverse of the other. It answers "when xx grows, does yy grow?", without asking for a straight line. For y=x3y = x^3 with positive and negative xx, Pearson's rr is below 1, and rs=1r_s = 1.

When values are tied, each of them gets the mean of the ranks they share (two values tied for places 2 and 3 both get 2.5), and rsr_s is calculated as Pearson's rr of the ranks.

Try it yourself

Four runners are ranked in two races. First race: A 1, B 2, C 3, D 4. Second race: A 1, B 3, C 2, D 4. Find ∑d2\sum d^2 and rsr_s.

Show a hint
Only B and C change places, each by one rank.
Show the solution

Answer: ∑d2=2\sum d^2 = 2, rs=0.8r_s = 0.8.

d ⁣: 0, −1, 1, 0∑d2=2rs=1−6×24×15=1−1260=0.8\begin{aligned} & d\colon\ 0,\ -1,\ 1,\ 0 \qquad \sum d^2 = 2 \\[4pt] & r_s = 1 - \frac{6 \times 2}{4 \times 15} = 1 - \frac{12}{60} = 0.8 \end{aligned}

The two races give almost the same order.

16A first look at the regression line

A strong correlation says that the points lie close to a line. Which line? The regression line (the least-squares line) is the one for which the sum of the squared vertical distances of the points is smallest. Topic 22 derives it; its result is short, and it is built from the numbers of this lesson:

y^=a+b×xb=r×sysx=∑(x−xˉ)(y−yˉ)∑(x−xˉ)2a=yˉ−b×xˉ\begin{aligned} \hat{y} &= a + b \times x \\[6pt] b &= r \times \frac{s_y}{s_x} = \frac{\sum (x - \bar{x})(y - \bar{y})}{\sum (x - \bar{x})^2} \\[6pt] a &= \bar{y} - b \times \bar{x} \end{aligned}

For the five students:

b=10520=5.25a=66−5.25×5=39.75y^=39.75+5.25×x\begin{aligned} b &= \frac{105}{20} = 5.25 \\[4pt] a &= 66 - 5.25 \times 5 = 39.75 \\[4pt] \hat{y} &= 39.75 + 5.25 \times x \end{aligned}

Every extra hour of study goes with 5.25 more marks on average. A student who studies 7 hours is predicted to get 39.75+5.25×7=76.539.75 + 5.25 \times 7 = 76.5.

246850607080hoursmark
The five students with their regression line. The line passes through the point of the means, 5 hours and 66 marks. For 7 hours it predicts 76.5 marks, the point in the second colour.

Three things to remember until Topic 22:

  • The slope has the sign of rr, but its size depends on the units; rr does not.
  • The line always passes through the point of the means (xˉ,yˉ)(\bar{x}, \bar{y}).
  • Predict only inside the range of the data (here 2 to 8 hours). The line says nothing reliable about 20 hours.

Try it yourself

For the three shops (x=1,2,3x = 1, 2, 3 thousand manat of advertising, y=2,4,9y = 2, 4, 9 thousand manat of sales) the sum of the products is 7 and the sum of squares of xx is 2; the means are 2 and 5. Find the slope and the intercept of the regression line, and the sales it predicts for x=2.5x = 2.5.

Show a hint
bb is the sum of the products divided by the sum of squares of xx; a=yˉ−b×xˉa = \bar{y} - b \times \bar{x}.
Show the solution

Answer: b=3.5b = 3.5, a=−2a = -2, prediction 6.75.

b=72=3.5a=5−3.5×2=−2y^=−2+3.5×2.5=6.75\begin{aligned} b &= \frac{7}{2} = 3.5 \\[4pt] a &= 5 - 3.5 \times 2 = -2 \\[4pt] \hat{y} &= -2 + 3.5 \times 2.5 = 6.75 \end{aligned}

Each extra thousand manat of advertising goes with 3.5 thousand manat more sales. The intercept −2-2 would be the "sales without advertising", which makes no sense: 0 lies outside the range of the data.

17Intervals and correlation in machine learning

An accuracy needs an interval. A model that is right on 850 of 1,000 test rows has the accuracy 0.85. This is a sample proportion, so its 95 percent interval is 0.85±1.960×0.85×0.151000≈0.85±0.0220.85 \pm 1.960 \times \sqrt{\dfrac{0.85 \times 0.15}{1000}} \approx 0.85 \pm 0.022. On a test set of only 100 rows the margin would be 0.07: two models with 0.85 and 0.88 could not be told apart.

Cross-validation gives a mean and an interval. The scores of the folds are a small sample: report their mean with ± t∗×sk\pm\, t^* \times \dfrac{s}{\sqrt{k}}, not a single number.

The bootstrap. When no formula exists (for a median, an F1-score, an AUC), the computer imitates the taking of new samples: it draws many samples with replacement from the data itself, calculates the statistic for each, and takes the middle 95 percent of the results as the interval. The idea is the same as in Part 4, done by simulation.

The correlation matrix. The first look at a new data set is the table of the correlations of all pairs of numerical columns, drawn as a heatmap (Topic 12). It shows which features go with the target, and which features go with each other.

Feature selection. A feature with a correlation near 0 with the target carries little linear information. Careful: a feature with r=0r = 0 can still be useful to a model that handles curves (Trap 1).

Multicollinearity. Two features with r=0.95r = 0.95 tell almost the same story. A linear model cannot decide how to share the effect between them, and its coefficients become unstable. One of them can usually be dropped (Topic 26).

Correlation is a cosine. Subtract the mean from each column, and rr is the cosine of the angle between the two centred vectors (Topic 13): r=a⋅b∣a∣×∣b∣r = \dfrac{\va \cdot \vb}{\len{\va} \times \len{\vb}}. The cosine similarity of embeddings later in the course is the same idea without the centring.

Correlation is not causation, in models too. A model learns what goes together, not what causes what. A feature that predicts well may be a consequence of the target, or a companion of the real cause.

Try it yourself

A classifier is right on 90 of 100 test rows. (a) Find the margin of error of a 95 percent confidence interval for its accuracy. (b) Find the interval. (c) A second model has the accuracy 0.93 on a test set of the same size. Can you say that it is clearly better?

Show a hint
An accuracy is a sample proportion: SE=0.9×0.1100\SE = \sqrt{\dfrac{0.9 \times 0.1}{100}}.
Show the solution

Answer: (a) 0.05880.0588; (b) (0.8412, 0.9588)(0.8412,\ 0.9588); (c) no.

SE=0.9×0.1100=0.0009=0.03E=1.960×0.03=0.0588interval: 0.90±0.0588=(0.8412, 0.9588)\begin{aligned} & \SE = \sqrt{\frac{0.9 \times 0.1}{100}} = \sqrt{0.0009} = 0.03 \\[4pt] & E = 1.960 \times 0.03 = 0.0588 \\[4pt] & \text{interval: } 0.90 \pm 0.0588 = (0.8412,\ 0.9588) \end{aligned}

With 100 rows the accuracy is known only to within about 6 points. The value 0.93 lies well inside the interval: the test set is too small to tell the two models apart.

18Common mistakes

Saying "there is a 95 percent probability that the mean is in this interval". The method is right 95 times in 100; this interval either contains the mean or does not.

Reading the interval as the range of the data. It is about the mean. Single values spread much more.

Using z∗z^* with a small sample and an estimated ss. That needs t∗t^* with df=n−1df = n - 1.

Taking the wrong column of the t table. 95 percent uses the column 0.025; 90 percent the column 0.05.

Forgetting n\sqrt{n}. The margin of error uses the standard error, not the standard deviation.

Rounding a sample size down. Always round up.

Pooling the proportions in an interval. Pooling belongs to the test; the interval uses the two separate proportions.

Believing that a wider interval is "more accurate". Higher confidence gives a wider interval, which says less.

Reading r=0r = 0 as "no relationship". It means no linear relationship. Look at the picture.

Reading correlation as cause. A lurking variable may drive both.

Reading rr as a slope or as a percentage. r=0.8r = 0.8 does not mean "80 percent" and not "y rises by 0.8". The share explained is r2r^2.

Forgetting outliers. One point can make or break a correlation.

Mixing up covariance and correlation. The covariance has units and no fixed range; rr has neither unit nor a value outside −1-1 to 11.

Dividing by the wrong thing. rr divides the sum of products by the square root of the product of the two sums of squares, not by their sum.

Common mistakes and what is right
WrongRight
"the mean is in (48, 52)(48,\ 52) with probability 0.95""we are 95 percent confident: the method catches the mean in 95 of 100 samples"
"95 percent of the bags weigh between 492 and 500 g""the mean weight of all bags is between 492 and 500 g"
xˉ±1.960×s\bar{x} \pm 1.960 \times sxˉ±t∗×sn\bar{x} \pm t^* \times \dfrac{s}{\sqrt{n}}
95 percent, df=15df = 15: t∗=1.753t^* = 1.753column 0.025: t∗=2.131t^* = 2.131
n=96.04n = 96.04, so 96round up: 97
r=0r = 0: "x and y are unrelated""no linear relationship; look at the scatter plot"
r=0.9r = 0.9: "x causes y""x and y go together; the cause needs an experiment"
r=0.5r = 0.5: "half of the variation is explained"r2=0.25r^2 = 0.25: a quarter
r=1.3r = 1.3impossible: check the calculation
r=10520+570r = \dfrac{105}{20 + 570}r=10520×570r = \dfrac{105}{\sqrt{20 \times 570}}

19Solved 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 confidence level is 95 percent. Give the ends of an interval for a mean with two decimals, and for a proportion with four.

Level 1: Intervals with z, proportions and sample sizes

Problem 1: Estimate and margin from an interval

A report gives the 95 percent confidence interval (48, 56)(48,\ 56) for a mean. What is the point estimate, and what is the margin of error?

Idea. An interval is "estimate ± margin": the estimate is the middle, the margin is half the width (Part 2).

estimate=48+562=52E=56−482=4\begin{aligned} \text{estimate} &= \frac{48 + 56}{2} = 52 \\[4pt] E &= \frac{56 - 48}{2} = 4 \end{aligned}

Why: the interval is always symmetric around the sample mean. From its two ends everything else can be recovered.

Problem 2: A z interval

The weights of a kind of fish have a known standard deviation of 8 g. A sample of 64 fish has the mean 50 g. Find the 95 percent confidence interval for the mean weight.

Idea. σ\sigma is known: xˉ±z∗×σn\bar{x} \pm z^* \times \dfrac{\sigma}{\sqrt{n}} with z∗=1.960z^* = 1.960 (Part 3).

SE=864=1E=1.960×1=1.96interval:50±1.96=(48.04, 51.96)\begin{aligned} \SE &= \frac{8}{\sqrt{64}} = 1 \\[4pt] E &= 1.960 \times 1 = 1.96 \\[4pt] \text{interval:}\quad & 50 \pm 1.96 = (48.04,\ 51.96) \end{aligned}

Why: the sample mean of 64 fish varies with a standard error of only 1 g, so 50 cannot be far from the truth: at most about 2 g, in 95 of 100 samples.

Problem 3: The same data, more confidence

Find the 99 percent confidence interval for the fish of Problem 2. Is it wider or narrower?

Idea. Only z∗z^* changes: 2.576 for 99 percent (Part 3).

E=2.576×1≈2.58interval:50±2.58=(47.42, 52.58)\begin{aligned} E &= 2.576 \times 1 \approx 2.58 \\[4pt] \text{interval:}\quad & 50 \pm 2.58 = (47.42,\ 52.58) \end{aligned}

Why: it is wider. To be right more often, the net must be larger. The data has not changed, only the promise.

Problem 4: Reading an interval

A 95 percent confidence interval for the mean delivery time is (29, 33)(29,\ 33) minutes. Which statements are right? (a) The sample mean is 31 minutes. (b) 95 percent of all deliveries take between 29 and 33 minutes. (c) If the study were repeated many times, about 95 percent of the intervals would contain the true mean. (d) A two-tailed test at 5 percent would reject H0 ⁣:μ=35H_0\colon \mu = 35.

Idea. The meaning of the confidence level (Part 4) and the link with tests (Part 9).

  • (a) Right: the estimate is the middle of the interval.
  • (b) Wrong: the interval is about the mean, not about single deliveries.
  • (c) Right: this is what "95 percent confident" means.
  • (d) Right: 35 lies outside the interval.

Why: (b) is the most common misreading. Single deliveries vary with the standard deviation ss; the mean varies with sn\dfrac{s}{\sqrt{n}}, which is much smaller.

Problem 5: The effect of the sample size

A measurement has σ=12\sigma = 12. Find the margin of error of a 95 percent interval for n=36n = 36 and for n=144n = 144.

Idea. The margin shrinks with n\sqrt{n} (Part 7).

n=36 ⁣:1.960×1236=1.960×2=3.92n=144 ⁣:1.960×12144=1.960×1=1.96\begin{aligned} n = 36\colon\quad & 1.960 \times \frac{12}{\sqrt{36}} = 1.960 \times 2 = 3.92 \\[4pt] n = 144\colon\quad & 1.960 \times \frac{12}{\sqrt{144}} = 1.960 \times 1 = 1.96 \end{aligned}

Why: four times the sample gives half the margin, not a quarter. Precision is expensive: every halving of the margin costs four times the data.

Problem 6: The sample size for a mean

The time that customers spend in a shop has a standard deviation of about 15 minutes. How many customers must be observed to estimate the mean time within 3 minutes, with 95 percent confidence?

Idea. n=(z∗×σE)2n = \left(\dfrac{z^* \times \sigma}{E}\right)^2, rounded up (Part 7).

n=(1.960×153)2=9.82=96.04round up: n=97\begin{aligned} n &= \left(\frac{1.960 \times 15}{3}\right)^2 = 9.8^2 = 96.04 \\[4pt] & \text{round up: } n = 97 \end{aligned}

Why: with 96 customers the margin would be a little more than 3 minutes. Rounding up keeps the promise.

Problem 7: An interval for a proportion

Of 600 customers of an online shop, 240 used a discount code. Find the 95 percent confidence interval for the share of all customers who use a code.

Idea. p^±z∗×p^(1−p^)n\hat{p} \pm z^* \times \sqrt{\dfrac{\hat{p}(1 - \hat{p})}{n}} (Part 6).

p^=240600=0.40SE=0.4×0.6600=0.0004=0.02E=1.960×0.02=0.0392interval: 0.40±0.0392=(0.3608, 0.4392)\begin{aligned} & \hat{p} = \frac{240}{600} = 0.40 \\[4pt] & \SE = \sqrt{\frac{0.4 \times 0.6}{600}} = \sqrt{0.0004} = 0.02 \\[4pt] & E = 1.960 \times 0.02 = 0.0392 \\[4pt] & \text{interval: } 0.40 \pm 0.0392 = (0.3608,\ 0.4392) \end{aligned}

Why: between about 36 and 44 percent of all customers use a code. The condition holds: 240 successes and 360 failures, both far above 10.

Problem 8: The sample size for a poll

A poll should estimate the share of voters for a party within 3 percentage points, with 95 percent confidence. Nothing is known about the share. How many people must be asked?

Idea. With no earlier estimate, use p=0.5p = 0.5: n=(z∗)2×0.25E2n = \dfrac{(z^*)^2 \times 0.25}{E^2} (Part 7).

n=1.9602×0.250.032=3.8416×0.250.0009≈1067.11round up: n=1068\begin{aligned} n &= \frac{1.960^2 \times 0.25}{0.03^2} = \frac{3.8416 \times 0.25}{0.0009} \approx 1067.11 \\[4pt] & \text{round up: } n = 1068 \end{aligned}

Why: p=0.5p = 0.5 makes p×(1−p)p \times (1 - p) as large as it can be, so 1,068 people are enough whatever the true share is. This is why so many polls have about a thousand participants.

Problem 9: From an interval to a test

A 95 percent confidence interval for a mean is (48.04, 51.96)(48.04,\ 51.96) (Problem 2). Without a new calculation: would a two-tailed test at α=0.05\alpha = 0.05 reject (a) H0 ⁣:μ=53H_0\colon \mu = 53, (b) H0 ⁣:μ=51H_0\colon \mu = 51?

Idea. A value outside the 95 percent interval is rejected at 5 percent; a value inside is not (Part 9).

  • (a) 53 is outside the interval: reject H0H_0.
  • (b) 51 is inside the interval: fail to reject H0H_0.

Why: the interval is the list of all values of μ\mu that the data does not contradict. One interval answers infinitely many tests.

Level 2: Intervals with t, and differences

Problem 10: Finding t

Find t∗t^* for (a) a 95 percent interval from a sample of 10 values, (b) a 99 percent interval from 25 values, (c) a 90 percent interval from 16 values.

Idea. df=n−1df = n - 1; the column is half of what is left outside the interval (Part 5).

Where the three critical values stand in the t table
dfdfColumnt∗t^*
(a)90.0252.262
(b)240.0052.797
(c)150.051.753

Why: a 95 percent interval leaves 5 percent outside, 2.5 percent in each tail, and the table is headed by the area of one tail.

Problem 11: A t interval from summary numbers

A sample of 16 rods has the mean 52.5 cm and the standard deviation 4 cm. Find the 95 percent confidence interval for the mean length.

Idea. σ\sigma is unknown: xˉ±t∗×sn\bar{x} \pm t^* \times \dfrac{s}{\sqrt{n}} with df=15df = 15 (Part 5).

SE=416=1t∗ (column 0.025, row 15)=2.131E=2.131×1≈2.13interval: 52.5±2.13=(50.37, 54.63)\begin{aligned} & \SE = \frac{4}{\sqrt{16}} = 1 \\[4pt] & t^* \text{ (column 0.025, row 15)} = 2.131 \\[4pt] & E = 2.131 \times 1 \approx 2.13 \\[4pt] & \text{interval: } 52.5 \pm 2.13 = (50.37,\ 54.63) \end{aligned}

Why: the machine should cut 50 cm, and 50 is outside the interval, in agreement with the t-test of the last lesson (t=2.5t = 2.5, rejected). The interval adds that the rods are between 0.37 and 4.63 cm too long on average.

Problem 12: A t interval from raw data

Five customers waited 10, 14, 12, 16 and 13 minutes. Find the 95 percent confidence interval for the mean waiting time.

Idea. First xˉ\bar{x} and ss (Topic 18), then the t interval (Part 5).

The five waiting times, their deviations from the mean 13 and the squares
xx1014121613Sum
x−xˉx - \bar{x}−3-31−1-1300
(x−xˉ)2(x - \bar{x})^29119020
xˉ=655=13s2=204=5s≈2.2361SE=2.23615=1df=4t∗ (column 0.025, row 4)=2.776E=2.776×1≈2.78interval: 13±2.78=(10.22, 15.78)\begin{aligned} & \bar{x} = \frac{65}{5} = 13 \\[4pt] & s^2 = \frac{20}{4} = 5 \qquad s \approx 2.2361 \\[4pt] & \SE = \frac{2.2361}{\sqrt{5}} = 1 \qquad df = 4 \\[4pt] & t^* \text{ (column 0.025, row 4)} = 2.776 \\[4pt] & E = 2.776 \times 1 \approx 2.78 \\[4pt] & \text{interval: } 13 \pm 2.78 = (10.22,\ 15.78) \end{aligned}

Why: with only five values t∗t^* is large (2.776 instead of 1.960), and the interval is wide: from about 10 to 16 minutes. A small sample gives an honest but vague answer.

Problem 13: An interval for paired differences

Six employees take a typing test before and after a course. The improvements (after minus before, in words a minute) are 2, 5, 3, 6, 4, 4. Find the 95 percent confidence interval for the mean improvement.

Idea. A t interval on the differences, df=n−1df = n - 1 (Part 8).

The six improvements, their deviations from the mean 4 and the squares
dd253644
d−dˉd - \bar{d}−2-21−1-1200
(d−dˉ)2(d - \bar{d})^2411400
dˉ=246=4sum of the squares: 10sd2=105=2SE=26≈0.5774df=5t∗ (column 0.025, row 5)=2.571E=2.571×0.5774≈1.48interval: 4±1.48=(2.52, 5.48)\begin{aligned} & \bar{d} = \frac{24}{6} = 4 \qquad \text{sum of the squares: } 10 \\[4pt] & s_d^2 = \frac{10}{5} = 2 \\[4pt] & \SE = \sqrt{\frac{2}{6}} \approx 0.5774 \qquad df = 5 \\[4pt] & t^* \text{ (column 0.025, row 5)} = 2.571 \\[4pt] & E = 2.571 \times 0.5774 \approx 1.48 \\[4pt] & \text{interval: } 4 \pm 1.48 = (2.52,\ 5.48) \end{aligned}

Why: the whole interval lies above 0, so the course really improves the speed, by between about 2.5 and 5.5 words a minute.

Problem 14: An interval for the difference of two means

Two classes of 12 students each write the same test. Class A: mean 65, standard deviation 5. Class B: mean 61, standard deviation 5. Find the 95 percent confidence interval for the difference of the means. Is the difference significant?

Idea. (xˉ1−xˉ2)±t∗×sp×1n1+1n2(\bar{x}_1 - \bar{x}_2) \pm t^* \times s_p \times \sqrt{\dfrac{1}{n_1} + \dfrac{1}{n_2}} with df=n1+n2−2df = n_1 + n_2 - 2 (Part 8), then look for 0 (Part 9).

sp=5SE=5×112+112≈2.041df=22t∗ (column 0.025)=2.074E=2.074×2.041≈4.23interval: 4±4.23=(−0.23, 8.23)\begin{aligned} & s_p = 5 \qquad \SE = 5 \times \sqrt{\frac{1}{12} + \frac{1}{12}} \approx 2.041 \\[4pt] & df = 22 \qquad t^* \text{ (column 0.025)} = 2.074 \\[4pt] & E = 2.074 \times 2.041 \approx 4.23 \\[4pt] & \text{interval: } 4 \pm 4.23 = (-0.23,\ 8.23) \end{aligned}

The interval contains 0: the difference is not significant at 5 percent.

Why: class A may be better by up to 8 marks, or class B by a quarter of a mark. "No difference" is still possible, as the t-test of the last lesson said (t≈1.960<2.074t \approx 1.960 < 2.074).

Problem 15: An interval for the difference of two proportions

Version A of an email is opened by 40 of 400 people, version B by 60 of 400. Find the 95 percent confidence interval for the difference of the opening rates (B minus A).

Idea. The two proportions are not pooled in an interval (Part 8).

p^1=0.10p^2=0.15difference=0.05SE=0.10×0.90400+0.15×0.85400SE=0.000225+0.00031875≈0.02332E=1.960×0.02332≈0.0457interval: 0.05±0.0457=(0.0043, 0.0957)\begin{aligned} & \hat{p}_1 = 0.10 \qquad \hat{p}_2 = 0.15 \qquad \text{difference} = 0.05 \\[4pt] & \SE = \sqrt{\frac{0.10 \times 0.90}{400} + \frac{0.15 \times 0.85}{400}} \\ & \phantom{\SE} = \sqrt{0.000225 + 0.00031875} \approx 0.02332 \\[4pt] & E = 1.960 \times 0.02332 \approx 0.0457 \\[4pt] & \text{interval: } 0.05 \pm 0.0457 \\ & \quad = (0.0043,\ 0.0957) \end{aligned}

Why: version B is opened more often by between 0.4 and 9.6 percentage points. Significant (0 is outside), but the size of the gain is very uncertain: it could be almost nothing or almost 10 points. More people are needed to know.

Problem 16: From one level to another

A study reports the 95 percent confidence interval (46, 54)(46,\ 54) for a mean, calculated with z∗z^*. Find the 99 percent interval from the same data.

Idea. Recover the standard error from the margin, then use the new z∗z^* (Parts 2 and 3).

estimate=50E=4SE=41.960≈2.0408new E=2.576×2.0408≈5.26interval: 50±5.26=(44.74, 55.26)\begin{aligned} & \text{estimate} = 50 \qquad E = 4 \\[4pt] & \SE = \frac{4}{1.960} \approx 2.0408 \\[4pt] & \text{new } E = 2.576 \times 2.0408 \approx 5.26 \\[4pt] & \text{interval: } 50 \pm 5.26 = (44.74,\ 55.26) \end{aligned}

Why: the margins of two levels are in the ratio of their critical values: 2.5761.960≈1.31\dfrac{2.576}{1.960} \approx 1.31. A 99 percent interval is always about 31 percent wider than the 95 percent one.

Problem 17: The accuracy of a model

A classifier is right on 850 of 1,000 rows of a test set. Find the 95 percent confidence interval for its accuracy.

Idea. An accuracy is a sample proportion (Parts 6 and 17).

p^=0.85SE=0.85×0.151000=0.0001275≈0.01129E=1.960×0.01129≈0.0221interval: 0.85±0.0221=(0.8279, 0.8721)\begin{aligned} & \hat{p} = 0.85 \\[4pt] & \SE = \sqrt{\frac{0.85 \times 0.15}{1000}} = \sqrt{0.0001275} \approx 0.01129 \\[4pt] & E = 1.960 \times 0.01129 \approx 0.0221 \\[4pt] & \text{interval: } 0.85 \pm 0.0221 = (0.8279,\ 0.8721) \end{aligned}

Why: "accuracy 0.85" really means "somewhere between 0.83 and 0.87". A second model with 0.86 on the same test set is not clearly better.

Level 3: Covariance, correlation and judgement

Problem 18: The sign of a correlation

Would you expect a positive, a negative or no correlation? (a) The age of a car and its price. (b) The height and the weight of adults. (c) The last digit of a phone number and the monthly bill. (d) The hours a student plays video games the day before an exam and the mark.

Idea. Ask: when one value is above its mean, is the other usually above or below its mean (Parts 10 and 11)?

  • (a) Negative: older cars are cheaper.
  • (b) Positive: taller people are heavier on average.
  • (c) None: the two have nothing to do with each other.
  • (d) Negative: probably, and the data must show how strongly.

Why: the sign can often be guessed. The strength cannot: that needs the data.

Problem 19: A covariance by hand

Find the covariance of x=1,2,3,4,5x = 1, 2, 3, 4, 5 and y=2,4,5,4,5y = 2, 4, 5, 4, 5.

Idea. Deviations, products, sum, divide by n−1n - 1 (Part 11).

The deviations of x and y from their means 3 and 4, and their products
Sum
x−xˉx - \bar{x}−2-2−1-10120
y−yˉy - \bar{y}−2-201010
product400026
xˉ=3yˉ=205=4sxy=64=1.5\begin{aligned} & \bar{x} = 3 \qquad \bar{y} = \frac{20}{5} = 4 \\[4pt] & s_{xy} = \frac{6}{4} = 1.5 \end{aligned}

Why: the covariance is positive: yy tends to rise with xx. Whether 1.5 is much cannot be said without the standard deviations.

Problem 20: The correlation coefficient by hand

Find rr for the data of Problem 19.

Idea. r=∑(x−xˉ)(y−yˉ)∑(x−xˉ)2×∑(y−yˉ)2r = \dfrac{\sum (x - \bar{x})(y - \bar{y})}{\sqrt{\sum (x - \bar{x})^2 \times \sum (y - \bar{y})^2}} (Part 12).

∑(x−xˉ)2=4+1+0+1+4=10∑(y−yˉ)2=4+0+1+0+1=6r=610×6=660≈67.7460≈0.7746\begin{aligned} \sum (x - \bar{x})^2 &= 4 + 1 + 0 + 1 + 4 = 10 \\[4pt] \sum (y - \bar{y})^2 &= 4 + 0 + 1 + 0 + 1 = 6 \\[4pt] r &= \frac{6}{\sqrt{10 \times 6}} = \frac{6}{\sqrt{60}} \\[4pt] &\approx \frac{6}{7.7460} \approx 0.7746 \end{aligned}

Why: a strong positive relationship, but not a perfect line: the fourth value of yy falls back. r2≈0.6r^2 \approx 0.6: about 60 percent of the variation of yy goes with xx.

Problem 21: A negative correlation

A shop tries the prices x=1,2,3,4,5x = 1, 2, 3, 4, 5 for a product and counts the sales in a week: y=10,8,7,5,5y = 10, 8, 7, 5, 5. Find rr.

Idea. The same steps; the products will be negative (Part 12).

The deviations of the prices and of the sales from their means 3 and 7
Sum
x−xˉx - \bar{x}−2-2−1-10120
y−yˉy - \bar{y}310−2-2−2-20
product−6-6−1-10−2-2−4-4−13-13
(y−yˉ)2(y - \bar{y})^29104418
xˉ=3yˉ=355=7∑(x−xˉ)2=10r=−1310×18=−13180r≈−1313.4164≈−0.9690\begin{aligned} & \bar{x} = 3 \qquad \bar{y} = \frac{35}{5} = 7 \qquad \sum (x - \bar{x})^2 = 10 \\[4pt] & r = \frac{-13}{\sqrt{10 \times 18}} = \frac{-13}{\sqrt{180}} \\ & \phantom{r} \approx \frac{-13}{13.4164} \approx -0.9690 \end{aligned}

Why: a very strong negative relationship: a higher price goes with fewer sales, almost on a falling line. Here the shop set the prices itself, so this is an experiment, and the price can be called the cause.

Problem 22: r from summary numbers, and the regression line

For 50 flats, the size xx (in m²) has the mean 70 and the standard deviation 20; the rent yy (in manat) has the mean 600 and the standard deviation 150. The covariance is 2,400. Find rr, r2r^2 and the regression line. Predict the rent of a flat of 90 m².

Idea. r=sxysx×syr = \dfrac{s_{xy}}{s_x \times s_y} (Part 12); b=r×sysxb = r \times \dfrac{s_y}{s_x}, a=yˉ−b×xˉa = \bar{y} - b \times \bar{x} (Part 16).

r=240020×150=24003000=0.8r2=0.64b=0.8×15020=6a=600−6×70=180y^=180+6×xx=90 ⁣:y^=180+6×90=720\begin{aligned} & r = \frac{2400}{20 \times 150} = \frac{2400}{3000} = 0.8 \qquad r^2 = 0.64 \\[4pt] & b = 0.8 \times \frac{150}{20} = 6 \\[4pt] & a = 600 - 6 \times 70 = 180 \\[4pt] & \hat{y} = 180 + 6 \times x \\[4pt] & x = 90\colon\quad \hat{y} = 180 + 6 \times 90 = 720 \end{aligned}

Why: the size explains 64 percent of the variation of the rents. Each extra square metre goes with 6 manat more rent on average. The other 36 percent come from things the size does not know: the district, the floor, the condition.

Problem 23: A change of units

The correlation between the temperature in degrees Celsius and the number of ice creams sold is r=0.85r = 0.85, and the covariance is 120. The temperatures are converted to Fahrenheit (F=1.8×C+32F = 1.8 \times C + 32). What are the new correlation and the new covariance?

Idea. rr does not change when a variable is shifted or rescaled; the covariance is multiplied by the factor (Parts 11 and 13).

r=0.85 (unchanged)covariance=1.8×120=216\begin{aligned} & r = 0.85 \text{ (unchanged)} \\[4pt] & \text{covariance} = 1.8 \times 120 = 216 \end{aligned}

Why: the shift of 32 disappears in the deviations. The factor 1.8 multiplies every deviation of the temperature, so it multiplies the covariance, and also sxs_x, and in rr the two cancel.

Problem 24: Is a correlation significant?

In a sample of 18 employees, the correlation between years of experience and salary is r=0.6r = 0.6. Is it significantly different from 0?

Idea. t=r×n−21−r2t = \dfrac{r \times \sqrt{n - 2}}{\sqrt{1 - r^2}} with df=n−2df = n - 2 (Part 14).

H0 ⁣:ρ=0H1 ⁣:ρ≠0t=0.6×161−0.36=0.6×40.64=2.40.8=3.0df=16column 0.025, row 16: 2.1203.0>2.120\begin{aligned} & H_0\colon \rho = 0 \qquad H_1\colon \rho \ne 0 \\[4pt] & t = \frac{0.6 \times \sqrt{16}}{\sqrt{1 - 0.36}} = \frac{0.6 \times 4}{\sqrt{0.64}} = \frac{2.4}{0.8} = 3.0 \\[4pt] & df = 16 \\[4pt] & \text{column 0.025, row 16: } 2.120 \\ & 3.0 > 2.120 \end{aligned}

Reject H0H_0. The correlation is significant.

Why: with 18 pairs, an rr of 0.6 is too large to be chance. It still explains only r2=0.36r^2 = 0.36 of the variation of the salaries.

Problem 25: Spearman's rank correlation

Five students are ranked by their results in mathematics and in physics. Find rsr_s.

The ranks of five students in mathematics and in physics
StudentABCDESum
Mathematics12345
Physics21354
dd−1-110−1-110
d2d^2110114

Idea. rs=1−6×∑d2n×(n2−1)r_s = 1 - \dfrac{6 \times \sum d^2}{n \times (n^2 - 1)} (Part 15).

rs=1−6×45×24=1−24120=0.8r_s = 1 - \frac{6 \times 4}{5 \times 24} = 1 - \frac{24}{120} = 0.8

Why: the two orders are almost the same; only neighbours change places. The differences dd always add up to 0, which is a useful check.

Problem 26: Three traps

(a) Find rr for x=−2,−1,0,1,2x = -2, -1, 0, 1, 2 and y=4,1,0,1,4y = 4, 1, 0, 1, 4. Are xx and yy related? (b) In a town, the number of fire engines sent to a fire correlates strongly with the damage of the fire. Should the town send fewer engines? (c) Among primary school children, shoe size correlates with reading skill. Explain.

Idea. rr measures only linear relationships, and correlation is not causation (Part 13).

xˉ=0yˉ=2products:(−2)(2)+(−1)(−1)+(0)(−2)+(1)(−1)+(2)(2)=−4+1+0−1+4=0r=0\begin{aligned} & \bar{x} = 0 \qquad \bar{y} = 2 \\[4pt] & \text{products:} \\ & \quad (-2)(2) + (-1)(-1) + (0)(-2) \\ & \quad + (1)(-1) + (2)(2) \\ & \quad = -4 + 1 + 0 - 1 + 4 = 0 \\[4pt] & r = 0 \end{aligned}

(a) r=0r = 0, and yet y=x2y = x^2 exactly: a perfect relationship that is not a line. (b) No. A third variable, the size of the fire, causes both: big fires get more engines and do more damage. (c) Age is the lurking variable: older children have larger feet and read better.

Why: a correlation coefficient is a summary of a picture. Without the picture and without thinking about where the data comes from, it misleads.

20Practice problems

Forty problems in three levels. Solve each one on paper, with the t table 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 confidence level is 95 percent. Give the ends of an interval for a mean with two decimals, for a proportion with four, and rr with four decimals.

Level 1: Intervals with z, proportions and sample sizes

  1. 1.A confidence interval for a mean is (20, 26)(20,\ 26). Find the point estimate and the margin of error.

    Show a hint
    The estimate is the middle of the interval.
    Show one possible solution

    Answer: Estimate 23, margin of error 3.

    estimate=20+262=23E=26−202=3\begin{aligned} \text{estimate} &= \frac{20 + 26}{2} = 23 \\[4pt] E &= \frac{26 - 20}{2} = 3 \end{aligned}
  2. 2.Give z∗z^* for a 90 percent, a 95 percent and a 99 percent confidence interval.

    Show a hint
    The table in Part 3.
    Show one possible solution

    Answer: 1.6451.645, 1.9601.960, 2.5762.576.

    90 percent leaves 5 percent in each tail: 1.6451.645. 95 percent leaves 2.5 percent: 1.9601.960. 99 percent leaves 0.5 percent: 2.5762.576.

  3. 3.A measurement has the known standard deviation 20. A sample of 25 values has the mean 120. Find the 95 percent confidence interval for the mean.

    Show a hint
    The standard error is 2025=4\dfrac{20}{\sqrt{25}} = 4.
    Show one possible solution

    Answer: (112.16, 127.84)(112.16,\ 127.84).

    SE=2025=4E=1.960×4=7.84interval:120±7.84=(112.16, 127.84)\begin{aligned} \SE &= \frac{20}{\sqrt{25}} = 4 \\[4pt] E &= 1.960 \times 4 = 7.84 \\[4pt] \text{interval:}\quad & 120 \pm 7.84 = (112.16,\ 127.84) \end{aligned}
  4. 4.Find the 90 percent confidence interval for the data of Problem 3.

    Show a hint
    Only z∗z^* changes.
    Show one possible solution

    Answer: (113.42, 126.58)(113.42,\ 126.58).

    E=1.645×4=6.58interval:120±6.58=(113.42, 126.58)\begin{aligned} E &= 1.645 \times 4 = 6.58 \\[4pt] \text{interval:}\quad & 120 \pm 6.58 = (113.42,\ 126.58) \end{aligned}
  5. 5.Find the 99 percent confidence interval for the data of Problem 3.

    Show a hint
    z∗=2.576z^* = 2.576.
    Show one possible solution

    Answer: (109.70, 130.30)(109.70,\ 130.30).

    E=2.576×4≈10.30interval:120±10.30=(109.70, 130.30)\begin{aligned} E &= 2.576 \times 4 \approx 10.30 \\[4pt] \text{interval:}\quad & 120 \pm 10.30 = (109.70,\ 130.30) \end{aligned}
  6. 6.From the same sample, a 90 percent and a 99 percent confidence interval are calculated. Which one is wider, and why?

    Show a hint
    Compare the critical values.
    Show one possible solution

    Answer: The 99 percent interval.

    The 99 percent interval uses z∗=2.576z^* = 2.576 instead of 1.645, so its margin of error is larger. To catch the true mean more often, the interval must be wider.

  7. 7.With a sample of 50 values, the margin of error of an interval is 6. What will it be, approximately, with a sample of 200 values from the same population?

    Show a hint
    The margin shrinks with n\sqrt{n}.
    Show one possible solution

    Answer: About 3.

    The sample is 4 times as large, so the standard error is divided by 4=2\sqrt{4} = 2:

    E=62=3E = \frac{6}{2} = 3
  8. 8.The standard deviation of a measurement is about 10. How many values are needed to estimate the mean within 2, with 95 percent confidence?

    Show a hint
    n=(z∗×σE)2n = \left(\dfrac{z^* \times \sigma}{E}\right)^2, rounded up.
    Show one possible solution

    Answer: 9797.

    n=(1.960×102)2=9.82=96.04round up: 97\begin{aligned} n &= \left(\frac{1.960 \times 10}{2}\right)^2 = 9.8^2 = 96.04 \\[4pt] & \text{round up: } 97 \end{aligned}
  9. 9.As Problem 8, but the mean should be known within 1.

    Show a hint
    Half the margin needs four times the sample.
    Show one possible solution

    Answer: 385385.

    n=(1.960×101)2=19.62=384.16round up: 385\begin{aligned} n &= \left(\frac{1.960 \times 10}{1}\right)^2 = 19.6^2 = 384.16 \\[4pt] & \text{round up: } 385 \end{aligned}

    That is four times the 96.04 of Problem 8.

  10. 10.Of 200 visitors of a website, 90 clicked on an offer. Find the 95 percent confidence interval for the share of all visitors who click.

    Show a hint
    p^=0.45\hat{p} = 0.45; the standard error is 0.45×0.55200\sqrt{\dfrac{0.45 \times 0.55}{200}}.
    Show one possible solution

    Answer: (0.3810, 0.5190)(0.3810,\ 0.5190).

    p^=90200=0.45SE=0.45×0.55200=0.0012375SE≈0.03518E=1.960×0.03518≈0.0690interval: 0.45±0.0690=(0.3810, 0.5190)\begin{aligned} & \hat{p} = \frac{90}{200} = 0.45 \\[4pt] & \SE = \sqrt{\frac{0.45 \times 0.55}{200}} = \sqrt{0.0012375} \\ & \phantom{\SE} \approx 0.03518 \\[4pt] & E = 1.960 \times 0.03518 \approx 0.0690 \\[4pt] & \text{interval: } 0.45 \pm 0.0690 = (0.3810,\ 0.5190) \end{aligned}
  11. 11.Of 400 customers, 320 are satisfied. Find the 99 percent confidence interval for the share of satisfied customers.

    Show a hint
    p^=0.8\hat{p} = 0.8, standard error 0.02, z∗=2.576z^* = 2.576.
    Show one possible solution

    Answer: (0.7485, 0.8515)(0.7485,\ 0.8515).

    p^=320400=0.80SE=0.8×0.2400=0.0004=0.02E=2.576×0.02≈0.0515interval: 0.80±0.0515=(0.7485, 0.8515)\begin{aligned} & \hat{p} = \frac{320}{400} = 0.80 \\[4pt] & \SE = \sqrt{\frac{0.8 \times 0.2}{400}} = \sqrt{0.0004} = 0.02 \\[4pt] & E = 2.576 \times 0.02 \approx 0.0515 \\[4pt] & \text{interval: } 0.80 \pm 0.0515 = (0.7485,\ 0.8515) \end{aligned}
  12. 12.How many people must a poll ask for a margin of error of 5 percentage points at 95 percent confidence, when nothing is known about the share?

    Show a hint
    Use p=0.5p = 0.5: n=(z∗)2×0.25E2n = \dfrac{(z^*)^2 \times 0.25}{E^2}.
    Show one possible solution

    Answer: 385385.

    n=1.9602×0.250.052=3.8416×0.250.0025=384.16round up: 385\begin{aligned} n &= \frac{1.960^2 \times 0.25}{0.05^2} = \frac{3.8416 \times 0.25}{0.0025} = 384.16 \\[4pt] & \text{round up: } 385 \end{aligned}
  13. 13.An earlier study found a share of about 20 percent. How many people are needed to estimate the share within 2 percentage points at 95 percent confidence?

    Show a hint
    Use p=0.2p = 0.2: n=(z∗)2×0.2×0.8E2n = \dfrac{(z^*)^2 \times 0.2 \times 0.8}{E^2}.
    Show one possible solution

    Answer: 15371537.

    n=1.9602×0.160.022=3.8416×0.160.0004=1536.64round up: 1537\begin{aligned} n &= \frac{1.960^2 \times 0.16}{0.02^2} = \frac{3.8416 \times 0.16}{0.0004} \\[4pt] &= 1536.64 \\[4pt] & \text{round up: } 1537 \end{aligned}

    With p=0.5p = 0.5 it would have been 2401: an earlier estimate saves people.

  14. 14.A 95 percent confidence interval for the mean weight of apples is (148, 156)(148,\ 156) g. True or false? (a) The sample mean is 152 g. (b) 95 percent of the apples weigh between 148 and 156 g. (c) A two-tailed test at 5 percent would not reject H0 ⁣:μ=150H_0\colon \mu = 150.

    Show a hint
    The interval is about the mean. A value inside it is not rejected.
    Show one possible solution

    Answer: (a) true, (b) false, (c) true.

    (a) The estimate is the middle: 148+1562=152\dfrac{148 + 156}{2} = 152. (b) False: the interval says where the mean of all apples lies, not where single apples lie. (c) 150 is inside the interval, so it is not rejected.

Level 2: Intervals with t, and differences

  1. 15.Find t∗t^* for a 95 percent confidence interval from a sample of 12 values.

    Show a hint
    df=11df = 11, column 0.025.
    Show one possible solution

    Answer: 2.2012.201.

    df=12−1=11df = 12 - 1 = 11. A 95 percent interval leaves 2.5 percent in each tail: column 0.025, row 11: 2.2012.201.

  2. 16.A sample of 25 values has the mean 83 and the standard deviation 10. Find the 95 percent confidence interval for the mean.

    Show a hint
    Standard error 2, df=24df = 24.
    Show one possible solution

    Answer: (78.87, 87.13)(78.87,\ 87.13).

    SE=1025=2t∗ (column 0.025, row 24)=2.064E=2.064×2≈4.13interval: 83±4.13=(78.87, 87.13)\begin{aligned} & \SE = \frac{10}{\sqrt{25}} = 2 \\[4pt] & t^* \text{ (column 0.025, row 24)} = 2.064 \\[4pt] & E = 2.064 \times 2 \approx 4.13 \\[4pt] & \text{interval: } 83 \pm 4.13 = (78.87,\ 87.13) \end{aligned}
  3. 17.Nine customers spent 31.5 manat on average, with a standard deviation of 3 manat. Find the 90 percent confidence interval for the mean.

    Show a hint
    90 percent: the column 0.05, row 8.
    Show one possible solution

    Answer: (29.64, 33.36)(29.64,\ 33.36).

    SE=39=1t∗ (column 0.05, row 8)=1.860E=1.860×1=1.86interval: 31.5±1.86=(29.64, 33.36)\begin{aligned} & \SE = \frac{3}{\sqrt{9}} = 1 \\[4pt] & t^* \text{ (column 0.05, row 8)} = 1.860 \\[4pt] & E = 1.860 \times 1 = 1.86 \\[4pt] & \text{interval: } 31.5 \pm 1.86 = (29.64,\ 33.36) \end{aligned}
  4. 18.Five students solved 7, 9, 8, 10 and 11 problems in an hour. Find the 95 percent confidence interval for the mean.

    Show a hint
    xˉ=9\bar{x} = 9, s2=2.5s^2 = 2.5, df=4df = 4.
    Show one possible solution

    Answer: (7.04, 10.96)(7.04,\ 10.96).

    xˉ=455=9squares of the deviations:4+0+1+1+4=10s2=104=2.5SE=2.55=0.5≈0.7071t∗ (column 0.025, row 4)=2.776E=2.776×0.7071≈1.96interval: 9±1.96=(7.04, 10.96)\begin{aligned} & \bar{x} = \frac{45}{5} = 9 \\[4pt] & \text{squares of the deviations:} \\ & \quad 4 + 0 + 1 + 1 + 4 = 10 \\[4pt] & s^2 = \frac{10}{4} = 2.5 \\[4pt] & \SE = \sqrt{\frac{2.5}{5}} = \sqrt{0.5} \approx 0.7071 \\[4pt] & t^* \text{ (column 0.025, row 4)} = 2.776 \\[4pt] & E = 2.776 \times 0.7071 \approx 1.96 \\[4pt] & \text{interval: } 9 \pm 1.96 = (7.04,\ 10.96) \end{aligned}
  5. 19.Find the 99 percent confidence interval for the data of Problem 16.

    Show a hint
    The column 0.005, row 24.
    Show one possible solution

    Answer: (77.41, 88.59)(77.41,\ 88.59).

    t∗ (column 0.005, row 24)=2.797E=2.797×2≈5.59interval: 83±5.59=(77.41, 88.59)\begin{aligned} & t^* \text{ (column 0.005, row 24)} = 2.797 \\[4pt] & E = 2.797 \times 2 \approx 5.59 \\[4pt] & \text{interval: } 83 \pm 5.59 = (77.41,\ 88.59) \end{aligned}
  6. 20.Six people learn a typing method. The changes in their speed are −1-1, 3, 2, 0, 4, 4 words a minute. Find the 95 percent confidence interval for the mean change. Does it contain 0?

    Show a hint
    dˉ=2\bar{d} = 2, sd2=4.4s_d^2 = 4.4, df=5df = 5.
    Show one possible solution

    Answer: (−0.20, 4.20)(-0.20,\ 4.20); yes.

    dˉ=126=2squares of the deviations:9+1+0+4+4+4=22sd2=225=4.4SE=4.46≈0.8563t∗ (column 0.025, row 5)=2.571E=2.571×0.8563≈2.20interval: 2±2.20=(−0.20, 4.20)\begin{aligned} & \bar{d} = \frac{12}{6} = 2 \\[4pt] & \text{squares of the deviations:} \\ & \quad 9 + 1 + 0 + 4 + 4 + 4 = 22 \\[4pt] & s_d^2 = \frac{22}{5} = 4.4 \\[4pt] & \SE = \sqrt{\frac{4.4}{6}} \approx 0.8563 \\[4pt] & t^* \text{ (column 0.025, row 5)} = 2.571 \\[4pt] & E = 2.571 \times 0.8563 \approx 2.20 \\[4pt] & \text{interval: } 2 \pm 2.20 = (-0.20,\ 4.20) \end{aligned}

    It contains 0: a two-tailed test at 5 percent would not reject "no change". (The one-tailed test of the last lesson did reject it: a one-tailed test at 5 percent belongs to a 90 percent interval.)

  7. 21.Sixteen students with a new method reach a mean of 30 points, sixteen others with the old method 26 points; both standard deviations are 4. Find the 95 percent confidence interval for the difference of the means.

    Show a hint
    sp=4s_p = 4, standard error 4×184 \times \sqrt{\dfrac{1}{8}}, df=30df = 30.
    Show one possible solution

    Answer: (1.11, 6.89)(1.11,\ 6.89).

    sp=4SE=4×116+116=4×18≈1.4142t∗ (column 0.025, row 30)=2.042E=2.042×1.4142≈2.89interval: 4±2.89=(1.11, 6.89)\begin{aligned} & s_p = 4 \\[4pt] & \SE = 4 \times \sqrt{\frac{1}{16} + \frac{1}{16}} = 4 \times \sqrt{\frac{1}{8}} \approx 1.4142 \\[4pt] & t^* \text{ (column 0.025, row 30)} = 2.042 \\[4pt] & E = 2.042 \times 1.4142 \approx 2.89 \\[4pt] & \text{interval: } 4 \pm 2.89 = (1.11,\ 6.89) \end{aligned}

    It does not contain 0: the new method is better, by between about 1 and 7 points.

  8. 22.Version A of a page: 60 buyers among 500 visitors. Version B: 90 among 500. Find the 95 percent confidence interval for the difference of the two shares (B minus A).

    Show a hint
    Do not pool: add p^(1−p^)n\dfrac{\hat{p}(1 - \hat{p})}{n} of both versions under the root.
    Show one possible solution

    Answer: (0.0159, 0.1041)(0.0159,\ 0.1041).

    p^1=0.12p^2=0.18difference=0.06SE=0.12×0.88500+0.18×0.82500SE=0.0002112+0.0002952≈0.02250E=1.960×0.02250≈0.0441interval: 0.06±0.0441=(0.0159, 0.1041)\begin{aligned} & \hat{p}_1 = 0.12 \qquad \hat{p}_2 = 0.18 \qquad \text{difference} = 0.06 \\[4pt] & \SE = \sqrt{\frac{0.12 \times 0.88}{500} + \frac{0.18 \times 0.82}{500}} \\ & \phantom{\SE} = \sqrt{0.0002112 + 0.0002952} \approx 0.02250 \\[4pt] & E = 1.960 \times 0.02250 \approx 0.0441 \\[4pt] & \text{interval: } 0.06 \pm 0.0441 \\ & \quad = (0.0159,\ 0.1041) \end{aligned}

    0 is outside the interval: version B sells better, by between about 1.6 and 10.4 percentage points.

  9. 23.A 95 percent confidence interval for the difference of two means is (2.1, 7.9)(2.1,\ 7.9). Is the difference significant at the 5 percent level?

    Show a hint
    Is 0 inside the interval?
    Show one possible solution

    Answer: Yes.

    0 is not inside the interval, so a two-tailed test at 5 percent rejects "no difference". The first mean is larger by between 2.1 and 7.9.

  10. 24.A 95 percent confidence interval for the difference of two means is (−1.2, 3.4)(-1.2,\ 3.4). Is the difference significant at the 5 percent level?

    Show a hint
    Is 0 inside the interval?
    Show one possible solution

    Answer: No.

    0 lies inside the interval, so "no difference" is not rejected. The first mean may be larger by up to 3.4 or smaller by up to 1.2.

  11. 25.A classifier is right on 180 of 200 test rows. Find the 95 percent confidence interval for its accuracy.

    Show a hint
    p^=0.9\hat{p} = 0.9; the standard error is 0.9×0.1200\sqrt{\dfrac{0.9 \times 0.1}{200}}.
    Show one possible solution

    Answer: (0.8584, 0.9416)(0.8584,\ 0.9416).

    p^=180200=0.90SE=0.09200=0.00045≈0.02121E=1.960×0.02121≈0.0416interval: 0.90±0.0416=(0.8584, 0.9416)\begin{aligned} & \hat{p} = \frac{180}{200} = 0.90 \\[4pt] & \SE = \sqrt{\frac{0.09}{200}} = \sqrt{0.00045} \approx 0.02121 \\[4pt] & E = 1.960 \times 0.02121 \approx 0.0416 \\[4pt] & \text{interval: } 0.90 \pm 0.0416 = (0.8584,\ 0.9416) \end{aligned}
  12. 26.A second classifier has the accuracy 0.88 on the same kind of test set of 200 rows. Looking at the interval of Problem 25, can you say that the first one is clearly better?

    Show a hint
    Is 0.88 inside the interval of the first model?
    Show one possible solution

    Answer: No.

    The interval of the first model reaches from about 0.858 to 0.942, and 0.88 lies inside it. With 200 test rows a difference of 2 points is well within the uncertainty. A larger test set, or a paired comparison on the same rows, is needed.

  13. 27.A poll asks 1,000 people. What is its largest possible margin of error at 95 percent confidence?

    Show a hint
    The margin is largest for p^=0.5\hat{p} = 0.5.
    Show one possible solution

    Answer: About 0.0310.031, 3.1 percentage points.

    E=1.960×0.5×0.51000=1.960×0.00025≈1.960×0.01581≈0.031\begin{aligned} E &= 1.960 \times \sqrt{\frac{0.5 \times 0.5}{1000}} \\[4pt] &= 1.960 \times \sqrt{0.00025} \\[4pt] &\approx 1.960 \times 0.01581 \approx 0.031 \end{aligned}
  14. 28.A study has a margin of error of 4. The researcher wants a margin of 1 at the same confidence level. By which factor must the sample grow?

    Show a hint
    The margin is divided by the square root of the factor.
    Show one possible solution

    Answer: 16 times.

    The margin must be divided by 4, and it shrinks with the square root of the sample size, so the sample must be 42=164^2 = 16 times as large.

Level 3: Covariance, correlation and judgement

  1. 29.Positive, negative or no correlation? (a) The outside temperature and the cost of heating a house. (b) The number of pages of a book and its weight. (c) The house number and the height of the person who lives there.

    Show a hint
    When one is above its mean, where is the other?
    Show one possible solution

    Answer: (a) negative, (b) positive, (c) none.

    (a) The warmer it is, the less heating is needed: negative. (b) More pages, more weight: positive. (c) The two have nothing to do with each other: no correlation.

  2. 30.Find the covariance of x=2,4,6,8x = 2, 4, 6, 8 and y=1,3,2,6y = 1, 3, 2, 6.

    Show a hint
    The means are 5 and 3. Divide the sum of the products by 3.
    Show one possible solution

    Answer: About 4.6674.667.

    The deviations of x and y from their means 5 and 3, and their products
    Sum
    x−xˉx - \bar{x}−3-3−1-1130
    y−yˉy - \bar{y}−2-20−1-130
    product60−1-1914
    sxy=143≈4.667s_{xy} = \frac{14}{3} \approx 4.667
  3. 31.Find rr for the data of Problem 30.

    Show a hint
    The sums of squares are 20 and 14.
    Show one possible solution

    Answer: About 0.83670.8367.

    ∑(x−xˉ)2=9+1+1+9=20∑(y−yˉ)2=4+0+1+9=14r=1420×14=14280≈1416.7332≈0.8367\begin{aligned} \sum (x - \bar{x})^2 &= 9 + 1 + 1 + 9 = 20 \\[4pt] \sum (y - \bar{y})^2 &= 4 + 0 + 1 + 9 = 14 \\[4pt] r &= \frac{14}{\sqrt{20 \times 14}} = \frac{14}{\sqrt{280}} \\[4pt] &\approx \frac{14}{16.7332} \approx 0.8367 \end{aligned}
  4. 32.Five weeks after an advertising campaign ended, the sales fell: for the weeks x=1,2,3,4,5x = 1, 2, 3, 4, 5 the sales were y=10,9,6,4,1y = 10, 9, 6, 4, 1. Find rr.

    Show a hint
    yˉ=6\bar{y} = 6. The products are negative.
    Show one possible solution

    Answer: About −0.9898-0.9898.

    The deviations of the weeks and of the sales from their means 3 and 6
    Sum
    x−xˉx - \bar{x}−2-2−1-10120
    y−yˉy - \bar{y}430−2-2−5-50
    product−8-8−3-30−2-2−10-10−23-23
    ∑(x−xˉ)2=10∑(y−yˉ)2=16+9+0+4+25=54r=−2310×54=−23540≈−2323.2379≈−0.9898\begin{aligned} \sum (x - \bar{x})^2 &= 10 \\[4pt] \sum (y - \bar{y})^2 &= 16 + 9 + 0 + 4 + 25 = 54 \\[4pt] r &= \frac{-23}{\sqrt{10 \times 54}} = \frac{-23}{\sqrt{540}} \\[4pt] &\approx \frac{-23}{23.2379} \approx -0.9898 \end{aligned}
  5. 33.Two variables have the covariance −18-18 and the standard deviations 6 and 5. Find rr and r2r^2.

    Show a hint
    r=sxysx×syr = \dfrac{s_{xy}}{s_x \times s_y}.
    Show one possible solution

    Answer: r=−0.6r = -0.6, r2=0.36r^2 = 0.36.

    r=−186×5=−1830=−0.6r2=0.36r = \frac{-18}{6 \times 5} = \frac{-18}{30} = -0.6 \qquad r^2 = 0.36

    36 percent of the variation of one variable goes with the other.

  6. 34.The correlation between the size of a flat and its rent is r=0.9r = 0.9. Which share of the variation of the rents is explained by the size?

    Show a hint
    The share is r2r^2.
    Show one possible solution

    Answer: 81 percent.

    r2=0.92=0.81r^2 = 0.9^2 = 0.81
  7. 35.The correlation between the height of people in centimetres and their weight in kilograms is 0.7. What is the correlation when the height is measured in metres and the weight in grams?

    Show a hint
    What does rescaling do to rr?
    Show one possible solution

    Answer: 0.70.7.

    Changing the units multiplies each variable by a positive factor, which does not change rr. It stays 0.7.

  8. 36.In a sample of 27 shops, the correlation between the size of the shop and its sales is r=0.5r = 0.5. Is it significantly different from 0?

    Show a hint
    t=r×n−21−r2t = \dfrac{r \times \sqrt{n - 2}}{\sqrt{1 - r^2}} with df=25df = 25.
    Show one possible solution

    Answer: t≈2.887t \approx 2.887, critical value 2.0602.060; yes.

    t=0.5×251−0.25=2.50.75≈2.50.8660≈2.887df=25column 0.025, row 25: 2.0602.887>2.060\begin{aligned} & t = \frac{0.5 \times \sqrt{25}}{\sqrt{1 - 0.25}} = \frac{2.5}{\sqrt{0.75}} \approx \frac{2.5}{0.8660} \approx 2.887 \\[4pt] & df = 25 \\[4pt] & \text{column 0.025, row 25: } 2.060 \\ & 2.887 > 2.060 \end{aligned}

    The correlation is significant.

  9. 37.In a sample of 11 students, the correlation between sleep and marks is r=0.4r = 0.4. Is it significantly different from 0?

    Show a hint
    df=9df = 9.
    Show one possible solution

    Answer: t≈1.309t \approx 1.309, critical value 2.2622.262; no.

    t=0.4×91−0.16=1.20.84≈1.20.9165≈1.309df=9column 0.025, row 9: 2.2621.309<2.262\begin{aligned} & t = \frac{0.4 \times \sqrt{9}}{\sqrt{1 - 0.16}} = \frac{1.2}{\sqrt{0.84}} \approx \frac{1.2}{0.9165} \approx 1.309 \\[4pt] & df = 9 \\[4pt] & \text{column 0.025, row 9: } 2.262 \\ & 1.309 < 2.262 \end{aligned}

    The correlation is not significant: with 11 students, an rr of 0.4 can be chance.

  10. 38.Two critics rank five films. The differences of the ranks are d=1,−1,0,2,−2d = 1, -1, 0, 2, -2. Find Spearman's rsr_s.

    Show a hint
    ∑d2=10\sum d^2 = 10, n=5n = 5.
    Show one possible solution

    Answer: 0.50.5.

    ∑d2=1+1+0+4+4=10rs=1−6×105×24=1−60120=0.5\begin{aligned} \sum d^2 &= 1 + 1 + 0 + 4 + 4 = 10 \\[4pt] r_s &= 1 - \frac{6 \times 10}{5 \times 24} = 1 - \frac{60}{120} = 0.5 \end{aligned}
  11. 39.For a group of students, the hours of study xx have the mean 5 and the standard deviation 2; the marks yy have the mean 60 and the standard deviation 10; and r=0.8r = 0.8. Find the regression line and predict the mark of a student who studies 7 hours.

    Show a hint
    b=r×sysxb = r \times \dfrac{s_y}{s_x}, a=yˉ−b×xˉa = \bar{y} - b \times \bar{x}.
    Show one possible solution

    Answer: y^=40+4×x\hat{y} = 40 + 4 \times x; 68.

    b=0.8×102=4a=60−4×5=40y^=40+4×7=68\begin{aligned} b &= 0.8 \times \frac{10}{2} = 4 \\[4pt] a &= 60 - 4 \times 5 = 40 \\[4pt] \hat{y} &= 40 + 4 \times 7 = 68 \end{aligned}
  12. 40.A study of 5,000 adults finds that people who own more books have a higher income (r=0.35r = 0.35, clearly significant). A newspaper writes: "Buy books and you will earn more." What is wrong?

    Show a hint
    Which third variable could raise both?
    Show one possible solution

    Answer: Correlation is not causation.

    The data was observed, not produced by an experiment. Education is a likely lurking variable: people with more education own more books and earn more. The direction could also be reversed: people with a higher income can afford more books. The correlation is real (it is significant) and of moderate size (r2≈0.12r^2 \approx 0.12), but it says nothing about what would happen if somebody bought books.

21Check 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.A confidence interval for a mean is (40, 52)(40,\ 52). What is its margin of error?

Question 2

2.What does "95 percent confident" say about a confidence interval for a mean?

Question 3

3.Which critical value z∗z^* belongs to a 95 percent confidence interval?

Question 4

4.σ=10\sigma = 10 is known, and a sample of 100 values has the mean 50. What is the 95 percent confidence interval for the mean?

Question 5

5.A 95 percent confidence interval is calculated from a sample of 16 values, and only ss is known. Where in the t table is t∗t^*?

Question 6

6.A study wants to halve its margin of error at the same confidence level. What must happen to the sample size?

Question 7

7.A 95 percent confidence interval for the difference of two means is (−0.8, 4.2)(-0.8,\ 4.2). What follows?

Question 8

8.For a set of pairs, the sum of the products of the deviations is 6, and the two sums of squares are 9 and 16. What is rr?

Question 9

9.What does r=−0.9r = -0.9 say?

Question 10

10.The correlation between two variables is r=0.6r = 0.6. Which share of the variation of one goes with the other?

Question 11

11.For a set of pairs, r=0r = 0. What can be concluded?

Question 12

12.Over a year, the sales of ice cream and the number of sunburns in a town correlate strongly. Which explanation is the most likely?

0 of 12 answered.

22Checking your work with Python

This is the only code of the lesson. NumPy and SciPy repeat three calculations of this lesson.

Python
import numpy as np
from scipy import stats

# The t interval of Solved problem 12: five waiting times
x = np.array([10, 14, 12, 16, 13])
se = x.std(ddof=1) / np.sqrt(len(x))
t_star = stats.t.ppf(0.975, len(x) - 1)
print(round(t_star, 3), round(x.mean() - t_star * se, 2), round(x.mean() + t_star * se, 2))

# Covariance and correlation of Parts 11 and 12: hours and marks
hours = np.array([2, 4, 5, 6, 8])
marks = np.array([50, 60, 65, 75, 80])
print(np.cov(hours, marks)[0, 1], round(np.corrcoef(hours, marks)[0, 1], 4))

# The test of the correlation (Part 14) and the regression line (Part 16)
result = stats.pearsonr(hours, marks)
line = stats.linregress(hours, marks)
print(round(result.pvalue, 4), round(line.slope, 2), round(line.intercept, 2))

# Spearman's rank correlation of Part 15: the two judges
print(round(stats.spearmanr([1, 2, 3, 4, 5, 6], [2, 1, 4, 3, 6, 5]).statistic, 4))

Output

2.776 10.22 15.78
26.25 0.9834
0.0026 5.25 39.75
0.8286

Change it and run: put the data of an interval you calculated by hand into x, and change 0.975 to 0.995 for a 99 percent interval. Then change one mark and watch the correlation and the slope.

The first line prints t∗t^* and the two ends of the interval. stats.t.ppf(0.975, 4) is the value with 97.5 percent of the t distribution to its left, which is the column 0.025 of the table. The second line is the covariance (NumPy's cov divides by n−1n - 1) and rr. The third line gives the p-value of the test H0 ⁣:ρ=0H_0\colon \rho = 0 and the slope and the intercept of the regression line. The last line is rsr_s of the two judges. All numbers agree with the calculations by hand.

By hand and in Python
By handIn Python
z∗z^* for 95 percentstats.norm.ppf(0.975)
t∗t^* for 95 percentstats.t.ppf(0.975, df)
t interval for a meanstats.t.interval(0.95, df, loc=x.mean(), scale=stats.sem(x))
covariancenp.cov(x, y)[0, 1]
correlationnp.corrcoef(x, y)[0, 1], or df.corr() for a whole table in pandas
correlation with its p-valuestats.pearsonr(x, y)
Spearman's rank correlationstats.spearmanr(x, y)
regression linestats.linregress(x, y)

np.cov and np.corrcoef return a small table (a matrix): the covariances or correlations of every pair. The entry [0, 1] is the one of x with y.

23Quick summary

Every idea of the lesson with its definition or rule
IdeaDefinition or rule
Point estimateone number from the sample for an unknown number of the population
Confidence intervalestimate ± critical value × standard error
Margin of errorhalf the width of the interval
Mean, σ\sigma knownxˉ±z∗×σn\bar{x} \pm z^* \times \dfrac{\sigma}{\sqrt{n}}
Mean, σ\sigma unknownxˉ±t∗×sn\bar{x} \pm t^* \times \dfrac{s}{\sqrt{n}}, df=n−1df = n - 1
Proportionp^±z∗×p^(1−p^)n\hat{p} \pm z^* \times \sqrt{\dfrac{\hat{p}(1 - \hat{p})}{n}}
z∗z^*1.645 (90 percent), 1.960 (95 percent), 2.576 (99 percent)
t∗t^*the column 0.05, 0.025 or 0.005 of the t table
Meaning of 95 percentthe method catches the true value in 95 of 100 samples
Widthlarger nn: narrower; higher level: wider; more spread: wider
Sample size for a meann=(z∗×σE)2n = \left(\dfrac{z^* \times \sigma}{E}\right)^2, rounded up
Sample size for a proportionn=(z∗)2×p(1−p)E2n = \dfrac{(z^*)^2 \times p(1 - p)}{E^2}; p=0.5p = 0.5 when nothing is known
Difference of two means(xˉ1−xˉ2)±t∗×sp×1/n1+1/n2(\bar{x}_1 - \bar{x}_2) \pm t^* \times s_p \times \sqrt{1/n_1 + 1/n_2}, df=n1+n2−2df = n_1 + n_2 - 2
Pairsdˉ±t∗×sdn\bar{d} \pm t^* \times \dfrac{s_d}{\sqrt{n}}
Difference of two proportions(p^1−p^2)±z∗×p^1(1−p^1)n1+p^2(1−p^2)n2(\hat{p}_1 - \hat{p}_2) \pm z^* \times \sqrt{\dfrac{\hat{p}_1(1 - \hat{p}_1)}{n_1} + \dfrac{\hat{p}_2(1 - \hat{p}_2)}{n_2}}, not pooled
Interval and testa value outside the 95 percent interval is rejected at α=0.05\alpha = 0.05
Covariancesxy=∑(x−xˉ)(y−yˉ)n−1s_{xy} = \dfrac{\sum (x - \bar{x})(y - \bar{y})}{n - 1}; has units
Correlationr=sxysx×sy=∑(x−xˉ)(y−yˉ)∑(x−xˉ)2×∑(y−yˉ)2r = \dfrac{s_{xy}}{s_x \times s_y} = \dfrac{\sum (x - \bar{x})(y - \bar{y})}{\sqrt{\sum (x - \bar{x})^2 \times \sum (y - \bar{y})^2}}; between −1-1 and 11
r2r^2the share of the variation of yy that goes with xx
Test of a correlationt=r×n−21−r2t = \dfrac{r \times \sqrt{n - 2}}{\sqrt{1 - r^2}}, df=n−2df = n - 2
Spearmanrs=1−6×∑d2n(n2−1)r_s = 1 - \dfrac{6 \times \sum d^2}{n(n^2 - 1)}, on ranks
Regression liney^=a+b×x\hat{y} = a + b \times x with b=r×sysxb = r \times \dfrac{s_y}{s_x} and a=yˉ−b×xˉa = \bar{y} - b \times \bar{x}