Skip to content
Glacius
ProbabilityConcept reference

Covariance

Covariance is the expectation of the product of two variables’ centered deviations.

On this page 9 sections
  1. Overview
  2. Understand the idea
  3. The binary table (0.1,0.2);(0.3,0.4) gives 0.4-0.7(0.6)=-0.02
  4. Use the actual support values, including their signs
  5. A closer look
  6. Key takeaway
  7. Further questions
  8. Sources & further reading
  9. Concept connections

01Understand the idea#

Covariance measures whether two variables tend to deviate from their means in the same direction. Pair deviations from the same observation, multiply them, and average with the distribution’s weights. Positive products represent same-direction deviations; negative products represent opposite-direction deviations.

Covariance averages how two variables deviate from their means together. A convenient calculation uses three expectations from the joint distribution.

Joint rows X=0,1 and columns Y=0,1 are (0.1,0.2) and (0.3,0.4). From these paired masses E[X]=0.7, E[Y]=0.6 and E[XY]=0.4. Covariance subtracts 0.7 times 0.6 from 0.4 to give −0.02.Joint rows X=0,1 and columns Y=0,1 are (0.1,0.2) and (0.3,0.4). From these paired masses E[X]=0.7, E[Y]=0.6 and E[XY]=0.4. Covariance subtracts 0.7 times 0.6 from 0.4 to give −0.02.
Figure 1Joint rows X=0,1 and columns Y=0,1 are (0.1,0.2) and (0.3,0.4). From these paired masses E[X]=0.7, E[Y]=0.6 and E[XY]=0.4. Covariance subtracts 0.7 times 0.6 from 0.4 to give −0.02.
Link to this figure ↗Download SVGDownload PNG

For binary X,YX,Y with joint rows (0.1,0.2);(0.3,0.4)(0.1,0.2);(0.3,0.4), the means are 0.7 and 0.6. Only (1,1)(1,1) contributes to E[XY]=0.4E[XY]=0.4.

Cov(X,Y)=E[XY]E[X]E[Y]\begin{gathered}\operatorname{Cov}(X,Y)\\=E[XY]\\-E[X]E[Y]\end{gathered}

Suppose equally likely outcomes are (X,Y)=(1,2)(X,Y)=(1,2) and (3,6)(3,6). Their means are 2 and 4. Deviation products are (1)(2)=2(-1)(-2)=2 and (1)(2)=2(1)(2)=2, so covariance is 2. Pairing an XX from one outcome with a YY from another would describe a different joint distribution.

Check your reasoning

Rows X=(0,1)X=(0,1); columns Y=(0,1)Y=(0,1). Joint rows: (0.5,0);(0,0.5)(0.5,0);(0,0.5). Find Cov(X,Y)\operatorname{Cov}(X,Y).

Show answer and explanation
0.25

0.5(0.5)(0.5)=0.250.5-(0.5)(0.5)=0.25.

02The binary table (0.1,0.2);(0.3,0.4) gives 0.4-0.7(0.6)=-0.02#

The binary table (0.1,0.2);(0.3,0.4)(0.1,0.2);(0.3,0.4) gives 0.40.7(0.6)=0.020.4-0.7(0.6)=-0.02. Returning 0.4 would omit centering. Positive covariance favors same-sign centered deviations; negative covariance favors opposite signs.

Check your reasoning

X rows, Y cols; each 0,1. Joint rows: (0.1,0.4);(0.3,0.2)(0.1,0.4);(0.3,0.2). A learner uses E[XY]E[XY]. Give covariance.

  1. A0.20.2
  2. B0.1-0.1
  3. C0.30.3
Show answer and explanation
0.1-0.1

0.20.3=0.10.2-0.3=-0.1.

03Use the actual support values, including their signs#

Use the actual support values, including their signs. Compute both means and the product mean before subtracting; joint cells need not have equal weights.

Zero covariance does not prove independence. Let XX be uniform on 1,0,1-1,0,1 and Y=X2Y=X^2. Then E[X]=E[XY]=0E[X]=E[XY]=0, so covariance is zero.

Yet P(X=0,Y=0)=1/3P(X=0,Y=0)=1/3, while P(X=0)P(Y=0)=1/9P(X=0)P(Y=0)=1/9. This cell fails the independence test.

Check your reasoning

X,Y are feature values. Rows X=(1,1)X=(-1,1); columns Y=(0,2)Y=(0,2). Joint rows: (0.2,0.3);(0.3,0.2)(0.2,0.3);(0.3,0.2). Find Cov(X,Y)\operatorname{Cov}(X,Y).

Show answer and explanation
-0.2

0.2(0)(1)=0.2-0.2-(0)(1)=-0.2.

04A closer look#

Feature covariance helps describe which measurements vary together and supplies entries of the covariance matrix used by PCA. Its units are the product of the two variables’ units. Zero covariance rules out linear co-movement as measured here, but does not generally imply independence.

Key takeaway

Subtract the product of means from the mean of products.

  • Compute covariance from a joint distribution.

Further questions

Does covariance zero prove independence?
No. Covariance captures centered products, and dependent variables can have zero covariance.

Sources & further reading

  1. [1]

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Covariance.” Math behind ML. /learn/p-covariance