From Paired Values to Correlation
A scatterplot can show whether two quantitative variables tend to increase together or move in opposite directions. Pearson’s correlation summarizes the direction and strength of their linear pattern with one number. To calculate it, we first compare each observation with its variable’s mean, then examine whether the resulting deviations tend to have the same or opposite signs.
The average product of paired standardized deviations gives correlation. The same calculation can be organized in a useful alternative way: add the products of the centered, unstandardized observations to obtain a sample covariance, then divide by the two sample standard deviations. This tutorial derives that relationship and applies it to hypothetical datasets.
This definition treats the paired observations as the data being summarized; it does not require a probability model. Before calculating, the \(x_i\) and \(y_i\) values must refer to the same individual or observational unit in each pair. Both variables must be quantitative, and both sample standard deviations must be greater than zero. If either variable is constant, the correlation formula divides by zero and correlation is undefined.
The covariance’s sign provides a directional clue. If an observation is above its \(x\)-mean and also above its \(y\)-mean, its centered product is positive. If it is above one mean but below the other, its product is negative. Covariance is measured in the product of the variables’ units, however, so its numerical size is not a unit-free measure of linear association. Dividing by the two standard deviations removes those units.
Deriving the Covariance Formula
For each pair, standardize both observations by subtracting the relevant sample mean and dividing by that variable’s sample standard deviation. The average product of these paired standardized values is Pearson’s sample correlation. Writing that definition in terms of the original deviations gives the covariance formula.
The standard deviations are constant across observations, so their product can be taken outside the sum. The remaining sum of centered cross-products is \((n-1)\operatorname{cov}(x,y)\), by the sample covariance definition. Substituting that identity completes the derivation.
The \(n-1\) factors cancel because covariance and sample standard deviations are all defined using the sample convention. This also explains why the centered cross-products, covariance, and correlation carry the same directional information: each uses the same paired deviations, while the standard deviations scale the result.
Worked Calculations
A reliable hand calculation keeps three quantities distinct: the sum of centered cross-products, the sample covariance, and the final correlation. The first is a sum, the second divides that sum by \(n-1\), and the last divides the covariance by both sample standard deviations.
Worked Example: A Positive Association
Suppose a hypothetical class records study hours \(x\) and quiz points \(y\) for four students. The paired data are \((1,2),(2,3),(3,5),(4,6)\). Calculate the correlation using covariance.
Check the setup. Each study-hour value is paired with the quiz score from the same student. Both variables are quantitative, and neither is constant, so the sample standard deviations are nonzero.
Find the means. $$ \bar{x}=\frac{1+2+3+4}{4}=2.5, \qquad \bar{y}=\frac{2+3+5+6}{4}=4. $$
The centered values are \(x_i-\bar{x}=(-1.5,-0.5,0.5,1.5)\) and \(y_i-\bar{y}=(-2,-1,1,2)\). Their paired products are \(3,0.5,0.5,3\), so their sum is \(7\). With \(n-1=3\), $$ \operatorname{cov}(x,y)=\frac{7}{3}=2.333. $$
The sums of squared deviations are \(5\) for \(x\) and \(10\) for \(y\). Therefore, $$ s_x=\sqrt{\frac{5}{3}}=1.291, \qquad s_y=\sqrt{\frac{10}{3}}=1.826. $$
Now divide the covariance by the product of the standard deviations: $$ r=\frac{2.333}{(1.291)(1.826)} =\frac{7/3}{\sqrt{5/3}\sqrt{10/3}} =\frac{7}{\sqrt{50}} \approx 0.9899. $$
The calculated correlation is about \(0.990\), indicating a strong positive linear association in these four hypothetical pairs. The correlation is unit-free; covariance here is in study-hours times quiz-points.
Worked Example: A Negative Association
A hypothetical researcher records a device’s operating time \(x\), in hours, and its remaining battery percentage \(y\) at four scheduled checks: \((1,8),(2,6),(3,5),(4,1)\). Calculate the sample covariance and correlation.
Check the setup. Each time and percentage are measured together at one check, both are quantitative, and both vary. The pairs can therefore be summarized using sample covariance and correlation.
The means are $$ \bar{x}=\frac{1+2+3+4}{4}=2.5, \qquad \bar{y}=\frac{8+6+5+1}{4}=5. $$
The centered values are \((-1.5,-0.5,0.5,1.5)\) for \(x\), and \((3,1,0,-4)\) for \(y\). The products are \(-4.5,-0.5,0,-6\), with sum \(-11\). Thus, $$ \operatorname{cov}(x,y)=\frac{-11}{4-1}=-\frac{11}{3}=-3.667. $$
The squared deviations sum to \(5\) for \(x\), and \(9+1+0+16=26\) for \(y\). Hence, $$ s_x=\sqrt{\frac{5}{3}}, \qquad s_y=\sqrt{\frac{26}{3}}. $$ The correlation is $$ r=\frac{-11/3}{\sqrt{5/3}\sqrt{26/3}} =-\frac{11}{\sqrt{130}} \approx -0.9648. $$
These hypothetical measurements have a strong negative linear association: higher recorded operating times tend to occur with lower remaining battery percentages. The covariance’s units are hours times percentage points; the correlation has no units.
Worked Example: Keeping the Calculation Organized
For four hypothetical plants, let \(x\) be height in centimeters and \(y\) be a leaf-count measure. The paired observations are \((2,1),(4,4),(6,3),(8,8)\). Find \(r\) by tabulating centered products and squared deviations.
Check the setup. The height and leaf count are paired by plant, are quantitative, and both vary. Thus both sample standard deviations are positive.
The means are \(\bar{x}=5\) and \(\bar{y}=4\). The centered values, their products, and squared deviations are:
| Pair | \(x_i-\bar{x}\) | \(y_i-\bar{y}\) | Product | \((x_i-\bar{x})^2\) | \((y_i-\bar{y})^2\) |
|---|---|---|---|---|---|
| (2, 1) | -3 | -3 | 9 | 9 | 9 |
| (4, 4) | -1 | 0 | 0 | 1 | 0 |
| (6, 3) | 1 | -1 | -1 | 1 | 1 |
| (8, 8) | 3 | 4 | 12 | 9 | 16 |
| Sum | 20 | 20 | 26 |
There are \(n=4\) pairs, so the covariance is \(20/3=6.667\). The sample standard deviations are $$ s_x=\sqrt{\frac{20}{3}}=2.582, \qquad s_y=\sqrt{\frac{26}{3}}=2.944. $$
Therefore, $$ r=\frac{20/3}{\sqrt{20/3}\sqrt{26/3}} =\frac{20}{\sqrt{520}} \approx 0.8771. $$
The hypothetical data show a positive linear association, with \(r\approx0.877\). The table makes an important bookkeeping point visible: the correlation depends on pairing each \(x\)-deviation with its own \(y\)-deviation. Rearranging one variable’s values among the plants would change the centered cross-products.
Denominators, Units, and Common Mistakes
In the sample covariance formula, the divisor is \(n-1\), matching the divisor used in the sample variances from which \(s_x\) and \(s_y\) are calculated. A frequent error is to divide the cross-product sum by \(n\) to find covariance but then use sample standard deviations based on \(n-1\). That mixes conventions and does not produce the stated covariance formula for \(r\).
There is a subtle distinction: if someone instead divides the cross-product sum by \(n\) and divides each sum of squared deviations by \(n\), all three quantities use the same alternative convention, and those \(n\) factors also cancel in their ratio. That is not the sample covariance convention defined here. For a standard sample calculation, use \(n-1\) consistently.
- Wrong reasoning: “The centered cross-products sum to \(7\), so the correlation is \(7\).” Correction: \(7\) is a sum with units, not correlation. Divide by \(n-1\) for sample covariance, then by \(s_xs_y\).
- Wrong reasoning: “The covariance is positive, so the variables have a strong association.” Correction: Its sign indicates direction, but its magnitude depends on the measurement units. Use correlation to summarize linear association on a unit-free scale.
- Wrong reasoning: “I can multiply the \(x\)-values from one individual by the \(y\)-values from another.” Correction: Centered cross-products must use the original matched pairs.
- Wrong reasoning: “A correlation calculation itself proves that one variable causes the other.” Correction: Correlation describes a linear pattern in paired data; by itself, it does not establish causation.
Check Your Understanding
Answer each question using the definitions and calculations in this tutorial.
- For paired data with \(n=6\), the centered cross-products sum to \(-15\). What is the sample covariance?
- Why must the \(x\)- and \(y\)-values be matched by observational unit when calculating covariance?
- If \(\operatorname{cov}(x,y)=12\), \(s_x=3\), and \(s_y=2\), calculate \(r\). What does its sign indicate?
- Explain why covariance has units but correlation does not.
- What happens to the covariance formula for correlation if one variable has sample standard deviation zero?