Math & Statistics

Covariance vs Correlation

Covariance tells you direction; correlation tells you direction and strength independent of scale.

🟢 beginner4 min readstatistics
Covariance Cov(X,Y) = E[(X - E[X])(Y - E[Y])] measures joint variability of two random variables, but its magnitude depends on measurement units. Pearson Correlation r = Cov(X,Y) / (σ_X σ_Y) normalizes covariance to [-1, +1], providing a scale-invariant measure of linear association. Key interview topics include Pearson vs Spearman correlation, covariance matrix construction, and correlation vs causation.

Defining Covariance vs Correlation

For samples X and Y:

Covariance:    Cov(X, Y) = 1/(N-1) ∑ (x_i - x̄)(y_i - ȳ)

Correlation:   r(X, Y)   = Cov(X, Y) / ( s_X · s_Y )
Unbounded Covariance (-∞, +∞)   ──Scale Normalization (s_X s_Y)──►  Bounded Correlation [-1, +1]

Pearson vs Spearman Rank Correlation

MetricFormulaRelationship AssessedOutlier Sensitivity
Pearson rCov(X,Y) / (σ_X σ_Y)Pure Linear (y = ax + b)High (Outliers pull the line)
Spearman r_sPearson applied to Rank(X) and Rank(Y)General Monotonic (y increases as x increases)Low (Ranks bound outlier impact)

The Covariance Matrix (Σ)

For a feature matrix X ∈ ℝᴺˣᵈ with zero-centered columns:

Σ = 1/(N-1) Xᵀ X   =   [ Var(X₁)      Cov(X₁,X₂)  ...  Cov(X₁,X_d) ]
                       [ Cov(X₂,X₁)   Var(X₂)     ...  Cov(X₂,X_d) ]

The diagonal entries are feature variances Var(X_i), and off-diagonal entries are pairwise covariances. The Covariance Matrix is always symmetric and positive semi-definite.

Say this out loud

"Covariance measures joint directional variation between two variables, but its magnitude depends on feature units. Pearson correlation normalizes covariance by dividing by standard deviations, yielding a scale-invariant measure of linear association from -1 to +1. Pearson misses non-linear relationships like Y = X², so for non-linear monotonic trends or heavy outliers, we use Spearman rank correlation."

Follow-ups to expect

Check yourself

Question 1 of 3

If feature X is height in meters and Y is weight in kg, what happens to Cov(X,Y) and Pearson Correlation r if height is converted to centimeters (multiplied by 100)?

More in Math & Statistics

See all →
Bayes’ Theorem4 minCentral Limit Theorem4 minLaw of Large Numbers4 min