Correlation and Covariance
Topic - Bivariate statistics evaluate the joint mathematical relationships between two random variables. While covariance detects whether variables move in the same or opposite directions, correlation normalizes covariance into a standardized, unitless metric bounded between .
1. Covariance
Covariance quantifies the joint variability of two continuous random variables and .
Mathematical Formulations
Directional Interpretation
- : Positive Association. As increases, tends to increase.
- : Negative Association. As increases, tends to decrease.
- : Zero Linear Association. Variables show no linear trend.
The Scale Dependency Flaw
Covariance retains the product of the original measurement units (e.g., kilograms meters). Consequently, the magnitude of covariance cannot indicate the strength of the relationship; scaling one variable by 1,000 inflates the covariance by 1,000 without altering the underlying relationship.
2. Pearson Correlation Coefficient ()
The Pearson product-moment correlation standardizes covariance by dividing it by the product of both variables' standard deviations, yielding a scale-free metric:
Boundary Properties (Cauchy-Schwarz Inequality)
By the Cauchy-Schwarz inequality, the Pearson correlation coefficient is strictly bounded: .
- Positive Correlation
- Negative Correlation
- Zero Correlation
- : Perfect positive collinearity. All points lie on an upward-sloping line.
- : Strong positive linear association.
- : Perfect negative collinearity. All points lie on a downward-sloping line.
- : Strong negative linear association.
- : Absence of any linear association.
- Crucial Warning: does not imply statistical independence. Deterministic non-linear relationships (e.g., over ) yield despite complete functional dependence.
3. Spearman's Rank Correlation ()
When data violates normality assumptions, contains extreme outliers, or is purely ordinal, Spearman's rank correlation coefficient evaluates the monotonic relationship by operating on the ranks of the observations:
Where represents the difference between the ranks of paired observations.
4. Implementation Lab
Compute and visualize bivariate covariance and correlation matrices using NumPy, Pandas, and Seaborn.
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Generate synthetic bivariate data
np.random.seed(42)
x = np.random.normal(50, 10, 500)
noise = np.random.normal(0, 5, 500)
y = 2.0 * x + noise
df = pd.DataFrame({'Feature_X': x, 'Feature_Y': y})
# Covariance Matrix
cov_matrix = df.cov()
print("--- Covariance Matrix ---")
print(cov_matrix)
# Pearson Correlation Matrix
corr_matrix = df.corr(method='pearson')
print("\n--- Pearson Correlation Matrix ---")
print(corr_matrix)
# Spearman Rank Correlation
spearman_corr = df['Feature_X'].corr(df['Feature_Y'], method='spearman')
print(f"\nSpearman Rank Correlation: {spearman_corr:.4f}")
Key Takeaways
- Correlation does not imply causation: A high correlation indicates an observed mathematical association, but gives no proof of a causal link. Confounding variables must be ruled out through controlled experiments.
- Scale Invariance: Covariance depends on feature units; Correlation is completely scale-invariant and bounded by .
- Linearity Constraint: Pearson correlation measures strictly linear relationships. Use scatterplots or Spearman correlation for non-linear monotonic data.
Next Topic: Probability Foundations - moving from descriptive metrics into formal probability spaces.