Covariance and correlation measure how two random variables change together . They answer: "When X X X goes up, does Y Y Y 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.
Intuition Why Do We Need These Measures?
Imagine you're building a house price predictor. You have features like square footage (X X X ) and price (Y Y Y ). Do they move together? If so, how strongly?
Covariance tells us the direction and scale of the joint variation.
Correlation normalizes covariance to a dimensionless number between − 1 -1 − 1 and + 1 +1 + 1 , making it interpretable regardless of units.
Think of covariance as raw teamwork between variables, and correlation as teamwork rated on a universal scale .
For random variables X X X and Y Y Y , the covariance is:
Cov ( X , Y ) = E [ ( X − μ X ) ( Y − μ Y ) ] \text{Cov}(X, Y) = \mathbb{E}[(X - \mu_X)(Y - \mu_Y)] Cov ( X , Y ) = E [( X − μ X ) ( Y − μ Y )]
where μ X = E [ X ] \mu_X = \mathbb{E}[X] μ X = E [ X ] and μ Y = E [ Y ] \mu_Y = \mathbb{E}[Y] μ Y = E [ Y ] .
Alternative form (easier for computation):
Cov ( X , Y ) = E [ X Y ] − E [ X ] E [ Y ] \text{Cov}(X, Y) = \mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y] Cov ( X , Y ) = E [ X Y ] − E [ X ] E [ Y ]
Start from the definition:
Cov ( X , Y ) = E [ ( X − μ X ) ( Y − μ Y ) ] \text{Cov}(X, Y) = \mathbb{E}[(X - \mu_X)(Y - \mu_Y)] Cov ( X , Y ) = E [( X − μ X ) ( Y − μ Y )]
Expand the product:
= E [ X Y − X μ Y − μ X Y + μ X μ Y ] = \mathbb{E}[XY - X\mu_Y - \mu_X Y + \mu_X \mu_Y] = E [ X Y − X μ Y − μ X Y + μ X μ Y ]
Apply linearity of expectation (expectation distributes over addition):
= E [ X Y ] − E [ X μ Y ] − E [ μ X Y ] + E [ μ X μ Y ] = \mathbb{E}[XY] - \mathbb{E}[X\mu_Y] - \mathbb{E}[\mu_X Y] + \mathbb{E}[\mu_X \mu_Y] = E [ X Y ] − E [ X μ Y ] − E [ μ X Y ] + E [ μ X μ Y ]
Why this step? Constants can be pulled out of expectations:
= E [ X Y ] − μ Y E [ X ] − μ X E [ Y ] + μ X μ Y = \mathbb{E}[XY] - \mu_Y \mathbb{E}[X] - \mu_X \mathbb{E}[Y] + \mu_X \mu_Y = E [ X Y ] − μ Y E [ X ] − μ X E [ Y ] + μ X μ Y
Substitute μ X = E [ X ] \mu_X = \mathbb{E}[X] μ X = E [ X ] and μ Y = E [ Y ] \mu_Y = \mathbb{E}[Y] μ Y = E [ Y ] :
= E [ X Y ] − μ Y μ X − μ X μ Y + μ X μ Y = \mathbb{E}[XY] - \mu_Y \mu_X - \mu_X \mu_Y + \mu_X \mu_Y = E [ X Y ] − μ Y μ X − μ X μ Y + μ X μ Y
Simplify (middle two terms cancel one, leaving):
Cov ( X , Y ) = E [ X Y ] − E [ X ] E [ Y ] \boxed{\text{Cov}(X, Y) = \mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y]} Cov ( X , Y ) = E [ X Y ] − E [ X ] E [ Y ]
Interpretation:
If X X X and Y Y Y are independent , then E [ X Y ] = E [ X ] E [ Y ] \mathbb{E}[XY] = \mathbb{E}[X]\mathbb{E}[Y] E [ X Y ] = E [ X ] E [ Y ] , so Cov ( X , Y ) = 0 \text{Cov}(X, Y) = 0 Cov ( X , Y ) = 0 .
Positive covariance : X X X and Y Y Y tend to increase together.
Negative covariance : when X X X increases, Y Y Y tends to decrease.
Units : covariance has units of X × Y X \times Y X × Y (e.g., meters × kilograms), making it hard to interpret magnitude.
Definition Correlation (Pearson Correlation Coefficient)
The correlation between X X X and Y Y Y is:
ρ X , Y = Cov ( X , Y ) σ X σ Y \rho_{X,Y} = \frac{\text{Cov}(X, Y)}{\sigma_X \sigma_Y} ρ X , Y = σ X σ Y Cov ( X , Y )
where σ X = Var ( X ) \sigma_X = \sqrt{\text{Var}(X)} σ X = Var ( X ) and σ Y = Var ( Y ) \sigma_Y = \sqrt{\text{Var}(Y)} σ Y = Var ( Y ) are the standard deviations.
Properties:
ρ X , Y ∈ [ − 1 , 1 ] \rho_{X,Y} \in [-1, 1] ρ X , Y ∈ [ − 1 , 1 ] (dimensionless)
ρ = + 1 \rho = +1 ρ = + 1 : perfect positive linear relationship
ρ = − 1 \rho = -1 ρ = − 1 : perfect negative linear relationship
ρ = 0 \rho = 0 ρ = 0 : no linear relationship (but nonlinear relationships may exist!)
Intuition: Covariance depends on the scales of X X X and Y Y Y . If we measure height in millimeters instead of meters, covariance scales by 1000 2 1000^2 100 0 2 ! Dividing by σ X σ Y \sigma_X \sigma_Y σ X σ Y removes this scale dependence.
Derivation of bounds (− 1 ≤ ρ ≤ 1 -1 \le \rho \le 1 − 1 ≤ ρ ≤ 1 ):
Consider standardized variables:
Z X = X − μ X σ X , Z Y = Y − μ Y σ Y Z_X = \frac{X - \mu_X}{\sigma_X}, \quad Z_Y = \frac{Y - \mu_Y}{\sigma_Y} Z X = σ X X − μ X , Z Y = σ Y Y − μ Y
These have mean 0 0 0 and variance 1 1 1 . The correlation is:
ρ = E [ Z X Z Y ] \rho = \mathbb{E}[Z_X Z_Y] ρ = E [ Z X Z Y ]
Cauchy-Schwarz inequality states:
∣ E [ Z X Z Y ] ∣ ≤ E [ Z X 2 ] E [ Z Y 2 ] = 1 ⋅ 1 = 1 |\mathbb{E}[Z_X Z_Y]| \le \sqrt{\mathbb{E}[Z_X^2] \mathbb{E}[Z_Y^2]} = \sqrt{1 \cdot 1} = 1 ∣ E [ Z X Z Y ] ∣ ≤ E [ Z X 2 ] E [ Z Y 2 ] = 1 ⋅ 1 = 1
Why this step? E [ Z X 2 ] = Var ( Z X ) = 1 \mathbb{E}[Z_X^2] = \text{Var}(Z_X) = 1 E [ Z X 2 ] = Var ( Z X ) = 1 by standardization.
Thus − 1 ≤ ρ ≤ 1 -1 \le \rho \le 1 − 1 ≤ ρ ≤ 1 . Equality holds when Z Y = ± Z X Z_Y = \pm Z_X Z Y = ± Z X (perfect linear relationship).
In practice, we estimate from n n n samples ( x 1 , y 1 ) , … , ( x n , y n ) (x_1, y_1), \ldots, (x_n, y_n) ( x 1 , y 1 ) , … , ( x n , y n ) :
The diagram shows scatter plots for different correlation values. Notice how:
ρ = 0 \rho = 0 ρ = 0 : cloud with no trend
∣ ρ ∣ = 0.5 |\rho| = 0.5 ∣ ρ ∣ = 0.5 : moderate trend
∣ ρ ∣ = 1 |\rho| = 1 ∣ ρ ∣ = 1 : points lie exactly on a line
Worked example Example 1: Computing Covariance by Hand
Given joint distribution:
X X X
Y Y Y
P ( X , Y ) P(X, Y) P ( X , Y )
1
2
0.2
1
4
0.3
3
2
0.1
3
4
0.4
Step 1: Compute marginal means
E [ X ] = 1 ( 0.2 + 0.3 ) + 3 ( 0.1 + 0.4 ) = 1 ( 0.5 ) + 3 ( 0.5 ) = 2 \mathbb{E}[X] = 1(0.2 + 0.3) + 3(0.1 + 0.4) = 1(0.5) + 3(0.5) = 2 E [ X ] = 1 ( 0.2 + 0.3 ) + 3 ( 0.1 + 0.4 ) = 1 ( 0.5 ) + 3 ( 0.5 ) = 2
E [ Y ] = 2 ( 0.2 + 0.1 ) + 4 ( 0.3 + 0.4 ) = 2 ( 0.3 ) + 4 ( 0.7 ) = 3.4 \mathbb{E}[Y] = 2(0.2 + 0.1) + 4(0.3 + 0.4) = 2(0.3) + 4(0.7) = 3.4 E [ Y ] = 2 ( 0.2 + 0.1 ) + 4 ( 0.3 + 0.4 ) = 2 ( 0.3 ) + 4 ( 0.7 ) = 3.4
Why this step? Marginal expectation sums over all joint outcomes for each variable.
Step 2: Compute E [ X Y ] \mathbb{E}[XY] E [ X Y ]
E [ X Y ] = ( 1 ⋅ 2 ) ( 0.2 ) + ( 1 ⋅ 4 ) ( 0.3 ) + ( 3 ⋅ 2 ) ( 0.1 ) + ( 3 ⋅ 4 ) ( 0.4 ) \mathbb{E}[XY] = (1 \cdot 2)(0.2) + (1 \cdot 4)(0.3) + (3 \cdot 2)(0.1) + (3 \cdot 4)(0.4) E [ X Y ] = ( 1 ⋅ 2 ) ( 0.2 ) + ( 1 ⋅ 4 ) ( 0.3 ) + ( 3 ⋅ 2 ) ( 0.1 ) + ( 3 ⋅ 4 ) ( 0.4 )
= 0.4 + 1.2 + 0.6 + 4.8 = 7 = 0.4 + 1.2 + 0.6 + 4.8 = 7 = 0.4 + 1.2 + 0.6 + 4.8 = 7
Why this step? Expectation of a function g ( X , Y ) g(X,Y) g ( X , Y ) is ∑ g ( x , y ) P ( x , y ) \sum g(x,y) P(x,y) ∑ g ( x , y ) P ( x , y ) .
Step 3: Apply formula
Cov ( X , Y ) = 7 − ( 2 ) ( 3.4 ) = 7 − 6.8 = 0.2 \text{Cov}(X, Y) = 7 - (2)(3.4) = 7 - 6.8 = 0.2 Cov ( X , Y ) = 7 − ( 2 ) ( 3.4 ) = 7 − 6.8 = 0.2
Interpretation: Positive covariance means X X X and Y Y Y tend to increase together (weakly).
Worked example Example 2: Sample Correlation from Data
Data points: ( 1 , 2 ) , ( 2 , 4 ) , ( 3 , 5 ) , ( 4 , 7 ) (1, 2), (2, 4), (3, 5), (4, 7) ( 1 , 2 ) , ( 2 , 4 ) , ( 3 , 5 ) , ( 4 , 7 )
Step 1: Compute means
x ˉ = 1 + 2 + 3 + 4 4 = 2.5 , y ˉ = 2 + 4 + 5 + 7 4 = 4.5 \bar{x} = \frac{1+2+3+4}{4} = 2.5, \quad \bar{y} = \frac{2+4+5+7}{4} = 4.5 x ˉ = 4 1 + 2 + 3 + 4 = 2.5 , y ˉ = 4 2 + 4 + 5 + 7 = 4.5
Step 2: Deviations
x i x_i x i
y i y_i y i
x i − x ˉ x_i - \bar{x} x i − x ˉ
y i − y ˉ y_i - \bar{y} y i − y ˉ
( x i − x ˉ ) ( y i − y ˉ ) (x_i - \bar{x})(y_i - \bar{y}) ( x i − x ˉ ) ( y i − y ˉ )
( x i − x ˉ ) 2 (x_i - \bar{x})^2 ( x i − x ˉ ) 2
( y i − y ˉ ) 2 (y_i - \bar{y})^2 ( y i − y ˉ ) 2
1
2
-1.5
-2.5
3.75
2.25
6.25
2
4
-0.5
-0.5
0.25
0.25
0.25
3
5
0.5
0.5
0.25
0.25
0.25
4
7
1.5
2.5
3.75
2.25
6.25
Sums: ∑ ( x i − x ˉ ) ( y i − y ˉ ) = 8 \sum (x_i-\bar{x})(y_i-\bar{y}) = 8 ∑ ( x i − x ˉ ) ( y i − y ˉ ) = 8 , ∑ ( x i − x ˉ ) 2 = 5 \sum (x_i-\bar{x})^2 = 5 ∑ ( x i − x ˉ ) 2 = 5 , ∑ ( y i − y ˉ ) 2 = 13 \sum (y_i-\bar{y})^2 = 13 ∑ ( y i − y ˉ ) 2 = 13
Why this step? We're computing the numerator and denominator pieces separately.
Step 3: Sample correlation
r = 8 5 13 = 8 65 = 8 8.062 ≈ 0.992 r = \frac{8}{\sqrt{5} \sqrt{13}} = \frac{8}{\sqrt{65}} = \frac{8}{8.062} \approx 0.992 r = 5 13 8 = 65 8 = 8.062 8 ≈ 0.992
Interpretation: Very strong positive linear relationship (nearly perfect).
Worked example Example 3: Zero Correlation ≠ Independence
Let X ∼ Uniform ( − 1 , 1 ) X \sim \text{Uniform}(-1, 1) X ∼ Uniform ( − 1 , 1 ) and Y = X 2 Y = X^2 Y = X 2 .
By symmetry:
E [ X ] = 0 (symmetric around zero) \mathbb{E}[X] = 0 \quad \text{(symmetric around zero)} E [ X ] = 0 (symmetric around zero)
E [ X Y ] = E [ X ⋅ X 2 ] = E [ X 3 ] = 0 (odd function) \mathbb{E}[XY] = \mathbb{E}[X \cdot X^2] = \mathbb{E}[X^3] = 0 \quad \text{(odd function)} E [ X Y ] = E [ X ⋅ X 2 ] = E [ X 3 ] = 0 (odd function)
Why this step? Odd functions integrated over symmetric intervals give zero.
Thus:
Cov ( X , Y ) = 0 − 0 ⋅ E [ Y ] = 0 \text{Cov}(X, Y) = 0 - 0 \cdot \mathbb{E}[Y] = 0 Cov ( X , Y ) = 0 − 0 ⋅ E [ Y ] = 0
But: Y Y Y is completely determined by X X X ! They are perfectly dependent , yet uncorrelated.
Key insight: Correlation only measures linear relationships. Nonlinear dependencies can have zero correlation.
Common mistake Mistake 1: "Zero Correlation Means Independence"
Wrong idea: If ρ = 0 \rho = 0 ρ = 0 , then X X X and Y Y Y are independent.
Why it feels right: We know independence implies zero covariance.
The truth: The converse is false! Zero covariance only rules out linear relationships. See Example 3 above: Y = X 2 Y = X^2 Y = X 2 has ρ = 0 \rho = 0 ρ = 0 but complete dependence.
Fix: Remember: independence ⇒ \Rightarrow ⇒ zero correlation, but zero correlation ⇏ \not\Rightarrow ⇒ independence.
Common mistake Mistake 2: "Covariance Magnitude Shows Strength"
Wrong idea: "Covariance of 100 is stronger than covariance of 10."
Why it feels right: Bigger number = bigger relationship?
The truth: Covariance depends on units! If you measure height in millimeters instead of meters, covariance multiplies by 1000 2 = 10 6 1000^2 = 10^6 100 0 2 = 1 0 6 . The magnitude is not interpretable across different variable pairs.
Fix: Always use correlation (ρ \rho ρ ) to compare relationship strength. It's scale-invariant.
Common mistake Mistake 3: "Correlation Implies Causation"
Wrong idea: If height and weight have ρ = 0.8 \rho = 0.8 ρ = 0.8 , then height causes weight (or vice versa).
Why it feels right: They clearly move together!
The truth: Correlation only shows association . Both could be caused by a third variable (e.g., age in growing children), or the relationship could be coincidental.
Fix: Use causal inference methods (randomized experiments, causal graphs, instrumental variables) to establish causation. Correlation is just a first clue.
Why covariance/correlation matter in ML:
Feature Engineering: Highly correlated features may be redundant (multicollinearity in regression).
PCA (Principal Component Analysis): Finds directions of maximum variance = eigenvectors of the covariance matrix.
Correlation-based Feature Selection: Remove features with ∣ ρ ∣ < ϵ |\rho| < \epsilon ∣ ρ ∣ < ϵ to the target.
Gaussian Processes: Covariance functions (kernels) define similarity between data points.
Portfolio Optimization: In finance ML, covariance matrices model asset co-movements.
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 COVEN for Covariance
C o-movement O f V ariables, E xpectation of products N et of product of expectations.
Formula: Cov ( X , Y ) = E [ X Y ] − E [ X ] E [ Y ] \text{Cov}(X,Y) = \mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y] Cov ( X , Y ) = E [ X Y ] − E [ X ] E [ Y ]
For correlation: "Divide by sigmas to normalize" — ρ = Cov σ X σ Y \rho = \frac{\text{Cov}}{\sigma_X \sigma_Y} ρ = σ X σ Y Cov .
#flashcards/ai-ml
What is the definition of covariance between random variables X X X and Y Y Y ? Cov ( X , Y ) = E [ ( X − μ X ) ( Y − μ Y ) ] = E [ X Y ] − 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] Cov ( X , Y ) = E [( X − μ X ) ( Y − μ Y )] = E [ X Y ] − E [ X ] 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} ρ X , Y = σ X σ Y Cov ( X , Y ) , which normalizes covariance to
[ − 1 , 1 ] [-1, 1] [ − 1 , 1 ]
What is the range of correlation ρ \rho ρ ? − 1 ≤ ρ ≤ 1 -1 \le \rho \le 1 − 1 ≤ ρ ≤ 1 (dimensionless, from Cauchy-Schwarz inequality)
If X X X and Y Y Y are independent, what is Cov ( X , Y ) \text{Cov}(X, Y) Cov ( X , Y ) ? Cov ( X , Y ) = 0 \text{Cov}(X, Y) = 0 Cov ( X , Y ) = 0 (but the converse is not always true!)
Does Cov ( X , Y ) = 0 \text{Cov}(X, Y) = 0 Cov ( X , Y ) = 0 imply X X X and Y Y Y are independent? No! Zero covariance only rules out linear relationships. Example:
Y = X 2 Y = X^2 Y = X 2 with
X X X 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 + Y ) = Var ( X ) + Var ( Y ) + 2 Cov ( X , Y ) \text{Var}(X + Y) = \text{Var}(X) + \text{Var}(Y) + 2\,\text{Cov}(X, Y) Var ( X + Y ) = Var ( X ) + Var ( Y ) + 2 Cov ( X , Y )
Why do we use n − 1 n-1 n − 1 in sample covariance instead of n n n ? Bessel's correction: we lose one degree of freedom computing the sample means, making the estimator unbiased.
What does ρ = + 1 \rho = +1 ρ = + 1 mean? Perfect positive linear relationship:
Y = a X + b Y = aX + b Y = a X + b with
a > 0 a > 0 a > 0
What does ρ = − 1 \rho = -1 ρ = − 1 mean? Perfect negative linear relationship:
Y = a X + b Y = aX + b Y = a X + b with
a < 0 a < 0 a < 0
Why is correlation better than covariance for comparing relationship strength? Correlation is scale-invariant (dimensionless), while covariance depends on units of
X X X and
Y Y Y .
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 ( a X + b , Y ) \text{Cov}(aX + b, Y) Cov ( a X + b , Y ) equal to? a Cov ( X , Y ) a\,\text{Cov}(X, Y) a Cov ( X , Y ) (constants
b b b drop out; scaling by
a a a scales covariance)
Master these concepts, and you'll understand how variables dance together—from theory to ML applications.
Joint variation of X and Y
E of X minus muX times Y minus muY
E of XY minus E of X times E of Y
Feature relationships PCA regression
Intuition Hinglish mein samjho
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 (X X X ) badhti hai toh price (Y Y Y ) 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 − 1 se + 1 +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 ρ = + 1 matlab perfect positive linear rishta, − 1 -1 − 1 matlab perfect negative, aur 0 0 0 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!