Variable Relationships › Covariance: Measuring Joint Variability

Michael BrenndoerferJune 3, 20255 min read
Machine Learning from ScratchAll chapters
Part 1 of 9Statistics 101
Chapter 6Variable Relationships › Covariance: Measuring Joint Variability
Reading section 3 of 11 · Covariance: Measuring Joint Variability · 5 minSection 3 of 11 · 5 min

Book contents

Section 3 of 11 · 5 min read

Variable Relationships › Covariance: Measuring Joint VariabilityOne section · Sections in this chapter3 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Covariance quantifies how two variables change together. When one variable tends to be above its mean at the same time the other is above its mean, they have positive covariance. When one tends to be above its mean while the other is below its mean, they have negative covariance. A covariance near zero suggests little linear relationship between the variables.

The population covariance between two variables XX and YY is defined as:

Cov(X,Y)=E[(X−μX)(Y−μY)]\text{Cov}(X, Y) = E[(X - \mu_X)(Y - \mu_Y)]

Where:

  • E[⋅]E[\cdot]: Expected value operator
  • μX=E[X]\mu_X = E[X]: Population mean of XX
  • μY=E[Y]\mu_Y = E[Y]: Population mean of YY

In practice, we work with sample data and estimate covariance using the sample covariance:

sXY=1n−1∑i=1n(xi−xˉ)(yi−yˉ)s_{XY} = \frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y})

Where:

  • nn: Sample size
  • xi,yix_i, y_i: Individual observations
  • xˉ,yˉ\bar{x}, \bar{y}: Sample means

The interpretation of covariance is straightforward in terms of direction: positive values indicate variables tend to move together in the same direction, while negative values indicate they move in opposite directions. However, the magnitude of covariance is difficult to interpret because it depends on the units and scales of the variables. A covariance of 100 might indicate a strong relationship for variables measured in small units, but a weak relationship for variables measured in large units. This limitation motivates the need for a standardized measure of association.

To better understand how covariance captures joint variability, consider the geometric interpretation. Each data point contributes to the covariance based on whether both variables are above their means (positive contribution), both below their means (positive contribution), or one above and one below (negative contribution). The following visualization shows this structure and how different quadrants contribute to the overall covariance measure.

Out[2]:
Visualization
Quadrant scatter plot showing covariance contributions.
Scatter plot showing covariance contributions by quadrant. Points are colored by the sign of their contribution to covariance: blue points (both deviations positive or both negative) contribute positively, while red points (one deviation positive, one negative) contribute negatively. The reference lines mark the means of X and Y, dividing the space into four quadrants. When positive contributions dominate, covariance is positive.
Bar chart showing cumulative covariance contributions by quadrant.
Cumulative contributions to covariance by quadrant. This bar chart quantifies how much each quadrant contributes to the total covariance. Quadrants I (both above mean) and III (both below mean) contribute positively, while quadrants II and IV contribute negatively. The net covariance is the sum of all contributions, revealing why this dataset has positive covariance.