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.

35 minAdvanced8 graded exercisesFree

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 xx values go with larger yy 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?
Scatter plot of weekly units sold against TV spend: a clear positive trend that flattens slightly at high spend, with many weeks at zero spend.TV spend (thousand SAR)Units sold (packs)
Weekly TV spend and units sold for a juice brand (156 weeks, synthetic). The column of points at zero spend are weeks between TV campaigns.
Show the data as a table
TV spend (thousand SAR)Units sold (packs)
04367
05380
75.37627
61.37919
62.66688
58.67884
667739
05879
04796
06707
05775
607577
67.78206
72.88885
72.37832
739029
658250
68.48799
698145
05831
06570
05668
06953
06784
49.48785
60.99182
51.29505
64.18240
07408
07306
06773
06541
55.38455
54.58957
53.79229
53.18703
55.17460
51.98039
05905
05477
06114
80.88945
75.19202
93.59112
85.58624
86.68919
06021
05725
05676
05329
83.58201
62.97164
75.58819
79.18230
05154
05265
06139
82.98482
75.98031
89.79162
88.78621
79.19362
88.710276
93.98955
788466
06361
06240
06718
06151
05667
106.39681
106.79296
96.49560
123.910197
100.610482
07032
07468
06456
06870
07602
07245
37.98807
42.17896
35.59124
36.48515
40.58897
41.69238
38.67039
05860
06382
05542
05216
05785
06580
109.69390
1178750
105.19130
122.910112
114.59322
1239794
06321
07010
05697
05463
05649
06896
05482
81.78272
92.38680
85.88411
91.38105
87.79232
05353
07431
06588
06596
06173
06563
89.49363
94.99345
92.48873
94.29133
92.59302
91.310174
07128
06167
07733
06950
102.29416
111.710983
106.211032
123.89927
959762
118.411045
122.811248
07315
07662
07385
07136
36.88404
36.77713
34.98159
33.88526
35.38267
39.17926
06386
06829
06636
06738
06582
06256
27.97878
29.87685
28.48746
28.76855
06151

Covariance measures whether the two variables move together. For each observation, multiply how far xx is from its mean by how far yy 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.

sxy=Cov⁡(x,y)=1n−1∑i=1n(xi−xˉ)(yi−yˉ)=Sxyn−1s_{xy} = \Cov(x, y) = \frac{1}{n-1}\sum_{i=1}^{n}(x_i - \bar{x})(y_i - \bar{y}) = \frac{S_{xy}}{n-1}
Sample covariance
xi,yix_i, y_i
the ii-th pair of observations
xˉ,yˉ\bar{x}, \bar{y}
the two sample means
SxyS_{xy}
the sum of cross-products ∑(xi−xˉ)(yi−yˉ)\sum (x_i - \bar{x})(y_i - \bar{y})
nn
the number of pairs; dividing by n−1n - 1 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 rr.

r=sxysx sy=SxySxx Syyr = \frac{s_{xy}}{s_x\, s_y} = \frac{S_{xy}}{\sqrt{S_{xx}\, S_{yy}}}
Pearson's correlation coefficient
sx,sys_x, s_y
the sample standard deviations of xx and yy
Sxx,SyyS_{xx}, S_{yy}
the sums of squared deviations ∑(xi−xˉ)2\sum (x_i - \bar{x})^2 and ∑(yi−yˉ)2\sum (y_i - \bar{y})^2

Worked example

Covariance and r by hand

Five pairs: xx = 1, 2, 3, 4, 5 and yy = 2, 4, 5, 4, 5. Find the covariance and the correlation.

  1. Means. xˉ=3\bar{x} = 3, yˉ=4\bar{y} = 4.

  2. Deviations. x−xˉx - \bar{x}: −2, −1, 0, 1, 2. y−yˉy - \bar{y}: −2, 0, 1, 0, 1.

  3. Cross-products (x−xˉ)(y−yˉ)(x - \bar{x})(y - \bar{y}): 4, 0, 0, 0, 2, so Sxy=6S_{xy} = 6. Also Sxx=4+1+0+1+4=10S_{xx} = 4 + 1 + 0 + 1 + 4 = 10 and Syy=4+0+1+0+1=6S_{yy} = 4 + 0 + 1 + 0 + 1 = 6.

  4. Covariance. sxy=6/(5−1)=1.50s_{xy} = 6/(5 - 1) = 1.50.

  5. Correlation. r=6/10×6=6/60=0.7746r = 6/\sqrt{10 \times 6} = 6/\sqrt{60} = 0.7746.

Covariance 1.50; r=0.775r = 0.775 — a fairly strong positive linear association.

PropertyWhat it means in practice
−1≤r≤1-1 \le r \le 1r=±1r = \pm 1 only when every point lies exactly on a straight line
No unitsChanging kilometres to miles or minutes to hours leaves rr unchanged (covariance changes)
SymmetricThe correlation of xx with yy equals the correlation of yy with xx
Linear onlyrr measures how closely points follow a straight line; a strong curve can give rr near 0
Not resistantOne extreme point can create, destroy or reverse a correlation
Properties of rr
Loading the interactive…
Generate data with a chosen correlation, then switch the pattern to curved or add an outlier.

Try this

  • Move the target rr from 0.2 to 0.9. At roughly what value does the trend become obvious by eye?

  • Switch the pattern to curved. The relationship is perfectly clear, yet Pearson's rr can be close to 0 — compare it with Spearman's.

  • Switch to outlier and drag the single point far from the rest. How much can one point change rr when n=20n = 20? And when n=200n = 200?

Scatter plot of daily energy use against temperature: two U-shaped bands (working days higher, weekends lower), rising steeply above about 22 degrees.Outdoor temperature (°C)Energy use (kWh)
Daily energy use against outdoor temperature for one office building (synthetic). Pearson's r is only 0.31 because the pattern is a curve, not a line.
Show the data as a table
Outdoor temperature (°C)Energy use (kWh)
9.42711.1
12.32658.9
13.32560.5
10.22670.3
10.82549.4
7.61320.4
5.61446.6
9.32721.3
8.92689.9
8.22664
9.32533.5
11.12555.6
7.81263
7.91312.8
7.62809.5
9.52656.7
9.32797.6
102673.8
10.22776.5
6.61319.4
6.61528
6.42897.5
9.52680.7
11.72471.4
11.22509.6
10.81338
9.91309.9
7.32723.8
6.52791.4
6.32728.4
7.62878.2
9.12609.5
8.41474.9
8.61413
12.42383.9
12.52308.3
12.72454.6
13.12439.8
11.72531.7
11.51234.6
13.41162
12.82273.3
13.22337.3
14.22386.4
142469.5
12.71427.1
11.91271.3
11.52543.8
12.12548.1
13.92465
122561.1
14.12303
13.41054
10.71159.2
11.22546.3
122333.9
12.52397.7
11.92440
11.32395.2
11.31156.4
12.81203.2
15.62299.3
13.62448.1
14.12440.2
14.32410.8
13.42394.3
14.11156.6
14.21098.7
11.12512.2
10.82624.7
10.72503.1
10.62469
13.22324.4
13.51146.2
15.81098.5
16.22332.2
17.92401.6
16.22337.9
14.82469.3
16.92528.5
17.21145.8
17.81255.2
20.22311.1
18.82302
17.72293
172226.6
142353.5
14.41133.8
13.81159.2
13.62616.7
13.32545.1
15.32298
16.42227.9
15.32453.6
15.31182
17.91259.9
172528.1
16.82450
17.52498.5
18.32375.7
182364
20.11253
20.11182.4
21.72372.2
19.12400.8
21.12393.6
21.62167.9
242408.8
27.11290
24.81196.6
23.92455.4
24.22329.4
212202.8
20.72359.3
20.92377
221215.2
221078.6
22.32160.3
22.12251.2
22.72263.9
22.62148.4
22.72399.8
22.31220.4
25.31120.1
22.62382.6
24.82662.1
27.42849.4
28.22923.6
26.92731
29.11485.6
28.81511.2
28.12901.3
29.63032.4
29.93094.4
27.12854.8
26.72834.6
26.71353.5
24.81344.5
26.82752.8
28.92961.8
30.53162.3
29.83064.1
28.91570.8
26.71468.5
26.72666.4
282913.2
31.33214.6
32.73378.3
31.73256.5
32.61609.1
31.71347.5
30.62903.5
28.52941.1
28.32856.8
30.83205.7
32.33263.8
331562
33.61557.2
32.43117.8
27.52712.4
28.92992.2
27.82988.1
303155.9
31.11561.6
32.21510.6
28.32791.1
29.92848.2
313054.4
28.82848.6
29.62977.3
32.11569.9
34.61652.4
31.13175.8
28.62942.7
30.23168.3
333323.9
32.13261.9
32.51712.1
30.61579.2
30.13124.9
343397.9
31.73329.6
35.13578.5
33.53492.7
32.31588.5
33.81585.4
303036.2
30.53056.5
33.83319.4
34.63390.2
343276.4
37.11675.2
36.41680.8
34.73392.2
33.83429.9
32.73395
33.13217.6
34.23566.9
34.21713.5
33.61534
31.22978.3
333267.7
32.93358
35.23531.8
35.13686.2
351677.4
33.51662.7
353660.8
35.93747.1
37.23768.8
383752.5
34.83564.5
33.21609.8
32.31602.4
33.93413.6
30.43169.3
28.52982.8
28.93056.2
29.43091.5
30.61545.3
31.61531.1
31.53322.2
30.63118.2
30.53089
31.63163.2
33.63358.9
33.91577.5
34.91610.9
32.73175.1
33.33296.6
32.53250.5
32.33488.4
32.83383
31.91676.6
31.11434.7
35.43636.3
33.53500.1
30.83159.2
302956.7
292990.6
31.31408.8
29.71325.3
313047.3
32.13202.5
30.43130.7
31.93184.5
31.12988.8
29.21280.1
28.31317.2
30.52961.7
29.32956.5
32.63317.4
28.82838.7
251294
241287.8
23.92569.6
25.72710.5
25.72692.8
26.82718.1
29.83032.9
27.91409
28.11433.8
24.92491.8
25.52475.5
25.82646.3
27.62869.3
26.12753
25.41271.9
25.61270.3
24.82447.4
24.12384
22.12225.1
23.62414.3
24.92556.7
24.81303.6
26.31448.9
25.62689.4
23.82537
24.22512
24.22543.6
23.72319
24.91119.1
23.31122.6
21.52287.9
23.32319.8
21.32101.5
25.52631.1
25.12567.2
20.31064.6
20.41015.5
202217.2
20.72318
182229.6
18.52422.9
14.32556.3
17.5973
15.41132.1
15.72353.1
17.32260.3
16.92303.9
19.62451
18.72504
19.21174.5
18.91218.9
16.72356.5
16.62399.9
16.72327.5
13.12470.5
13.92244.6
17.31041.2
141080
14.22227.7
15.62190.2
13.92435.8
12.92606.5
10.71416.9
10.41332.9
11.32544.6
12.62544.6
132551
13.12582.6
15.82432.2
13.81298.7
12.31196.1
10.92514
8.52761.9
10.32630.1
9.32718
11.82563.6
13.91158.9
15.81100.6
13.82491.6
14.82460
14.12445.1
12.42488.4
12.62529
12.71251.4
13.11273
122395.6
10.42409.1
10.32567.2
10.22638.7
14.12418.5
13.11094.6
11.81267.8
12.32551.4
112543.3
9.32675.8
8.42706.7
8.92634.8
8.71384.5
8.31417.1
7.72804.7
6.82688.1
7.32726.9
8.42748
8.22677.3
91293.3
9.91367.7
10.32675

Is the correlation real or sampling noise? The population correlation is written ρ\rho (rho). To test H0:ρ=0H_0: \rho = 0, convert rr into a tt statistic with n−2n - 2 degrees of freedom. A confidence interval for ρ\rho uses Fisher's zz-transformation (the lab reports it).

t=rn−21−r2,df=n−2t = r\sqrt{\frac{n-2}{1-r^2}}, \qquad df = n - 2
Test of H0: ρ = 0
rr
the sample correlation
nn
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?

  1. Estimate. r=−0.640r = -0.640: runners who train more tend to finish faster (lower times).

  2. Test. t=−0.6396498/(1−(−0.6396)2)=−18.57t = -0.6396\sqrt{498/(1 - (-0.6396)^2)} = -18.57 on 498 df, two-sided p < 0.0001.

  3. Interval. 95% CI for ρ\rho: (−0.689, −0.585).

  4. Strength. r2=0.409r^2 = 0.409: 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 r=−0.64r = -0.64. The analyst converts distance to miles and time to hours. What happens to rr?

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

Not started

Seven students recorded their weekly study hours (xx: 10, 11, 2, 3, 6, 8, 13) and their quiz scores (yy, out of 100: 94, 102, 57, 55, 76, 66, 106). Calculate the sample covariance and Pearson's correlation rr. Give the covariance to 2 decimal places and rr 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

Not started

Write an expression for Pearson's correlation rr 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

Not started

Match each description of a scatter plot (50 points each) to the most plausible Pearson correlation.

Points rise from left to right and lie close to a straight line
Points fall from left to right, tightly along a straight line
Points fall from left to right, with a loose but visible downward drift
A cloud with only a slight upward tilt
A symmetric U shape: high at both ends, low in the middle

Exercise 4 · Practice, difficulty 2 of 3

Is the correlation significant?

Not started

A survey of n=40n = 40 employees found a correlation of r=0.35r = 0.35 between weekly overtime hours and a wellbeing score. Test H0:ρ=0H_0: \rho = 0 against a two-sided alternative: give the tt statistic (2 decimal places) and the p-value (4 decimal places).

Exercise 5 · Application, difficulty 2 of 3

Training and finish time

Not started

In the running_club dataset, test the correlation between training_km_week and finish_min (Pearson, two-sided). Report rr, the tt statistic and the 95% confidence interval for ρ\rho. 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

Not started

Nine machines' ages (xx, years) and annual maintenance costs (yy, 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 rr 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

Not started

For the 360 days with a meter reading in energy_use, the correlation between outdoor_temp_c and energy_kwh is r=0.31r = 0.31. Plot the data in the lab (Visualise → scatter). Which conclusion is justified?

Data: energy_use · Open in the lab to explore and calculate.

Choose one

Exercise 8 · Interpretation, difficulty 2 of 3

Report the correlation

Not started

For the running club, r=−0.64r = -0.64 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.

0 words

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