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 and the sample standard deviation (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.Why a single number from a sample is not enough, and what a margin of error is
- 2.The confidence interval for a mean when is known (z) and when it is not (t)
- 3.What "95 percent confident" means, and what it does not mean
- 4.The confidence interval for a proportion, and the margin of error of a poll
- 5.What makes an interval wide or narrow, and how to plan the sample size
- 6.Confidence intervals for differences: two means, pairs, two proportions
- 7.How a confidence interval and a hypothesis test say the same thing in two ways
- 8.Scatter plots: direction, form and strength of a relationship
- 9.Covariance and the Pearson correlation coefficient , calculated by hand
- 10.The properties of , and its traps: curves, outliers, and "correlation is not causation"
- 11., the test of a correlation, and Spearman's rank correlation
- 12.A first look at the regression line
- 13.Where intervals and correlation are used in machine learning
| Written | Meaning |
|---|---|
| , , | the mean, the standard deviation and the size of a sample |
| , , | the mean, the standard deviation and a proportion of the population (unknown) |
| the proportion in a sample ("p hat") | |
| the standard error: how much an estimate varies from sample to sample | |
| , | the critical value that belongs to the confidence level |
| the margin of error: half the width of the interval | |
| degrees of freedom | |
| , | the means of the two variables and |
| , | their standard deviations |
| the covariance of and | |
| the Pearson correlation coefficient of a sample; (the Greek letter rho) for a population | |
| Spearman'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 and . 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.
| Later in the course | What it takes from this lesson |
|---|---|
| Linear regression (Topic 22) | the slope of the line is ; measures how well the line fits |
| Regularization and feature selection | strongly correlated features carry the same information twice |
| Classification metrics, cross-validation | an accuracy is reported with its interval; folds give a mean and a standard error |
| Embeddings and cosine similarity | the 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.
| Unknown in the population | Point estimate from the sample |
|---|---|
| the mean | the sample mean |
| the proportion | the sample proportion |
| the standard deviation | the sample standard deviation |
| the difference |
2.2The shape of every interval
Topic 17 showed how much a sample mean varies: its standard error is , 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:
- The estimate is the centre of the interval.
- The standard error says how much the estimate varies from sample to sample.
- The critical value ( or ) says how many standard errors we go to each side. It depends on how confident we want to be.
The interval goes from to ; its width is .
Try it yourself
Show a hint
Show the solution
Answer: Estimate 41, margin of error 4, width 8.
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 of the population is known:
The critical value cuts off the middle part of the standard normal curve:
| Confidence level | Area in each tail | |
|---|---|---|
| 90 percent | 0.05 | 1.645 |
| 95 percent | 0.025 | 1.960 |
| 99 percent | 0.005 | 2.576 |
For 95 percent: between and 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.
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 with ), 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:
| Level | Margin of error | Interval | |
|---|---|---|---|
| 90 percent | 1.645 | 3.29 | |
| 95 percent | 1.960 | 3.92 | |
| 99 percent | 2.576 | 5.15 |
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
Show a hint
Show the solution
Answer: , , interval .
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 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 , and about 5 would miss it.
A machine whose true mean is 500 g, with σ = 10 g. Take samples of 25 bags and build an interval from each.
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 lies in ." After the interval is calculated, nothing is random any more: 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 , 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
Show a hint
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: .
5The confidence interval for a mean when σ is not known
5.1The t interval
In real work is unknown and the sample standard deviation takes its place. As in the last lesson, the price is the t distribution with :
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.
t = 2.228 cuts off the area 0.025 in the right tail: the critical value of a one-tailed test at 2.5 percent, or of a two-tailed test at 5 percent.
| df | 0.10 | 0.05 | 0.025 | 0.01 | 0.005 |
|---|---|---|---|---|---|
| 1 | 3.078 | 6.314 | 12.706 | 31.821 | 63.657 |
| 2 | 1.886 | 2.920 | 4.303 | 6.965 | 9.925 |
| 3 | 1.638 | 2.353 | 3.182 | 4.541 | 5.841 |
| 4 | 1.533 | 2.132 | 2.776 | 3.747 | 4.604 |
| 5 | 1.476 | 2.015 | 2.571 | 3.365 | 4.032 |
| 6 | 1.440 | 1.943 | 2.447 | 3.143 | 3.707 |
| 7 | 1.415 | 1.895 | 2.365 | 2.998 | 3.499 |
| 8 | 1.397 | 1.860 | 2.306 | 2.896 | 3.355 |
| 9 | 1.383 | 1.833 | 2.262 | 2.821 | 3.250 |
| 10 | 1.372 | 1.812 | 2.228 | 2.764 | 3.169 |
| 11 | 1.363 | 1.796 | 2.201 | 2.718 | 3.106 |
| 12 | 1.356 | 1.782 | 2.179 | 2.681 | 3.055 |
| 13 | 1.350 | 1.771 | 2.160 | 2.650 | 3.012 |
| 14 | 1.345 | 1.761 | 2.145 | 2.624 | 2.977 |
| 15 | 1.341 | 1.753 | 2.131 | 2.602 | 2.947 |
| 16 | 1.337 | 1.746 | 2.120 | 2.583 | 2.921 |
| 17 | 1.333 | 1.740 | 2.110 | 2.567 | 2.898 |
| 18 | 1.330 | 1.734 | 2.101 | 2.552 | 2.878 |
| 19 | 1.328 | 1.729 | 2.093 | 2.539 | 2.861 |
| 20 | 1.325 | 1.725 | 2.086 | 2.528 | 2.845 |
| 21 | 1.323 | 1.721 | 2.080 | 2.518 | 2.831 |
| 22 | 1.321 | 1.717 | 2.074 | 2.508 | 2.819 |
| 23 | 1.319 | 1.714 | 2.069 | 2.500 | 2.807 |
| 24 | 1.318 | 1.711 | 2.064 | 2.492 | 2.797 |
| 25 | 1.316 | 1.708 | 2.060 | 2.485 | 2.787 |
| 26 | 1.315 | 1.706 | 2.056 | 2.479 | 2.779 |
| 27 | 1.314 | 1.703 | 2.052 | 2.473 | 2.771 |
| 28 | 1.313 | 1.701 | 2.048 | 2.467 | 2.763 |
| 29 | 1.311 | 1.699 | 2.045 | 2.462 | 2.756 |
| 30 | 1.310 | 1.697 | 2.042 | 2.457 | 2.750 |
| 40 | 1.303 | 1.684 | 2.021 | 2.423 | 2.704 |
| 60 | 1.296 | 1.671 | 2.000 | 2.390 | 2.660 |
| 120 | 1.289 | 1.658 | 1.980 | 2.358 | 2.617 |
| ∞ (z) | 1.282 | 1.645 | 1.960 | 2.326 | 2.576 |
| Confidence level | Column of the t table |
|---|---|
| 90 percent | 0.05 |
| 95 percent | 0.025 |
| 99 percent | 0.005 |
If your 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.
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 (, not rejected). With instead of the interval would have been 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.
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.
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 (). An outlier in a small sample makes the interval unreliable.
Try it yourself
Show a hint
Show the solution
Answer: , , interval .
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 ().
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):
There is one difference from the test of the last lesson. A test assumes and uses the claimed in the standard error. An interval assumes nothing, so the only value available is itself.
Condition: at least 10 successes and 10 failures in the sample ( and ).
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.
"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 is largest when , where it is 0.25. So the widest possible 95 percent margin for a sample of is
| Sample size | Margin of error (at most) |
|---|---|
| 100 | 0.098 |
| 400 | 0.049 |
| 1,000 | 0.031 |
| 2,500 | 0.020 |
| 10,000 | 0.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.
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
Show a hint
Show the solution
Answer: , , interval .
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
| Change | The interval becomes |
|---|---|
| a larger sample | narrower: four times the sample, half the margin |
| a higher confidence level | wider: grows from 1.645 to 1.960 to 2.576 |
| more spread in the data ( or ) | 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 ?
Always round up: a smaller would give a margin that is a little too large. 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?
7.3The sample size for a proportion
is not known before the study. If there is an earlier estimate, use it. If not, use : it gives the largest , 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.
Half the margin needs four times the people: for 1 point the poll would need 9,604.
Try it yourself
Show a hint
Show the solution
Answer: (a) ; (b) .
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 of the last lesson:
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.
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 ). 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:
Example. The eight students of the last lesson improved by marks with . The 95 percent interval:
The course improves the marks by between 0.90 and 5.10 on average.
8.3Two proportions
Here the two proportions are not pooled: pooling belongs to the test, where 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).
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
Show a hint
Show the solution
Answer: ; it does not contain 0.
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 that a two-tailed test at would not reject.
| The value lies | The two-tailed test at |
|---|---|
| inside the 95 percent interval | fails to reject |
| outside the 95 percent interval | rejects |
The reason is one inequality read in two directions: is inside the interval when is at most , and that is exactly when is at most .
| Confidence level | Belongs to the two-tailed test at |
|---|---|
| 90 percent | |
| 95 percent | |
| 99 percent |
For a difference, the value to look for is 0: an interval for 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 and one with : 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 in the standard error and the interval uses .)
Try it yourself
Show a hint
Show the solution
Answer: (a) reject; (b) fail to reject; (c) .
(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: .
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 .
The first step is always a picture. A scatter plot draws every pair as one point: to the right, upwards.
10.2What to look for
| Look for | Question | Words |
|---|---|---|
| Direction | When grows, does grow or fall? | positive, negative, none |
| Form | Do the points follow a line, a curve, or nothing? | linear, curved, no pattern |
| Strength | How closely do the points follow the pattern? | strong, moderate, weak |
| Unusual points | Is there a point far from all the others? | outlier |
a curve: a strong relationship
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
Show a hint
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:
| Student | 1 | 2 | 3 | 4 | 5 | Sum |
|---|---|---|---|---|---|---|
| Hours | 2 | 4 | 5 | 6 | 8 | 25 |
| Mark | 50 | 60 | 65 | 75 | 80 | 330 |
The means are and . A student who studied more than the mean has a positive deviation ; one with a mark above the mean has a positive . 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
| Student | 1 | 2 | 3 | 4 | 5 | Sum |
|---|---|---|---|---|---|---|
| 0 | 1 | 3 | 0 | |||
| 9 | 14 | 0 | ||||
| product | 48 | 6 | 0 | 9 | 42 | 105 |
Every product is positive or zero: students below the mean in hours are below the mean in marks, and the other way round.
11.2The definition
The covariance is the average product of the deviations. For a sample, the sum is divided by , as for the variance:
For the five students:
- : the variables tend to move in the same direction.
- : when one is above its mean, the other tends to be below.
- near 0: no linear tendency.
The covariance of a variable with itself is its variance: .
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 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
Show a hint
Show the solution
Answer: , , sum of the products 7, .
| Shop 1 | Shop 2 | Shop 3 | Sum | |
|---|---|---|---|---|
| 0 | 1 | 0 | ||
| 4 | 0 | |||
| product | 3 | 0 | 4 | 7 |
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:
This is the Pearson correlation coefficient. The cancels too, which gives the formula that is easiest by hand:
12.2The calculation in a table
| Student | product | ||||||
|---|---|---|---|---|---|---|---|
| 1 | 2 | 50 | 48 | 9 | 256 | ||
| 2 | 4 | 60 | 6 | 1 | 36 | ||
| 3 | 5 | 65 | 0 | 0 | 0 | 1 | |
| 4 | 6 | 75 | 1 | 9 | 9 | 1 | 81 |
| 5 | 8 | 80 | 3 | 14 | 42 | 9 | 196 |
| Sum | 25 | 330 | 0 | 0 | 105 | 20 | 570 |
A very strong positive linear relationship: more hours go with higher marks, almost on a straight line.
The same through the definition: , , and
12.3The steps
- Calculate the two means.
- Write the deviations and (each column adds up to 0).
- Multiply them pair by pair, and square each of them.
- Add the three columns.
- is the sum of the products, divided by the square root of (the sum of squares of times the sum of squares of ).
The five students studied 2, 4, 5, 6 and 8 hours. Move their marks and follow the calculation of r.
| hours − 5 | −3 | −1 | 0 | 1 | 3 | sum 0 |
|---|---|---|---|---|---|---|
| mark − mean | −16 | −6 | −1 | 9 | 14 | sum 0 |
| product | 48 | 6 | 0 | 9 | 42 | sum 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:
For the five students:
The same sum of products.
12.5Reading r
is always between and .
| Meaning | |
|---|---|
| all points lie exactly on a rising line | |
| near | strong positive linear relationship |
| near | no linear relationship |
| near | strong negative linear relationship |
| all points lie exactly on a falling line |
A rough guide for the strength, by the size of :
| Strength | |
|---|---|
| below 0.3 | weak |
| 0.3 to 0.7 | moderate |
| above 0.7 | strong |
These borders are a habit, not a law. In physics an 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:
That is why has no unit, and why it does not care in which units and were measured.
Try it yourself
Show a hint
Show the solution
Answer: Sums of squares 2 and 26, .
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 and .
- Symmetric: the correlation of with is the correlation of with .
- Shifting and rescaling change nothing: hours or minutes, Celsius or Fahrenheit, stays the same. (Multiplying one variable by a negative number changes the sign.)
- only for a perfect line. The slope of the line does not matter: and both give . 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 is called the coefficient of determination. It is the share of the variation of that the linear relationship with accounts for.
For the students, : about 97 percent of the differences between the marks go together with the differences in hours. An of 0.5 sounds like "half", but : only a quarter of the variation is explained.
without the far point: r = −0.33
three groups, each rising
13.3Trap 1: r sees only lines
Take and , so . is known exactly from : the strongest relationship there can be. Yet the products of the deviations are , which add up to 0, and . 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. 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.
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 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 and can have four explanations:
| Explanation | Example |
|---|---|
| causes | more study, higher marks |
| causes | "people who take vitamins are healthier": or do healthy people look after themselves more? |
| a third variable causes both | ice cream and drowning (the weather); shoe size and reading skill of children (age) |
| chance | with 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
Show a hint
Show the solution
Answer: (a) ; (b) 0.36; (c) a third variable: the size of the town.
(a) A change of currency multiplies every price by a positive factor: stays . (b) , 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 . But five points are very few: even if hours and marks had nothing to do with each other in the population (), a sample of five would show some correlation by chance. The question of Topic 19 comes back: could this be chance?
For the students:
The critical value (two-tailed, 5 percent, row 3) is , and : reject . Even with five students the correlation is significant, because it is so strong.
The sample size matters a great deal. The same can be chance in a small sample and beyond doubt in a large one:
| Smallest significant | |
|---|---|
| 5 | 0.88 |
| 10 | 0.63 |
| 20 | 0.44 |
| 30 | 0.36 |
| 100 | 0.20 |
With 10 pairs, even could be chance. With 100 pairs, is already significant, and still weak: as in Topic 19, significant is not the same as important. tells how much the relationship matters.
Try it yourself
Show a hint
Show the solution
Answer: , , critical value ; significant.
Reject : 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 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 replaces every value by its rank (1 for the smallest, 2 for the next, and so on) and then calculates Pearson's of the ranks. When there are no ties, a short formula gives the same number:
is the difference between the two ranks of the same person or thing.
15.2Example
Two judges rank six dishes of a cooking competition.
| Dish | A | B | C | D | E | F | Sum |
|---|---|---|---|---|---|---|---|
| Judge 1 | 1 | 2 | 3 | 4 | 5 | 6 | |
| Judge 2 | 2 | 1 | 4 | 3 | 6 | 5 | |
| 1 | 1 | 1 | 0 | ||||
| 1 | 1 | 1 | 1 | 1 | 1 | 6 |
The judges agree strongly: they never differ by more than one place.
15.3Reading r_s
is read like : between and . means that the two orders are exactly the same; that one is the reverse of the other. It answers "when grows, does grow?", without asking for a straight line. For with positive and negative , Pearson's is below 1, and .
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 is calculated as Pearson's of the ranks.
Try it yourself
Show a hint
Show the solution
Answer: , .
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:
For the five students:
Every extra hour of study goes with 5.25 more marks on average. A student who studies 7 hours is predicted to get .
Three things to remember until Topic 22:
- The slope has the sign of , but its size depends on the units; does not.
- The line always passes through the point of the means .
- Predict only inside the range of the data (here 2 to 8 hours). The line says nothing reliable about 20 hours.
Try it yourself
Show a hint
Show the solution
Answer: , , prediction 6.75.
Each extra thousand manat of advertising goes with 3.5 thousand manat more sales. The intercept 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 . 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 , 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 can still be useful to a model that handles curves (Trap 1).
Multicollinearity. Two features with 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 is the cosine of the angle between the two centred vectors (Topic 13): . 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
Show a hint
Show the solution
Answer: (a) ; (b) ; (c) no.
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 with a small sample and an estimated . That needs with .
Taking the wrong column of the t table. 95 percent uses the column 0.025; 90 percent the column 0.05.
Forgetting . 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 as "no relationship". It means no linear relationship. Look at the picture.
Reading correlation as cause. A lurking variable may drive both.
Reading as a slope or as a percentage. does not mean "80 percent" and not "y rises by 0.8". The share explained is .
Forgetting outliers. One point can make or break a correlation.
Mixing up covariance and correlation. The covariance has units and no fixed range; has neither unit nor a value outside to .
Dividing by the wrong thing. divides the sum of products by the square root of the product of the two sums of squares, not by their sum.
| Wrong | Right |
|---|---|
| "the mean is in 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" |
| 95 percent, : | column 0.025: |
| , so 96 | round up: 97 |
| : "x and y are unrelated" | "no linear relationship; look at the scatter plot" |
| : "x causes y" | "x and y go together; the cause needs an experiment" |
| : "half of the variation is explained" | : a quarter |
| impossible: check the calculation | |
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 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).
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. is known: with (Part 3).
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 changes: 2.576 for 99 percent (Part 3).
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 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 .
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 ; the mean varies with , which is much smaller.
Problem 5: The effect of the sample size
A measurement has . Find the margin of error of a 95 percent interval for and for .
Idea. The margin shrinks with (Part 7).
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. , rounded up (Part 7).
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. (Part 6).
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 : (Part 7).
Why: makes 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 (Problem 2). Without a new calculation: would a two-tailed test at reject (a) , (b) ?
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 .
- (b) 51 is inside the interval: fail to reject .
Why: the interval is the list of all values of that the data does not contradict. One interval answers infinitely many tests.
Level 2: Intervals with t, and differences
Problem 10: Finding t
Find 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. ; the column is half of what is left outside the interval (Part 5).
| Column | |||
|---|---|---|---|
| (a) | 9 | 0.025 | 2.262 |
| (b) | 24 | 0.005 | 2.797 |
| (c) | 15 | 0.05 | 1.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. is unknown: with (Part 5).
Why: the machine should cut 50 cm, and 50 is outside the interval, in agreement with the t-test of the last lesson (, 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 and (Topic 18), then the t interval (Part 5).
| 10 | 14 | 12 | 16 | 13 | Sum | |
|---|---|---|---|---|---|---|
| 1 | 3 | 0 | 0 | |||
| 9 | 1 | 1 | 9 | 0 | 20 |
Why: with only five values 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, (Part 8).
| 2 | 5 | 3 | 6 | 4 | 4 | |
|---|---|---|---|---|---|---|
| 1 | 2 | 0 | 0 | |||
| 4 | 1 | 1 | 4 | 0 | 0 |
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. with (Part 8), then look for 0 (Part 9).
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 ().
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).
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 for a mean, calculated with . Find the 99 percent interval from the same data.
Idea. Recover the standard error from the margin, then use the new (Parts 2 and 3).
Why: the margins of two levels are in the ratio of their critical values: . 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).
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 and .
Idea. Deviations, products, sum, divide by (Part 11).
| Sum | ||||||
|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 0 | |||
| 0 | 1 | 0 | 1 | 0 | ||
| product | 4 | 0 | 0 | 0 | 2 | 6 |
Why: the covariance is positive: tends to rise with . Whether 1.5 is much cannot be said without the standard deviations.
Problem 20: The correlation coefficient by hand
Find for the data of Problem 19.
Idea. (Part 12).
Why: a strong positive relationship, but not a perfect line: the fourth value of falls back. : about 60 percent of the variation of goes with .
Problem 21: A negative correlation
A shop tries the prices for a product and counts the sales in a week: . Find .
Idea. The same steps; the products will be negative (Part 12).
| Sum | ||||||
|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 0 | |||
| 3 | 1 | 0 | 0 | |||
| product | 0 | |||||
| 9 | 1 | 0 | 4 | 4 | 18 |
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 (in m²) has the mean 70 and the standard deviation 20; the rent (in manat) has the mean 600 and the standard deviation 150. The covariance is 2,400. Find , and the regression line. Predict the rent of a flat of 90 m².
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 , and the covariance is 120. The temperatures are converted to Fahrenheit (). What are the new correlation and the new covariance?
Idea. does not change when a variable is shifted or rescaled; the covariance is multiplied by the factor (Parts 11 and 13).
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 , and in the two cancel.
Problem 24: Is a correlation significant?
In a sample of 18 employees, the correlation between years of experience and salary is . Is it significantly different from 0?
Idea. with (Part 14).
Reject . The correlation is significant.
Why: with 18 pairs, an of 0.6 is too large to be chance. It still explains only 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 .
| Student | A | B | C | D | E | Sum |
|---|---|---|---|---|---|---|
| Mathematics | 1 | 2 | 3 | 4 | 5 | |
| Physics | 2 | 1 | 3 | 5 | 4 | |
| 1 | 0 | 1 | 0 | |||
| 1 | 1 | 0 | 1 | 1 | 4 |
Idea. (Part 15).
Why: the two orders are almost the same; only neighbours change places. The differences always add up to 0, which is a useful check.
Problem 26: Three traps
(a) Find for and . Are and 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. measures only linear relationships, and correlation is not causation (Part 13).
(a) , and yet 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 with four decimals.
Level 1: Intervals with z, proportions and sample sizes
- 1.A confidence interval for a mean is . 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.
- 2.Give 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: , , .
90 percent leaves 5 percent in each tail: . 95 percent leaves 2.5 percent: . 99 percent leaves 0.5 percent: .
- 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 .Show one possible solution
Answer: .
- 4.Find the 90 percent confidence interval for the data of Problem 3.
Show a hint
Only changes.Show one possible solution
Answer: .
- 5.Find the 99 percent confidence interval for the data of Problem 3.
Show a hint
.Show one possible solution
Answer: .
- 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 instead of 1.645, so its margin of error is larger. To catch the true mean more often, the interval must be wider.
- 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 .Show one possible solution
Answer: About 3.
The sample is 4 times as large, so the standard error is divided by :
- 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
, rounded up.Show one possible solution
Answer: .
- 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: .
That is four times the 96.04 of Problem 8.
- 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
; the standard error is .Show one possible solution
Answer: .
- 11.Of 400 customers, 320 are satisfied. Find the 99 percent confidence interval for the share of satisfied customers.
Show a hint
, standard error 0.02, .Show one possible solution
Answer: .
- 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 : .Show one possible solution
Answer: .
- 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 : .Show one possible solution
Answer: .
With it would have been 2401: an earlier estimate saves people.
- 14.A 95 percent confidence interval for the mean weight of apples is 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 .
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: . (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
- 15.Find for a 95 percent confidence interval from a sample of 12 values.
Show a hint
, column 0.025.Show one possible solution
Answer: .
. A 95 percent interval leaves 2.5 percent in each tail: column 0.025, row 11: .
- 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, .Show one possible solution
Answer: .
- 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: .
- 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
, , .Show one possible solution
Answer: .
- 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: .
- 20.Six people learn a typing method. The changes in their speed are , 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
, , .Show one possible solution
Answer: ; yes.
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.)
- 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
, standard error , .Show one possible solution
Answer: .
It does not contain 0: the new method is better, by between about 1 and 7 points.
- 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 of both versions under the root.Show one possible solution
Answer: .
0 is outside the interval: version B sells better, by between about 1.6 and 10.4 percentage points.
- 23.A 95 percent confidence interval for the difference of two means is . 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.
- 24.A 95 percent confidence interval for the difference of two means is . 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.
- 25.A classifier is right on 180 of 200 test rows. Find the 95 percent confidence interval for its accuracy.
Show a hint
; the standard error is .Show one possible solution
Answer: .
- 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.
- 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 .Show one possible solution
Answer: About , 3.1 percentage points.
- 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 times as large.
Level 3: Covariance, correlation and judgement
- 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.
- 30.Find the covariance of and .
Show a hint
The means are 5 and 3. Divide the sum of the products by 3.Show one possible solution
Answer: About .
The deviations of x and y from their means 5 and 3, and their products Sum 1 3 0 0 3 0 product 6 0 9 14 - 31.Find for the data of Problem 30.
Show a hint
The sums of squares are 20 and 14.Show one possible solution
Answer: About .
- 32.Five weeks after an advertising campaign ended, the sales fell: for the weeks the sales were . Find .
Show a hint
. The products are negative.Show one possible solution
Answer: About .
The deviations of the weeks and of the sales from their means 3 and 6 Sum 0 1 2 0 4 3 0 0 product 0 - 33.Two variables have the covariance and the standard deviations 6 and 5. Find and .
Show a hint
.Show one possible solution
Answer: , .
36 percent of the variation of one variable goes with the other.
- 34.The correlation between the size of a flat and its rent is . Which share of the variation of the rents is explained by the size?
Show a hint
The share is .Show one possible solution
Answer: 81 percent.
- 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 ?Show one possible solution
Answer: .
Changing the units multiplies each variable by a positive factor, which does not change . It stays 0.7.
- 36.In a sample of 27 shops, the correlation between the size of the shop and its sales is . Is it significantly different from 0?
Show a hint
with .Show one possible solution
Answer: , critical value ; yes.
The correlation is significant.
- 37.In a sample of 11 students, the correlation between sleep and marks is . Is it significantly different from 0?
Show a hint
.Show one possible solution
Answer: , critical value ; no.
The correlation is not significant: with 11 students, an of 0.4 can be chance.
- 38.Two critics rank five films. The differences of the ranks are . Find Spearman's .
Show a hint
, .Show one possible solution
Answer: .
- 39.For a group of students, the hours of study have the mean 5 and the standard deviation 2; the marks have the mean 60 and the standard deviation 10; and . Find the regression line and predict the mark of a student who studies 7 hours.
Show a hint
, .Show one possible solution
Answer: ; 68.
- 40.A study of 5,000 adults finds that people who own more books have a higher income (, 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 (), 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.
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.
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 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 ) and . The third line gives the p-value of the test and the slope and the intercept of the regression line. The last line is of the two judges. All numbers agree with the calculations by hand.
| By hand | In Python |
|---|---|
| for 95 percent | stats.norm.ppf(0.975) |
| for 95 percent | stats.t.ppf(0.975, df) |
| t interval for a mean | stats.t.interval(0.95, df, loc=x.mean(), scale=stats.sem(x)) |
| covariance | np.cov(x, y)[0, 1] |
| correlation | np.corrcoef(x, y)[0, 1], or df.corr() for a whole table in pandas |
| correlation with its p-value | stats.pearsonr(x, y) |
| Spearman's rank correlation | stats.spearmanr(x, y) |
| regression line | stats.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
| Idea | Definition or rule |
|---|---|
| Point estimate | one number from the sample for an unknown number of the population |
| Confidence interval | estimate ± critical value × standard error |
| Margin of error | half the width of the interval |
| Mean, known | |
| Mean, unknown | , |
| Proportion | |
| 1.645 (90 percent), 1.960 (95 percent), 2.576 (99 percent) | |
| the column 0.05, 0.025 or 0.005 of the t table | |
| Meaning of 95 percent | the method catches the true value in 95 of 100 samples |
| Width | larger : narrower; higher level: wider; more spread: wider |
| Sample size for a mean | , rounded up |
| Sample size for a proportion | ; when nothing is known |
| Difference of two means | , |
| Pairs | |
| Difference of two proportions | , not pooled |
| Interval and test | a value outside the 95 percent interval is rejected at |
| Covariance | ; has units |
| Correlation | ; between and |
| the share of the variation of that goes with | |
| Test of a correlation | , |
| Spearman | , on ranks |
| Regression line | with and |