4.9.12Probability Theory & Statistics

Covariance and correlation

1,586 words7 min readdifficulty · medium6 backlinks

WHAT are we measuring?


WHY this exact formula? (Derivation from scratch)

Step 1 — Encode "move together". Take the deviation XμXX-\mu_X. It is positive when XX is high, negative when low. Same for YY.

Step 2 — Multiply the deviations. The product (XμX)(YμY)(X-\mu_X)(Y-\mu_Y) is:

  • positive when both are high or both are low (they agree),
  • negative when one is high while the other is low (they disagree).

Why this step? Multiplication is the cheapest operation that gives ++ for agreement and - for disagreement.

Step 3 — Average over the distribution. Take the expectation so agreements and disagreements net out: Cov(X,Y)=E[(XμX)(YμY)].\operatorname{Cov}(X,Y)=E[(X-\mu_X)(Y-\mu_Y)]. Net positive ⇒ they generally move together.

Step 4 — A computational shortcut. Expand: E[(XμX)(YμY)]=E[XYXμYμXY+μXμY]E[(X-\mu_X)(Y-\mu_Y)] = E[XY - X\mu_Y - \mu_X Y + \mu_X\mu_Y] =E[XY]μYE[X]μXE[Y]+μXμY=E[XY]μXμY.= E[XY] - \mu_Y E[X] - \mu_X E[Y] + \mu_X\mu_Y = E[XY]-\mu_X\mu_Y.

Figure — Covariance and correlation

WHY does correlation lie in [1,1][-1,1]? (Cauchy–Schwarz)

Let U=XμXU=X-\mu_X, V=YμYV=Y-\mu_Y. Consider, for any real tt: E[(UtV)2]0(a square is never negative).E\big[(U - tV)^2\big] \ge 0 \quad\text{(a square is never negative).} Expand: E[U2]2tE[UV]+t2E[V2]0.E[U^2] - 2t\,E[UV] + t^2 E[V^2] \ge 0. This is a quadratic in tt that is always 0\ge 0, so its discriminant must be 0\le 0: (2E[UV])24E[U2]E[V2]0\big(2E[UV]\big)^2 - 4\,E[U^2]\,E[V^2] \le 0 (E[UV])2E[U2]E[V2].\Rightarrow (E[UV])^2 \le E[U^2]\,E[V^2]. But E[UV]=Cov(X,Y)E[UV]=\operatorname{Cov}(X,Y), E[U2]=σX2E[U^2]=\sigma_X^2, E[V2]=σY2E[V^2]=\sigma_Y^2. Hence Cov(X,Y)2σX2σY2    CovσXσY1.\operatorname{Cov}(X,Y)^2 \le \sigma_X^2\sigma_Y^2 \;\Rightarrow\; \left|\frac{\operatorname{Cov}}{\sigma_X\sigma_Y}\right|\le 1. Why this step? Equality holds only when UtV=0U-tV=0 i.e. YY is an exact linear function of XX — that is why ρ=±1\rho=\pm1 means perfectly linear.


Key properties (all provable from the definition)

Derive property 3: Var(X+Y)=Cov(X+Y,X+Y)\operatorname{Var}(X+Y)=\operatorname{Cov}(X+Y,X+Y); expand by bilinearity into Cov(X,X)+Cov(X,Y)+Cov(Y,X)+Cov(Y,Y)\operatorname{Cov}(X,X)+\operatorname{Cov}(X,Y)+\operatorname{Cov}(Y,X)+\operatorname{Cov}(Y,Y). ∎


Worked examples


Common mistakes (steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine two friends on swings. Covariance asks: when one swings forward, does the other usually swing forward too? If yes → positive; if one goes forward while the other goes back → negative; if there's no pattern → about zero. But "how far they swing" depends on the swing size, which is unfair to compare. So correlation is like measuring their teamwork on a fixed report card from 1-1 (perfectly opposite) to +1+1 (perfectly together), where 00 means "no teamwork." It ignores how big the swings are and only judges how well they match.


Active recall

Define covariance in words.
The average of the product of each variable's deviation from its own mean: E[(XμX)(YμY)]E[(X-\mu_X)(Y-\mu_Y)].
State the computational formula for covariance.
Cov(X,Y)=E[XY]E[X]E[Y]\operatorname{Cov}(X,Y)=E[XY]-E[X]E[Y].
What is Cov(X,X)\operatorname{Cov}(X,X)?
Var(X)\operatorname{Var}(X) — covariance generalises variance.
Define the correlation coefficient.
ρ=Cov(X,Y)/(σXσY)\rho=\operatorname{Cov}(X,Y)/(\sigma_X\sigma_Y).
Why does ρ\rho always lie in [1,1][-1,1]?
Cauchy–Schwarz: Cov2σX2σY2\operatorname{Cov}^2 \le \sigma_X^2\sigma_Y^2, from E[(UtV)2]0E[(U-tV)^2]\ge0 having discriminant 0\le0.
What does ρ=±1\rho=\pm1 mean geometrically?
YY is an exact linear function of XX (equality case of Cauchy–Schwarz).
Does ρ=0\rho=0 imply independence?
No. It only rules out LINEAR dependence; e.g. Y=X2Y=X^2 with symmetric XX gives ρ=0\rho=0 yet full dependence.
Does independence imply Cov=0\operatorname{Cov}=0?
Yes, since E[XY]=E[X]E[Y]E[XY]=E[X]E[Y].
How does covariance behave under aX+baX+b?
Cov(aX+b,cY+d)=acCov(X,Y)\operatorname{Cov}(aX+b,cY+d)=ac\,\operatorname{Cov}(X,Y); shifts don't matter, scales multiply.
Formula for Var(X+Y)\operatorname{Var}(X+Y)?
Var(X)+Var(Y)+2Cov(X,Y)\operatorname{Var}(X)+\operatorname{Var}(Y)+2\operatorname{Cov}(X,Y).
Why prefer correlation over covariance for comparing relationships?
Correlation is unitless and bounded in [1,1][-1,1], so it measures strength independent of scale/units.

Connections

  • Variance and Standard Deviation — covariance is the two-variable generalisation.
  • Expectation of Random Variables — every formula here is built from E[]E[\cdot].
  • Independence of Random Variables — independence ⇒ zero covariance (not conversely).
  • Cauchy–Schwarz Inequality — guarantees ρ1|\rho|\le1.
  • Linear Regression — slope =Cov(X,Y)/Var(X)=\operatorname{Cov}(X,Y)/\operatorname{Var}(X); r2r^2 is goodness of fit.
  • Covariance Matrix — collects all pairwise covariances for a random vector.

Concept Map

generalises to

definition

expand and average

Y equals X gives

rescale by sigmaX sigmaY

Cauchy-Schwarz bounds

positive: agree, negative: disagree

equality when

unit-free, comparable

obeys

Variance: one variable wiggle

Covariance

E of X minus muX times Y minus muY

E of XY minus E of X E of Y

Cov X,X = Var X

Correlation rho

rho in -1 to 1

Product of deviations

Y exact linear function of X

Compare strength across units

Symmetry & Bilinearity

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, covariance ka matlab simple hai: jab XX apne average se upar jaata hai, kya YY bhi upar jaata hai? Agar haan, toh dono "saath-saath move" karte hain aur covariance positive aata hai. Agar XX upar par YY neeche, toh disagreement, covariance negative. Formula yaad rakho: E[XY]E[X]E[Y]E[XY]-E[X]E[Y] — yaani "product ki average minus average ka product". Aur ek mast baat — agar Y=XY=X rakh do toh covariance seedha variance ban jaata hai, matlab covariance variance ka bada bhai hai.

Problem ye hai ki covariance ke units hote hain (jaise kg·cm), aur agar tum XX ko 1000 se multiply karo toh covariance bhi 1000 guna ho jaata — par actual rishta wahi rahta hai. Isliye hum correlation ρ=Cov/(σXσY)\rho=\operatorname{Cov}/(\sigma_X\sigma_Y) use karte hain, jo hamesha 1-1 se +1+1 ke beech rehta hai. ρ=+1\rho=+1 matlab perfect line upar ki taraf, 1-1 matlab perfect line neeche ki taraf, 00 matlab koi linear pattern nahi. Ye [1,1][-1,1] wali property Cauchy–Schwarz inequality se aati hai — bas itna yaad rakho ki "square kabhi negative nahi hota" se poora proof nikal jaata hai.

Sabse bada trap exam mein: ρ=0\rho=0 ka matlab independent nahi hota! Example: Y=X2Y=X^2 jahan XX symmetric hai, wahan ρ=0\rho=0 aata hai par YY pura XX pe depend karta hai. Covariance sirf linear rishta dekhta hai, curve wale rishte ko miss kar deta hai. Aur ek aur — correlation high hone se causation prove nahi hota (ice cream aur drowning dono summer ki wajah se badhte hain, ek dusre ko cause nahi karte). Yeh do points clear ho gaye toh aadha chapter pakka.

Go deeper — visual, from zero

Test yourself — Probability Theory & Statistics

Connections