1.3.9Probability & Statistics

Covariance and correlation

2,635 words12 min readdifficulty · medium2 backlinks

Overview

Covariance and correlation measure how two random variables change together. They answer: "When XX goes up, does YY tend to go up (positive), go down (negative), or stay unrelated (zero)?" These concepts are foundational for understanding feature relationships in machine learning, dimensionality reduction (PCA), and regression.


Core Intuition


Definitions and Derivations

Derivation of the Computational Form

Start from the definition: Cov(X,Y)=E[(XμX)(YμY)]\text{Cov}(X, Y) = \mathbb{E}[(X - \mu_X)(Y - \mu_Y)]

Expand the product: =E[XYXμYμXY+μXμY]= \mathbb{E}[XY - X\mu_Y - \mu_X Y + \mu_X \mu_Y]

Apply linearity of expectation (expectation distributes over addition): =E[XY]E[XμY]E[μXY]+E[μXμY]= \mathbb{E}[XY] - \mathbb{E}[X\mu_Y] - \mathbb{E}[\mu_X Y] + \mathbb{E}[\mu_X \mu_Y]

Why this step? Constants can be pulled out of expectations: =E[XY]μYE[X]μXE[Y]+μXμY= \mathbb{E}[XY] - \mu_Y \mathbb{E}[X] - \mu_X \mathbb{E}[Y] + \mu_X \mu_Y

Substitute μX=E[X]\mu_X = \mathbb{E}[X] and μY=E[Y]\mu_Y = \mathbb{E}[Y]: =E[XY]μYμXμXμY+μXμY= \mathbb{E}[XY] - \mu_Y \mu_X - \mu_X \mu_Y + \mu_X \mu_Y

Simplify (middle two terms cancel one, leaving): Cov(X,Y)=E[XY]E[X]E[Y]\boxed{\text{Cov}(X, Y) = \mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y]}

Interpretation:

  • If XX and YY are independent, then E[XY]=E[X]E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\mathbb{E}[Y], so Cov(X,Y)=0\text{Cov}(X, Y) = 0.
  • Positive covariance: XX and YY tend to increase together.
  • Negative covariance: when XX increases, YY tends to decrease.
  • Units: covariance has units of X×YX \times Y (e.g., meters × kilograms), making it hard to interpret magnitude.

Why Normalize by Standard Deviations?

Intuition: Covariance depends on the scales of XX and YY. If we measure height in millimeters instead of meters, covariance scales by 100021000^2! Dividing by σXσY\sigma_X \sigma_Y removes this scale dependence.

Derivation of bounds (1ρ1-1 \le \rho \le 1):

Consider standardized variables: ZX=XμXσX,ZY=YμYσYZ_X = \frac{X - \mu_X}{\sigma_X}, \quad Z_Y = \frac{Y - \mu_Y}{\sigma_Y}

These have mean 00 and variance 11. The correlation is: ρ=E[ZXZY]\rho = \mathbb{E}[Z_X Z_Y]

Cauchy-Schwarz inequality states: E[ZXZY]E[ZX2]E[ZY2]=11=1|\mathbb{E}[Z_X Z_Y]| \le \sqrt{\mathbb{E}[Z_X^2] \mathbb{E}[Z_Y^2]} = \sqrt{1 \cdot 1} = 1

Why this step? E[ZX2]=Var(ZX)=1\mathbb{E}[Z_X^2] = \text{Var}(Z_X) = 1 by standardization.

Thus 1ρ1-1 \le \rho \le 1. Equality holds when ZY=±ZXZ_Y = \pm Z_X (perfect linear relationship).


Sample Estimates (Finite Data)

In practice, we estimate from nn samples (x1,y1),,(xn,yn)(x_1, y_1), \ldots, (x_n, y_n):


Visual Understanding

Figure — Covariance and correlation

The diagram shows scatter plots for different correlation values. Notice how:

  • ρ=0\rho = 0: cloud with no trend
  • ρ=0.5|\rho| = 0.5: moderate trend
  • ρ=1|\rho| = 1: points lie exactly on a line

Worked Examples


Common Mistakes


Key Properties and Facts


Machine Learning Context

Why covariance/correlation matter in ML:

  1. Feature Engineering: Highly correlated features may be redundant (multicollinearity in regression).
  2. PCA (Principal Component Analysis): Finds directions of maximum variance = eigenvectors of the covariance matrix.
  3. Correlation-based Feature Selection: Remove features with ρ<ϵ|\rho| < \epsilon to the target.
  4. Gaussian Processes: Covariance functions (kernels) define similarity between data points.
  5. Portfolio Optimization: In finance ML, covariance matrices model asset co-movements.

Feynman Technique (ELI12)

Recall Explain Like I'm 12

Imagine you and your friend are both learning to skateboard. Every week, you both try harder tricks.

Covariance is like asking: "When I learn a harder trick, does my friend also tend to learn a harder trick the same week?" If yes, covariance is positive. If when I do better, they do worse, it's negative. If there's no pattern, it's zero. But here's the thing: if I measure my progress in "tricks learned" and they measure in "hours practiced," the numbers get messy. Maybe I learned 5 tricks, they practiced 20 hours—how do we compare?

Correlation fixes this by putting everything on the same scale from -1 to +1. It's like rating our teamwork: +1 means we're perfectly in sync (when I improve, they improve the exact same amount relative to our own ranges), -1 means we're perfect opposites, and 0 means we're doing our own thing with no pattern.

So: covariance = raw teamwork, correlation = teamwork on a report card scale.


Mnemonic


Active Recall Flashcards

#flashcards/ai-ml

What is the definition of covariance between random variables XX and YY?
Cov(X,Y)=E[(XμX)(YμY)]=E[XY]E[X]E[Y]\text{Cov}(X, Y) = \mathbb{E}[(X - \mu_X)(Y - \mu_Y)] = \mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y]
What is the formula for Pearson correlation coefficient?
ρX,Y=Cov(X,Y)σXσY\rho_{X,Y} = \frac{\text{Cov}(X, Y)}{\sigma_X \sigma_Y}, which normalizes covariance to [1,1][-1, 1]
What is the range of correlation ρ\rho?
1ρ1-1 \le \rho \le 1 (dimensionless, from Cauchy-Schwarz inequality)
If XX and YY are independent, what is Cov(X,Y)\text{Cov}(X, Y)?
Cov(X,Y)=0\text{Cov}(X, Y) = 0 (but the converse is not always true!)
Does Cov(X,Y)=0\text{Cov}(X, Y) = 0 imply XX and YY are independent?
No! Zero covariance only rules out linear relationships. Example: Y=X2Y = X^2 with XX symmetric around zero has zero covariance but complete dependence.
What is the formula for the variance of a sum Var(X+Y)\text{Var}(X + Y)?
Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y)\text{Var}(X + Y) = \text{Var}(X) + \text{Var}(Y) + 2\,\text{Cov}(X, Y)
Why do we use n1n-1 in sample covariance instead of nn?
Bessel's correction: we lose one degree of freedom computing the sample means, making the estimator unbiased.
What does ρ=+1\rho = +1 mean?
Perfect positive linear relationship: Y=aX+bY = aX + b with a>0a > 0
What does ρ=1\rho = -1 mean?
Perfect negative linear relationship: Y=aX+bY = aX + b with a<0a < 0
Why is correlation better than covariance for comparing relationship strength?
Correlation is scale-invariant (dimensionless), while covariance depends on units of XX and YY.
Give one ML application of covariance.
PCA (Principal Component Analysis) uses the covariance matrix to find directions of maximum variance.
What is the property Cov(aX+b,Y)\text{Cov}(aX + b, Y) equal to?
aCov(X,Y)a\,\text{Cov}(X, Y) (constants bb drop out; scaling by aa scales covariance)

Connections

  • 1.3.01-Random-variables — covariance extends variance to pairs of RVs
  • 1.3.05-Expectation-and-variance — covariance uses expectation operator
  • 1.3.08-Joint-and-marginal-distributions — covariance computed from joint distributions
  • 2.4.01-Principal-component-analysis — PCA diagonalizes the covariance matrix
  • 3.2.05-Linear-regression — correlation measures strength of linear relationship
  • 3.2.06-Multicolinearity — high inter-feature correlation causes issues
  • 1.2.04-Independence — independent RVs have zero covariance
  • 4.1.03-Feature-selection — correlation-based methods for feature ranking

Master these concepts, and you'll understand how variables dance together—from theory to ML applications.

Concept Map

measured by

defined as

expand and simplify

gives

has

implies zero

normalized by

yields

removed by

bounded

applied in

Joint variation of X and Y

Covariance

E of X minus muX times Y minus muY

E of XY minus E of X times E of Y

Independence

Sign gives direction

Units of X times Y

Correlation rho

Divide by sigmaX sigmaY

Range minus 1 to plus 1

Feature relationships PCA regression

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, covariance aur correlation ka core idea bilkul simple hai — ye dono batate hain ki do variables ek saath kaise move karte hain. Jaise ghar ka example lo: agar square footage (XX) badhti hai toh price (YY) bhi badhti hai kya? Agar dono saath badhte hain toh positive relationship, agar ek badhne par doosra ghatta hai toh negative, aur agar koi connection hi nahi toh zero. Covariance humein direction aur scale dono batata hai, par uska problem yeh hai ki uski units confusing hoti hain (jaise meters × kilograms), isliye magnitude samajhna mushkil ho jaata hai.

Yahi par correlation aata hai hero banke. Correlation basically covariance ko normalize kar deta hai standard deviations se divide karke, jisse humein ek clean dimensionless number milta hai jo hamesha 1-1 se +1+1 ke beech rehta hai. Iska matlab ab units ka koi tension nahi — chahe height millimeters mein maapo ya meters mein, correlation same rahega. ρ=+1\rho = +1 matlab perfect positive linear rishta, 1-1 matlab perfect negative, aur 00 matlab koi linear relationship nahi (dhyaan rakhna, non-linear ho sakta hai still!). Isiliye correlation ko "universal scale par rated teamwork" samajh lo.

Ye concept AI-ML mein bahut zaroori hai kyunki jab tum features ke beech relationships samajhte ho, tabhi tum acche models bana paate ho. PCA (dimensionality reduction), regression, aur feature selection — sab jagah yeh idea kaam aata hai. Agar do features highly correlated hain toh shayad ek redundant hai, ya phir strong correlation matlab woh feature target predict karne mein useful ho sakta hai. Bas ek cheez yaad rakhna: correlation sirf linear relationship pakadta hai, aur correlation kabhi bhi causation prove nahi karta — yeh ML mein ek common trap hai jismein log fas jaate hain!

Go deeper — visual, from zero

Test yourself — Probability & Statistics

Connections