1.3.9 · D2Probability & Statistics

Visual walkthrough — Covariance and correlation

1,830 words8 min readBack to topic

This page rebuilds the central result of Covariance and correlation from nothing. We will earn the number (the correlation) one picture at a time. By the end you will see why always lives between and , and why does not mean "unrelated".

We only assume you know what a random variable is (a number whose value depends on chance) and what an average / expectation is (the balance-point of all its possible values). Everything else we build here.


Step 1 — Two numbers that wobble together

WHAT. We have two random quantities. Call them (say, house size) and (say, price). Each sample of the world gives us a pair — one dot on a flat picture. This picture of dots is a joint distribution made visible.

WHY. Before we can measure "do they move together?", we need to see the moving. The dot cloud is the raw data of teamwork.

PICTURE. Below, each dot is one observation. The two dashed lines are the means (a vertical line at ) and (a horizontal line at ) — the balance-points of and alone.

Figure — Covariance and correlation

Step 2 — Move the origin to the centre of the cloud

WHAT. For every dot we compute two deviations:

WHY. "Together-ness" is about being on the same side of the centre, not about raw position. A dot at tells us nothing until we know the middle is (barely above) or (far above). Subtracting the mean re-centres the whole cloud on .

PICTURE. The dashed cross becomes the new origin. Every dot now lives in one of four quadrants. Look at the sign of each deviation:

Figure — Covariance and correlation
  • Top-right: and → product .
  • Bottom-left: both negative → negative × negative product .
  • Top-left / bottom-right: one positive, one negative → product .

Step 3 — Multiply the deviations: the sign carries the message

WHAT. For each dot form the product of its two deviations:

WHY. Multiplication is exactly the tool that turns "same side / opposite side" into a sign. We chose the product (not the sum, not the distance) because only a product flips sign when exactly one factor is negative — precisely the top-left / bottom-right case we want to count as "disagreeing". Think of as the signed area of the rectangle from the centre to the dot.

PICTURE. Each dot gets a shaded rectangle. Warm (magenta) rectangles are positive ; violet ones are negative. A cloud that leans up-right is mostly warm.

Figure — Covariance and correlation

Step 4 — Average the products: this IS covariance

WHAT. Average all the little signed rectangles:

WHY. One dot is noise. The average rectangle tells us which quadrants dominate. If warm rectangles win, the average is positive; if violet wins, negative; if they tie, zero.

PICTURE. Three clouds, three average-rectangle verdicts: positive, zero, negative.

Figure — Covariance and correlation

The shortcut formula, derived here. The rectangle form is the picture, but there is a faster way to compute. Expand the product inside the average, using that and are fixed numbers (constants), and that the average of a sum is the sum of the averages (linearity of expectation):

Here we pulled the constants outside their averages, then used and . Two of the last three terms are and one subtracts , so they collapse to :

The trouble: this number's size is meaningless. Stretch the -axis by (millimetres instead of metres) and every rectangle stretches . We must normalise.


Step 5 — Squash both axes to a fair, unit-free grid

WHAT. Replace and by their standardised versions:

where is the standard deviation — the typical distance of from its own mean.

WHY. Dividing by measures each deviation in units of its own typical spread. A dot " to the right" means the same thing whether is in millimetres or miles. This kills the unit problem from Step 4.

PICTURE. The stretched cloud (left) becomes a round cloud (right) where each axis is now measured in units of one standard deviation — a step of along means "one typical spread of ". Both and now have mean and spread .

Figure — Covariance and correlation

Step 6 — Why can never leave the corridor

WHAT. We show by looking at a single guaranteed-non-negative quantity:

WHY. A square is never negative, and an average of never-negative things is never negative. This one honest fact is the whole cage. Expand it:

Now use the two facts we built in Step 5: and (each standardised variable has variance ). And . Substitute:

Repeat with to get .

PICTURE. The two parabolas never dip below the axis; their averages pin inside the corridor.

Figure — Covariance and correlation

The corridor endpoints, made visible. Equality (touching or ) happens exactly when , i.e. the dots lie on a perfect straight line. The next picture shows both extreme cases as scatter plots: (rising line) and (falling line). These are the walls of the corridor.

Figure — Covariance and correlation

Step 7 — The edge case: does NOT mean "unrelated"

WHAT. Let be uniform on and set . Then

WHY. Our rectangles only detect lines. For a symmetric U-shape, the warm rectangles on the right cancel the warm ones on the left in a way that zeroes the average — even though is completely pinned down by . This is the boundary of what can see, and every honest reader must meet it.

PICTURE. A perfect parabola of dots: obviously dependent, yet . The left half's rectangles exactly cancel the right half's.

Figure — Covariance and correlation

The one-picture summary

Everything above, in one frame: raw dots → subtract means → sign the rectangles → average → squash to fair units → read off the corridor.

Figure — Covariance and correlation
Recall Feynman retelling — say it back in plain words

We start with a scatter of dot-pairs. First we find the middle of the cloud and slide it to the origin — now every dot is "left/right of centre" and "up/down of centre". For each dot we multiply those two distances; the product is positive when the dot agrees with an up-right trend and negative when it disagrees. We average all those signed rectangles — that average is covariance: a direction of teamwork. But its size lies (change units, size changes), so we shrink each axis by its own typical spread (one standard deviation), giving a fair round grid. The average rectangle on that grid is correlation . Because the average of a square is never negative, can never escape to ; it hits the ends only when the dots sit on a perfect line. If either variable never moves (), isn't even defined — there's nothing to compare. And finally: if the dots make a curve like a U, the left-side and right-side rectangles cancel — so even though the variables are joined at the hip. measures straight-line teamwork, nothing more.


Where this leads

  • The rectangle-averaging idea generalises to a whole covariance matrix, the engine of PCA.
  • near between features is the warning sign of multicollinearity in linear regression, and a lever in feature selection.
Recall

What does the sign of the deviation-product encode? ::: Whether the dot agrees (positive, up-right or down-left) or disagrees (negative, up-left or down-right) with a rising trend. Why divide covariance by ? ::: To make it unit-free/scale-invariant, so its size is comparable across variable pairs. Which single inequality cages inside ? ::: (a square's average can't be negative). When is undefined? ::: When or (a variable with no spread — division by zero). Does prove independence? ::: No — it only rules out a linear relationship; curves like give while being fully dependent.