Tutorials › Introduction to Statistics › The Covariance Formula for Correlation

Scatterplots and Correlation · Tutorial 233 of 996

The Covariance Formula for Correlation

Learn how centered cross-products form sample covariance and how dividing by both sample standard deviations gives Pearson’s correlation.

Beginner 9 min read

What You'll Learn

  • Define sample covariance using paired observations and centered cross-products.
  • Derive the covariance formula for Pearson’s correlation from standardized values.
  • Calculate sample covariance and correlation from a small dataset.
  • Check calculations using sums of squared deviations and the sample standard deviations.
  • Recognize common errors involving mismatched denominators and covariance units.

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.

Definition: For \(n\) paired observations \((x_i,y_i)\), the sample covariance is $$ \operatorname{cov}(x,y)=\frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})}{n-1}. $$ It summarizes whether deviations from the two sample means tend to have the same sign or opposite signs. The sample standard deviations are \(s_x\) and \(s_y\), calculated from the \(x\)- and \(y\)-values using the same \(n-1\) denominator.

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.

Definition: If \(s_x>0\) and \(s_y>0\), Pearson’s sample correlation is $$ r=\frac{1}{n-1}\sum_{i=1}^{n} \left(\frac{x_i-\bar{x}}{s_x}\right) \left(\frac{y_i-\bar{y}}{s_y}\right). $$

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.

$$ \begin{aligned} r &=\frac{1}{n-1}\sum_{i=1}^{n} \left(\frac{x_i-\bar{x}}{s_x}\right) \left(\frac{y_i-\bar{y}}{s_y}\right)\\ &=\frac{1}{(n-1)s_xs_y} \sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})\\ &=\frac{(n-1)\operatorname{cov}(x,y)} {(n-1)s_xs_y}\\ &=\frac{\operatorname{cov}(x,y)}{s_xs_y}. \end{aligned} $$

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.

Theorem: For paired sample data with \(s_x>0\) and \(s_y>0\), $$ r=\frac{\operatorname{cov}(x,y)}{s_xs_y}. $$ Use \(\operatorname{cov}(x,y)=\sum (x_i-\bar{x})(y_i-\bar{y})/(n-1)\), and calculate both sample standard deviations with denominator \(n-1\).

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-3999
(4, 4)-10010
(6, 3)1-1-111
(8, 8)3412916
Sum202026

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.
Key takeaway: Center each variable at its own sample mean, multiply deviations within each pair, and sum. Dividing by \(n-1\) gives sample covariance; dividing that covariance by \(s_xs_y\) gives Pearson’s correlation.

Check Your Understanding

Answer each question using the definitions and calculations in this tutorial.

  1. For paired data with \(n=6\), the centered cross-products sum to \(-15\). What is the sample covariance?
  2. Why must the \(x\)- and \(y\)-values be matched by observational unit when calculating covariance?
  3. If \(\operatorname{cov}(x,y)=12\), \(s_x=3\), and \(s_y=2\), calculate \(r\). What does its sign indicate?
  4. Explain why covariance has units but correlation does not.
  5. What happens to the covariance formula for correlation if one variable has sample standard deviation zero?