Track 9 · Correlation and Regression · Lesson 1 of 15
Covariance and correlation
Calculate covariance and Pearson's correlation, test whether a correlation differs from zero, and recognise what r cannot see.
A running club wants to tell members how strongly weekly training volume is related to race times, and a facilities manager wants to know how closely a building's energy use tracks the outdoor temperature. One number — the correlation coefficient — summarises a straight-line relationship, but only if you know when it can be trusted.
Every relationship between two numeric variables starts with a scatter plot: one point per observation, the explanatory variable on the horizontal axis and the response on the vertical axis. Describe what you see in three words before computing anything:
- Direction — do larger values go with larger values (positive) or smaller ones (negative)?
- Form — do the points follow a straight line, a curve, or clusters?
- Strength — how tightly do the points hug that form?
Show the data as a table
| TV spend (thousand SAR) | Units sold (packs) |
|---|---|
| 0 | 4367 |
| 0 | 5380 |
| 75.3 | 7627 |
| 61.3 | 7919 |
| 62.6 | 6688 |
| 58.6 | 7884 |
| 66 | 7739 |
| 0 | 5879 |
| 0 | 4796 |
| 0 | 6707 |
| 0 | 5775 |
| 60 | 7577 |
| 67.7 | 8206 |
| 72.8 | 8885 |
| 72.3 | 7832 |
| 73 | 9029 |
| 65 | 8250 |
| 68.4 | 8799 |
| 69 | 8145 |
| 0 | 5831 |
| 0 | 6570 |
| 0 | 5668 |
| 0 | 6953 |
| 0 | 6784 |
| 49.4 | 8785 |
| 60.9 | 9182 |
| 51.2 | 9505 |
| 64.1 | 8240 |
| 0 | 7408 |
| 0 | 7306 |
| 0 | 6773 |
| 0 | 6541 |
| 55.3 | 8455 |
| 54.5 | 8957 |
| 53.7 | 9229 |
| 53.1 | 8703 |
| 55.1 | 7460 |
| 51.9 | 8039 |
| 0 | 5905 |
| 0 | 5477 |
| 0 | 6114 |
| 80.8 | 8945 |
| 75.1 | 9202 |
| 93.5 | 9112 |
| 85.5 | 8624 |
| 86.6 | 8919 |
| 0 | 6021 |
| 0 | 5725 |
| 0 | 5676 |
| 0 | 5329 |
| 83.5 | 8201 |
| 62.9 | 7164 |
| 75.5 | 8819 |
| 79.1 | 8230 |
| 0 | 5154 |
| 0 | 5265 |
| 0 | 6139 |
| 82.9 | 8482 |
| 75.9 | 8031 |
| 89.7 | 9162 |
| 88.7 | 8621 |
| 79.1 | 9362 |
| 88.7 | 10276 |
| 93.9 | 8955 |
| 78 | 8466 |
| 0 | 6361 |
| 0 | 6240 |
| 0 | 6718 |
| 0 | 6151 |
| 0 | 5667 |
| 106.3 | 9681 |
| 106.7 | 9296 |
| 96.4 | 9560 |
| 123.9 | 10197 |
| 100.6 | 10482 |
| 0 | 7032 |
| 0 | 7468 |
| 0 | 6456 |
| 0 | 6870 |
| 0 | 7602 |
| 0 | 7245 |
| 37.9 | 8807 |
| 42.1 | 7896 |
| 35.5 | 9124 |
| 36.4 | 8515 |
| 40.5 | 8897 |
| 41.6 | 9238 |
| 38.6 | 7039 |
| 0 | 5860 |
| 0 | 6382 |
| 0 | 5542 |
| 0 | 5216 |
| 0 | 5785 |
| 0 | 6580 |
| 109.6 | 9390 |
| 117 | 8750 |
| 105.1 | 9130 |
| 122.9 | 10112 |
| 114.5 | 9322 |
| 123 | 9794 |
| 0 | 6321 |
| 0 | 7010 |
| 0 | 5697 |
| 0 | 5463 |
| 0 | 5649 |
| 0 | 6896 |
| 0 | 5482 |
| 81.7 | 8272 |
| 92.3 | 8680 |
| 85.8 | 8411 |
| 91.3 | 8105 |
| 87.7 | 9232 |
| 0 | 5353 |
| 0 | 7431 |
| 0 | 6588 |
| 0 | 6596 |
| 0 | 6173 |
| 0 | 6563 |
| 89.4 | 9363 |
| 94.9 | 9345 |
| 92.4 | 8873 |
| 94.2 | 9133 |
| 92.5 | 9302 |
| 91.3 | 10174 |
| 0 | 7128 |
| 0 | 6167 |
| 0 | 7733 |
| 0 | 6950 |
| 102.2 | 9416 |
| 111.7 | 10983 |
| 106.2 | 11032 |
| 123.8 | 9927 |
| 95 | 9762 |
| 118.4 | 11045 |
| 122.8 | 11248 |
| 0 | 7315 |
| 0 | 7662 |
| 0 | 7385 |
| 0 | 7136 |
| 36.8 | 8404 |
| 36.7 | 7713 |
| 34.9 | 8159 |
| 33.8 | 8526 |
| 35.3 | 8267 |
| 39.1 | 7926 |
| 0 | 6386 |
| 0 | 6829 |
| 0 | 6636 |
| 0 | 6738 |
| 0 | 6582 |
| 0 | 6256 |
| 27.9 | 7878 |
| 29.8 | 7685 |
| 28.4 | 8746 |
| 28.7 | 6855 |
| 0 | 6151 |
Covariance measures whether the two variables move together. For each observation, multiply how far is from its mean by how far is from its mean. When both are above (or both below) their means the product is positive; when one is above and the other below it is negative. The covariance averages these products.
- the -th pair of observations
- the two sample means
- the sum of cross-products
- the number of pairs; dividing by matches the sample variance
The covariance has awkward units (kilometres × minutes, SAR × packs) and its size depends on the scale of measurement, so it cannot tell you whether a relationship is strong. Dividing by both standard deviations removes the units and gives Pearson's correlation coefficient .
- the sample standard deviations of and
- the sums of squared deviations and
Worked example
Covariance and r by hand
Five pairs: = 1, 2, 3, 4, 5 and = 2, 4, 5, 4, 5. Find the covariance and the correlation.
Means. , .
Deviations. : −2, −1, 0, 1, 2. : −2, 0, 1, 0, 1.
Cross-products : 4, 0, 0, 0, 2, so . Also and .
Covariance. .
Correlation. .
Covariance 1.50; — a fairly strong positive linear association.
| Property | What it means in practice |
|---|---|
| only when every point lies exactly on a straight line | |
| No units | Changing kilometres to miles or minutes to hours leaves unchanged (covariance changes) |
| Symmetric | The correlation of with equals the correlation of with |
| Linear only | measures how closely points follow a straight line; a strong curve can give near 0 |
| Not resistant | One extreme point can create, destroy or reverse a correlation |
Show the data as a table
| Outdoor temperature (°C) | Energy use (kWh) |
|---|---|
| 9.4 | 2711.1 |
| 12.3 | 2658.9 |
| 13.3 | 2560.5 |
| 10.2 | 2670.3 |
| 10.8 | 2549.4 |
| 7.6 | 1320.4 |
| 5.6 | 1446.6 |
| 9.3 | 2721.3 |
| 8.9 | 2689.9 |
| 8.2 | 2664 |
| 9.3 | 2533.5 |
| 11.1 | 2555.6 |
| 7.8 | 1263 |
| 7.9 | 1312.8 |
| 7.6 | 2809.5 |
| 9.5 | 2656.7 |
| 9.3 | 2797.6 |
| 10 | 2673.8 |
| 10.2 | 2776.5 |
| 6.6 | 1319.4 |
| 6.6 | 1528 |
| 6.4 | 2897.5 |
| 9.5 | 2680.7 |
| 11.7 | 2471.4 |
| 11.2 | 2509.6 |
| 10.8 | 1338 |
| 9.9 | 1309.9 |
| 7.3 | 2723.8 |
| 6.5 | 2791.4 |
| 6.3 | 2728.4 |
| 7.6 | 2878.2 |
| 9.1 | 2609.5 |
| 8.4 | 1474.9 |
| 8.6 | 1413 |
| 12.4 | 2383.9 |
| 12.5 | 2308.3 |
| 12.7 | 2454.6 |
| 13.1 | 2439.8 |
| 11.7 | 2531.7 |
| 11.5 | 1234.6 |
| 13.4 | 1162 |
| 12.8 | 2273.3 |
| 13.2 | 2337.3 |
| 14.2 | 2386.4 |
| 14 | 2469.5 |
| 12.7 | 1427.1 |
| 11.9 | 1271.3 |
| 11.5 | 2543.8 |
| 12.1 | 2548.1 |
| 13.9 | 2465 |
| 12 | 2561.1 |
| 14.1 | 2303 |
| 13.4 | 1054 |
| 10.7 | 1159.2 |
| 11.2 | 2546.3 |
| 12 | 2333.9 |
| 12.5 | 2397.7 |
| 11.9 | 2440 |
| 11.3 | 2395.2 |
| 11.3 | 1156.4 |
| 12.8 | 1203.2 |
| 15.6 | 2299.3 |
| 13.6 | 2448.1 |
| 14.1 | 2440.2 |
| 14.3 | 2410.8 |
| 13.4 | 2394.3 |
| 14.1 | 1156.6 |
| 14.2 | 1098.7 |
| 11.1 | 2512.2 |
| 10.8 | 2624.7 |
| 10.7 | 2503.1 |
| 10.6 | 2469 |
| 13.2 | 2324.4 |
| 13.5 | 1146.2 |
| 15.8 | 1098.5 |
| 16.2 | 2332.2 |
| 17.9 | 2401.6 |
| 16.2 | 2337.9 |
| 14.8 | 2469.3 |
| 16.9 | 2528.5 |
| 17.2 | 1145.8 |
| 17.8 | 1255.2 |
| 20.2 | 2311.1 |
| 18.8 | 2302 |
| 17.7 | 2293 |
| 17 | 2226.6 |
| 14 | 2353.5 |
| 14.4 | 1133.8 |
| 13.8 | 1159.2 |
| 13.6 | 2616.7 |
| 13.3 | 2545.1 |
| 15.3 | 2298 |
| 16.4 | 2227.9 |
| 15.3 | 2453.6 |
| 15.3 | 1182 |
| 17.9 | 1259.9 |
| 17 | 2528.1 |
| 16.8 | 2450 |
| 17.5 | 2498.5 |
| 18.3 | 2375.7 |
| 18 | 2364 |
| 20.1 | 1253 |
| 20.1 | 1182.4 |
| 21.7 | 2372.2 |
| 19.1 | 2400.8 |
| 21.1 | 2393.6 |
| 21.6 | 2167.9 |
| 24 | 2408.8 |
| 27.1 | 1290 |
| 24.8 | 1196.6 |
| 23.9 | 2455.4 |
| 24.2 | 2329.4 |
| 21 | 2202.8 |
| 20.7 | 2359.3 |
| 20.9 | 2377 |
| 22 | 1215.2 |
| 22 | 1078.6 |
| 22.3 | 2160.3 |
| 22.1 | 2251.2 |
| 22.7 | 2263.9 |
| 22.6 | 2148.4 |
| 22.7 | 2399.8 |
| 22.3 | 1220.4 |
| 25.3 | 1120.1 |
| 22.6 | 2382.6 |
| 24.8 | 2662.1 |
| 27.4 | 2849.4 |
| 28.2 | 2923.6 |
| 26.9 | 2731 |
| 29.1 | 1485.6 |
| 28.8 | 1511.2 |
| 28.1 | 2901.3 |
| 29.6 | 3032.4 |
| 29.9 | 3094.4 |
| 27.1 | 2854.8 |
| 26.7 | 2834.6 |
| 26.7 | 1353.5 |
| 24.8 | 1344.5 |
| 26.8 | 2752.8 |
| 28.9 | 2961.8 |
| 30.5 | 3162.3 |
| 29.8 | 3064.1 |
| 28.9 | 1570.8 |
| 26.7 | 1468.5 |
| 26.7 | 2666.4 |
| 28 | 2913.2 |
| 31.3 | 3214.6 |
| 32.7 | 3378.3 |
| 31.7 | 3256.5 |
| 32.6 | 1609.1 |
| 31.7 | 1347.5 |
| 30.6 | 2903.5 |
| 28.5 | 2941.1 |
| 28.3 | 2856.8 |
| 30.8 | 3205.7 |
| 32.3 | 3263.8 |
| 33 | 1562 |
| 33.6 | 1557.2 |
| 32.4 | 3117.8 |
| 27.5 | 2712.4 |
| 28.9 | 2992.2 |
| 27.8 | 2988.1 |
| 30 | 3155.9 |
| 31.1 | 1561.6 |
| 32.2 | 1510.6 |
| 28.3 | 2791.1 |
| 29.9 | 2848.2 |
| 31 | 3054.4 |
| 28.8 | 2848.6 |
| 29.6 | 2977.3 |
| 32.1 | 1569.9 |
| 34.6 | 1652.4 |
| 31.1 | 3175.8 |
| 28.6 | 2942.7 |
| 30.2 | 3168.3 |
| 33 | 3323.9 |
| 32.1 | 3261.9 |
| 32.5 | 1712.1 |
| 30.6 | 1579.2 |
| 30.1 | 3124.9 |
| 34 | 3397.9 |
| 31.7 | 3329.6 |
| 35.1 | 3578.5 |
| 33.5 | 3492.7 |
| 32.3 | 1588.5 |
| 33.8 | 1585.4 |
| 30 | 3036.2 |
| 30.5 | 3056.5 |
| 33.8 | 3319.4 |
| 34.6 | 3390.2 |
| 34 | 3276.4 |
| 37.1 | 1675.2 |
| 36.4 | 1680.8 |
| 34.7 | 3392.2 |
| 33.8 | 3429.9 |
| 32.7 | 3395 |
| 33.1 | 3217.6 |
| 34.2 | 3566.9 |
| 34.2 | 1713.5 |
| 33.6 | 1534 |
| 31.2 | 2978.3 |
| 33 | 3267.7 |
| 32.9 | 3358 |
| 35.2 | 3531.8 |
| 35.1 | 3686.2 |
| 35 | 1677.4 |
| 33.5 | 1662.7 |
| 35 | 3660.8 |
| 35.9 | 3747.1 |
| 37.2 | 3768.8 |
| 38 | 3752.5 |
| 34.8 | 3564.5 |
| 33.2 | 1609.8 |
| 32.3 | 1602.4 |
| 33.9 | 3413.6 |
| 30.4 | 3169.3 |
| 28.5 | 2982.8 |
| 28.9 | 3056.2 |
| 29.4 | 3091.5 |
| 30.6 | 1545.3 |
| 31.6 | 1531.1 |
| 31.5 | 3322.2 |
| 30.6 | 3118.2 |
| 30.5 | 3089 |
| 31.6 | 3163.2 |
| 33.6 | 3358.9 |
| 33.9 | 1577.5 |
| 34.9 | 1610.9 |
| 32.7 | 3175.1 |
| 33.3 | 3296.6 |
| 32.5 | 3250.5 |
| 32.3 | 3488.4 |
| 32.8 | 3383 |
| 31.9 | 1676.6 |
| 31.1 | 1434.7 |
| 35.4 | 3636.3 |
| 33.5 | 3500.1 |
| 30.8 | 3159.2 |
| 30 | 2956.7 |
| 29 | 2990.6 |
| 31.3 | 1408.8 |
| 29.7 | 1325.3 |
| 31 | 3047.3 |
| 32.1 | 3202.5 |
| 30.4 | 3130.7 |
| 31.9 | 3184.5 |
| 31.1 | 2988.8 |
| 29.2 | 1280.1 |
| 28.3 | 1317.2 |
| 30.5 | 2961.7 |
| 29.3 | 2956.5 |
| 32.6 | 3317.4 |
| 28.8 | 2838.7 |
| 25 | 1294 |
| 24 | 1287.8 |
| 23.9 | 2569.6 |
| 25.7 | 2710.5 |
| 25.7 | 2692.8 |
| 26.8 | 2718.1 |
| 29.8 | 3032.9 |
| 27.9 | 1409 |
| 28.1 | 1433.8 |
| 24.9 | 2491.8 |
| 25.5 | 2475.5 |
| 25.8 | 2646.3 |
| 27.6 | 2869.3 |
| 26.1 | 2753 |
| 25.4 | 1271.9 |
| 25.6 | 1270.3 |
| 24.8 | 2447.4 |
| 24.1 | 2384 |
| 22.1 | 2225.1 |
| 23.6 | 2414.3 |
| 24.9 | 2556.7 |
| 24.8 | 1303.6 |
| 26.3 | 1448.9 |
| 25.6 | 2689.4 |
| 23.8 | 2537 |
| 24.2 | 2512 |
| 24.2 | 2543.6 |
| 23.7 | 2319 |
| 24.9 | 1119.1 |
| 23.3 | 1122.6 |
| 21.5 | 2287.9 |
| 23.3 | 2319.8 |
| 21.3 | 2101.5 |
| 25.5 | 2631.1 |
| 25.1 | 2567.2 |
| 20.3 | 1064.6 |
| 20.4 | 1015.5 |
| 20 | 2217.2 |
| 20.7 | 2318 |
| 18 | 2229.6 |
| 18.5 | 2422.9 |
| 14.3 | 2556.3 |
| 17.5 | 973 |
| 15.4 | 1132.1 |
| 15.7 | 2353.1 |
| 17.3 | 2260.3 |
| 16.9 | 2303.9 |
| 19.6 | 2451 |
| 18.7 | 2504 |
| 19.2 | 1174.5 |
| 18.9 | 1218.9 |
| 16.7 | 2356.5 |
| 16.6 | 2399.9 |
| 16.7 | 2327.5 |
| 13.1 | 2470.5 |
| 13.9 | 2244.6 |
| 17.3 | 1041.2 |
| 14 | 1080 |
| 14.2 | 2227.7 |
| 15.6 | 2190.2 |
| 13.9 | 2435.8 |
| 12.9 | 2606.5 |
| 10.7 | 1416.9 |
| 10.4 | 1332.9 |
| 11.3 | 2544.6 |
| 12.6 | 2544.6 |
| 13 | 2551 |
| 13.1 | 2582.6 |
| 15.8 | 2432.2 |
| 13.8 | 1298.7 |
| 12.3 | 1196.1 |
| 10.9 | 2514 |
| 8.5 | 2761.9 |
| 10.3 | 2630.1 |
| 9.3 | 2718 |
| 11.8 | 2563.6 |
| 13.9 | 1158.9 |
| 15.8 | 1100.6 |
| 13.8 | 2491.6 |
| 14.8 | 2460 |
| 14.1 | 2445.1 |
| 12.4 | 2488.4 |
| 12.6 | 2529 |
| 12.7 | 1251.4 |
| 13.1 | 1273 |
| 12 | 2395.6 |
| 10.4 | 2409.1 |
| 10.3 | 2567.2 |
| 10.2 | 2638.7 |
| 14.1 | 2418.5 |
| 13.1 | 1094.6 |
| 11.8 | 1267.8 |
| 12.3 | 2551.4 |
| 11 | 2543.3 |
| 9.3 | 2675.8 |
| 8.4 | 2706.7 |
| 8.9 | 2634.8 |
| 8.7 | 1384.5 |
| 8.3 | 1417.1 |
| 7.7 | 2804.7 |
| 6.8 | 2688.1 |
| 7.3 | 2726.9 |
| 8.4 | 2748 |
| 8.2 | 2677.3 |
| 9 | 1293.3 |
| 9.9 | 1367.7 |
| 10.3 | 2675 |
Is the correlation real or sampling noise? The population correlation is written (rho). To test , convert into a statistic with degrees of freedom. A confidence interval for uses Fisher's -transformation (the lab reports it).
- the sample correlation
- the number of pairs
Worked example
Training volume and race time
In the running club data (500 finishers), is weekly training distance correlated with finish time?
Estimate. : runners who train more tend to finish faster (lower times).
Test. on 498 df, two-sided p < 0.0001.
Interval. 95% CI for : (−0.689, −0.585).
Strength. : about 41% of the variation in finish times is associated with a straight-line relationship to training volume.
A moderately strong negative linear association, far too strong to be sampling noise. It does not show that training causes faster times — fitter runners may simply choose to train more (see Correlation is not causation).
Quick check
A dataset records distance in kilometres and time in minutes, with . The analyst converts distance to miles and time to hours. What happens to ?
Exercises
The lesson is complete when every exercise is answered correctly. Hints are free; viewing a worked solution before solving means the exercise won't count towards mastery.
0 of 8 exercises correct (not saved — sign in to keep your progress)
Exercise 1 · Guided, difficulty 1 of 3
Covariance and r from seven pairs
Seven students recorded their weekly study hours (: 10, 11, 2, 3, 6, 8, 13) and their quiz scores (, out of 100: 94, 102, 57, 55, 76, 66, 106). Calculate the sample covariance and Pearson's correlation . Give the covariance to 2 decimal places and to 3.
Study hours x (n = 7)
10, 11, 2, 3, 6, 8, 13
Quiz score y (n = 7)
94, 102, 57, 55, 76, 66, 106
Exercise 2 · Practice, difficulty 1 of 3
Write the formula for r
Write an expression for Pearson's correlation in terms of the sum of cross-products Sxy and the sums of squares Sxx and Syy.
Exercise 3 · Practice, difficulty 1 of 3
Match the scatter to r
Match each description of a scatter plot (50 points each) to the most plausible Pearson correlation.
Exercise 4 · Practice, difficulty 2 of 3
Is the correlation significant?
A survey of employees found a correlation of between weekly overtime hours and a wellbeing score. Test against a two-sided alternative: give the statistic (2 decimal places) and the p-value (4 decimal places).
Exercise 5 · Application, difficulty 2 of 3
Training and finish time
In the running_club dataset, test the correlation between training_km_week and finish_min (Pearson, two-sided). Report , the statistic and the 95% confidence interval for . Use the lab's Test tab (correlation).
Data: running_club · Open in the lab to explore and calculate.
Exercise 6 · Challenge, difficulty 2 of 3
One unusual point
Nine machines' ages (, years) and annual maintenance costs (, thousand SAR) are shown. A tenth machine, aged 22 years with a cost of 20.9 thousand SAR (it was rebuilt last year), is then added.
Calculate for the nine machines and for all ten. Give both to 3 decimal places.
Age x (years) (n = 9)
5, 7, 10, 4, 6, 8, 1, 3, 2
Cost y (k SAR) (n = 9)
29, 34.8, 40.9, 29.6, 32.7, 33.7, 26.3, 27.4, 23.9
Exercise 7 · Interpretation, difficulty 2 of 3
A weak r for a strong relationship
For the 360 days with a meter reading in energy_use, the correlation between outdoor_temp_c and energy_kwh is . Plot the data in the lab (Visualise → scatter). Which conclusion is justified?
Data: energy_use · Open in the lab to explore and calculate.
Exercise 8 · Interpretation, difficulty 2 of 3
Report the correlation
For the running club, between weekly training distance and 10 km finish time (n = 500, 95% CI −0.69 to −0.58). Write two or three sentences for the club newsletter describing the relationship: its direction, its strength, and what it does not show.
What you can now do
- Describe a scatter plot by direction, form and strength
- Calculate the sample covariance and Pearson's r, and explain why r has no units
- Test H0: ρ = 0 and report a confidence interval for a correlation
- Recognise when r misleads: curved relationships and influential outliers